第17周报告1
实验目的:学会用结构体编程序的操作
实验目的:学会用结构体编程序的操作
实验内容:从文件中读入数据,排序。
/* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 苗影
* 完成日期: 2011 年 12 月 19 日
* 版本号:
* 对任务及求解方法的描述部分
* 输入描述:文件score.txt
* 问题描述:从文件score.txt中读入180名学生的分数,将排序后的结果在屏幕上输出。
* 程序输出:score.txt
*/
#include <fstream>
#include <iostream>
using namespace std;
struct subject
{
double c;
double math;
double english;
};
struct student
{
char num[12];
char name[10];
subject grade;
double score;
}stu[180];
student student1;
void bubble_sort(student stu[],int num);
void output_stu(student stu[],int num);
int main ()
{
int i,j;
ifstream infile("score.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<180;i++)
{
infile>>stu[i].num>>stu[i].name>>stu[i].grade.c>>stu[i].grade.math
>>stu[i].grade.english;
stu[i].score=stu[i].grade.c+stu[i].grade.math+stu[i].grade.english;
}
infile.close();
cout<<" 排序后的成绩单"<<endl;
cout<<endl;
cout<<" 学号 姓名 C++ Math English 总分"<<endl;
cout<<endl;
bubble_sort(stu,180);
output_stu( stu,180);
cout<<" 获得奖学金的名单"<<endl;
cout<<endl;
i=0;
j=0;
do
{
if(stu[j].grade.c>60 && stu[j].grade.math>60 && stu[j].grade.english >60)
{
cout<<stu[j].name<<'\t';
i++;
}
j++;
}while(i<30 && j<180);
cout<<endl;
cout<<" ";
return 0;
}
void bubble_sort(student stu[],int num)
{
int i,j;
for(j=0;j<num-1;j++)
for(i=0;i<num-j-1;i++)
if(stu[i].score<stu[i+1].score)
{
student1=stu[i];
stu[i]=stu[i+1];
stu[i+1]=student1;
}
}
void output_stu(student stu[],int num)
{
int i;
for(i=0;i<num;i++)
{
cout<<" "<<stu[i].num<<'\t'<<'\t'
<<stu[i].grade.c <<'\t'<<stu[i].grade.math<<'\t'
<<stu[i].grade.english<<'\t'<<stu[i].score<<endl;
}
}
经验积累:
1.我还不太会使用文件里的数据。
2.
3.
上机感言:能让程序愁死人!