在TC下调试运行通过。
没有注释,阅读比较困难。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define maxmenu 7
struct student
{
long sno;
char sname[30];
char sclass[30];
float sscore;
};
void dispstr(char ch[]);
int Add(struct student stu[],int n);
int Del(struct student stu[],int n);
void Seek(struct student stu[],int n);
void seekbysno(struct student stu[],int n);
void seekbysname(struct student stu[],int n);
void seekbysclass(struct student stu[],int n);
void seekbysscore(struct student stu[],int n);
void Sort(struct student stu[],int n);
void sortbysno(struct student stu[],int n);
void sortbysscore(struct student stu[],int n);
void Correct(struct student stu[],int n);
void Disp(struct student stu[],int n);
void Disptxt();
void pri(struct student stu[],int i);
void num(int n);
main()
{
struct student stu[100];
int menupos=0,i,m=0;
char menuitem[][10]={" Add "," Delete "," Seek "," Correct "," Sort "," Display "," Exit "};
Disptxt();
textbackground(BLUE);
textcolor(YELLOW);
window(1,5,80,25);
clrscr();
while(1)
{
num(m);
window(1,4,80,4);
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
for(i=0;i<maxmenu;i++)
{
gotoxy(i*10+1,1);
printf("%s",menuitem[i]);
}
window(1,4,80,4);
textcolor(WHITE);
textbackground(BLACK);
gotoxy(menupos*10+1,1);
dispstr(menuitem[menupos]);
i=getch();
if(!i)
{i=getch();
switch(i)
{
case 75:menupos--;
if(menupos<0) menupos=maxmenu-1;
break;
case 77:menupos++;
if(menupos==maxmenu) menupos=0;
break;
}
}
if(i!=0xd) continue;
if(menupos==6) break;
window(1,5,80,25);
textcolor(YELLOW);
textbackground(BLUE);
clrscr();
switch(menupos)
{
case 0:m=Add(stu,m);break;
case 1:m=Del(stu,m);break;
case 2:Seek(stu,m);break;
case 3:Correct(stu,m);break;
case 4:Sort(stu,m);break;
case 5:Disp(stu,m);break;
}
}
}
int Add(struct student stu[],int n)
{
float f;
int i;
if(n>100) printf("Number fill out!!!\n");
else
{
printf("Please input the NUMBER of this student:");
scanf("%ld",&stu[n].sno);
for(i=0;i<n;i++)
{
if(stu[n].sno==stu[i].sno)
{
printf("This student is already here!\nAdd student FAIL!\n");
return(n);
break;
}
}
printf("Please input the NAME of this student:");
scanf("%s",stu[n].sname);
printf("Please input the CLASS of this student:");
scanf("%s",stu[n].sclass);
printf("Please input the SCORE of this student:");
scanf("%f",&f);stu[n].sscore=f;
printf("Add student sucess!\n");
return(n+1);
}
}
int Del(struct student stu[],int n)
{
int i,j,h=0;
long s;
if(n==0) {printf("None student could be delete!\nPlease add student first!!!");return(n=0);}
else
{
printf("Please input the STUDENT NUMBER you want to delete:");
scanf("%ld",&s);
for(i=0;i<n;i++)
{
if(stu[i].sno==s)
{
h=1;
for(j=i;j<n;j++) stu[j]=stu[j+1];
break;
}
}
if(h==0) {printf("This student is no hear!\n");return(n);}
if(h==1) {printf("Delete success!\n");return(n-1);}
}
}
void Seek(struct student stu[],int n)
{
int ch;
if(n==0) printf("None student could be seeked!\nPlease add student first!!!");
else
{
printf("Please chose the way to seek:\n");
printf("1.By student number.\n");
printf("2.By student name.\n");
printf("3.By student class.\n");
printf("4.By student score.\n");
ch=getch();
switch(ch)
{
case '1':seekbysno(stu,n);break;
case '2':seekbysname(stu,n);break;
case '3':seekbysclass(stu,n);break;
case '4':seekbysscore(stu,n);break;
}
}
}
void Correct(struct student stu[],int n)
{
float f;
long s;
int i,m,h;
if(n==0) printf("None student could be corrected!\nPlease add student first!!!");
else
{
printf("Input NUMBER that you want to Correct:\n");
scanf("%ld",&s);
for(i=0;i<n;i++)
{
if(stu[i].sno==s) {m=i;h=1;break;}
else h=0;
}
if(h==1)
{
printf("Please input the new NUMBER:\n");
scanf("%d",&stu[m].sno);
printf("Please input the new NAME:\n");
scanf("%s",stu[m].sname);
printf("Please input the new CLASS:\n");
scanf("%s",stu[m].sclass);
printf("Please input the new SCORE:\n");
scanf("%f",&f);stu[m].sscore=f;
printf("Correct information success!\n");
}
if(h==0) printf("Correct fail!\nBecause haven't found this student!!!");
}
}
void Sort(struct student stu[],int n)
{
char ch;
if(n==0) printf("None student could be sorted!\nplease add student first!!!");
else
{
printf("Please chose the way to Sort:\n");
printf("1.By student number.\n");
printf("2.By student score.\n");
ch=getch();
switch(ch)
{
case'1':sortbysno(stu,n);break;
case'2':sortbysscore(stu,n);break;
}
}
}
void Disp(struct student stu[],int n)
{
int i;
if(n==0) printf("None student could be Display!\nPlease add student first!!!");
else
{
printf("ID NAME CLASS SCORE\n");
for(i=0;i<n;i++)
pri(stu,i);
}
}
void dispstr(char ch[])
{
int k=0;
while (ch[k]) putch(ch[k++]);
}
void Disptxt()
{
textbackground(CYAN);
textcolor(BLACK);
clrscr();
window(1,1,80,3);
printf("Designer:\002zhong\001zhi\001huai\002 ID:0950303054 Class:JiKe-(2)BAN\n");
printf("\n");
textcolor(WHITE);
window(1,3,80,3);
clrscr();
printf(" \002Student Information Manage System\002\n");
}
void num(int n)
{
textbackground(WHITE);
textcolor(BLACK);
window(61,6,79,9);
clrscr();
window(62,7,80,7);
printf("Nowhave_%d_student",n);
window(62,8,80,8);
printf("in the SYSTEM!");
}
void pri(struct student stu[],int i)
{
printf("%-14ld",stu[i].sno);
printf("%-17s",stu[i].sname);
printf("%-15s",stu[i].sclass);
printf("%6.2f\n",stu[i].sscore);
}
void seekbysno(struct student stu[],int n)
{
long s;
int i;
printf("Please input the NUMBER to seek:");
scanf("%ld",&s);
printf("\n");
for(i=0;i<n;i++)
{
if(stu[i].sno==s)
{
printf("The information of this student is:\n");
printf("ID NAME CLASS SCORE\n");
pri(stu,i);
break;
}
}
}
void seekbysname(struct student stu[],int n)
{
char s[30];
int i;
printf("Please input the NAME to seek:");
scanf("%s",s);
printf("ID NAME CLASS SCORE\n");
for(i=0;i<n;i++)
{
if(strcmp(stu[i].sname,s)==0) pri(stu,i);
}
}
void seekbysclass(struct student stu[],int n)
{
char s[30];
int i;
printf("Please input the CLASS to seek:");
scanf("%s",s);
printf("ID NAME CLASS SCORE\n");
for(i=0;i<n;i++)
{
if(strcmp(stu[i].sclass,s)==0) pri(stu,i);
}
}
void seekbysscore(struct student stu[],int n)
{
int i;
float f;
printf("Please input the SCORE to seek:");
scanf("%f",&f);
printf("ID NAME CLASS SCORE\n");
for(i=0;i<n;i++)
{
if(stu[i].sscore==f) pri(stu,i);
}
}
void sortbysno(struct student stu[],int n)
{
struct student h;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(stu[j].sno<stu[k].sno) k=j;
if(k!=i)
{
h=stu[i];
stu[i]=stu[k];
stu[k]=h;
}
}
printf("SORT with student number:\n");
printf("ID NAME CLASS SCORE\n\n");
for(i=0;i<n;i++)
pri(stu,i);
}
void sortbysscore(struct student stu[],int n)
{
struct student h;
int i,j,k;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(stu[j].sscore<stu[k].sscore) k=j;
if(k!=i)
{
h=stu[i];
stu[i]=stu[k];
stu[k]=h;
}
}
printf("SORT with student score:\n");
printf("ID NAME CLASS SCORE\n\n");
for(i=0;i<n;i++)
pri(stu,i);
}