编写程序:输入30个学生的五门成绩,计算并输出每科成绩的平均分,最高分,最低分。

如题所述

#include<stdio.h> /*引用库函数*/
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
typedef struct /*定义结构体数组*/
{
char num[10]; /*学号*/
char name[20]; /*姓名*/
int score; /*成绩*/
}Student;
Student stu[80]; /*结构体数组变量*/
int menu_select() /*菜单函数*/
{
char c;
do{
system("cls"); /*运行前清屏*/
printf("\t\t****Students' Grade Management System****\n"); /*菜单选择*/
printf("\t\t | 1. Input Records |\n");
printf("\t\t | 2. Display All Records |\n");
printf("\t\t | 3. Sort |\n");
printf("\t\t | 4. Insert a Record |\n");
printf("\t\t | 5. Delete a Record |\n");
printf("\t\t | 6. Query |\n");
printf("\t\t | 7. Statistic |\n");
printf("\t\t | 8. Add Records from a Text File|\n");
printf("\t\t | 9. Write to a Text file |\n");
printf("\t\t | 0. Quit |\n");
printf("\t\t*****************************************\n");
printf("\t\t\tGive your Choice(0-9):");
c=getchar(); /*读入选择*/
}while(c<'0'||c>'9');
return(c-'0'); /*返回选择*/
}
int Input(Student stud[],int n) /*输入若干条记录*/
{int i=0;
char sign,x[10]; /*x[10]为清除多余的数据所用*/
while(sign!='n'&&sign!='N') /*判断*/
{ printf("\t\t\tstudent's num:"); /*交互输入*/
scanf("\t\t\t%s",stud[n+i].num);
printf("\t\t\tstudent's name:");
scanf("\t\t\t%s",stud[n+i].name);
printf("\t\t\tstudent's score:");
scanf("\t\t\t%d",&stud[n+i].score);
gets(x); /*清除多余的输入*/
printf("\t\t\tany more records?(Y/N)");
scanf("\t\t\t%c",&sign); /*输入判断*/
i++;
}
return(n+i);
}
void Display(Student stud[],int n) /*显示所有记录*/
{
int i;
printf("\t\t\t-----------------------------------\n"); /*格式头*/
printf("\t\t\tnumber name score\n");
printf("\t\t\t-----------------------------------\n");
for(i=1;i<n+1;i++) /*循环输入*/
{
printf("\t\t\t%-16s%-15s%d\n",stud[i-1].num,stud[i-1].name,stud[i-1].score);
if(i>1&&i%10==0) /*每十个暂停*/
{printf("\t\t\t-----------------------------------\n"); /*格式*/
printf("\t\t\t");
system("pause");
printf("\t\t\t-----------------------------------\n");
}
}
printf("\t\t\t");
system("pause");
}
void Sort_by_num(Student stud[],int n) /*按学号排序*/
{ int i,j,*p,*q,s;
char t[10];
for(i=0;i<n-1;i++) /*冒泡法排序*/
for(j=0;j<n-1-i;j++)
if(strcmp(stud[j].num,stud[j+1].num)>0)
{strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
p=&stud[j+1].score;
q=&stud[j].score;
s=*p;
*p=*q;
*q=s;
}
}
int Insert_a_record(Student stud[],int n) /*插入一条记录*/
{char x[10]; /*清除多余输入所用*/
printf("\t\t\tstudent's num:"); /*交互式输入*/
scanf("\t\t\t%s",stud[n].num);
printf("\t\t\tstudent's name:");
scanf("\t\t\t%s",stud[n].name);
printf("\t\t\tstudent's score:");
scanf("\t\t\t%d",&stud[n].score);
gets(x);
n++;
Sort_by_num(stud,n); /*调用排序函数*/
printf("\t\t\tInsert Successed!\n"); /*返回成功信息*/
return(n);
}
int Delete_a_record(Student stud[],int n) /*按姓名查找,删除一条记录*/
{ char s[20];
int i=0,j;
printf("\t\t\ttell me his(her) name:"); /*交互式问寻*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判断*/
if(i==n)
{ printf("\t\t\tnot find!\n"); /*返回失败信息*/
return(n);
}
for(j=i;j<n-1;j++) /*删除操作*/
{
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].name,stud[j+1].name);
stud[j].score=stud[j+1].score;
}
printf("\t\t\tDelete Successed!\n"); /*返回成功信息*/
return(n-1);
}
void Query_a_record(Student stud[],int n) /*查找并显示一个记录*/
{ char s[20];
int i=0;
printf("\t\t\tinput his(her) name:"); /*交互式输入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&i<n) i++; /*查找判断*/
if(i==n)
{ printf("\t\t\tnot find!\n"); /*输入失败信息*/
return;

}
printf("\t\t\this(her) number:%s\n",stud[i].num); /*输出该学生信息*/
printf("\t\t\this(her) score:%d\n",stud[i].score);
}
void Statistic(Student stud[],int n) /*新增功能,输出统计信息*/
{ int i,j=0,k=0,sum=0;
float aver; /*成绩平均值*/
for(i=0;i<n;i++) /*循环输入判断*/
{
sum+=stud[i].score;
if(stud[j].score>stud[i].score) j=i;
if(stud[k].score<stud[i].score) k=i;
}
aver=1.0*sum/n;
printf("\t\t\tthere are %d records.\n",n); /*总共记录数*/
printf("\t\t\tthe hignest score:\n"); /*最高分*/
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[j].num,stud[j].name,stud[j].score);
printf("\t\t\tthe lowest score:\n"); /*最低分*/
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[k].num,stud[k].name,stud[k].score);
printf("\t\t\tthe average score is %5.2f\n",aver); /*平均分*/
}
int AddfromText(Student stud[],int n) /*从文件中读入数据*/
{ int i=0,num;
FILE *fp; /*定义文件指针*/
char filename[20]; /*定义文件名*/
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename); /*输入文件名*/
if((fp=fopen(filename,"rb"))==NULL) /*打开文件*/
{ printf("\t\t\tcann't open the file\n"); /*打开失败信息*/
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*读入总记录量*/
while(i<num) /*循环读入数据*/
{
fscanf(fp,"%s%s%d",stud[n+i].num,stud[n+i].name,&stud[n+i].score);
i++;
}
n+=num;
fclose(fp); /*关闭文件*/
printf("\t\t\tSuccessed!\n");
printf("\t\t\t");
system("pause");
return(n);
}
void WritetoText(Student stud[],int n) /*将所有记录写入文件*/
{
int i=0;
FILE *fp; /*定义文件指针*/
char filename[20]; /*定义文件名*/
printf("\t\t\tWrite Records to a Text File\n"); /*输入文件名*/
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL) /*打开文件*/
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循环写入数据*/
while(i<n)
{
fprintf(fp,"%-16s%-15s%d\n",stud[i].num,stud[i].name,stud[i].score);
i++;
}
fclose(fp); /*关闭文件*/
printf("Successed!\n"); /*返回成功信息*/
}
void main() /*主函数*/
{
int n=0;
for(;;)
{
switch(menu_select()) /*选择判断*/
{
case 1:
printf("\t\t\tInput Records\n"); /*输入若干条记录*/
n=Input(stu,n);
break;
case 2:
printf("\t\t\tDisplay All Records\n"); /*显示所有记录*/
Display(stu,n);
break;
case 3:
printf("\t\t\tSort\n");
Sort_by_num(stu,n); /*按学号排序*/
printf("\t\t\tSort Suceessed!\n");
printf("\t\t\t");
system("pause");
break;
case 4:
printf("\t\t\tInsert a Record\n");
n=Insert_a_record(stu,n); /*插入一条记录*/
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\tDelete a Record\n");
n=Delete_a_record(stu,n); /*按姓名查找,删除一条记录*/
printf("\t\t\t");
system("pause");
break;
case 6:
printf("\t\t\tQuery\n");
Query_a_record(stu,n); /*查找并显示一个记录*/
printf("\t\t\t");
system("pause");
break;
case 7:
printf("\t\t\tStatistic\n");
Statistic(stu,n); /*新增功能,输出统计信息*/
printf("\t\t\t");
system("pause");
break;
case 8:
printf("\t\t\tAdd Records from a Text File\n");
n=AddfromText(stu,n); /*新增功能,输出统计信息*/
break;
case 9:
printf("\t\t\tWrite to a Text file\n");
WritetoText(stu,n); /*循环写入数据*/
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\tHave a Good Luck,Bye-bye!\n"); /*结束程序*/
printf("\t\t\t");
system("pause");
exit(0);
}
}
}追问

辛苦了。真复杂。

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-10
#include<stdio.h>
void mian ()
{
float score[30][5];
float sum[5]={0,0,0,0,0},average[5],highest,lowest;
int i,j;
for(i=0;i<30;i++)
{
printf("\n输入第%d个学生的成绩",i);
for(j=0;j<5;j++)
scanf("%f",&score[i][j]);
}
highest=lowest=score[0][0];
for(j=0;j<5;j++)
{
for(i=0;i<30;i++)
{
if(score[i][j]>highest)
highest=score[i][j];
if(score[i][j]<lowest)
lowest=score[i][j];
sum[j]=sum[j]+score[i][j];
}
average[j]=sum[i]/30.0;
}
for(j=0;j<5;j++)
printf("第课%d平均分是%5.2f\n",j,average);
printf("最高分是%5.2f最低分是%5.2f\n",highest,lowest);
}

程序已经测试。追问

还有两个error

追答

void mian ()
改为 void main()

追问

第2个回答  2011-05-10
差不多的,自己修改下。

#include <stdio.h>
#include <string.h>

const int MAX_NUM = 5;

const int MAX_NAME_LEN = 127;
const int MAX_SUBJECT = 3;
const double SUBJECTS = 3.0;

typedef struct _Student
{
char szName[MAX_NAME_LEN + 1];
int nArryScore[MAX_SUBJECT];
int nSum;
double dAverage;
}Student;

void Sort(Student students[], int n)
{
Student stTmp;
int nTmp = 0;
int nPos = 0;
int nIndex = 0;
for (nPos = 0; nPos < n - 1; nPos++)
{
nTmp = nPos;
memset(&stTmp, 0, sizeof(Student));
for (nIndex = nPos + 1; nIndex < n; nIndex++)
{
if (students[nIndex].nSum > students[nTmp].nSum)
{
nTmp = nIndex;
}
}

memcpy(&stTmp, &students[nTmp], sizeof(Student));
memcpy(&students[nTmp], &students[nPos], sizeof(Student));
memcpy(&students[nPos], &stTmp, sizeof(Student));
}
}

int main (void)
{
printf("请输入五个同学三门课的成绩:\n");

Student students[MAX_NUM];
memset(students, 0, sizeof(students));

Student* pStudent = NULL;

int nCnt = 0;
int nPos = 0;
while (MAX_NUM > nCnt)
{
printf("第%d个同学的记录:", nCnt + 1);

pStudent = &students[nCnt];
scanf("%s %d %d %d", pStudent->szName,
&pStudent->nArryScore[0],
&pStudent->nArryScore[1],
&pStudent->nArryScore[2]);
for (nPos = 0; nPos < MAX_SUBJECT; nPos++)
{
pStudent->nSum += pStudent->nArryScore[nPos];
}

pStudent->dAverage = pStudent->nSum / SUBJECTS;
nCnt++;

getchar();
}

Sort(students, MAX_NUM);

printf("------------------------------------------------\n");
printf("输出排序后五个同学的三门成绩:\n");
printf("------------------------------------------------\n");
printf("排序 姓名 课1 课2 课3 总分 平均分\n");
for (nPos = 0; nPos < MAX_NUM; nPos++)
{
pStudent = &students[nPos];
printf("第%d名: %s %d %d %d %d %.2f\n", nPos + 1,
pStudent->szName,
pStudent->nArryScore[0],
pStudent->nArryScore[1],
pStudent->nArryScore[2],
pStudent->nSum,
pStudent->dAverage);
}

return 0;
}追问

辛苦了,程序很好用。

追答

给我分就可以了。

本回答被提问者采纳

编写程序:输入30个学生的五门成绩,计算并输出每科成绩的平均分,最高...
for(i=1;i<n+1;i++) \/*循环输入*\/ { printf("\\t\\t\\t%-16s%-15s%d\\n",stud[i-1].num,stud[i-1].name,stud[i-1].score);if(i>1&&i%10==0) \/*每十个暂停*\/ {printf("\\t\\t\\t---\\n"); \/*格式*\/ printf("\\t\\t\\t");system("pause");printf("\\t\\t\\t---\\n...

请问这个C程序怎么写?输入30个学生5门课程的成绩,按照每个学生的平均...
printf("\\t国文成绩:%d\\n",student[i].chinese);printf("\\t计算机成绩:%d\\n",student[i].computer);

C语言编程。一班级三十人。五门课程。编程..1。所有课程最高分。二...
printf("输出排序后五个同学的三门成绩:\\n");printf("---\\n");printf("排序 姓名 课1 课2 课3 总分 平均分\\n");for (nPos = 0; nPos < MAX_NUM; nPos++){ pStudent = &students[nPos];printf("第%d名: %s %d %d %d %d %.2f\\n", nPos + 1,pStudent->...

求C语言编写程序,输入学生五门课程的成绩,计算并输出总成绩和平均数
printf("请输入第%d门成绩:",i+1);scanf("%f",&score);total+=score;} ave=total\/5;printf("该生的总分为:%f\\n",total);printf("该生的平均分为:%f\\n",ave);system("pause");return 0;}

...编写程序的功能是从键盘上输入若干学生的成绩,统计并输出_百度知 ...
include<stdio.h> int main() {int n=0,i,x,max=-1,min=9999,s=0; do {scanf("%d",&x); if(x>=0) {s+=x; n++; if(x>max)max=x; if(x<min)min=x; } }while(x>=0); printf("人数=%d\\n总分=%d\\n最高分=%d\\n最低分=%d\\n平均分=%.2f",n,s,...

...并且算五门成绩总分和平均分和最高分,最低分?
double min =Integer.MAX_VALUE;\/\/整形的最大值 double max=Integer.MIN_VALUE;\/\/整形的最小值 double avg = 0;\/\/平均分 double temp = 0;\/\/输入的分数 for (int i = 0; i < 5; i++) { temp = s.nextFloat();sum=sum+temp;if (min > temp) { min = temp;} if(max<temp)...

求助c语言代码
接着,我们编写了一个函数generate_scores来生成随机成绩,并计算每个班、每门课程的平均分。然后,我们编写了一个函数sort_scores来对平均分进行排序。最后,在main函数中,我们调用这些函数来生成成绩、计算平均分和排序,并输出结果。

...输入一个学生某学期的M(M=5)门课程的成绩(整数型),计算并输出...
include<stdio.h> void main(){ int c1, c2, c3, c4,c5,c;printf("请输入五科的成绩:(以空格隔开)\\n");scanf("%d%d%d",&c1,&c2,&c3,&c4,&c5);c=c1+c2+c3+c4+c5;printf("平均成绩为%.1f\/n",c\/5.0);}

数组:设计程序统计一个班的五门成绩的各科总成绩和平均成绩,好要统计...
dim sum[5],average[5],max[5],min[5] '分别为总成绩、平均成绩、最高分、最低分 dim i,j for i=1 to 5 sum[i]=0 ‘初始化总成绩为0 max[i]=0 ’初始化最高分为0 min[i]=999 ‘初始化最低分为999 ,也可取最大的满分 next i for i=1 to 5 for j=1 to M...

...个学生的三门课考试成绩(百分制计算),计算并输出该学生的考试总成绩...
include<stdio.h> int main(void){ double a,b,c,p,q;while(scanf("%lf %lf %lf",&a,&b,&c)!=EOF){ p=a+b+c;q=p\/3.0;printf("%.1lf %.1lf\\n",p,q);} return 0;}

相似回答