第十六周实验报告2

来源:互联网 发布:网络热门歌曲排行榜 编辑:程序博客网 时间:2024/06/11 15:48

实验目的:

文件score.dat中保存的是100名学生的姓名和C++课、高数和英语成绩。

(1)定义学生类,其中包含姓名、C++课、高数和英语成绩及总分、均分数据成员,成员函数根据需要确定。

(2)读入这名学生的成绩,用对象数组进行存储。

(3)求出各科和总分的最高分。

(4)请按总分的降序(高成绩在前,低成绩在后)排序

(5)在屏幕上显示各科及总分的最高分,排序后的成绩单(包括总分)保存到文件odered_score.dat中。

实验代码:

#include <iostream>#include <fstream>#include <string>using namespace std;class Student{public:string name;double C;double math;double english;double addition();double average();void display();};double Student::addition(){return C + math + english;}double Student::average(){return (C + math + english) / 3;}void Student::display(){cout << name << '\t' << C << '\t' << math << '\t' << english << '\t';}int main(){Student stu[100];double max_c, max_math, max_english, max_addition;ifstream infile("score.txt", ios::in);if(!infile){cerr << "open error!" << endl;exit(1);}for(int i = 0; i < 100; i++){infile >> stu[i].name >> stu[i].C >> stu[i].math >> stu[i].english;stu[i].display();cout << stu[i].addition() << '\t';cout << stu[i].average() << endl;}max_c = stu[0].C;    for(int i = 1; i < 100; i++){if(stu[i].C > max_c){max_c = stu[i].C;}}cout << "C++的最高成绩为:" << max_c << endl;max_math = stu[0].math;for(int i = 1; i < 100; i++){if(stu[i].math > max_math){max_math = stu[i].math;}}cout << "高数的最高成绩为:" << max_math << endl;max_english = stu[0].english;for(int i = 1; i < 100; i++){if(stu[i].english > max_english){max_english = stu[i].english;}}cout << "英语的最高成绩为:" << max_english << endl;    max_addition = stu[0].addition();for(int i = 1; i < 100; i++){if(stu[i].addition() > max_addition){max_addition = stu[i].addition();}}    cout << "总分的最高成绩为:" << max_addition << endl;Student t;for(int j = 0; j < 99; j++){for(int i = 0; i < 99 - j; i++){if(stu[i].addition() > stu[i + 1].addition()){t.name  = stu[i].name;t.C = stu[i].C;t.math = stu[i].math;t.english = stu[i].english;stu[i].name = stu[i + 1].name;stu[i].C = stu[i + 1].C;stu[i].math = stu[i + 1].math;stu[i].english = stu[i + 1].english;stu[i + 1].name = t.name;stu[i + 1].C = t.C;stu[i + 1].math = t.math;stu[i + 1].english = t.english;}}}cout << endl;cout << "按照总分高低排序:" << endl;for(int i = 0; i < 100; i++){stu[i].display();cout << stu[i].addition() << '\t';cout << stu[i].average() << endl;}ofstream outfile("ordered_score.txt");if(!outfile){cerr << "ordered_salary.txt error!" << endl;exit(1);}for(int i = 0; i < 100; i++){outfile << stu[i].name << '\t' << stu[i].C << '\t' << stu[i].math << '\t' << stu[i].english << '\t' << stu[i].addition() << '\t'<< stu[i].average() << endl;}outfile.close();    system("pause");    return 0;}


实验结果:

刘得意  60      98      75      233     77.6667王锐    63      90      96      249     83何煜中  90      73      82      245     81.6667王磊    87      86      92      265     88.3333冯松    89      98      83      270     90裴培    75      82      91      248     82.6667马骁    62      67      90      219     73马婧    98      84      87      269     89.6667周俊升  57      68      96      221     73.6667贺祺    61      96      72      229     76.3333李桐    93      83      86      262     87.3333高路    63      74      98      235     78.3333冯佳媛  61      79      81      221     73.6667张迪    99      88      80      267     89张里响  85      65      96      246     82王瑞麒  89      83      91      263     87.6667徐金竹  75      89      73      237     79赵媛媛  77      75      66      218     72.6667宋宗杰  94      100     92      286     95.3333张佳玮  61      98      96      255     85王姝    70      91      90      251     83.6667宋媛媛  61      94      92      247     82.3333于莉    55      66      78      199     66.3333于浩    78      84      72      234     78王竞    90      87      67      244     81.3333崔赞    91      67      93      251     83.6667宋静    69      85      73      227     75.6667王磊    71      78      77      226     75.3333方圆    70      79      76      225     75李朋    90      82      97      269     89.6667马佳    60      90      100     250     83.3333张龙    62      100     78      240     80马里    73      95      73      241     80.3333韩明    83      97      88      268     89.3333马立    73      90      83      246     82吴清正  89      97      85      271     90.3333印虹    92      68      75      235     78.3333田苗苗  75      91      71      237     79卫青    66      73      77      216     72冷云    89      88      71      248     82.6667葛志伟  100     79      71      250     83.3333范振光  98      87      89      274     91.3333王芳    71      97      99      267     89杨超    67      73      82      222     74杨梦婕  89      99      67      255     85梁雅宁  55      88      100     243     81王琦    98      95      98      291     97吴玮    69      76      68      213     71杨阔    90      91      98      279     93贾伟林  63      90      86      239     79.6667刘亚新  77      81      95      253     84.3333金昕    92      67      69      228     76董一伟  93      88      80      261     87汤娜    68      85      71      224     74.6667周恒    87      82      69      238     79.3333张笑    86      88      76      250     83.3333文静    93      88      85      266     88.6667杨华鑫  81      81      68      230     76.6667苏明霞  59      79      94      232     77.3333黄京    62      75      96      233     77.6667佟欣    60      79      98      237     79张雯    69      70      93      232     77.3333刘京西  67      78      78      223     74.3333徐嘉琦  90      75      87      252     84魏佳    100     94      80      274     91.3333高举    81      99      91      271     90.3333边里    56      94      87      237     79何佳成  70      75      78      223     74.3333赵旭洋  87      91      94      272     90.6667孙大伟  65      69      98      232     77.3333鲁继森  84      79      75      238     79.3333白涛    57      82      75      214     71.3333蔺剑飞  88      75      79      242     80.6667兰天    83      66      74      223     74.3333王悦    79      82      70      231     77陈世勃  70      92      65      227     75.6667张昊    94      83      96      273     91高清    76      83      84      243     81王欣欣  71      83      78      232     77.3333王欢欢  57      33      66      156     52叶丹    87      80      96      263     87.6667李悦    63      79      97      239     79.6667郭倩    69      94      69      232     77.3333刘盈    99      72      93      264     88杨洁    96      79      87      262     87.3333徐一菡  85      45      62      192     64王蒙    67      97      89      253     84.3333张敏    85      75      89      249     83桂佳    60      73      65      198     66林倩    67      77      80      224     74.6667任盛达  57      86      88      231     77吴佳林  96      65      82      243     81黄金龙  85      90      78      253     84.3333陈美珠  82      72      83      237     79冼丹    100     89      89      278     92.6667唐楠    68      97      77      242     80.6667张扬    77      65      93      235     78.3333宋航彬  80      71      91      242     80.6667薛淇文  89      71      75      235     78.3333刘紫亮  72      98      84      254     84.6667C++的最高成绩为:100高数的最高成绩为:100英语的最高成绩为:100总分的最高成绩为:291按照总分高低排序:王欢欢  57      33      66      156     52徐一菡  85      45      62      192     64桂佳    60      73      65      198     66于莉    55      66      78      199     66.3333吴玮    69      76      68      213     71白涛    57      82      75      214     71.3333卫青    66      73      77      216     72赵媛媛  77      75      66      218     72.6667马骁    62      67      90      219     73周俊升  57      68      96      221     73.6667冯佳媛  61      79      81      221     73.6667杨超    67      73      82      222     74刘京西  67      78      78      223     74.3333何佳成  70      75      78      223     74.3333兰天    83      66      74      223     74.3333汤娜    68      85      71      224     74.6667林倩    67      77      80      224     74.6667方圆    70      79      76      225     75王磊    71      78      77      226     75.3333宋静    69      85      73      227     75.6667陈世勃  70      92      65      227     75.6667金昕    92      67      69      228     76贺祺    61      96      72      229     76.3333杨华鑫  81      81      68      230     76.6667王悦    79      82      70      231     77任盛达  57      86      88      231     77苏明霞  59      79      94      232     77.3333张雯    69      70      93      232     77.3333孙大伟  65      69      98      232     77.3333王欣欣  71      83      78      232     77.3333郭倩    69      94      69      232     77.3333刘得意  60      98      75      233     77.6667黄京    62      75      96      233     77.6667于浩    78      84      72      234     78高路    63      74      98      235     78.3333印虹    92      68      75      235     78.3333张扬    77      65      93      235     78.3333薛淇文  89      71      75      235     78.3333徐金竹  75      89      73      237     79田苗苗  75      91      71      237     79佟欣    60      79      98      237     79边里    56      94      87      237     79陈美珠  82      72      83      237     79周恒    87      82      69      238     79.3333鲁继森  84      79      75      238     79.3333贾伟林  63      90      86      239     79.6667李悦    63      79      97      239     79.6667张龙    62      100     78      240     80马里    73      95      73      241     80.3333蔺剑飞  88      75      79      242     80.6667唐楠    68      97      77      242     80.6667宋航彬  80      71      91      242     80.6667梁雅宁  55      88      100     243     81高清    76      83      84      243     81吴佳林  96      65      82      243     81王竞    90      87      67      244     81.3333何煜中  90      73      82      245     81.6667张里响  85      65      96      246     82马立    73      90      83      246     82宋媛媛  61      94      92      247     82.3333裴培    75      82      91      248     82.6667冷云    89      88      71      248     82.6667王锐    63      90      96      249     83张敏    85      75      89      249     83马佳    60      90      100     250     83.3333葛志伟  100     79      71      250     83.3333张笑    86      88      76      250     83.3333王姝    70      91      90      251     83.6667崔赞    91      67      93      251     83.6667徐嘉琦  90      75      87      252     84刘亚新  77      81      95      253     84.3333王蒙    67      97      89      253     84.3333黄金龙  85      90      78      253     84.3333刘紫亮  72      98      84      254     84.6667张佳玮  61      98      96      255     85杨梦婕  89      99      67      255     85董一伟  93      88      80      261     87李桐    93      83      86      262     87.3333杨洁    96      79      87      262     87.3333王瑞麒  89      83      91      263     87.6667叶丹    87      80      96      263     87.6667刘盈    99      72      93      264     88王磊    87      86      92      265     88.3333文静    93      88      85      266     88.6667张迪    99      88      80      267     89王芳    71      97      99      267     89韩明    83      97      88      268     89.3333马婧    98      84      87      269     89.6667李朋    90      82      97      269     89.6667冯松    89      98      83      270     90吴清正  89      97      85      271     90.3333高举    81      99      91      271     90.3333赵旭洋  87      91      94      272     90.6667张昊    94      83      96      273     91范振光  98      87      89      274     91.3333魏佳    100     94      80      274     91.3333冼丹    100     89      89      278     92.6667杨阔    90      91      98      279     93宋宗杰  94      100     92      286     95.3333王琦    98      95      98      291     97请按任意键继续. . .


实验心得:

老师真的是用心良苦啊。将这个周学习的文件操作及文件流的问题和起一阵子学习的类的应用结合到一起了。整个题目难度相比任务一略有增加,不过还好,因为,说实话,类的题目我们做过不少,而且这个任务中对类的操作考察的不是很难所以整个题目不算太难,所以做起来不是太吃力,只是,同样的,在排序的时候,会有一点思考,因为,在老师给的.score中没有总分和均分的数据,那么,在输出数据和排序的时候真的不是太容易,我的做法自然也不知道是不是很好,因为在冒泡法的时候,有点麻烦。至于其他的,也没什么难点了吧!

 

原创粉丝点击