跪求:用C语言编写学生信息管理系统

2024-12-04 14:50:53
推荐回答(1个)
回答1:

网上有很多类似的例子,改改就可以了
#include "stdio.h"
#include"stdlib.h"
#include "string.h"
#include "graphics.h"
#include "dos.h"
#define M 200 /*定义常数M,表示最大记录数*/
struct date1
{
int year;
int month;
int day;
};
typedef struct
{
char number[10]; /*学号*/
char name[10]; /*姓名*/
char sex[2]; /*性别*/
struct date1 birthday;/*出生日期*/
int age ; /*年龄*/
float chinese;/*语文*/
float maths; /*数学*/
float english; /*英语*/
}ADDRESS;
int enter(ADDRESS t[]); /*输入学生记录信息*/
void list(ADDRESS t[],int n); /*显示学生信息*/
int menu_select(); /*主菜单函数*/
void print(ADDRESS t); /*显示指定的一条记录*/
int find(ADDRESS t[],int n,char *s) ; /*查找函数*/
void search(ADDRESS t[],int n); /*按姓名查找显示记录*/
int delet(ADDRESS t[],int n); /*删除记录*/
int add(ADDRESS t[],int n); /*插入记录*/
void save(ADDRESS t[],int n,char*s);
int load(ADDRESS t[],char*s);
void sort(ADDRESS t[], int n,int m);
void sss();
char e;
char*s;
void main()
{
int i;
int length;
char c,s[20];
ADDRESS adr[M]; /*定义结构体数组*/
printf("\n\n Welcome to the system! \n"); /*欢迎词*/
getch(); /*压任意键继续*/
length=load(adr,"aaa.txt");
aa: for(;;) /*无限循环*/
{ i=menu_select(); /*调用主菜单函数,返回值整数作开关语句的条件*/
switch(i)
{
case 0: sss();
getchar();
printf("This will clear the previous reconds!!!\n");
sss();
printf("Are you sure(y/n)");
c=getchar();
if(c=='y')
length=enter(adr);/*输入记录*/
else goto aa;
break;
case 1: sss();
list(adr,length); break; /*显示全部记录*/
case 2: sss();
printf("\nplease input num \n"); /*提示信息输入下标*/
scanf("%d",&i); /*输入选择项*/
if(i>0&&i<=length) /* 判断是否下标是否出界*/
print(adr[i-1]); /*显示指定的那条记录*/
else printf(" Which did not record!"); break;
case 3: sss();
search(adr,length);break; /*查找记录*/
case 4: sss();
length=delete(adr,length);break; /*删除记录*/
case 5: sss();
length=add(adr,length); break; /*插入记录*/
case 6: sss();
printf("please input the file name:");
scanf("%s",s);
save(adr,length,s);break;
case 7: sss();
printf("please input the file name:");
scanf("%s",s);
sss();
length=load(adr,s); break;

case 8: sss();
printf("sort by number or by name?(0/1):");
scanf("%d",&e);
sss();
sort(adr,length,e); break;
case 9: sss();
clrscr(); /*清屏*/
save(adr,length,"aaa.txt");
printf("\n\n See you later!\n");
sss();
exit(0); /*如返回值为2则程序结束*/
}
}
}
/*菜单函数,函数返回值为整数,代表所选的菜单项*/
menu_select()
{
int c;
char s[5];
clrscr();

printf("\n\n *********************************MENU**********************************\n");
printf(" ! 0. Enter record | !\n");
printf(" ! 1. List the file | !\n");
printf(" ! 2. List A record | !\n");
printf(" ! 3. Search record on name | !\n");
printf(" ! 4. Delete a record | !\n");
printf(" ! 5. add record | !\n");
printf(" ! 6. save as the file | !\n");
printf(" ! 7. lod a file | !\n");
printf(" ! 8. sort to make a new file | !\n");
printf(" ! 9. Quit | !\n");
printf(" ***********************************************************************\n");
do{
printf("\n Enter you choice(0~9):"); /*提示输入选项*/
scanf("%s",s);
c=atoi(s);
if(c==0&&strcmp(s,"0")!=0) c=1000;
}while(c<0||c>9); /*选择项不在0~6之间重输入*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
/***输入通讯录,形参为结构体数组,函数值返回类型为整型表示记录长度*/
int enter(ADDRESS t[])
{
int i,n;char g[5];
clrscr(); /*清屏*/
printf("\nplease input num \n"); /*提示信息*/
scanf("%s",g); /*输入记录数*/
n=atoi(g);
printf("please input record \n"); /*提示输入记录*/
printf("Number Name Sex Birthday(year,month,day) Age Chinese Maths English\n");
printf("-------------------------------------------------------------------------\n");
for(i=0;i{
scanf("%s%s%s%d%d%d%d%f%f%f",t[i].number,t[i].name,t[i].sex,&t[i].birthday.year,&t[i].birthday.month,&t[i].birthday.day,&t[i].age,&t[i].chinese,&t[i].maths,&t[i].english); /*输入记录*/
printf("-------------------------------------------------------------------------\n");
}
return n; /*返回记录条数*/
}
/*显示记录,参数为记录数组和记录条数*/
void list(ADDRESS t[],int n)
{
int i;
clrscr();
printf("*************************************************************************\n");
printf("Number Name Sex Birthday Age Chinese Maths English\n");
printf("-------------------------------------------------------------------------\n");
for(i=0;i{
printf("%-11s%-11s%-8s%-d%-d%-8d%-7d%-8.1f%-8.1f%-8.1f\n",t[i].number,t[i].name,t[i].sex,t[i].birthday.year, t[i].birthday.month,t[i].birthday.day,t[i].age,t[i].chinese,t[i].maths,t[i].english);
if((i+1)%10==0) /*判断输出是否达到10条记录*/
{
printf("Press any key continue...\n"); /*提示信息*/
getch(); /*压任意键继续*/
}
}
printf("***********************************end***********************************\n");
printf("\n\nPress any key continue...\n"); /*提示信息*/
getch(); /*压任意键继续*/
}
/*显示指定的一条记录*/
void print(ADDRESS t)
{
clrscr();
printf("\n\n*************************************************************************\n");
printf("Number Name Sex Birthday Age Chinese Maths English\n");
printf("-------------------------------------------------------------------------\n");
printf("%-11s%-11s%-8s%-d%-d%-8d%-7d%-8.1f%-8.1f%-8.1f\n",t.number,t.name,t.sex,t.birthday.year, t.birthday.month,t.birthday.day,t.age,t.chinese,t.maths,t.english);
/* printf("%-11s%-11s%-8s%-15s%-7d%-8.1f%-8.1f%-8.1f\n",t.number,t.name,t.sex,t.birthday,t.age,t.chinese,t.maths,t.english); */
printf("**************************************end**********************************\n");
printf("\n\nPress any key continue...\n"); /*提示信息*/
getch(); /*压任意键继续*/
}
/*查找函数,参数为记录数组和记录条数以及姓名s */
int find(ADDRESS t[],int n,char *s)
{
int i;
for(i=0;i{
if(strcmp(s,t[i].name)==0) /*记录中的姓名和待比较的姓名是否相等*/
return i; /*相等,则返回该记录的下标号,程序提前结结束*/
}
return n; /*返回n值*/
}
/*查找记录*/
void search(ADDRESS t[],int n)
{
char s[20]; /*保存待查找姓名字符串*/
int i; /*保存查找到结点的序号*/
clrscr(); /*清屏*/
printf("please search name:");
scanf("%s",s); /*输入待查找姓名*/
i=find(t,n,s); /*调用find函数,得到一个整数*/
if(i>n-1) /*如果整数i值大于n-1,说明没找到*/
{ printf("not found\n");
getch(); /*压任意键继续*/ }
else
print(t[i]); /*找到,调用显示函数显示记录*/
}
/*删除函数,参数为记录数组和记录条数*/
int delete(ADDRESS t[],int n)
{
char s[20]; /*要删除记录的姓名*/
int i,j;
printf("please deleted name:"); /*提示信息*/
scanf("%s",s);/*输入姓名*/
i=find(t,n,s); /*调用find函数*/
if(i>n-1) /*如果i>n-1超过了数组的长度*/
{ printf("no found not deleted\n"); /*显示没找到要删除的记录*/
getch(); /*压任意键继续*/ }
else
{
print(t[i]); /*调用输出函数显示该条记录信息*/
printf("Are you sure delete it(y/n)\n"); /*确认是否要删除*/
if(getch()=='y') /*如果确认删除*/
{
for(j=i+1;j{
strcpy(t[j-1].number,t[j].number);
strcpy(t[j-1].name,t[j].name);
strcpy(t[j-1].sex,t[j].sex);
t[j-1].birthday.year=t[j].birthday.year;
t[j-1].birthday.month=t[j].birthday.month;
t[j-1].birthday.day=t[j].birthday.day;

/*strcpy(t[j-1].birthday,t[j].birthday); */
t[j-1].age=t[j].age;
t[j-1].chinese=t[j].chinese;
t[j-1].maths=t[j].maths;
t[j-1].english=t[j].english;
}
n--; /*记录数减1*/
}
}
return n; /*返回记录数*/
}
/*插入记录函数,参数为结构体数组和记录数*/
int add(ADDRESS t[],int n)/*插入函数,参数为结构体数组和记录数*/
{
ADDRESS temp; /*新插入记录信息*/
int i,j;
char s[20]; /*确定插入在哪个记录之前*/
printf("please input record:\n");
printf("*************************************************************************\n");
printf("Number Name Sex Birthday Age Chinese Maths English\n");
printf("-------------------------------------------------------------------------\n");
scanf("%s%s%s%d%d%d%d%f%f%f",temp.number,temp.name,temp.sex,&temp.birthday.year,&temp.birthday.month,&temp.birthday.day,&temp.age,&temp.chinese,&temp.maths,&temp.english);

/* scanf("%s%s%s%s%d%f%f%f",temp.number,temp.name,temp.sex,temp.birthday,&temp.age,&temp.chinese,&temp.maths,&temp.english); */

printf("-------------------------------------------------------------------------\n");
printf("please input locate name \n");
scanf("%s",s); /*输入插入位置的姓名*/
i=find(t,n,s); /*调用find,确定插入位置*/
for(j=n-1;j>=i;j--) /*从最后一个结点开始向后移动一条*/
{
strcpy(t[j+1].number,t[j].number);
strcpy(t[j+1].name,t[j].name);
strcpy(t[j+1].sex,t[j].sex);
t[j+1].birthday.year=t[j].birthday.year;
t[j+1].birthday.month=t[j].birthday.month;
t[j+1].birthday.day=t[j].birthday.day;
t[j+1].age=t[j].age;
t[j+1].chinese=t[j].chinese;
t[j+1].maths=t[j].maths;
t[j+1].english=t[j].english;

}

strcpy(t[i].number,temp.number);
strcpy(t[i].name,temp.name);
strcpy(t[i].sex,temp.sex);
t[i].birthday.year=temp.birthday.year;
t[i].birthday.month=temp.birthday.month;
t[i].birthday.day=temp.birthday.day;
t[i].age=temp.age;
t[i].chinese=temp.chinese;
t[i].maths=temp.maths;
t[i].english=temp.english;

n++; /*记录数加1*/
return n; /*返回记录数*/
}
void save(ADDRESS t[],int n,char*s)
{
int i;
FILE *fp;
if((fp=fopen(s,"wb"))==NULL)
{
printf("can not open the file\n");
exit(1);
}
fprintf(fp,"%d\r\n",n);
for(i=0;i{
fprintf(fp,"%-11s%-11s%-8s%-d%-d%-8d%-7d%-8.1f%-8.1f%-8.1f\n",t[i].number,t[i].name,t[i].sex,t[i].birthday.year,t[i].birthday.month,t[i].birthday.day,t[i].age,t[i].chinese,t[i].maths,t[i].english);

}
fclose(fp);
printf("save success!");
}
int load(ADDRESS t[],char*s)
{
int i,n;
FILE *fp;
if((fp=fopen(s,"rb"))==NULL)
{
printf("can not open the file\n");
getch();
return 0;
}
fscanf(fp,"%d\r\n",&n);
for(i=0;i{

fscanf(fp,"%s%s%s%d%d%d%d%f%f%f",t[i].number,t[i].name,t[i].sex,&t[i].birthday.year,&t[i].birthday.month,&t[i].birthday.day,&t[i].age,&t[i].chinese,&t[i].maths,&t[i].english);

fprintf(fp,"\n");
}
fclose(fp);
if(n>0)
printf("%d! success read from file",n);
else printf("Not data");
getch();
return n;
}
void sort(ADDRESS t[], int n, int m)
{
int i,j,k;

char b[15],c[20],d[1];
int f[3];
int h;
float w,s,x;
switch(m)
{

case 0:
for(i=0;i{
k=i;
for(j=i+1;jif(strcmp(t[j].number,t[k].number)<0)
k=j;

strcpy(b,t[k].number); strcpy(c,t[k].name); strcpy(d,t[k].sex);
f[0]=t[k].birthday.year;f[1]=t[k].birthday.month;f[2]=t[k].birthday.day;
h=t[k].age; w=t[k].chinese; s=t[k].maths; x=t[k].english;

strcpy(t[k].number,t[i].number);strcpy(t[k].name,t[i].name);strcpy(t[k].sex,t[i].sex);
t[k].birthday.year=t[i].birthday.year;t[k].birthday.month=t[i].birthday.month;t[k].birthday.day=t[i].birthday.day;
t[k].age=t[i].age; t[k].chinese=t[i].chinese; t[k].maths=t[i].maths; t[k].english=t[i].english;

strcpy(t[i].number,b);strcpy(t[i].name,c);strcpy(t[i].sex,d);
t[i].birthday.year=f[0];t[i].birthday.month=f[1];t[i].birthday.day=f[2];
t[i].age=h; t[i].chinese=w; t[i].maths=s; t[i].english=x;

}
sss();
printf("sort by name success!");
getch();
break;

case 1:
for(i=0;i{
k=i;
for(j=i+1;jif(strcmp(t[j].name,t[k].name)<0)
k=j;

strcpy(b,t[k].number); strcpy(c,t[k].name); strcpy(d,t[k].sex);
f[0]=t[k].birthday.year;f[1]=t[k].birthday.month;f[2]=t[k].birthday.day;
h=t[k].age; w=t[k].chinese; s=t[k].maths; x=t[k].english;

strcpy(t[k].number,t[i].number);strcpy(t[k].name,t[i].name);strcpy(t[k].sex,t[i].sex);
t[k].birthday.year=t[i].birthday.year;t[k].birthday.month=t[i].birthday.month;t[k].birthday.day=t[i].birthday.day;
t[k].age=t[i].age; t[k].chinese=t[i].chinese; t[k].maths=t[i].maths; t[k].english=t[i].english;

strcpy(t[i].number,b);strcpy(t[i].name,c);strcpy(t[i].sex,d);
t[i].birthday.year=f[0];t[i].birthday.month=f[1];t[i].birthday.day=f[2];
t[i].age=h; t[i].chinese=w; t[i].maths=s; t[i].english=x;
}
sss();
printf("sort by unit success!");
getch();
break;

default: printf("your number inputs is wrong ,pless input a right number.");
sss();
getch();
break;
}

}
void sss()
{
int z;
for(z=1;z<50;z++)
{ sound(z*300);
delay(2000);}
nosound();
}