第17周 任务二(完整版)

来源:互联网 发布:虚拟机流畅运行mac 编辑:程序博客网 时间:2024/05/21 11:19
实验目的:学会建立动态链表实验内容:学会建立动态链表* 程序头部注释开始* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生* All rights reserved.* 文件名称:  动态链表                           * 作    者:  薛广晨                           * 完成日期:  2011       年  12     月  16      日* 版本号:  x1.0       * 对任务及求解方法的描述部分* 输入描述:数据来自score.txt* 问题描述:(1)结构体中的成员多些;(2)从文件中读入数据;(3)一边读数据一边得计算总分,为计算总分的平均值也得做些准备;(4)并不是输出所有的节点* 程序输出:所有的节点* 程序头部的注释结束(此处也删除了斜杠)//下面是程序//下面是程序#include <fstream>#include"iomanip"#include<string>#include<iostream>using namespace std;void cin_score();void cout_score();struct Student{ string num; char name[12]; double grade[3]; double score;    struct Student *next;};int num=180;double ave_score;Student *head=NULL,*p,*q;int main(){ cin_score(); cout<<"      总分高于平均总分且没有挂科的同学:"<<endl;    cout_score();    return 0;}void cin_score(){ //从文件读入学生成绩的结构体数组;    //下面的程序建立起一个有180个节点的动态链表    ifstream infile("score.txt",ios::in); double most_score=0;    for(int i=0;i<num;i++) {  p = new Student;    if(!infile)  {   cerr<<"open error!"<<endl;   exit(1);  }    {   infile>>p->num>>p->name>>p->grade[0]>>p->grade[1]>>p->grade[2];   p->score=p->grade[0]+p->grade[1]+p->grade[2];   most_score=most_score+p->score;   ave_score=most_score/num;  }                 p->next=NULL;        if (i==0)   head=p;        else   q->next=p;  q=p;   } infile.close();}void cout_score(){  //输出节点 p=head; cout<<"学号"<<'\t'<<'\t'<<setiosflags(ios::left)<<setw(9)<<"姓名"<<setw(7)<<"C++"<<setw(8)<<"高数"<<setw(8)<<"英语"<<setw(8)<<"总分"<<endl; while(p!=NULL) {  if(p->score>ave_score)   if(p->grade[0]>=60)    if(p->grade[1]>=60)     if(p->grade[2]>=60)     {      cout<<p->num<<" "<<setw(8)<<p->name<<": "<<p->grade[0]<<"\t"<<p->grade[1]<<"\t"<<p->grade[2]<<"\t"<<p->score<<endl;     }     p=p->next; }}

运行结果: