任 务:
设计一个保存学生成绩信息的结构(包括学号、姓名、课程名、平时成绩、考试成绩、总评成绩),用于保存N个学生信息。分别设计函数实现以下功能:
功 能:
1.学生信息录入:从文件studentin.txt读取N个学生信息(包括学号、姓名、课程名、平时成绩、考试成绩),如文件不存在则从键盘输入N个学生的信息(初始化),并将输入信息存入文件studentin.txt中;
2.学生信息修改:通过输入学号查找并修改学生成绩(平时成绩、考试成绩);
3.学生成绩统计:计算每个学生的总评成绩(平时20%+考试80%),并输出完整的学生信息;
4.学生信息浏览:输出全部学生信息;
5.退出系统:将处理后的学生信息数据存入studentout.txt文件,并退出系统。
要 求:
1. 编写函数实现各功能模块;
2. 在主函数中设计(并调用)一个菜单,输入相关功能序号并调用各函数实现;
void get_a_menu()
{printf(" Menu \n");
printf("********1-学生信息录入********\n");
printf("********2-学生信息修改********\n");
printf("********3-学生成绩统计********\n");
printf("********4-学生信息浏览********\n");
printf("**********5-退出系统**********\n");
}
STU * input(STU *head)
{STU *tail=head;
STU *p;
char temp[30];
FILE *fp;
fp=fopen("studentin.txt","r+");
char c; c=fgetc(fp); rewind(fp);
if(c==EOF)
{int flag;
printf("文件为空,请手动输入学生成绩信息。\n成绩将同步到文件中。\n");
printf("输入“1”开始,输入”-1“结束:");
scanf("%d",&flag); getchar();
while(flag!=-1)
{ if(head!=NULL)
{ fprintf(fp,"\n");
}
p=(STU *)malloc(sizeof(STU));
printf("学生学号:"); gets(temp);
p->stu_num=(char *)malloc(strlen(temp)+1);
strcpy(p->stu_num,temp); fprintf(fp,"%s ",p->stu_num);
printf("学生姓名:");gets(temp);
p->stu_name=(char *)malloc(strlen(temp)+1);
strcpy(p->stu_name,temp); fprintf(fp,"%s ",p->stu_name);
printf("输入课程名:");gets(temp);
p->course_name=(char *)malloc(strlen(temp)+1);
strcpy(p->course_name,temp); fprintf(fp,"%s ",p->course_name);
printf("输入平时分:");scanf("%lf",&p->daily_score);
fprintf(fp,"%.1f ",p->daily_score);
printf("输入考试分:") ;scanf("%lf",&p->exam_score);
fprintf(fp,"%.1f ",p->exam_score);
p->total_score=p->daily_score*.2+p->exam_score*.8;
fprintf(fp,"%.1f",p->total_score);
p->next=NULL;
if(head==NULL)
{ head=p;
tail=p;
}
else
{ tail->next=p;
tail=p;
}
printf("输入“1”继续,输入”-1“结束:");
scanf("%d",&flag); getchar();
}
printf("输入完毕,成绩已保存。\n");
fclose(fp);
}
else
{while(!feof(fp))
{ p=(STU *)malloc(sizeof(STU));
fscanf(fp,"%s",temp);
p->stu_num=(char *)malloc(strlen(temp)+1);strcpy(p->stu_num,temp);
fscanf(fp,"%s",temp);
p->stu_name=(char *)malloc(strlen(temp)+1);strcpy(p->stu_name,temp);
fscanf(fp,"%s",temp);
p->course_name=(char *)malloc(strlen(temp)+1);strcpy(p->course_name,temp);
fscanf(fp,"%lf",&p->daily_score);
fscanf(fp,"%lf",&p->exam_score);
fscanf(fp,"%lf",&p->total_score);
p->next=NULL;
if(head==NULL)
{ head=p;
tail=p;
}
else
{ tail->next=p;
tail=p;
}
}
printf("信息从文件录入完毕。");
fclose(fp);
}
return head;
}
void grade_check(STU *head)
{STU *p=head;
if(p==NULL)
{printf("未录入成绩,输入”1“录入成绩。");
}
else
{printf("学号\t学生姓名\t课程名\t平时分\t考试分\t总评分\n");
while(p!=NULL)
{printf("%s\t%s\t\t%s\t%.1f\t%.1f\t%.1f\n",p->stu_num,p->stu_name,p->course_name,
p->daily_score,p->exam_score,p->total_score);
p=p->next;
}
printf("\n成绩输出完毕。");
}
}
STU * modify(STU *head)
{if(head==NULL)
{printf("未录入成绩,输入”1“录入成绩。");
}
else
{char key[30]; STU *p=head; getchar();
printf("请输入需要修改信息的学生的学号:");gets(key);
while(p!=NULL)
{ if(strcmp(key,p->stu_num)==0)
{ printf("请输入修改后的平时分:");scanf("%lf",&p->daily_score);
printf("请输入修改后的考试分:");scanf("%lf",&p->exam_score);
p->total_score=p->daily_score*.2+p->exam_score*.8;
break;
}
else
{ p=p->next;
}
}
if(p!=NULL)
{ p=head;
FILE *fp;
fp=fopen("studentin.txt","w+");
while(p!=NULL)
{ if(p->next!=NULL)
{fprintf(fp,"%s %s %s %.1f %.1f %.1f\n",p->stu_num,p->stu_name,p->course_name,
p->daily_score,p->exam_score,p->total_score);
}
else
{fprintf(fp,"%s %s %s %.1f %.1f %.1f",p->stu_num,p->stu_name,p->course_name,
p->daily_score,p->exam_score,p->total_score);
}
p=p->next;
}
printf("修改完成!已同步到文件。\n");
fclose(fp);
}
else
{ printf("查无此人。\n");
}
}
return head;
}
最后答案
最后答案
最后答案#include#include#includestruct student
{char *stu_num;
char *stu_name;
char *course_name;
double daily_score;
double exam_score;
double total_score;
struct student *next;
};
typedef struct student STU;
void get_a_menu();
STU * input(STU *);
void grade_check(STU *);
STU * modify(STU *);
int main()
{STU *head=NULL;
get_a_menu();
while(1)
{int n;
printf("\n\n请输入相应功能编号:");
scanf("%d",&n);
switch(n)
{ case 1: head=NULL;head=input(head);break;
case 2: head=modify(head);break;
case 3: grade_check(head);break;
case 4: grade_check(head);break;
case 5:exit(0);
default : printf("Input error! Try again.\n");
}
}
return 0;
}
void get_a_menu()
{printf(" Menu \n");
printf("********1-学生信息录入********\n");
printf("********2-学生信息修改********\n");
printf("********3-学生成绩统计********\n");
printf("********4-学生信息浏览********\n");
printf("**********5-退出系统**********\n");
}
STU * input(STU *head)
{STU *tail=head;
STU *p;
char temp[30];
FILE *fp;
fp=fopen("studentin.txt","r+");
char c; c=fgetc(fp); rewind(fp);
if(c==EOF)
{int flag;
printf("文件为空,请手动输入学生成绩信息。\n成绩将同步到文件中。\n");
printf("输入“1”开始,输入”-1“结束:");
scanf("%d",&flag); getchar();
while(flag!=-1)
{ if(head!=NULL)
{ fprintf(fp,"\n");
}
p=(STU *)malloc(sizeof(STU));
printf("学生学号:"); gets(temp);
p->stu_num=(char *)malloc(strlen(temp)+1);
strcpy(p->stu_num,temp); fprintf(fp,"%s ",p->stu_num);
printf("学生姓名:");gets(temp);
p->stu_name=(char *)malloc(strlen(temp)+1);
strcpy(p->stu_name,temp); fprintf(fp,"%s ",p->stu_name);
printf("输入课程名:");gets(temp);
p->course_name=(char *)malloc(strlen(temp)+1);
strcpy(p->course_name,temp); fprintf(fp,"%s ",p->course_name);
printf("输入平时分:");scanf("%lf",&p->daily_score);
fprintf(fp,"%.1f ",p->daily_score);
printf("输入考试分:") ;scanf("%lf",&p->exam_score);
fprintf(fp,"%.1f ",p->exam_score);
p->total_score=p->daily_score*.2+p->exam_score*.8;
fprintf(fp,"%.1f\n",p->total_score);
p->next=NULL;
if(head==NULL)
{ head=p;
tail=p;
}
else
{ tail->next=p;
tail=p;
}
printf("输入“1”继续,输入”-1“结束:");
scanf("%d",&flag); getchar();
}
printf("输入完毕,成绩已保存。\n");
fclose(fp);
}
else
{while(!feof(fp))
{ p=(STU *)malloc(sizeof(STU));
fscanf(fp,"%s",temp);
p->stu_num=(char *)malloc(strlen(temp)+1);strcpy(p->stu_num,temp);
fscanf(fp,"%s",temp);
p->stu_name=(char *)malloc(strlen(temp)+1);strcpy(p->stu_name,temp);
fscanf(fp,"%s",temp);
p->course_name=(char *)malloc(strlen(temp)+1);strcpy(p->course_name,temp);
fscanf(fp,"%lf",&p->daily_score);
fscanf(fp,"%lf",&p->exam_score);
fscanf(fp,"%lf",&p->total_score);
p->next=NULL;
if(head==NULL)
{ head=p;
tail=p;
}
else
{ tail->next=p;
tail=p;
}
}
printf("信息从文件录入完毕。");
fclose(fp);
}
return head;
}
void grade_check(STU *head)
{STU *p=head;
if(p==NULL)
{printf("未录入成绩,输入”1“录入成绩。");
}
else
{printf("学号\t学生姓名\t课程名\t平时分\t考试分\t总评分\n");
while(p!=NULL)
{printf("%s\t%s\t\t%s\t%.1f\t%.1f\t%.1f\n",p->stu_num,p->stu_name,p->course_name,
p->daily_score,p->exam_score,p->total_score);
p=p->next;
}
printf("\n成绩输出完毕。");
}
}
STU * modify(STU *head)
{if(head==NULL)
{printf("未录入成绩,输入”1“录入成绩。");
}
else
{char key[30]; STU *p=head; getchar();
printf("请输入需要修改信息的学生的学号:");gets(key);
while(p!=NULL)
{ if(strcmp(key,p->stu_num)==0)
{ printf("请输入修改后的平时分:");scanf("%lf",&p->daily_score);
printf("请输入修改后的考试分:");scanf("%lf",&p->exam_score);
p->total_score=p->daily_score*.2+p->exam_score*.8;
break;
}
else
{ p=p->next;
}
}
if(p!=NULL)
{ p=head;
FILE *fp;
fp=fopen("studentin.txt","w+");
while(p!=NULL)
{ if(p->next!=NULL)
{fprintf(fp,"%s %s %s %.1f %.1f %.1f\n",p->stu_num,p->stu_name,p->course_name,
p->daily_score,p->exam_score,p->total_score);
}
else
{fprintf(fp,"%s %s %s %.1f %.1f %.1f",p->stu_num,p->stu_name,p->course_name,
p->daily_score,p->exam_score,p->total_score);
}
}
printf("修改完成!已同步到文件。\n");
fclose(fp);
}
else
{ printf("查无此人。\n");
}
}
return head;
}
事后总结
事后总结
事后总结你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧