實(shí)驗(yàn)6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define len 15
#define N 50
#define MAX_LINE 1024
struct Student{
? ? ? ?int q;
? ?char code[len+1];
? ?char name[len+1];
? ?float score[3];
? ?float average;
? ?float total;?
}stu[N];
int k=1,n=0,m=0; /*全局變量,n為當(dāng)前記錄的學(xué)生人數(shù),m為新增加的學(xué)生人數(shù)*/
void insert(); //插入信息,并計(jì)算總分和平均分?
void modify(); //修改信息
void del(); //刪除信息?
void menu();?
void sort();?
int main()
{
? ? ?while(k) menu();
? ? ?system("pause");
return 0;?
}? ?
void insert(){
? ? ?int i=n,j,flag;
printf("請(qǐng)輸入新增加的學(xué)生數(shù):\n");?
scanf("%d",&m);
if(m>0){
? ? ? ? do{
? ? ?flag=1;
while(flag){
? ? ?flag=0;
? ? ?printf("請(qǐng)輸入第 %d 個(gè)學(xué)生的學(xué)號(hào):\n",i+1);
? ? ?scanf("%s",stu[i].code);
? ? ?for(j=0;j<i;j++)?
? ? ? ? ? ? ?if(strcmp(stu[i].code,stu[j].code)==0){
? ? ? ? ?printf("已有該學(xué)號(hào),請(qǐng)檢查后重新輸入!\n");
flag=1;
break;
}?
}?
printf("請(qǐng)輸入第 %d 個(gè)學(xué)生的學(xué)號(hào):\n",i+1);
scanf("%s",stu[i].code);
printf("請(qǐng)輸入第 %d 個(gè)學(xué)生的姓名:\n",i+1);
scanf("%d",&stu[i].name);
printf("請(qǐng)輸入第 %d 個(gè)學(xué)生的語(yǔ)文成績(jī):\n",i+1);
scanf("%f",&stu[i].score[0]);
printf("請(qǐng)輸入第 %d 個(gè)學(xué)生的數(shù)學(xué)成績(jī):\n",i+1);
scanf("%f",&stu[i].score[1]);
printf("請(qǐng)輸入第 %d 個(gè)學(xué)生的英語(yǔ)成績(jī):\n",i+1);
scanf("%f",&stu[i].score[2]);?
stu[i].total=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
stu[i].average=stu[i].total/3.0;?
if(flag==0) i++;
? ? }while(i<n+m);
}
n+=m;
printf("信息增加完畢!\n\n");
sort();
system("pause");
}?
void modify(){
? ? ? ? ?int i,item,num=-1;
char sex1,s1[len+1],s2[len+1]; /*最長(zhǎng)長(zhǎng)度加1*/
float score1;
printf("請(qǐng)輸入要修改的學(xué)生的學(xué)號(hào):\n");
scanf("%s",s1);
for(i=0;i<n;i++)
? ? ? ? ?if(strcmp(stu[i].code,s1)==0)
num=i; /*保存要修改學(xué)生的序號(hào)*/
? ? ?if(num!=-1){
? ? ?printf("------------------------\n");?
printf("1.修改姓名\n");
printf("2.修改語(yǔ)文成績(jī)\n");?
printf("3.修改數(shù)學(xué)成績(jī)\n");
printf("4.修改英語(yǔ)成績(jī)\n");
printf("5.退出本菜單\n");
printf("------------------------\n");
while(1){
? ? ? ? ?printf("請(qǐng)選擇子菜單編號(hào):");?
scanf("%d",&item);?
switch(item){
? ? ? ? ? ? ?case 1:
? ? ?printf("請(qǐng)輸入新的姓名:\n");
? ? ?scanf("%s",s2);?
strcpy(stu[num].name,s2);
break;
? ? ? ? ?case 2:
? ? ?printf("請(qǐng)輸入新的語(yǔ)文成績(jī):\n");
scanf("%f",&score1);
stu[num].score[0]=score1;?
break;
case 3:
? ? ?printf("請(qǐng)輸入新的數(shù)學(xué)成績(jī):\n");
scanf("%f",&score1);
stu[num].score[1]=score1;?
break;?
case 4:
? ? ?printf("請(qǐng)輸入新的英語(yǔ)成績(jī):\n");
scanf("%f",&score1);
stu[num].score[2]=score1;?
break;
? ? ?case 5:
? ? ?return;
default:
? ? ?printf("請(qǐng)?jiān)?-5之間選擇\n");
}?
} ??
? ? ?}
printf("修改完畢!請(qǐng)及時(shí)保存!\n");
system("pause");
}?
void del(){
? ? ? ? ?int i,j,flag=0;
? ? ? ? ?char s1[len+1];
? ? ? ? ?printf("請(qǐng)輸入要?jiǎng)h除學(xué)生的學(xué)號(hào):\n");
scanf("%s",s1);
for(i=0;i<n;i++)
? ? ? ? ?if(strcmp(stu[i].code,s1)==0){ //查找學(xué)生記錄?
? ? ?flag=1;
for(j=i;j<n-1;j++)
? ? ? ? ?stu[j]=stu[j+1];
? ? ?}
if(flag==0) printf("該學(xué)號(hào)不存在!\n");?
if(flag==1) {
? ? ? ? ?printf("刪除成功!\n");
n--;
}
system("pause");
}?