编的程序里有问题大家帮忙看看吧~~!
题目是有30个学生,每个学生的数据包括学号、姓名及3门课成绩,从键盘输入每个学生的数据,并完成如下功能:
1、计算并输出每个学生平均成绩;
2、计算并输出每门课程的平均分;
3、按学生平均分从低到高排序,并将排序结果存入文件中;
4、从排序结果中,查找满足某一平均分的学生信息,并输出;
提示:定义结构体类型存放30个同学的成绩,排序方法可以采用冒泡法或其它方法,在函数中实现输入、求平均、排序、查找等功能,在主函数中调用函数。
#include "stdafx.h"
#include "conio.h"
#include "stdlib.h"
#include"stdio.h"
#include"string.h"
#include "iostream.h"
#define F sizeof(student)
#define NULL 0
#define M 30
typedef struct scores
{ int english;
int math;
int c_language;
int all;
}TP;
typedef struct students
{ char sid[15];
char name[15];
TP score;
struct students *next;
}student;
student *input()
{ student *head,*p1,*p2;
int n=0; char ch;
//clrscr();
system("cls");
head=(student *)malloc(F);head->next=NULL;
do
{ n++;
printf("\n\nPlease input %d student message: \n\n",n);
printf("\t%d student sid: ",n);
p1=(student *)malloc(F);p1->next=NULL;
scanf("%s",p1->sid);
printf("\n\t%d student name: ",n);
scanf("%s",p1->name);
printf("\n\t%d student scores(englesh,math,c_language): ",n);
scanf("%d,%d,%d",&p1->score.english,&p1->score.math,&p1->score.c_language);
p1->score.all=p1->score.english+p1->score.math+p1->score.c_language;
if(n==1)
{ head->next=p1;p2=p1; }
else
{ p2->next=p1;
p2=p1;
}
printf("\n\n\t\t\tContinue or back (press y/n): ");
ch=getchar();
}while(ch=='y'||ch=='Y');
return head;
}
void average1(student *head)
{ student *p; int j;
//clrscr();
system("cls");
p=head->next;
while(p)
{ j=p->score.all/3;
printf("\n\nname: %s\taverage: %d",p->name,j);
p=p->next;
}
printf("\n\n\nPress eny key return.");
getchar();
}
void average2(student *head)
{
student *p;int n=0,temp1=0,temp2=0,temp3=0;
p=head->next;
while(p)
{ temp1+=p->score.english;
temp2+=p->score.math;
temp3+=p->score.c_language;
p=p->next;n++;
}
printf("\n\naverage english is : %d\naverage math is : %d\naverage c_language is : %d\t",temp1/n,temp2/n,temp3/n);
}
student *sort(student *head)
{ student *head1,*p,*q,*r; int temp1=0,temp2=0,temp3=0,temp4; char s[15],n[15];
head1=head;
for(p=head1->next;p->next!=NULL;p=p->next)
{ r=p;
for(q=p->next;q;q=q->next)
if(q->score.all>r->score.all)
r=q;
if(r!=p)
{ strcpy(s,p->sid);strcpy(n,p->name);
temp1=p->score.english;
temp2=p->score.math;
temp3=p->score.c_language;
temp4=p->score.all;
strcpy(p->sid,r->sid);strcpy(p->name,r->name);
p->score.english=r->score.english;
p->score.math=r->score.math;
p->score.c_language=r->score.c_language;
p->score.all=r->score.all;
strcpy(r->sid,s);strcpy(r->name,n);
r->score.english=temp1;
r->score.math=temp2;
r->score.c_language=temp3;
r->score.all=temp4;
}
}
return head1;
}
void output(student *head)
{ student *head2,*p;int i=1;
//clrscr();
system("cls");
head2=sort(head);
for(p=head2->next;p!=NULL;p=p->next)
printf("\n\nname: %s\tsid: %s\tenglish: %d\tmath: %d\tc_language: %d\taverage: %d\tmingci: %d",p->name,p->sid,p->score.english,p->score.math,p->score.c_language,p->score.all/3,i++);
average2(head);
printf("\n\n\n\t\tPress eny key back.");
getchar();
}
void main()
{ student *head,*p1,*p2;
int i=0,j=1;
head=input();
do
{ //clrscr();
//system("cls");
printf("\n\n(1): average1.\n\n(2): average2.\n\n(3): sort.\n\n(4): output.\n\n\n Please choose: ");
scanf("%d",&i);
switch(i)
{ case 1: average1(head); break;
case 2: //clrscr();
system("cls");
average2(head); printf("\n\n\nPress eny key retuen.");getchar(); break;
case 3: //clrscr();
system("cls");
p1=sort(head); for(p2=p1->next;p2!=NULL;p2=p2->next) printf("\n\t\tname: %s\tmingci:%d",p2->name,j++);printf("\n\n\nPress eny key back.");getchar(); break;
case 4: output(head); break;
default: printf("\nYour choose is not right.");break;
}
}while(i!=-1);
}
程序有点缺陷~~就是输入完一个学生的成绩后没办法输入下一个学生成绩,大家帮帮忙改改~~