第17周 任务一

来源:互联网 发布:虚拟机流畅运行mac 编辑:程序博客网 时间:2024/06/03 17:50
实验目的:学会自定义结构体;实验内容:学会自定义结构体* 程序头部注释开始* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生* All rights reserved.* 文件名称:成绩处理                             * 作    者:薛广晨                             * 完成日期:2011    年  12     月  15      日* 版本号:x1.0         * 对任务及求解方法的描述部分* 输入描述:(1)从文件中读出数据,存放到你定义的结构体数组中;(2)求出每名同学的总分(可以读入过程中“顺便”计算);(3)按总分排序(降序);(4)输出排序后的成绩单;(5)有30 名同学可以获得奖学金,规则是总分高者优先,有挂科不能得奖学金请输出得奖学金同学的名单.* 程序输出:排序后的结果* 程序头部的注释结束(此处也删除了斜杠)//下面是程序#include <fstream>#include"iomanip"#include<string>#include<iostream>using namespace std;void ouput_student();//输出学生成绩的结构体数组;void cin_student();//从文件得到学生成绩的结构体数组;void cout_student();//储存学生成绩的结构体数组;void bubble_student();//排序;void jxj_student(int);//输出获奖学金的学生;struct Student{ string num; char name[12]; double grade[3]; double score;};Student stu[180];int num=180;int main(){ cin_student();//从文件读入学生成绩的结构体数组; cout<<"排序前学生成绩的结构体数组:"<<endl; ouput_student();//输出学生成绩的结构体数组; bubble_student();//排序; cout<<"排序后学生成绩的结构体数组:"<<endl; ouput_student();//输出学生成绩的结构体数组; cout_student();//保存到文件; jxj_student(30);//输出获奖学金的学生; return 0;}void cin_student(){    int i;    ifstream infile("score.txt",ios::in);    if(!infile) {  cerr<<"open error!"<<endl;        exit(1); }    for(i=0;i<num;i++) {  infile>>stu[i].num>>stu[i].name>>stu[i].grade[0]>>stu[i].grade[1]>>stu[i].grade[2];  stu[i].score=stu[i].grade[0]+stu[i].grade[1]+stu[i].grade[2];     }    infile.close();    cout<<endl;}void cout_student(){    ofstream outfile("out_score.txt",ios::out);    if(!outfile) {  cerr<<"open error!"<<endl;        exit(1); }    for(int i=0;i<num;i++)  outfile<<stu[i].num<<"      "<<stu[i].name<<"        "<<stu[i].grade[0]<<"     "<<stu[i].grade[1]<<"     "<<stu[i].grade[2]<<"    "<<stu[i].score<<endl; outfile.close();}void bubble_student(){ int i,j,k; Student stu1;    for(i=0;i<num-1;i++) {  k=i;        for(j=i+1;j<num;j++)   if(stu[j].score>=stu[k].score)   {   stu1=stu[k];   stu[k]=stu[j];   stu[j]=stu1;   } }}void ouput_student(){ int i; cout<<"学号"<<'\t'<<'\t'<<setiosflags(ios::left)<<setw(9)<<"姓名"<<setw(7)<<"C++"<<setw(8)<<"高数"<<setw(8)<<"英语"<<setw(8)<<"总分"<<endl; for(i=0;i<num;i++) {   cout<<stu[i].num<<"   "<<setw(8)<<stu[i].name<<": "<<stu[i].grade[0]   <<"\t"<<stu[i].grade[1]<<"\t"<<stu[i].grade[2]<<"\t"<<stu[i].score<<endl;  } cout<<endl; }void jxj_student(int num){ int count=0;    cout<<"获得奖学金的同学是:"<<endl;    for(int i=0;;i++) {  if(stu[i].grade[0]>=60)        if(stu[i].grade[1]>=60)        if(stu[i].grade[2]>=60)  {            cout<<setw(2)<<count+1<<setw(8)<<stu[i].name;            count++;   if(count>=num)break;            if(count%5==0)               cout<<endl;  } }}

运行结果:(贴图)



经验积累:
1.自定义很好用,但感觉有点不好理解
2.有了自定义,处理类似的题就很easy啦

上机感言:啊,终于做完啦,在朦胧中找到了一丝感觉,很爽