增加或刪除學(xué)生信息在C語言的程序中如何編寫?
#include "stdio.h"
struct student
{
int ID;
char name[20];
struct
{
int year,month,day;
}birthday;
float scores[3];
char address[100];
};
void stud_out(struct student *a,int n);
int stud_ins(struct student *a,int n);
void stud_del(struct student *a,int *n,int j);
int main()
{
struct student stu[50]={
{1001,"李天驕",1990,12,3,87.5,93.6,79.5,"北京聯(lián)合大學(xué)信息學(xué)院97號"},
{1002,"張璍萸",1991,2,13,97.5,83.6,89.5,"北京聯(lián)合大學(xué)信息學(xué)院97號"},
{1003,"陳自逸",1989,10,31,77.5,88.6,79.5,"北京聯(lián)合大學(xué)信息學(xué)院97號"}};
int num=3;
int n,k;
stud_out(stu,num);
while(1)
{
printf("請選擇操作內(nèi)容:1.增加學(xué)生;2.刪除學(xué)生;0.退出系統(tǒng)!\n");
scanf("%d",&k);
if(k==1)
{
num=stud_ins(stu,num);
stud_out(stu,num);
}
else if(k==2)
{
printf("請選擇刪除第幾個學(xué)生\n");
scanf("%d",&n);
stud_del(stu,&num,n);
printf("刪除第%d個學(xué)生后\n",n);
stud_out(stu,num);
}
else
{
break;
}
}
return 0;
}
int stud_ins(struct student *a,int n)
{
int i;
struct student tmp;
printf("輸入學(xué)號\n");
scanf("%d",&tmp.ID);
printf("輸入姓名\n");
fflush(stdin);
scanf("%s",tmp.name);
printf("輸入生日(年月日)\n");
scanf("%d",&tmp.birthday.year);
scanf("%d",&tmp.birthday.month);
scanf("%d",&tmp.birthday.day);
printf("請輸入C實驗、C語言理論、C筆試成績\n");
for(i=0;i<3;i++)
scanf("%f",&tmp.scores[i]);
printf("請輸入地址\n");
fflush(stdin);
scanf("%s",&tmp.address);
a[n]=tmp;
return n+1;
}
void stud_del(struct student *a,int *n,int j)
{
int i;
for(i=j-1;i<*n-1;i++)
a[i]=a[i+1];
(*n)--;
}
void stud_out(struct student *a,int n)
{
int i,k;
printf(" 學(xué)號 姓名 生日 C實驗 C理論 C筆試 地 址\n");
for(i=0;i<n;i++)
{
printf("%5d",a[i].ID);
printf("%7s",a[i].name);
printf("%5d",a[i].birthday.year);
printf("%3d",a[i].birthday.month);
printf("%3d",a[i].birthday.day);
for(k=0;k<3;k++)
printf("%7.1f",a[i].scores[k]);
printf("\t%s",a[i].address);
printf("\n");
}
}
點擊加載更多評論>>