?? 源代碼.txt
字號:
#include <stdio.h> /*頭文件說明*/
#include<dos.h>
#include<stdlib.h>
#include<string.h> /*字符串函數(shù)*/
#include<mem.h> /*內(nèi)存操作函數(shù)*/
#include<ctype.h> /*字符操作函數(shù)*/
#include<alloc.h> /*動態(tài)地址分配函數(shù)*/
#define L sizeof(S)
typedef struct stu /*定義結(jié)構(gòu)體數(shù)組用于緩存數(shù)據(jù)*/
{char num[6];
char name[5];
int score[3];
int sum;
float average;
int order;
struct stu *next;
}S;
/*各函數(shù)聲明*/
S *initiate(); /*初始化函數(shù)*/
int menu_select(); /*菜單函數(shù)*/
S *create(); /*創(chuàng)建鏈表*/
void print(S *head); /* 顯示全部記錄*/
void search(S *head); /*查找記錄*/
S *delete(S *head); /*刪除記錄*/
S *sort(S *head); /*排序*/
S *insert(S *head,S *new); /*插入記錄*/
void save(S *head); /*保存文件*/
S *load(); /*讀文件*/
/*主函數(shù)界面*/
main()
{S *head,new;
head=initiate(); /*鏈表初始化,使head的值為NULL*/
for(;;) /*循環(huán)無限次*/
{switch(menu_select())
{
case 1:head=create();break;
case 2:print(head);break;
case 3:search(head);break;
case 4:head=delete(head);break;
case 5:head=sort(head);break;
case 6:head=insert(head,&new);break; /*&new表示返回地址*/
case 7:save(head);break;
case 8:head=load(); break;
case 9:exit(0); /*如菜單返回值為9則程序結(jié)束*/
}
}
}
/*初始化函數(shù)*/
S *initiate()
{
return NULL; /*返回空指針*/
}
/*菜單選擇函數(shù)*/
menu_select()
{int n;
struct date d; /*定義時間結(jié)構(gòu)體*/
getdate(&d); /*讀取系統(tǒng)日期并把它放到結(jié)構(gòu)體d中*/
printf("press any key to enter the menu......"); /*按任一鍵進入主菜單*/
getch(); /*從鍵盤讀取一個字符,但不顯示于屏幕*/
clrscr(); /*清屏*/
printf("********************************************************************************\n");
printf("\t\t Welcome to\n");
printf("\nThe student information management system of Information and Technology School of CSU\n");
printf("*************************************MENU***************************************\n");
printf("\t\t\t1. Enter the record\n"); /*輸入學(xué)生成績記錄*/
printf("\t\t\t2. Print the record\n"); /*顯示*/
printf("\t\t\t3. Search record on name\n"); /*尋找*/
printf("\t\t\t4. Delete a record\n"); /*刪除*/
printf("\t\t\t5. Sort to make new a file\n"); /*排序*/
printf("\t\t\t6. Insert record to list\n"); /*插入*/
printf("\t\t\t7. Save the file\n"); /*保存*/
printf("\t\t\t8. Load the file\n"); /*讀取*/
printf("\t\t\t9. Quit\n"); /*退出*/
printf("\n\t\t Made by Mao Jinyong\n");
printf("********************************************************************************\n");
printf("\t\t\t\t%d\\%d\\%d\n",d.da_year,d.da_mon,d.da_day); /*顯示當前系統(tǒng)日期*/
do{
printf("\n\t\t\tEnter your choice(1~9):");
scanf("%d",&n);
}while(n<1||n>9); /*如果選擇項不在1~9之間則重輸*/
return(n); /*返回選擇項,主函數(shù)根據(jù)該數(shù)調(diào)用相應(yīng)的函數(shù)*/
}
/*輸入函數(shù)*/
S *create()
{int i,s;
S *head=NULL,*p; /* 定義函數(shù).此函數(shù)帶回一個指向鏈表頭的指針*/
clrscr();
for(;;)
{p=(S *)malloc(L); /*開辟一個新的單元*/
if(!p) /*如果指針p為空*/
{printf("\nOut of memory."); /*輸出內(nèi)存溢出*/
return (head); /*返回頭指針,下同*/
}
printf("Enter the num(0:list end):");
scanf("%s",p->num);
if(p->num[0]=='0') break; /*如果學(xué)號首字符為0則結(jié)束輸入*/
printf("Enter the name:");
scanf("%s",p->name);
printf("Please enter the %d scores\n",3); /*提示開始輸入成績*/
s=0; /*計算每個學(xué)生的總分,初值為0*/
for(i=0;i<3;i++) /*3門課程循環(huán)3次*/
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*確保成績在0~100之間*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各門成績*/
}
p->sum=s; /*將總分保存*/
p->average=(float)s/3; /*先用強制類型轉(zhuǎn)換將s轉(zhuǎn)換成float型,再求平均值*/
p->order=0; /*未排序前此值為0*/
p->next=head; /*將頭結(jié)點做為新輸入結(jié)點的后繼結(jié)點*/
head=p; /*新輸入結(jié)點為新的頭結(jié)點*/
}
return(head);
}
/* 顯示全部記錄函數(shù)*/
void print(S *head)
{int i=0; /* 統(tǒng)計記錄條數(shù)*/
S *p; /*移動指針*/
clrscr();
p=head; /*初值為頭指針*/
printf("\n************************************S************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf(" | Num | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
i, p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*查找記錄函數(shù)*/
void search(S *head)
{S *p; /* 移動指針*/
char s[5]; /*存放姓名用的字符數(shù)組*/
clrscr();
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*將頭指針賦給p*/
while(strcmp(p->name,s) && p != NULL) /*當記錄的姓名不是要找的,或指針不為空時*/
p=p->next; /*移動指針,指向下一結(jié)點*/
if(p!=NULL) /*如果指針不為空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->num,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no num %s S on the list.\n",s); /*顯示沒有該學(xué)生*/
}
/*刪除記錄函數(shù)*/
S *delete(S *head)
{int n;
S *p1,*p2; /*p1為查找到要刪除的結(jié)點指針,p2為其前驅(qū)指針*/
char c,s[6]; /*s[6]用來存放學(xué)號,c用來輸入字母*/
clrscr();
printf("Please enter the deleted num: ");
scanf("%s",s);
p1=p2=head; /*給p1和p2賦初值頭指針*/
while(strcmp(p1->num,s) && p1 != NULL) /*當記錄的學(xué)號不是要找的,或指針不為空時*/
{p2=p1; /*將p1指針值賦給p2作為p1的前驅(qū)指針*/
p1=p1->next; /*將p1指針指向下一條記錄*/
}
if(strcmp(p1->num,s)==0) /*學(xué)號找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Num | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p1->num,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the S Y/N ?"); /*提示是否要刪除,輸入Y刪除,N則退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不刪除,則跳出本循環(huán)*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,說明被刪結(jié)點是首結(jié)點*/
head=p1->next; /*把第二個結(jié)點地址賦予head*/
else
p2->next=p1->next; /*否則將一下結(jié)點地址賦給前一結(jié)點地址*/
n=n-1;
printf("\nNum %s S have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*刪除后就跳出循環(huán)*/
}
}
}
else
printf("\nThere is no num %s S on the list.\n",s); /*找不到該結(jié)點*/
return(head);
}
/*排序函數(shù)*/
S *sort(S *head)
{int i=0; /*保存名次*/
S *p1,*p2,*t,*temp; /*定義臨時指針*/
temp=head->next; /*將原表的頭指針所指的下一個結(jié)點作頭指針*/
head->next=NULL; /*第一個結(jié)點為新表的頭結(jié)點*/
while(temp!=NULL) /*當原表不為空時,進行排序*/
{
t=temp; /*取原表的頭結(jié)點*/
temp=temp->next; /*原表頭結(jié)點指針后移*/
p1=head; /*設(shè)定移動指針p1,從頭指針開始*/
p2=head; /*設(shè)定移動指針p2做為p1的前驅(qū),初值為頭指針*/
while(t->average<p1->average&&p1!=NULL) /*作成績平均分比較*/
{
p2=p1; /*待排序點值小,則新表指針后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,說明待排序點值大,應(yīng)排在首位*/
{
t->next=p1; /*待排序點的后繼為p*/
head=t; /*新頭結(jié)點為待排序點*/
}
else /*待排序點應(yīng)插入在中間某個位置p2和p1之間,如p為空則是尾部*/
{
t->next=p1; /*t的后繼是p1*/
p2->next=t; /*p2的后繼是t*/
}
}
p1=head; /*已排好序的頭指針賦給p1,準備填寫名次*/
while(p1!=NULL) /*當p1不為空時,進行下列操作*/
{
i++; /*結(jié)點序號*/
p1->order=i; /*將結(jié)點序號賦值給名次*/
p1=p1->next; /*指針后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入記錄函數(shù)*/
S *insert(S *head,S *new)
{S *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一個結(jié)點*/
p0=new; /*p0指向要插入的結(jié)點*/
printf("\nPlease enter a new record.\n"); /*提示輸入記錄信息*/
printf("Enter the num:");
scanf("%s",new->num);
printf("Enter the name:");
scanf("%s",new->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新記錄的總分,初值為0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&new->score[i]);
if(new->score[i]>100||new->score[i]<0)
printf("Data error,please enter again.\n");
}while(new->score[i]>100||new->score[i]<0);
sum1=sum1+new->score[i]; /*累加各門成績*/
}
new->sum=sum1; /*將總分存入新記錄中*/
new->average=(float)sum1/3;
new->order=0;
if(head==NULL) /*原來的鏈表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的結(jié)點作為頭結(jié)點*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向剛才p1指向的結(jié)點*/
p1=p1->next; /*p1后移一個結(jié)點*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原來第一個結(jié)點之前*/
else p2->next=p0; /*插到p2指向的結(jié)點之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的結(jié)點之后*/
}
n=n+1; /*結(jié)點數(shù)加1*/
head=sort(head); /*調(diào)用排序的函數(shù),將學(xué)生成績重新排序*/
printf("\nS %s have been inserted.\n",new->name);
printf("Don't forget to save the new file.\n");
return(head);
}
/*保存數(shù)據(jù)到文件函數(shù)*/
void save(S *head)
{FILE *fp; /*定義指向文件的指針*/
S *p; /* 定義移動指針*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*為輸出打開一個二進制文件,為只寫方式*/
{
printf("Cannot open the file\n");
return; /*若打不開則返回菜單*/
}
printf("\nSaving the file......\n");
p=head; /*移動指針從頭指針開始*/
while(p!=NULL) /*如p不為空*/
{
fwrite(p,L,1,fp); /*寫入一條記錄*/
p=p->next; /*指針后移*/
}
fclose(fp); /*關(guān)閉文件*/
printf("Save the file successfully!\n");
}
/* 從文件讀數(shù)據(jù)函數(shù)*/
S *load()
{S *p1,*p2,*head=NULL; /*定義記錄指針變量*/
FILE *fp; /* 定義指向文件的指針*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打開一個二進制文件,為只讀方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(S *)malloc(L); /*開辟一個新單元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環(huán)讀數(shù)據(jù)直到文件尾結(jié)束*/
{
if(fread(p1,L,1,fp)!=1) break; /*如果沒讀到數(shù)據(jù),跳出循環(huán)*/
p1->next=(S *)malloc(L); /*為下一個結(jié)點開辟空間*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向剛才p1指向的結(jié)點*/
p1=p1->next; /*指針后移,新讀入數(shù)據(jù)鏈到當前表尾*/
}
p2->next=NULL; /*最后一個結(jié)點的后繼指針為空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -