#include <fstream>
#include<iostream>
#include"iomanip"
#include "string"
using namespace std;
void show_score(int num);//输出成绩;
void cin_score(int num);//从文件得到成绩;
//定义结构体;
struct Student
{
char num[12];
string name;
float grade[4];
struct Student *next;
};
Student *head=NULL,*p,*q;//定义Student类型的有关指针变量;
double add_score=0;
int main( )
{
int num=180;
cin_score(num);//从文件读入成绩;
cout<<"总分高于平均总分且没有挂科的同学:"<<endl;
show_score(num);
return 0;
}
//定义从文件输入成绩函数;
void cin_score(int num)
{
int i;
ifstream infile("score.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<num;i++)
{
p = new Student;
infile>>p->num>>p->name>>p->grade[0]>>p->grade[1]>>p->grade[2];
p->grade[3]=p->grade[0]+p->grade[1]+p->grade[2];
add_score=add_score+p->grade[3];
p->next=NULL;
if (i==0)
head=p;
else
q->next=p;
q=p;
}
infile.close();
}
//定义输出函数;
void show_score(int num)
{
double average_score=add_score/num;
cout<<setiosflags(ios::left)<<setw(13)<<"学号"<<setw(8)<<"姓名"<<setw(8)<<"总分"<<endl;
p=head;
while(p!=NULL)
{
if(p->grade[3]>average_score)
if(p->grade[1]>=60)
if(p->grade[2]>=60)
if(p->grade[3]>=60)
{
cout<<p->num<<" "<<setw(8)<<p->name<<setw(8)<<p->grade[3]<<endl;
}
p=p->next;
}
cout<<endl;
![](http://hi.csdn.net/attachment/201203/5/0_1330932683vw84.gif)
![](http://hi.csdn.net/attachment/201203/5/0_133093268807p8.gif)