任务说明:
假设有40个学生被邀请来给自助餐厅的食品和服务质量打分,分数划分为1~10这10个等级(1表示最低分,10表示最高分),试编程打印统计调查结果。其中Grade代表等级,Count代表人数,Histogram表示以图形方式显示该等级的人数。程序执行结果如下:
Grade Count Histogram
1 2 **
2 3 ***
3 1 *
4 4 ****
5 2 **
6 2 **
7 2 **
8 0
9 3 ***
10 1 *
[友情提示]:
定义一个含有40个元素的一维整型数组score,用来存放每个学生打的分数。再定义一个含有11个元素的一维整型数组count,该数组的每个元素作为计数器使用,如数组元素count[1]用来记录等级(分数)为1的人数,…,count[10]用来记录等级(分数)为10的人数。实际打分人数由键盘读入。
输入要求: "%d",详见输入样例
输出要求: "Grade\tCount\tHistogram\n",详见输出样例
输入输出样例:
Input sample1:
20
1 9 1 2 4 2 2 3 4 5 5 6 6 4 7 7 9 4 9 10
Output sample1:
Grade Count Histogram
1 2 **
2 3 ***
3 1 *
4 4 ****
5 2 **
6 2 **
7 2 **
8 0
9 3 ***
10 1 *
要输入的分数太多,我懒得手数,用2位随机数代替手输入,输入函数我单独写了,你需要调用就行了,详细看备注。
#include <stdio.h>
#include <math.h>
void main()
{
char ip[33]="";
int i=31,j,k=3,sum[4]={0,0,0,0};
puts("请输入完整二进制ip地址");
gets(ip );
puts("转化后为:");
while(i>=0)
{
j=0;
while(j<8)
{
sum[k]+=(ip[i]-48)*pow(2,j);
i--;
j++;
}
k--;
}
printf("%d:%d:%d:%d\n",sum[0],sum[1],sum[2],sum[3]);
}
#include <stdio.h>
#include <math.h>
void main()
{
//比如ip为:11111111 11111111 11111111 11111111 //按照8421法转化为16进制
int ip=0xffffffff; //
printf("%d.%d.%d.%d\n",(ip >> 24)&0xff,(ip >> 16)&0xff,(ip>>8)&0xff,ip&0xff);
}
以下的代码就可以实现你的所有要求了(但是是用C++编的,不知道楼主是要C++还是C的):
#include<iostream>
#include<fstream>
using namespace std;
void show_original_files(ifstream& out);
void add_message(ifstream& in,ofstream& add,int& n);
void show_changed_files(ifstream& in,int n);
int main()
{
int n=0;
ifstream fin;
ofstream fout;
cout<<"加载message.txt文件。。。\n";
fout.open("message.txt",ios::app);
if(fout.fail())
{
cout<<"文件加载错误!";
exit(1);
}
fin.open("message.txt");
if(fin.fail())
{
cout<<"文件加载错误!";
exit(1);
}
show_original_files(fin);
add_message(fin,fout,n);
fout.close();
show_changed_files(fin,n);
fin.close();
return 0;
}
void show_original_files(ifstream& in)
{
char ch;
in.get(ch);
if(in.eof())
cout<<"\n原文件内容为空。";
else
{
cout<<"\n原文件内容为:";
while(!in.eof())
{
cout<<ch;
in.get(ch);
}
}
cout<<endl;
}
void add_message(ifstream& in,ofstream& add,int& n)
{
char ch;
cout<<"\n输入你要添加到文件的内容:\n";
cin.get(ch);
while(ch!='\n')
{
add<<ch;
n++;
cin.get(ch);
}
}
void show_changed_files(ifstream& in,int n)
{
char ch;
int i;
cout<<"\n你想输出文件的哪部分内容?(1.整个文件;2.刚才输入的信息)\n"
<<"请选择:";
cin>>i;
if(i==1)
{
in.clear();
in.seekg(0);
cout<<"\n添加后整个文件内容为:";
in.get(ch);
while(!in.eof())
{
cout<<ch;
in.get(ch);
}
cout<<endl;
}
else if(i==2)
{
in.clear();
cout<<"\n你刚才输入的信息为:";
in.get(ch);
while(!in.eof())
{
cout<<ch;
in.get(ch);
}
cout<<endl;
}
}
运行结果
不过输入0.001的输出和你的例子不一样。仅供参考。
你的代码修改如下:
#include <stdio.h>pi1和pi2没有实际作用。