任 務(wù):
??設(shè)計一個保存學(xué)生成績信息的結(jié)構(gòu)(包括學(xué)號、姓名、課程名、平時成績、考試成績、總評成績),用于保存N個學(xué)生信息。分別設(shè)計函數(shù)實(shí)現(xiàn)以下功能:
功 能:
??1.學(xué)生信息錄入:從文件studentin.txt讀取N個學(xué)生信息(包括學(xué)號、姓名、課程名、平時成績、考試成績),如文件不存在則從鍵盤輸入N個學(xué)生的信息(初始化),并將輸入信息存入文件studentin.txt中;
??2.學(xué)生信息修改:通過輸入學(xué)號查找并修改學(xué)生成績(平時成績、考試成績);
??3.學(xué)生成績統(tǒng)計:計算每個學(xué)生的總評成績(平時20%+考試80%),并輸出完整的學(xué)生信息;
??4.學(xué)生信息瀏覽:輸出全部學(xué)生信息;
??5.退出系統(tǒng):將處理后的學(xué)生信息數(shù)據(jù)存入studentout.txt文件,并退出系統(tǒng)。
要 求:
??1. 編寫函數(shù)實(shí)現(xiàn)各功能模塊;
??2. 在主函數(shù)中設(shè)計(并調(diào)用)一個菜單,輸入相關(guān)功能序號并調(diào)用各函數(shù)實(shí)現(xiàn);
void get_a_menu()
{printf(" Menu \n");
printf("********1-學(xué)生信息錄入********\n");
printf("********2-學(xué)生信息修改********\n");
printf("********3-學(xué)生成績統(tǒng)計********\n");
printf("********4-學(xué)生信息瀏覽********\n");
printf("**********5-退出系統(tǒng)**********\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("文件為空,請手動輸入學(xué)生成績信息。\n成績將同步到文件中。\n");
printf("輸入“1”開始,輸入”-1“結(jié)束:");
scanf("%d",&flag); getchar();
while(flag!=-1)
{ if(head!=NULL)
{ fprintf(fp,"\n");
}
p=(STU *)malloc(sizeof(STU));
printf("學(xué)生學(xué)號:"); gets(temp);
p->stu_num=(char *)malloc(strlen(temp)+1);
strcpy(p->stu_num,temp); fprintf(fp,"%s ",p->stu_num);
printf("學(xué)生姓名:");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”繼續(xù),輸入”-1“結(jié)束:");
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("學(xué)號\t學(xué)生姓名\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("請輸入需要修改信息的學(xué)生的學(xué)號:");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請輸入相應(yīng)功能編號:");
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-學(xué)生信息錄入********\n");
printf("********2-學(xué)生信息修改********\n");
printf("********3-學(xué)生成績統(tǒng)計********\n");
printf("********4-學(xué)生信息瀏覽********\n");
printf("**********5-退出系統(tǒng)**********\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("文件為空,請手動輸入學(xué)生成績信息。\n成績將同步到文件中。\n");
printf("輸入“1”開始,輸入”-1“結(jié)束:");
scanf("%d",&flag); getchar();
while(flag!=-1)
{ if(head!=NULL)
{ fprintf(fp,"\n");
}
p=(STU *)malloc(sizeof(STU));
printf("學(xué)生學(xué)號:"); gets(temp);
p->stu_num=(char *)malloc(strlen(temp)+1);
strcpy(p->stu_num,temp); fprintf(fp,"%s ",p->stu_num);
printf("學(xué)生姓名:");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”繼續(xù),輸入”-1“結(jié)束:");
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("學(xué)號\t學(xué)生姓名\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("請輸入需要修改信息的學(xué)生的學(xué)號:");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;
}
事后總結(jié)
事后總結(jié)
事后總結(jié)你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧