只能在TC下運行.....
項目要求:
1. 題目:學生信息管理系統
2. 程序要求:
(1)學生信息錄入功能
l 用戶鍵盤輸入每個學生的信息:學號、姓名、性別、數學、英語、政治、語文四門課成績。
l 可插入一個或多個學生信息到當前編輯的班級數據中。
(2)文件保存功能
l 學生信息每一班存爲一個數據文件,數據文件可在程序中打開、編輯和重新保存;
l 用戶輸入學生信息可隨時保存數據文件。
(3)文件打開功能
l 序只能對當前打開的數據文件進行編輯。
(4)查詢功能
l 瀏覽所有學生信息
l 按學號查詢學生信息
l 按姓名查詢學生信息
l 查詢一個班總成績和平均成績
l 查詢一個班某一門課總成績和平均成績
l 查詢某一門課分數段(<60,60-69,70-79,80-89,>90)學生數。
(5)報表輸出功能
l 按學號輸出一個班學生信息:學號、姓名、性別、數學、英語、政治、語文成績、總成績,到屏幕和文件。
l 按總成績輸出從高到低輸出學號、姓名信息。
注:以上功能以菜單形式供用戶使用,有一定的容錯功能。
3. 開發語言環境:Microsoft VC++6.0 或Turbo C2.0
4.數據結構:數組或鏈表
5. 程序源代碼要求:
(1)函數名、變量名採用英文縮寫;
(2)源代碼的書寫採用遞進格式;
(3)程序行和程序段須有註釋。
菜單功能詳解……
1.增加班級 :輸入班級名回車即可,如果班級已存在則提示是否覆蓋。創建後選擇班級。
2.選擇班級 :進入系統首先要選擇班級後才能進行操作,否則自動列出班級提示進行選擇班級並列出已在在班級
3.信息保存:操作過程中可隨時選擇對操作班級數據進行保存(寫入文件)
4.信息輸入:提示輸入學生的學號,性別,各科成績。且限制輸入內容,如成績不能大於100,小於0,不能輸入字符。性別只能是man/woman……
5.信息更改:輸入學號ID,查找該ID學生信息,選擇更改該ID號學生信息的各項內容。
6.信息刪除:根據ID或名字進行刪除學生信息……
7.信息查尋:根據 ID 或 名字 或 分數 進行查尋。ID:可單個或範圍進行查尋。分數:可精確到各科成績、平均分,總分 進行單個或範圍查尋。名字輸入名字直接進行查尋
8.報表輸出:可根據信息錄入時間 或 ID 或 各項分數 進行從大到小 或 從小到大列表輸出學生信息
9.刪除班級:刪除班級數據文件
0.退出系統
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<dos.h>
#include<alloc.h>
#include<dir.h>
#define X0 10
#define scanf fflush(stdin);scanf
int change=0;
char FileName[MAXPATH];
char NewPath[MAXPATH];
char OldPath[MAXPATH];
char *str[9]={"ID","name","sex","math","english","politics","chinese","total","average"};
typedef struct NODE
{
long int ID;
char name[20];
char sex[6];
float math;
float english;
float politics;
float chinese;
float total;
float average;
struct NODE *next;
}ListNode;
typedef ListNode *LinkList;
LinkList p=NULL;
int IsYes() /* 是否按下了Y,或按下N */
{
char ch;
do
{
ch=getch();
}while(ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N');
if(ch=='y'||ch=='Y')
return 1;
else
return 0;
}
int Select(int x,int y) /* 選擇x~y的數,否則提示重新輸入 */
{
int i;
printf("\n\nyour choice:");
scanf("%d",&i);
while(i>y||i<x)
{
printf("\nplease enter(%d~%d):",x,y);
scanf("%d",&i);
}
return i;
}
void outtextxy(int x, int y,char *str) /* 在屏幕x,y位置輸出str字符串 */
{
union REGS regs;
regs.h.ah = 2;
regs.h.bh = 0;
regs.h.dh = y;
regs.h.dl = x;
int86(0x10, ®s, ®s);
printf(str);
}
void ShowInfo(LinkList q,int j) /* 顯示信息 */
{
int i;
for(i=0;i<9;i++)
{
switch(i)
{
case 0:outtextxy(i*9+1,j,"");printf("%-ld",q->ID);break;
case 1:outtextxy(i*9+1,j,"");printf("%-8s",q->name);break;
case 2:outtextxy(i*9+1,j,"");printf("%-8s",q->sex);break;
case 3:outtextxy(i*9+1,j,"");printf("%-8g",q->math);break;
case 4:outtextxy(i*9+1,j,"");printf("%-8g",q->english);break;
case 5:outtextxy(i*9+1,j,"");printf("%-8g",q->politics);break;
case 6:outtextxy(i*9+1,j,"");printf("%-8g",q->chinese);break;
case 7:outtextxy(i*9+1,j,"");printf("%-8g",q->total);break;
case 8:outtextxy(i*9+1,j,"");printf("%-8g",q->average);break;
default:break;
}
}
}
LinkList NumSort(int k,char *item) /* 信息排序 */
{
LinkList q,r,s,t,u,w;
int i;
int j=0;
for(i=0;i<9;i++)
if(!strcmp(item,str[i]))
break;
if(!(i==0||i==3||i==4||i==5||i==6||i==7||i==8)||(k!=1&&k!=2))
{
clrscr();
printf("Parameter passing incorrect.");
getch();
exit(0);
}
u=(LinkList)malloc(sizeof(ListNode));
for(q=p,r=s=NULL;q!=NULL;q=q->next)
{
w=(LinkList)malloc(sizeof(ListNode));
w->next=NULL;
if(p==q)
{
s=w;
*s=*p;
s->next=NULL;
u->next=s;
}
else
{
for(r=s,t=u;r!=NULL;r=r->next,t=t->next)
{
switch(i)
{
case 0:j=k==1?(r->ID>=q->ID):(r->ID<=q->ID);break;
case 3:j=k==1?(r->math>=q->math):(r->math<=q->math);break;
case 4:j=k==1?(r->english>=q->english):(r->english<=q->english);break;
case 5:j=k==1?(r->politics>=q->politics):(r->politics<=q->politics);break;
case 6:j=k==1?(r->chinese>=q->chinese):(r->chinese<=q->chinese);break;
case 7:j=k==1?(r->total>=q->total):(r->total<=q->total);break;
case 8:j=k==1?(r->average>=q->average):(r->average<=q->average);break;
default:break;
}
if(k==1&&j)
{
*w=*q;
w->next=r;
t->next=w;
s=u->next;
break;
}
else if(k==2&&j)
{
*w=*q;
w->next=r;
t->next=w;
s=u->next;
break;
}
}
if(!j)
{
*w=*q;
t->next=w;
w->next=NULL;
}
}
}
free(u);
return s;
}
void ShowSort(char *item) /* 選擇排序方式及顯示排序 */
{
LinkList s,q;
int i,j;
while(1)
{
clrscr();
printf("1.small to large 2.large to small 0.back");
i=Select(0,2);
if(i==0)
break;
s=NumSort(i,item);
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
while(s!=NULL)
{
ShowInfo(s,++j);
q=s;
s=s->next;
free(q);
}
getch();
}
}
int ClassAdd() /* 文件創建 */
{
FILE *fp;
LinkList q;
int k=1;
clrscr();
printf("class name:");
scanf("%s",FileName);
strcpy(NewPath,OldPath);
strcat(NewPath,FileName);
if((fp=fopen(NewPath,"rb"))!=NULL)
{
clrscr();
printf("class already exit!\n\noverwrite it(Y/N)");
if(!IsYes())
k=0;
}
if(k)
{
if((fp=fopen(NewPath,"wb"))==NULL)
{
clrscr();
printf("fail add!\n\npress any key continue...");
getch();
return 0;
}
fclose(fp);
clrscr();
printf("add success!\n\npress any key continue...");
getch();
return 1;
}
return 0;
}
int ClassSelect(int OpenDirectly) /* 文件打開 */
{
FILE *fp;
int i=1,j;
long end;
LinkList q;
clrscr();
if(!OpenDirectly)
{
printf("Exist Class:\n\n");
strcpy(NewPath,"dir ");
strcat(NewPath,OldPath);
NewPath[strlen(NewPath)-1]=0;
strcat(NewPath," /o/p/w/b/a:-d");
system(NewPath);
printf("\n\nselect class name:");
scanf("%s",FileName);
}
strcpy(NewPath,OldPath);
strcat(NewPath,FileName);
if((fp=fopen(NewPath,"rb"))==NULL)
{
clrscr();
printf("\nfail select!\n\npress any key continue...");
getch();
return 0;
}
fseek(fp,0L,SEEK_END);
end=ftell(fp);
rewind(fp);
while(!feof(fp)&&(end-ftell(fp))>=sizeof(ListNode))
{
if(i)
{
p=q=(LinkList)malloc(sizeof(ListNode));
p->next=NULL;
i=0;
}
else
{
q->next=(LinkList)malloc(sizeof(ListNode));
q=q->next;
q->next=NULL;
}
fread(q,sizeof(ListNode),1,fp);
if(q==p)
{
if(q->ID<0||q->ID>0x7fffffff||19<strlen(q->name)||strcmp(q->sex,"man")&&strcmp(q->sex,"woman")||q->math>100||q->math<0||q->english>100||q->english<0||q->politics>100||q->politics<0||q->chinese>100||q->chinese<0)
{
clrscr();
printf("illegal data file!\n\npress any key continue...");
getch();
free(q);
p=NULL;
return 0;
}
}
}
clrscr();
if(!OpenDirectly)
{
printf("select success!\n\npress any key continue...");
getch();
}
fclose(fp);
return 1;
}
void ClassSave() /* 文件保存 */
{
FILE *fp;
LinkList q;
clrscr();
strcpy(NewPath,OldPath);
strcat(NewPath,FileName);
if((fp=fopen(NewPath,"wb"))==NULL)
{
printf("fail save!\n\npress any key continue...");
getch();
return;
}
rewind(fp);
while(p!=NULL)
{
fwrite(p,sizeof(ListNode),1,fp);
q=p;
p=p->next;
free(q);
}
change=0;
fclose(fp);
printf("save success!\n\npress any key continue...");
getch();
}
int IsSave() /* 判斷有沒有保存,是否保存*/
{
LinkList q;
clrscr();
if(change==1)
{
printf("a file opened.save it?(Y/N)");
if(IsYes())
{
ClassSave();
return 1;
}
return 0;
}
else
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
return 0;
}
float NumLess100(char *buf) /* 判斷輸入的數是否是:大於0小於100且最多一位小數的浮點數*/
{
int i,j,l,k;
char str[30];
while(1)
{
scanf("%s",str);
k=0;
l=strlen(str);
j=l>4?0:1;
if(j)
{
for(i=0;i<l;i++)
{
if(str[i]!='.'&&(str[i]<48||str[i]>57))
j=0;
if(str[i]=='.')
k++;
if(k>1)
j=0;
}
}
if(j)
if(100<atof(str)||0>atof(str))
j=0;
if(j)
break;
printf("\nplease enter num(<=100 and no more than 1 fraction).\n");
printf(buf);
}
return atof(str);
}
void Input(int k,LinkList q) /* 信息輸入及判斷輸入是否正確 */
{
int i,j,l;
char str[30];
switch(k)
{
case 0:
printf("\nID :");
while(1)
{
scanf("%s",str);
l=strlen(str);
j=l>10?0:1;
if(j)
for(i=0;i<l;i++)
if(str[i]<48||str[i]>57)
j=0;
if(j)
{
LinkList r=p;
while(r!=NULL&&p->ID!=-1)
{
if(r->ID==atol(str))
break;
r=r->next;
}
if(r!=NULL&&p->ID!=-1)
{
printf("\nID Exist!\n");
j=0;
}
}
if(j)
break;
printf("\nplease enter less than 10 num.\n\nID :");
}
q->ID=atol(str);
break;
case 1:
while(1)
{
printf("\nname :");
scanf("%s",str);
if(strlen(str)<19)
break;
printf("\n please enter name less than 20 character.\n");
}
strcpy(q->name,str);
break;
case 2:
printf("\nsex :");
while(1)
{
scanf("%s",q->sex);
if(!(strcmp(q->sex,"man")&&strcmp(q->sex,"woman")))
break;
printf("\nplease enter(man/woman).\n\nsex :");
}
break;
case 3:printf("\nmath :");q->math=NumLess100("\nmath :");break;
case 4:printf("\nenglish :");q->english=NumLess100("\nenglish :");break;
case 5:printf("\npolitics :");q->politics=NumLess100("\npolitics :");break;
case 6:printf("\nchinese :");q->chinese=NumLess100("\nchinese :");break;
case 7:q->total=q->math+q->english+q->politics+q->chinese;break;
case 8:q->average=q->total/4;break;
}
}
void InfoInput() /*信息錄入 */
{
LinkList q,r,s;
int i;
char ch;
while(1)
{
q=p;
clrscr();
outtextxy(20,1,"Please enter student information.");
if(q!=NULL)
{
while(q->next!=NULL)
q=q->next;
q->next=(LinkList)malloc(sizeof(ListNode));
q=q->next;
q->next=NULL;
}
else
{
p=q=(LinkList)malloc(sizeof(ListNode));
p->next=NULL;
}
q->ID=-1;
for(i=0;i<9;i++)
{
Input(i,q);
change=1;
}
clrscr();
printf("Continue?(Y/N)");
if(!IsYes())
break;
}
}
void InfoSearch() /* 信息查詢 */
{
LinkList q;
char buf[30];
int i,j;
unsigned long id;
float score;
while(1)
{
clrscr();
printf("search by: 1.ID 2.name 3.Score 0.back");
i=Select(0,3);
if(i==0)
break;
switch(i)
{
case 1:
while(1)
{
q=p;
clrscr();
printf("1.search one 2.search by rang 0.back");
i=Select(0,2);
if(i==0)
break;
else if(i==1)
{
printf("\nenter the ID:");
scanf("%ld",&id);
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
while(q!=NULL)
{
if(q->ID==id)
ShowInfo(q,++j);
q=q->next;
}
if(j==0)
printf("\ndidn't find!");
}
else if(i==2)
{
unsigned long x,y;
q=p;
printf("\nenter the range(like:10 32):");
while(1)
{
scanf("%ld%ld",&x,&y); ;
if(x<=y)
break;
printf("the later must be great.enter again:");
}
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
while(q!=NULL)
{
if(q->ID>=x&&q->ID<=y)
ShowInfo(q,++j);
q=q->next;
}
if(j==0)
printf("\ndidn't find!\n");
}
printf("\nContinue searching by ID...(Y/N)?");
if(!IsYes())
break;
}
break;
case 2:
while(1)
{
q=p;
clrscr();
printf("enter the name:");
scanf("%s",buf);
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
while(q!=NULL)
{
if(!strcmp(q->name,buf))
ShowInfo(q,++j);
q=q->next;
}
if(j==0)
printf("\ndidn't find!\n");
printf("\nContinue searching by name...(Y/N)?");
if(!IsYes())
break;
}
break;
case 3:
while(1)
{
int k;
float x,y;
q=p;
clrscr();
printf("1.math 2.english 3.politics 4.chinese 5.total 6.average 0.back");
k=Select(0,6);
if(k==0)
break;
k+=2;
printf("\nenter Score rangs(like 60 90):");
while(1)
{
scanf("%g%g",&x,&y); ;
if(x<=y)
break;
printf("\nthe later must be great.enter again:");
}
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
while(q!=NULL)
{
switch(k)
{
case 3:
if(q->math>=x&&q->math<=y)
ShowInfo(q,++j);
break;
case 4:
if(q->english>=x&&q->english<=y)
ShowInfo(q,++j);
break;
case 5:
if(q->politics>=x&&q->politics<=y)
ShowInfo(q,++j);
break;
case 6:
if(q->chinese>=x&&q->chinese<=y)
ShowInfo(q,++j);
break;
case 7:
if(q->total>=x&&q->total<=y)
ShowInfo(q,++j);
break;
case 8:
if(q->average>=x&&q->average<=y)
ShowInfo(q,++j);
break;
default:
break;
}
q=q->next;
}
if(j==0)
printf("\ndidn't find!\n");
printf("\nContinue searching by score...(Y/N)?");
if(!IsYes())
break;
}
break;
default:break;
}
}
}
void InfoModify() /* 信息更改 */
{
LinkList q;
int id,i,j;
char ch;
char item[30];
float x;
while(1)
{
clrscr();
outtextxy(0,0,"Please enter the ID to Modify:");
scanf("%ld",&id);
q=p;
while(q!=NULL)
{
if(q->ID==id)
break;
q=q->next;
}
if(q!=NULL&&q->ID==id)
{
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
ShowInfo(q,++j);
while(1)
{
printf("\n\nenter item(like ID):");
do
{
scanf("%s",item);
for(i=0;i<7;i++)
if(!strcmp(item,str[i]))
break;
if(i==7)
printf("\nItem not exit!enter again:");
}while(i>6);
Input(i,q);
if(i>2&&i<7)
{
q->total=q->math+q->english+q->politics+q->chinese;
q->average=q->total/4;
}
change=1;
printf("\nContinue modify the same item?(Y/N)");
if(!IsYes())
break;
}
}
clrscr();
if(q==NULL)
printf("The ID you enter not exit!\n\n");
printf("Continue modify others?(Y/N)");
if(!IsYes())
break;
}
}
void InfoDelete() /* 信息刪除 */
{
LinkList q,r;
int i,j;
while(1)
{
clrscr();
printf("delete by: 1.ID 2.name 0.back");
i=Select(0,2);
if(i==0)
break;
else if(i==1)
{
while(1)
{
unsigned long int id;
q=r=p;
clrscr();
printf("\n\nenter delete ID:");
scanf("%ld",&id);
while(q!=NULL)
{
if(q->ID==id)
break;
r=q;
q=q->next;
}
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
if(q!=NULL)
{
ShowInfo(q,++j);
printf("\n\ndelete?(Y/N)");
if(IsYes())
{
if(r->ID==id)
{
if(r->next==NULL)
{
free(q);
r=p=q=NULL;
}
else
{
p=p->next;
free(q);
q=r=p;
}
}
else
{
r->next=q->next;
free(q);
}
printf("\n\ndeleted!");
change=1;
}
}
else
printf("\n\nnot exit!");
printf("\n\ndelete by ID continue?(Y/N)");
if(!IsYes())
break;
}
}
else if(i==2)
{
while(1)
{
char buf[30];
int k=0;
q=r=p;
clrscr();
printf("\n\nenter delete name:");
scanf("%s",&buf);
while(q!=NULL)
{
if(!strcmp(q->name,buf))
{
clrscr();
k++;
if(k>1)
{
printf("\n\ncontine to delete name(%s)?(Y/N)",buf);
if(!IsYes())
break;
}
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
ShowInfo(q,++j);
printf("\n\ndelete?(Y/N)");
if(IsYes())
{
if(!strcmp(r->name,buf))
{
if(p->next==NULL)
{
free(p);
r=p=q=NULL;
}
else
{
p=p->next;
free(q);
q=p;
}
}
else
{
r->next=q->next;
free(q);
q=r;
}
change=1;
printf("\n\ndeleted... ");
}
}
r=q;
q=q->next;
}
if(j!=0&&k<=1)
{
clrscr();
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
printf("\n\ndidn't find.");
}
printf("\n\ncontine delete by name?(Y/N)");
if(!IsYes())
break;
}
}
}
}
void ReportOutput() /* 報表輸出 */
{
LinkList q,s;
int i,j,k=0;
while(1)
{
clrscr();
printf("sort by: 1.time 2.ID 3.score 0.back");
i=Select(0,3);
switch(i)
{
case 1:
clrscr();
q=p;
for(i=0,j=0;i<9;i++)
outtextxy(i*9+1,j,str[i]);
while(q!=NULL)
{
ShowInfo(q,++j);
q=q->next;
}
getch();
break;
case 2:
ShowSort("ID");
break;
case 3:
while(1)
{
clrscr();
printf("sort by: 1. math 2.english 3.politics 4.chinese 5.total 6.average 0.back");
i=Select(0,6);
if(i==0)
break;
switch(i)
{
case 1:ShowSort("math");break;
case 2:ShowSort("english");break;
case 3:ShowSort("politics");break;
case 4:ShowSort("chinese");break;
case 5:ShowSort("total");break;
case 6:ShowSort("average");break;
default:break;
}
}
break;
case 0:
k=1;
break;
default:clrscr();printf("error!");getch();break;
}
if(k)
break;
}
}
void ClassDelete() /* 班級文件刪除 */
{
clrscr();
printf("Exist Class:\n\n");
strcpy(NewPath,"dir ");
strcat(NewPath,OldPath);
NewPath[strlen(NewPath)-1]=0;
strcat(NewPath," /b/o/p/w/a:-d");
system(NewPath);
printf("\n\nselect class name:");
scanf("%s",FileName);
strcpy(NewPath,"del /p ");
strcat(NewPath,OldPath);
strcat(NewPath,FileName);
system(NewPath);
getch();
}
void AuthorInfo() /* 作者信息 */
{
int i;
char *str[8]={"System Name:","Student management system",
"Author :","zhaopeng",
"Student ID :","200900008071",
"Class :","0902"};
clrscr();
for(i=0;i<4;i++)
{
outtextxy(20,i*2+4,str[i*2]);
outtextxy(40,i*2+4,str[i*2+1]);
}
printf("\n\n\n\t\t Press any key to continue...");
getch();
}
int TF(int IsSelect,void(*visit)(void)) /* 一個函數調用的模型,用於減少代碼量 */
{
if(IsSelect)
visit();
else
{
IsSelect=ClassSelect(0);
if(IsSelect==1)
visit();
}
return IsSelect;
}
void StudentSystem() /* 學生管理系統開始菜單 */
{
int i;
int IsSelect=0;
int choice;
char *str[10]={"1.Class Add",
"2.Class Select",
"3.Information Save",
"4.Information Input",
"5.Information Modify",
"6.Information Delete",
"7.Information Search",
"8.Report Output",
"9.Class Delete",
"0.Exit The System"};
mkdir("classdata");
getcwd(OldPath,MAXPATH);
if(OldPath[strlen(OldPath)-1]=='\\')
strcat(OldPath,"classdata\\");
else
strcat(OldPath,"\\classdata\\");
while(1)
{
clrscr();
printf("\n -----------------welcome to student management system-----------------");
for(i=0;i<10;i++)
outtextxy(30,i*2+3,str[i]);
printf("\n\n\t\t Please input your choice");
if(IsSelect)
printf("(class selected):");
else
printf("(class unselect):");
choice=23;
scanf("%d",&choice);
clrscr();
switch(choice)
{
case 1:
if(IsSelect)
IsSave();
IsSelect=0;
ClassAdd();
IsSelect=ClassSelect(1);
break;
case 2:
if(IsSelect)
IsSave();
IsSelect=0;
IsSelect=ClassSelect(0);
break;
case 3:
if(IsSelect)
{
ClassSave();
IsSelect=ClassSelect(1);
}
else
{
printf("no class select!\n\npress any key continue...");
getch();
}
break;
case 4:IsSelect=TF(IsSelect,InfoInput);break;
case 5:IsSelect=TF(IsSelect,InfoModify);break;
case 6:IsSelect=TF(IsSelect,InfoDelete);break;
case 7:IsSelect=TF(IsSelect,InfoSearch);break;
case 8:IsSelect=TF(IsSelect,ReportOutput);break;
case 9:if(IsSelect){IsSave();IsSelect=0;}ClassDelete();break;
default:
break;
}
if(choice==0)
break;
}
if(change)
IsSave();
}
void main()
{
AuthorInfo();
StudentSystem();
}