实验目的:学会在程序中建立动态链表
实验内容:运用动态链表完成同学成绩的排列
* 程序头部注释开始(为避免提交博文中遇到的问题,将用于表明注释的斜杠删除了)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作
* 完成日期:2011年
* 版 本号:
* 对任务及求解方法的描述部分
* 输入描述:从score.txt文件中读取同学的成绩
* 问题描述:运用动态链表
* 程序输出
* 算法设计:……
* 程序头部的注释结束(此处也删除了斜杠)
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#define NULL 0
using namespace std;
struct subject
{
double chinese;
double math;
double english;
};
struct Student
{
char num[12];
string name;
subject grade;
double score;
struct Student*next;
};
int main()
{
Student*head=NULL,*p,*q;
int a,score=0;
double ave=0;
ifstream infile("score.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(a=0;a<180;a++)
{
p=new Student;
infile>>p->num;
infile>>p->name;
infile>>p->grade.chinese>>p->grade.math>>p->grade.english;
p->score=p->grade.chinese+p->grade.english+p->grade.math;
p->next=NULL;
if(a==0)
head=p;
else
q->next=p;
q=p;
}
infile.close();
ave=score/180;
p=head;
do
{
if(p->score>ave&&p->grade.chinese>60&&p->grade.english>60&&p->grade.math>60)
{
cout<<setiosflags(ios::left)<<setw(9)<<p->name<<" "<<p->num<<" "<<" "<<p->score<<endl;
p=p->next;
}
else
p=p->next;
}while(p!=NULL);
return 0;
}