C++第2周项目3——用结构体数组作计算
2013-04-15 18:38
435 查看
【项目3-用结构体数组作计算】接项目2,
(1)从文件中读出数据,存放到你定义的结构体数组中,即项目2(1);
(2)求出每名同学的总分(可以在读入过程中“顺便”计算);
(3)按总分排序(降序);
(4)输出排序后的成绩单;
(5)有30名同学可以获得奖学金,规则是总分高者优先,有挂科不能得奖学金。请输出可以得奖学金同学的名单。
(6)选做:统计各门课及总分的平均成绩、最高和最低成绩、标准偏差,并输出。
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct student
{
char num[15];
char name[20];
double grade[3];
double score;
};
int main()
{
student stu[200];
student temp; //此变量用于后面总分降序排列
int i,j,sum; //sum用于记录数据数量
double cpp_score,math_score,english_score,total_score,max_score,min_score;
student max_stu,min_stu; //记录总分最大的学生的结构体
ifstream infile("score.txt",ios::in);
if(!infile)
{
cout<<"open error!"<<endl;
exit(1);
}
i=0,sum=0,total_score=0;
max_score=-1,min_score=1000;
cpp_score=0,math_score=0,english_score=0;
while(infile>>stu[i].num&&infile>>stu[i].name&&infile>>stu[i].grade[0]&&infile>>stu[i].grade[1]&&infile>>stu[i].grade[2]) //注释①
{
stu[i].score=stu[i].grade[0]+stu[i].grade[1]+stu[i].grade[2];
total_score+=stu[i].score;
cpp_score+=stu[i].grade[0];
math_score+=stu[i].grade[1];
english_score+=stu[i].grade[2];
if(stu[i].score>max_score)
{
max_score=stu[i].score;
max_stu=stu[i];
}
if(min_score>stu[i].score)
{
min_score=stu[i].score;
min_stu=stu[i];
}
i+=1;
sum+=1;
}
infile.close();
cout<<"***************************************"<<endl;
cout<<"C++这门课的平均分为: "<<cpp_score/sum<<endl;
cout<<"math这门课的平均分为: "<<math_score/sum<<endl;
cout<<"english这门课的平均分为: "<<english_score/sum<<endl;
cout<<"三科总分的平均分为: "<<total_score/sum<<endl;
cout<<"总分最高的学生是: "<<endl<<max_stu.num<<" "<<max_stu.name<<" "<<max_stu.score<<endl;
cout<<"总分最低的学生是: "<<endl<<min_stu.num<<" "<<min_stu.name<<" "<<min_stu.score<<endl;
cout<<"***************************************"<<endl;
for(i=0;i<sum-1;i++) //按照总分(降序)排序。。
{
for(j=0;j<sum-i-1;j++)
{
if(stu[j].score<stu[j+1].score)
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for(i=0;i<sum;i++) //输出成绩单
{
cout<<stu[i].num<<" "<<stu[i].name<<" ";
for(j=0;j<3;j++)
cout<<stu[i].grade[j]<<" ";
cout<<stu[i].score<<endl;
}
cout<<"***************************************"<<endl;
cout<<endl<<"获得奖学金的同学有: "<<endl;
j=0;
for(i=0;i<sum;i++)
{
if(stu[i].grade[0]>=60&&stu[i].grade[1]>=60&&stu[i].grade[2]>=60)
{
cout<<stu[i].num<<" "<<stu[i].name<<endl;
j+=1;
}
if(j==30)
break;
}
cout<<"***************************************"<<endl;
return 0;
}
注释①: 为什么要用那样的while呢? 因为当判断是否读取到文件末尾时(infile.eof()) 它会再读取一行,判断是否有数据,才能判断是否读到文件末尾,
也就是说 如果改成while(!infile.eof())将会多出一个循环,这就导致 stu数组多一组 0 0 0 0 0,影响判断最低分,和总人数等
如果我所描述还不懂得话。。看这个链接吧 →http://blog.csdn.net/shuilan0066/article/details/4669451
相关文章推荐
- C++第2周项目3——用结构体数组作计算
- C++第2周(春)项目2 有序的结构体数组
- C++第2周项目1——有序的结构体数组
- 第2周项目三-用结构体数组做计算
- C++第2周项目1——有序的结构体数组
- C++第2周项目2——读取数据到结构体数组
- 第二周项目三————结构体数组计算
- 第2周任务3:用结构体数组做计算
- 项目三--用结构体数组作计算
- C++第二周项目2-读取数据到结构体数组
- 项目3-用结构体数组作计算
- C++第2周项目6——点结构体与枚举
- 第2周项目一-有序的结构体数组
- C++第2周(春)项目3 文件+结构体实现实用系统
- 2013第二周上机任务项目三(用结构体数组做计算)
- C++第1周项目3——结构体数组处理学生成绩
- C++第1周项目3——结构体数组处理学生成绩
- 第二周项目三—用结构体数组做计算
- 2013c++ 第二周任务-用结构体数组做计算
- 第二周 项目三:用结构体数组作计算