- //编写一个学生成绩管理程序。创建一个单链表,要求输入和打印学生成绩,并求出所有学生的平均成绩
- #include<iostream>
- #include<string>
- using namespace std;
- struct student{
- long num;
- string name;
- float score;
- struct student *next;
- };
- void input(student *&head){
- int n=0;
- char ch='#';
- student *newNode,*p;
- do{
- newNode=new student;
- cout<<"输入第"<<n+1<<"个学生的学号:";
- cin>>newNode->num;
- cout<<"输入第"<<n+1<<"个学生的姓名:";
- cin>>newNode->name;
- cout<<"输入第"<<n+1<<"个学生的分数:";
- cin>>newNode->score;
- printf("\\n");
- cout<<"输入#结束,&继续"<<endl;
- cin>>ch;
- if(ch!='#'&&ch!='&'){
- cout<<"ERROR,输入#结束,&继续";
- cin>>ch;
- }
- n++;
- if(head==NULL)
- head=newNode;
- else
- p->next=newNode;
- p=newNode;
- }while(ch!='#');
- p->next=NULL;
- return;
- }
- //打印学生的成绩
- void print(student *const head){
- student *p;
- int i=0;
- if(head==NULL)return;
- p=head;
- cout<<"打印学生的成绩:"<<endl;
- cout<<"学号\\t姓名\\t成绩"<<endl;
- do{
- cout<<p->num<<'\\t'<<p->name<<'\\t'<<p->score<<endl;
- p=p->next;
- }while(p!=NULL);
- }
- long average(student *head){
- student *p;
- float sum=0.0;
- p=head;
- int n=0;
- do{
- sum+=p->score;
- n++;
- p=p->next;
- }while(p!=NULL);
- long ave=sum/n;
- return ave;
- }
- int main(){
- student *stu=NULL;
- input(stu);
- print(stu);
- cout<<"全班的平均成绩:"<<average(stu)<<endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/090520149481.html
来源: http://www.codesnippet.cn/detail/090520149481.html