[cpp]
/*
* 程序的版權和版本聲明部分
* Copyright (c)2013, 煙台大學計算機學院學生
* All rightsreserved.
* 文件名稱:score.cpp
* 作 者:紀子龍
* 完成日期:2013 年3 月 8 日
* 版本號: v1.0
* 輸入描述:無
* 問題描述:無
* 輸出:無
*/
#include <iostream>
#include <fstream> //操作文件必寫
using namespace std;
struct Score
{
char num[14];
char nam[30];
int cpp;
int math;
int english;
int sumg;
struct Score *next;
};
void sort1(Score p[],int n);
void output(Score p[],int n);
int main()
{
Score *head=NULL,*p,*q;
int i=0,j,m;
long asg[200];
Score score[200];
//以輸入的方式(ios::in)打開文件,注意score.txt必須在文件夾中存在
ifstream infile("score.txt",ios::in);
//測試是否成功打開,打開失敗時(如要讀的數據文件不存在)退出
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(!infile.eof())
{
p=new Score;
infile>>p->num>>p->nam>>p->cpp>>p->math>>p->english;
p->sumg=p->cpp+p->math+p->english;
p->next=NULL;
if(i==0)
head=p;
else
q->next=p;
q=p;
i++;
}
infile.close();
p=head;
asg[0]=head->sumg;
for(j=1;j<i;j++)
asg[j]=asg[j-1]+score[j].sumg;
m=asg[j]/j;
cout<<"總分高於平均總分且沒有掛科的同學及成績單:"<<endl;
while(p!=NULL)
{
if((p->cpp)>59&&(p->math)>59&&(p->english)>59&&(p->sumg)>m)
{
cout<<p->nam<<" "<<p->cpp<<" "<<p->math<<" "<<p->english<<" "<<p->sumg<<endl;
p=p->next;
}
else
p=p->next;
}
return 0;
}
運行結果 :