第一篇:圖書管理系統(tǒng)(含源代碼)c語言_數(shù)據(jù)結構課程設計報告
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
數(shù)據(jù)結構大作業(yè) 圖書管理系統(tǒng)
工程管理 121279044 伍楊
目錄一、二、三、題目要求...................................................................2 總體設計...................................................................2 編碼實現(xiàn)...................................................................3 1)定義圖書結構體.......................................................3 2)登記操作...............................................................4 3)查看操作...............................................................8 4)刪除操作.............................................................11 5)Main函數(shù)...........................................................20四、五、六、調試與測試..............................................................26 五心得體會..............................................................28 用戶手冊.................................................................28
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
一、題目要求
1)目的要求
本課程設計任務的目的是要求學生按照分析、設計、編碼、調試和測試的軟件開發(fā)過程獨立完成管理系統(tǒng)設計,以及C語言算法的掌握,并能最終實現(xiàn)本系統(tǒng)的功能要求,通過這個程序可以學習到以前調試短程序沒有的的經(jīng)驗。2)題目要求
實現(xiàn)圖書管理信息系統(tǒng)的設計。要求實現(xiàn)圖書添加、顯示全部圖書、查詢、借閱和歸還。主要考查利用文件的操作!
二、總體設計
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
三、編碼實現(xiàn)
1)定義圖書結構體
struct book{
char bookname[20];
//書名
int NO;
//書編號
char type[20];
//類型
int date;
//到書日期 };struct person{
char name[10];
//姓名
char classes[20];
//班級
int number;
//學號
char telephone[12];
//聯(lián)系電話
int NO;
//書編號
char bookname[20];
//書名
int borrowdate;
//借書日期
int returndate;
//還書日期
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
2)登記操作
void new_book(){ FILE *fp;struct book b;//登記新書
int i,j;
printf(“請朱老師輸入此次收到的書本總數(shù):”);if((fp=fopen(“shuku.txt”,“a”))==NULL){ printf(“File open error!n”);exit(0);}
scanf(“%d”,&i);
for(j=0;j
printf(“請朱老師輸入書名:”);scanf(“%s”,b.bookname);fprintf(fp,“%s”,b.bookname);printf(“請朱老師輸入書編號:”);scanf(“%d”,&b.NO);fprintf(fp,“
%d”,b.NO);printf(“請朱老師輸入類型:”);scanf(“%s”,b.type);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
}
} fprintf(fp,“
%s”,b.type);printf(“請朱老師輸入到書日期:”);scanf(“%d”,&b.date);fprintf(fp,“
%d”,b.date);if(fclose(fp)){
} printf(“Can not close the file!n”);exit(0);void new_person()
{
FILE *fp;struct person p;char choice;
//登記借書
if((fp=fopen(“jieshujilu.txt”,“a”))==NULL){ printf(“File open error!n”);exit(0);}
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
printf(“請朱老師輸入借書人姓名:”);scanf(“%s”,p.name);fprintf(fp,“%s”,p.name);printf(“請朱老師輸入借書人班級:”);scanf(“%s”,p.classes);fprintf(fp,“
%s”,p.classes);printf(“請朱老師輸入借書人學號:”);scanf(“%d”,&p.number);fprintf(fp,“
%d”,p.number);printf(“請朱老師輸入借書人聯(lián)系電話:”);scanf(“%s”,p.telephone);fprintf(fp,“
%s”,p.telephone);printf(“請朱老師輸入書編號:”);scanf(“%d”,&p.NO);fprintf(fp,“
%d”,p.NO);printf(“請朱老師輸入書名:”);scanf(“%s”,p.bookname);fprintf(fp,“
%s”,p.bookname);printf(“請朱老師輸入借書日期:”);scanf(“%d”,&p.borrowdate);fprintf(fp,“
%d”,p.borrowdate);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
printf(“請朱老師輸入還書日期:”);scanf(“%d”,&p.returndate);fprintf(fp,“
%d”,p.returndate);printf(“nt您想繼續(xù)嗎?(y/n)”);scanf(“ %c”,&choice);if(choice=='Y'||choice=='y'){
} system(“cls”);new_person();if(fclose(fp)){
} printf(“Can not close the file!n”);exit(0);}實現(xiàn)程序對文件的讀取 void Read(){
int i=0;int j=0;ifstream in(“Libra.txt”,ios::out);in>>i;
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
nt;
} all=i;if(i>0&&i<=Max){
in>>data[j].id>>data[j].name>>data[j].type>>data[j].status>>data[j].coufor(j=1;j<=i;j++){ } } in.close();
3)查看操作
v void see_book(){
FILE *fp;long NO;char bookname[20];char type[20];long date;//查看書庫記錄
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
} if((fp=fopen(“shuku.txt”,“r”))==NULL){
} printf(“File open error!n”);exit(0);while(!feof(fp)){ fscanf(fp,“%s%ld%s%ld”,bookname,&NO,type,&date);printf(“%-10s %-10ld %-10s %ldn”,bookname,NO,type,date);};if(fclose(fp)){
} printf(“Can not close the file!n”);exit(0);void see_person(){ //查看所有借書記錄
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
FILE *fp;char name[10];
char classes[20];
int number;char telephone[20];
int NO;
char bookname[20];
int borrowdate;
int returndate;
fscanf(fp,“%s %s %ld %s %ld %s %ld %ld”,name,classes,&number,telephonwhile(!feof(fp)){ if((fp=fopen(“jieshujilu.txt”,“r”))==NULL){
} printf(“File open error!n”);exit(0);e,&NO,bookname,&borrowdate,&returndate);printf(“%-5s %-5s %ld %-5s %ld %-5s %ld %ldn”,name,classes,number,telephone,NO,bookname,borrowdate,returndate);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
} };if(fclose(fp)){
} printf(“Can not close the file!n”);exit(0);
4)刪除操作
void delete_books()
{
int number;void deletebooks();
printf(“請輸入您要刪除的書編號:”);scanf(“%d”,&number);FILE *fp;struct book b;
//刪除舊書
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
if((fp=fopen(“shuku.txt”,“r”))==NULL){
} printf(“不能打開此文件!n”);exit(0);while(!feof(fp)){ fscanf(fp,“%s %d %s %d”,b.bookname,&b.NO,b.type,&b.date);
} if(b.NO==number){
} printf(“nnt***************圖書信息*******************n”);printf(“nt圖書書名:%25s”,b.bookname);printf(“nt----------”);printf(“nt圖書編號:%25d”,b.NO);printf(“nt----------”);printf(“nt圖書類型:%23s”,b.type);printf(“nt----------”);printf(“nt到書日期:%25d”,b.date);printf(“nt----------”);
deletebooks();
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
}
void deletebooks(){
while(!feof(fp)){ fscanf(fp,“%s %d %s %d”,b.bookname,&b.NO,b.type,&b.date);if((fp=fopen(“shuku.txt”,“r”))==NULL){
} if((fp1=fopen(“tempshuku.txt”,“w”))==NULL){
//建立一個臨時文件
} printf(“不能打開此文件!n”);exit(0);printf(“不能打開此文件!n”);exit(0);printf(“nn確認刪除?請再次輸入書編號:”);scanf(“%d”,&number);FILE *fp,*fp1,*fp2,*fp3;int number;struct book b;
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
} if(b.NO==number)continue;else
fprintf(fp1,“%s %d %s %d”,b.bookname,b.NO,b.type,b.date);fclose(fp);fclose(fp1);if((fp2=fopen(“tempshuku.txt”,“r”))==NULL){
} if((fp3=fopen(“shuku.txt”,“w”))==NULL){
//清空書庫
} while(!feof(fp2)){
//將臨時文件的內容寫人源文件
} fscanf(fp2,“%s %d %s %d”,b.bookname,&b.NO,b.type,&b.date);fprintf(fp3,“%s %d %s %d”,b.bookname,b.NO,b.type,b.date);printf(“不能打開此文件!n”);exit(0);printf(“不能打開此文件!n”);exit(0);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
}
void delete_returnbook(){
if((fp=fopen(“jieshujilu.txt”,“r”))==NULL){
} printf(“不能打開此文件!n”);exit(0);printf(“n請輸入所還書本的書編號:”);scanf(“%d”,&numbers);FILE *fp;int numbers;struct person p;void deletereturnbook();char choice;printf(“n
刪除成功!n”);fclose(fp2);fclose(fp3);
//刪除借書記錄
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
while(!feof(fp)){
fscanf(fp,“%s %s %ld %s %ld %s %ld %ld”,p.name,p.classes,&p.number,p.telephone,&p.NO,p.bookname,&p.borrowdate,&p.returndate);
if(p.NO==numbers){
printf(“nt***************圖書信息*******************n”);printf(“nt借書人姓名:%20s”,p.name);printf(“nt----------”);printf(“nt借書人班級:%20s”,p.classes);printf(“nt----------”);printf(“nt借書人學號:%20d”,p.number);printf(“nt----------”);printf(“nt借書人聯(lián)系電話:%20s”,p.telephone);printf(“nt----------”);printf(“nt圖書編號:%24d”,p.NO);printf(“nt----------”);printf(“nt圖書名稱:%23s”,p.bookname);printf(“nt----------”);printf(“nt借書日期:%25d”,p.borrowdate);printf(“nt----------”);printf(“nt還書日期:%25d”,p.returndate);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
}
}
} printf(“nt----------”);
deletereturnbook();
printf(“nt您想繼續(xù)嗎?(y/n)”);scanf(“ %c”,&choice);if(choice=='Y'||choice=='y'){
} system(“cls”);delete_returnbook();fclose(fp);void deletereturnbook(){ FILE *fp,*fp1,*fp2,*fp3;struct person p;
int numbers;printf(“nn確認刪除?請再次輸入書編號:”);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
scanf(“%d”,&numbers);if((fp=fopen(“jieshujilu.txt”,“r”))==NULL){
} if((fp1=fopen(“tempbook.txt”,“w”))==NULL){
} printf(“不能打開此文件!n”);exit(0);printf(“不能打開此文件!n”);exit(0);while(!feof(fp)){ fscanf(fp,“%s %s %d %s %d %s %d %d”,p.name,p.classes,&p.number,p.telephone,&p.NO,p.bookname,&p.borrowdate,&p.returndate);
if(p.NO==numbers)continue;else
fprintf(fp1,“%s %s %d %s %d %s %d %d”,p.name,p.classes,p.number,p.telephone,p.NO,p.bookname,p.borrowdat
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
e,p.returndate);
fscanf(fp2,“%s %s %d %s %d %s %d %d”,p.name,p.classes,while(!feof(fp2)){
//將臨時文件寫人源文件 if((fp2=fopen(“tempbook.txt”,“r”))==NULL){
} if((fp3=fopen(“jieshujilu.txt”,“w”))==NULL){
} printf(“不能打開此文件!n”);exit(0);printf(“不能打開此文件!n”);exit(0);fclose(fp);fclose(fp1);} &p.number,p.telephone,&p.NO,p.bookname,&p.borrowdate,&p.returndate);
fprintf(fp3,“%s %s %d %s %d %s %d %d”,p.name,p.數(shù)據(jù)結構大作業(yè) 121279044 伍楊
classes,p.number,p.telephone,p.NO,p.bookname,p.borrowdate,p.returndate);
} printf(“n
刪除成功!n”);fclose(fp2);fclose(fp3);} 5)Main函數(shù)
int main(void){
do{ printf(“nnn
圖書館管理系統(tǒng)na”);printf(“ *******************************************************n”);int choice;char choice2;struct book;struct person;
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
printf(“ ***朱老師您好吖********功能選項: 登記******請按1,******n”);printf(“ ******************************* 查看/查詢*請按2 ******n”);printf(“ ******************************* 刪除***** 請按3 ******n”);printf(“ ******************************* 退出***** 請按0 ******n”);printf(“ *******************************************************nnn”);printf(“
請選擇功能:”);scanf(“%d”,&choice);switch(choice){ case 1:
printf(“
登記選項:新書登記請按1,借書登記請按2,返回請按3n”);printf(“請選擇:”);scanf(“%d”,&choice);switch(choice){ case 1:
system(“cls”);
//清屏 printf(“新書資料登記:nn”);new_book();
//新書登記 printf(“登記完畢!n”);printf(“n”);scanf(“ %c”,&choice2);system(“cls”);break;
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
case 2:
system(“cls”);printf(“借書資料登記:nn”);new_person();
//借書登記 printf(“n press anykey ”);scanf(“ %c”,&choice2);system(“cls”);break;case 3:
} break;system(“cls”);break;
case 2: printf(“
查看/查詢選項:書庫查看請按1,總借書記錄查看請按2,到期記錄查詢請按3,返回請按4n”);
printf(“請選擇:”);scanf(“%d”,&choice);switch(choice){ case 1:
system(“cls”);printf(“歡迎朱老師進入書庫!nn”);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
printf(“書名
書編號
類型
到書日期n”);printf(“-----------n”);see_book();
//書庫顯示 printf(“n press anykey ”);scanf(“ %c”,&choice2);system(“cls”);break;case 2:
system(“cls”);printf(“歡迎朱老師進入借書記錄!nn”);printf(“姓名
班級 學號 聯(lián)系電話 書編號 書名
借書日期 到書日期n”);
printf(“-------------------------n”);see_person();
//借書記錄顯示
printf(“n press anykey ”);scanf(“ %c”,&choice2);system(“cls”);break;case 3:
system(“cls”);search_person();
//顯示符合記錄 printf(“n press anykey ”);
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
scanf(“ %c”,&choice2);system(“cls”);break;case 4:
} break;system(“cls”);break;case 3: printf(“
刪除選項:舊書刪除請按1,借書記錄刪除請按2,返回請按3n”);
printf(“請選擇:”);scanf(“%d”,&choice);switch(choice){ case 1:
system(“cls”);delete_books();
//刪除ing printf(“n press anykey ”);scanf(“ %c”,&choice2);system(“cls”);break;case 2:
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
}
system(“cls”);delete_returnbook();
//刪除ing printf(“n press anykey ”);scanf(“ %c”,&choice2);system(“cls”);break;case 3:
} break;system(“cls”);break;case 0:
} system(“cls”);}while(choice!= 0);return 0;
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
四、調試與測試
主菜單
登記
數(shù)據(jù)結構大作業(yè) 121279044 伍楊
查看
刪除
生成的文件內信息
數(shù)據(jù)結構大作業(yè) 121279044 伍楊五、五心得體會
經(jīng)過這次大作業(yè),我覺得代碼的編寫,最主要的的是編程思想,語言其實不是太重要,思路最重要!
六、用戶手冊
程序執(zhí)行文件為 lib sys.exe,打開執(zhí)行文件后按提示操作即可
第二篇:c語言課程設計-學籍管理系統(tǒng)(含源代碼)
課 程 設 計 課程設計名稱: C語言程序設計 題 目:學籍管理系統(tǒng) 學 生 姓 名: 學生學號 : 學 院(系): 軟件學院 專 業(yè) 班 級: 112021 指 導 教 師:
設計時間: 2012 年 9 月 日 ? 2012 年 9月_ 14 日
實驗題目:學籍管理系統(tǒng)一、實驗目的
綜合應用所學的C語言程序設計知識,自行設計并實現(xiàn)一個較為完整的小型管理信息系統(tǒng)。通過系統(tǒng)分析、系統(tǒng)設計、編程實現(xiàn),寫實驗報告等環(huán)節(jié),初步掌握軟件系統(tǒng)的設計方法和步驟,提高靈活運用程序語言進行軟件開發(fā)的技能,提高程序設計水平和分析問題、解決問題的能力。
二、實驗內容
1):熟悉C語言的開發(fā)環(huán)境,按照給定的上機步驟練習完成;
2):熟悉C程序的編輯,編譯,鏈接和運行的過程。3):編譯一個應用系統(tǒng)程序,形成一個軟件系統(tǒng)。
三.實驗要求
1.1、分析系統(tǒng)功能
(1)用戶進入主菜單后,就會在看到,菜單選項中添加有系統(tǒng)的各項功能,進入的
應的選項就可進行相應的操作.其主要功能有:
1、錄入學生信息
2、刪除學生信息
3、查詢學生信息
4、學生信息排序
5、改學生信息
6、保存退出系統(tǒng)
(2)用戶選擇所需操作的選項,進入相應的操作界面,在這里用戶就可開始進行操作。
四、使用說明
學生學籍管理系統(tǒng)是針對學生信息的管理,主要功能是添加學生信息、刪除學生信息、查詢學生信息、學生信息排序、修改學生信息、保存信息。
1,用戶打開程序,進入主界面,輸入學生信息如圖
2,按回車進入主菜單,列出各項功能如圖
輸入1,是查詢整個班級的學生的信息,如圖
輸入2,是查詢個別學生的信息,如查詢第一學生的信息,如圖
輸入3,是刪除個別學生的信息,如刪除第一個學生,如圖
輸入4,是插入某些學生的信息,如插入第三個,如圖
輸入5,是修改某個同學的信息,如刪除第一個,如圖
三、心得體會
兩周的課程過起來其實也是很快的。這是我第一次做課程設計,起初還沒做的時候覺得很快自己就將得編一個較大的程序,將會很有意思。帶著最初的好奇心,新鮮感就這樣開始了第一天的編程,結果是大失所望。做課程設計并不是自己想象中的那樣有意思,而是很枯燥,很乏味的。也沒想象中的那樣簡單,并不是像我們平時上C語言課時,每次編的那些小程序,沒那么簡單。我們現(xiàn)在要做的就是將我們平時學的,做的那些小程序都合理的湊到一塊兒來。而把這些小程序都加到一塊兒來,并不是隨意的將它們放到一個程序中就完事的,而是必須得合理,且得顧及到各個方面。
正是由于編程的紛繁復雜,且結構的嚴謹,因此編程的過程中到處是困難和問題。它考驗的不僅是我們的平時用功程度,以及我們對所學知識的熟練掌握程度、應用的靈活程度,它還考驗我們的毅力。在剛開始的幾天時,由于前一陣忙于各科的考試,C語言已經(jīng)好久沒碰了,所學的知識都有點遺忘了,在編寫時處處碰壁,一直會停頓翻書,編得自己都開始心煩意亂了,實在是編不下去了,于是索性就停了三天去看書,先把書給吃透。并在后期的程序調試中也碰到不少的問題,好多問題自己反復檢查了幾遍都沒查出,但在老師的幫助下還是一下就查出了。并不是這些問題多難,而是不夠心細。因此做課程設計、編程時,它還考驗并鍛煉我們的心細程度。
經(jīng)過這次的課程設計的實踐,我受益頗多,不僅是對我掌握知識、靈活運用知識的一次考驗和鍛煉,也是對我生活態(tài)度的一次鍛煉,讓我學會心細和擁有毅力,更具信心和恒心,碰到困難不再退縮,而是堅強面對。
四,程序編碼
/*做一個學生的學籍管理系統(tǒng),有輸入,查詢,刪除,增加,修改等功能*/ #include
#include
/*聲明數(shù)組的大小,可以任意改動*/ int board[50][50];/*聲明一個表格的數(shù)組*/ int cur_x, cur_y;/*定義坐標*/
void init();/*聲明一個初始化界面的函數(shù)*/ void clear();/*清除界面的函數(shù)*/
void draw_board();/*聲明一個函數(shù)畫表格*/ struct student{/*創(chuàng)建一個學生的結構體*/
char stuNo[8];
/*學生的學號*/
char name[10];/*學生的姓名*/
char sex[2];/*學生的性別*/
char score[4];
/*學生的分數(shù)*/
char address[10];/*學生的地址*/ };void init()/*初始化函數(shù)*/ {
int gdriver, gmode, i, j;
gdriver = DETECT;/*圖形界面的驅動聲明*/
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver, &gmode, “");
for(i = 0;i < 10;i ++)
for(j = 0;j < 10;j++)/*聲明坐標的間距*/
board[i][j] = 0;
cur_x = 1;
cur_y = 1;}
void destroy()/*關閉圖形驅動器*/ {
closegraph();}
void draw_board(int n)/*畫表格的函數(shù)*/ {
int i, j;
for(i = 20;i <=5*160+80;i += 90)/*劃橫線的循環(huán)*/
{
line(i, 20, i,(n+1)*60+20);
}
for(i = 20;i <=(n+1)*60+30;i += 60)/*劃縱線的循環(huán)*/
{
line(20, i, 5*110+10, i);
} }
void main(){
struct student stu[SIZE],stu_temp;/*聲明結構體變量*/
FILE *fp;
/*聲明文件型的指針*/
int i,j,n,m,h;
int c=1;/*c為循環(huán)次數(shù)*/
h=0;
fp=fopen(”c:list.txt“,”wb+“);/*打開寫入文件*/
init();/*調用函數(shù)*/
draw_board(SIZE);
if(fp==NULL)/*驗證文件是否為空*/
{
printf(”cannot open this filen“);
exit(0);
}
printf(”input all %d students's data.n“,SIZE);/*畫輸入學生信息的表*/
gotoxy(17,4);
printf(”stuNo“);
gotoxy(29,4);
printf(”name“);
gotoxy(41,4);
printf(”sex“);
gotoxy(52,4);
printf(”score“);
gotoxy(63,4);
printf(”address“);
for(i=0;i { gotoxy(7,4*(i+2)); printf(”%d“,i); gotoxy(17,4*(i+2)); scanf(”%s“,&stu[i].stuNo); gotoxy(29,4*(i+2)); scanf(”%s“,&stu[i].name); gotoxy(41,4*(i+2)); scanf(”%s“,&stu[i].sex); gotoxy(52,4*(i+2)); scanf(”%s“,&stu[i].score); gotoxy(63,4*(i+2)); scanf(”%s“,&stu[i].address); } for(i=0;i if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) { printf(”file write error!“); exit(0); } rewind(fp); clrscr(); for(c=1;c<100;c++)/*創(chuàng)建一個圖形界面*/ { textbackground(0); textcolor(1); gotoxy(29,7); printf(”read->1“); gotoxy(29,9); printf(”find->2“); gotoxy(29,11); printf(”delete->3“); gotoxy(29,13); printf(”insert->4“); gotoxy(29,15); printf(”modify->5“); gotoxy(29,17); printf(”plese enter j= “); scanf(”%d“,&j); clrscr(); if(j==1)/*當輸入為1時,顯示整個班級 {的學生信息*/ draw_board(SIZE); gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); for(i=0;i { fread(&stu_temp,sizeof(struct student),1,fp);出每個學生的信息*/ gotoxy(7,4*(i+2)); printf(”%d“,i); gotoxy(17,4*(i+2)); printf(”%s“,stu[i].stuNo); gotoxy(29,4*(i+2)); printf(”%s“,stu[i].name); gotoxy(41,4*(i+2)); printf(”%s“,stu[i].sex); gotoxy(52,4*(i+2)); printf(”%s“,stu[i].score); gotoxy(63,4*(i+2)); printf(”%s“,stu[i].address); } fclose(fp); /*關閉文件*/ getch();/*留在當前界面*/ clrscr();/*清屏*/ } if(j==2)/*當輸入為2時,查找某個學生的 {信息*/ rewind(fp);/*移動指針到最前*/ printf(”look up the nth(n<4)student,plese enter n= :n“);/*輸入要查找的學生 scanf(”%d“,&i);位置*/ clrscr(); draw_board(1);/*畫表格*/ gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); fseek(fp,(i-1)*sizeof(struct student),0);/*打開文件查找,讀出信 fread(&stu_temp,sizeof(struct student),1,fp);息*/ gotoxy(7,8); printf(”%d“,i); gotoxy(17,8); printf(”%s“,stu[i].stuNo); gotoxy(29,8); printf(”%s“,stu[i].name); gotoxy(41,8); printf(”%s“,stu[i].sex); gotoxy(52,8); printf(”%s“,stu[i].score); gotoxy(63,8); printf(”%s“,stu[i].address); } fclose(fp); getch(); clrscr(); if(j==3)/*當輸入為3,刪除某個學生的信息*/ { h=h-1;/*表格少畫一格*/ printf(”you want delete nth student,plese enter n= “); scanf(”%d“,&n); draw_board(SIZE+h);/*調用函數(shù)畫表格*/ gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); for(m=n;m { strcpy(stu[m].stuNo,stu[m+1].stuNo); strcpy(stu[m].name,stu[m+1].name); strcpy(stu[m].sex,stu[m+1].sex); strcpy(stu[m].score,stu[m+1].score); strcpy(stu[m].address,stu[m+1].address); } for(i=0;i { fread(&stu_temp,sizeof(struct student),1,fp);個表格*/ gotoxy(7,4*(i+2)); printf(”%d“,i); gotoxy(17,4*(i+2)); printf(”%s“,stu[i].stuNo); gotoxy(29,4*(i+2)); printf(”%s“,stu[i].name); gotoxy(41,4*(i+2)); printf(”%s“,stu[i].sex); gotoxy(52,4*(i+2)); printf(”%s“,stu[i].score); gotoxy(63,4*(i+2)); printf(”%s“,stu[i].address); } fclose(fp); getch(); clrscr(); } if(j==4)/*當輸入為4時,增加一個學生 {信息*/ h=h+1; printf(”you want insert nth student,plese enter n= “); scanf(”%d“,&n); for(m=n;m {息*/ strcpy(stu[m+1].stuNo,stu[m].stuNo); strcpy(stu[m+1].name,stu[m].name); strcpy(stu[m+1].sex,stu[m].sex); strcpy(stu[m+1].score,stu[m].score); strcpy(stu[m+1].address,stu[m].address); } draw_board(1);/*調用函數(shù)畫表格*/ gotoxy(7,4*2); printf(”%d“,n); gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); gotoxy(17,8);/*輸入一個新學生的 scanf(”%s“,&stu[n].stuNo);信息*/ gotoxy(29,8); scanf(”%s“,&stu[n].name); gotoxy(41,8); scanf(”%s“,&stu[n].sex); gotoxy(52,8); scanf(”%s“,&stu[n].score); gotoxy(63,8); scanf(”%s“,&stu[n].address); gotoxy(7,8); printf(”%d“,i); gotoxy(17,8); printf(”%s“,stu[n].stuNo); gotoxy(29,8); printf(”%s“,stu[n].name); gotoxy(41,8); printf(”%s“,stu[n].sex); gotoxy(52,8); printf(”%s“,stu[n].score); gotoxy(63,8); printf(”%s“,stu[n].address); clrscr(); draw_board(SIZE+h); gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); for(i=0;i { fread(&stu_temp,sizeof(struct student),1,fp);表格*/ gotoxy(7,4*(i+2)); printf(”%d“,i); gotoxy(17,4*(i+2)); printf(”%s“,stu[i].stuNo); gotoxy(29,4*(i+2)); printf(”%s“,stu[i].name); gotoxy(41,4*(i+2)); printf(”%s“,stu[i].sex); gotoxy(52,4*(i+2)); printf(”%s“,stu[i].score); gotoxy(63,4*(i+2)); printf(”%s“,stu[i].address); } fclose(fp); getch(); clrscr(); } if(j==5)/*當輸入為5,修改某個學生 {信息*/ printf(”you want to modify nth student information,plese enter n= “); scanf(”%d“,&n); /*輸入修改的學生的位置*/ draw_board(1); draw_board(1); gotoxy(7,4*2); printf(”%d“,n); gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); gotoxy(17,8); /*輸入新的學生信息*/ scanf(”%s“,&stu[n].stuNo); gotoxy(29,8); scanf(”%s“,&stu[n].name); gotoxy(41,8); scanf(”%s“,&stu[n].sex); gotoxy(52,8); scanf(”%s“,&stu[n].score); gotoxy(63,8); scanf(”%s“,&stu[n].address); gotoxy(7,8); clrscr(); draw_board(SIZE); gotoxy(17,4); printf(”stuNo“); gotoxy(29,4); printf(”name“); gotoxy(41,4); printf(”sex“); gotoxy(52,4); printf(”score“); gotoxy(63,4); printf(”address“); for(i=0;i { fread(&stu_temp,sizeof(struct student),1,fp); gotoxy(7,4*(i+2)); printf(”%d“,i); gotoxy(17,4*(i+2)); printf(”%s“,stu[i].stuNo); gotoxy(29,4*(i+2)); printf(”%s“,stu[i].name); gotoxy(41,4*(i+2)); printf(”%s“,stu[i].sex); gotoxy(52,4*(i+2)); printf(”%s“,stu[i].score); gotoxy(63,4*(i+2)); printf(”%s",stu[i].address); } fclose(fp);/*關閉文件*/ getch();/*保留在這個界面上*/ clrscr();/*清屏*/ } } } #include //編號// char name[20]; //起點和終點// char time[5]; //出發(fā)時間// int price; //車票價格// int amount; //剩余數(shù)量// struct Node *next;}Node;//創(chuàng)建鏈表并輸入數(shù)據(jù)// struct Node *creat(){ struct Node *head,*r,*s; int i=0; char choice; head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;r=head;do { s=(struct Node *)malloc(sizeof(struct Node));s->next=NULL;printf(“請輸入第%d種火車票的信息:n”,++i);printf(“請輸入火車的編號:”); scanf(“%d”,&s->num); printf(“起點和終點:”);scanf(“%s”,s->name);printf(“出發(fā)時間:”);scanf(“%s”,s->time);printf(“車票價格:”);scanf(“%d”,&s->price);printf(“剩余數(shù)量:”);scanf(“%d”,&s->amount); r->next=s; r=s; printf(“Continue?(Y/N)”);scanf(“%s”,&choice);}while(choice=='Y'||choice=='y'); r->next=NULL;return(head);} //將單鏈表中的信息保存到文件1.txt中// void save(struct Node *h){ struct Node *s;FILE *fp; char filename[10]=“1.txt”; fp=fopen(“1.txt”,“wt”);if(fp==NULL){ printf(“n寫文件出錯,按任意鍵退出!”);getchar();exit(1);} for(s=h->next;s!=NULL;s=s->next) fprintf(fp,“%d %s %s %d %d n”,s->num,s->name,s->time,s->price,s->amount); getchar();fclose(fp);} // 從文件1.txt中讀取信息并存入單鏈表中// struct Node *read(){ struct Node *head,*r,*s;FILE *fp;char filename[10]=“zl.txt”;fp=fopen(“1.txt”,“rt”);if(fp==NULL){ printf(“讀文件錯誤,按任意鍵退出!”);getchar();exit(1);} head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;r=head;while(!feof(fp)){ s=(struct Node *)malloc(sizeof(struct Node));fscanf(fp,“%d %s %s %d %d”,&s->num,s->name,s->time,&s->price,&s->amount); r->next=s;r=s; } r->next=NULL;fclose(fp); return head;} //將鏈表中的數(shù)據(jù)輸出// void print(struct Node *h){ struct Node *s; printf(“n火車票信息如下:n”); printf(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n”);printf(“編號 起點和終點 出發(fā)時間 車票價格 剩余票數(shù):n”); for(s=h->next;s->next!=NULL;s=s->next){ printf(“ %d %10s %5s %10d %6dn”,s->num,s->name,s->time,s->price,s->amount);} } //鏈表查詢// struct Node * find(struct Node *h){ int i,j;char s[20];printf(“tt 查詢方法有以下幾種:n”);printf(“tt 1.火車票編號n”);printf(“tt 2.起點和終點n”);printf(“tt 3.出發(fā)時間n”);printf(“tt 4.車票價格n”);printf(“tt 5.剩余票數(shù)n”);printf(“請輸入您要查詢的方法的序號:”);scanf(“%d”,&i);switch(i){ case 1:printf(“請輸入你要查詢火車票的編號:”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->num==j)return h; } return NULL;break;case 2:printf(“請輸入您要查詢火車票的起點和終點:”);scanf(“%s”,s);while(h->next!=NULL){ h=h->next;if(strcmp(h->name,s)==0) return h; } return NULL;break;case 3:printf(“請輸入您要查詢火車票的時間:”); scanf(“%s”,s); while(h->next!=NULL) { h=h->next; if(strcmp(h->time,s)==0) return h; } return NULL; break;case 4:printf(“請輸入你要查詢火車票的價格 :”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->price==j) return h; } return NULL; break;case 5:printf(“請輸入你要查詢火車票的剩余票數(shù):”);scanf(“%d”,&j); while(h->next!=NULL) { h=h->next; if(h->amount==j) return h; } return NULL; break;} } //修改信息// change(struct Node *h,int k){ int j;struct Node *p;p=find(h);printf(“------------n”);printf(“t 您要修改哪一項?n”);printf(“t 1.火車編號n”);printf(“t 2.起點和終點n”);printf(“t 3.出發(fā)時間n”);printf(“t 4.車票價格n”); printf(“t 5.剩余票數(shù)n”);printf(“t 0.退出系統(tǒng)n”); printf(“------------n”);printf(“請輸入您要修改項的編號:”);scanf(“%d”,&j);switch(j) { case 1: printf(“修改后的火車編號:”); scanf(“%d”,&p->num); break; case 2: printf(“修改后的起點和終點:”); scanf(“%s”,p->name); break; case 3: printf(“修改后的出發(fā)時間:”); scanf(“%s”,p->time); break; case 4: printf(“修改后的車票價格:”); scanf(“%d”,&p->price); break; case 5: printf(“修改后的剩余票數(shù):”); scanf(“%d”,&p->amount); break; case 0:break;} } //刪除信息// delete(struct Node *h){ struct Node *p; int j; printf(“請輸入您要刪除的火車票的編號:”);scanf(“%d”,&j);p=h->next; if(p==NULL) return 0;while(p!=NULL){ if(p->num==j){ h->next=p->next; free(p); return 1;} h=p;p=p->next; } return 0;} //添加信息// void append(){ struct Node *p; FILE *fp; fp=fopen(“1.txt”,“at+”); if(fp==NULL) { printf(“寫文件出錯,按任意鍵返回.n”);getchar();exit(1); } printf(“請輸入要添加的火車票的信息:火車編號,起點和終點,出發(fā)時間,車票價格,剩余票數(shù):n”);scanf(“%d%s%s%d%d”,&p->num,p->name,p->time,&p->price,&p->amount);fprintf(fp,“%d %s %s %d %dn”,p->num,p->name,p->time,p->price,p->amount);getchar();fclose(fp);} //數(shù)據(jù)的統(tǒng)計// void count(struct Node *h){ struct Node *s;s=h;int i,j,k,n=0;printf(“*****************************************************************************n”); printf(“tt 請選擇您要統(tǒng)計項目的序號:n”); printf(“tt 1.車票價格n”); printf(“tt 2.剩余票數(shù)n”);printf(“tt 0.退出界面n”); scanf(“%d”,&i);switch(i) { case 1: printf(“請輸入您要統(tǒng)計車票的價格的標準:”); scanf(“%d”,&j); printf(“tt 請選擇低于或高于標準:n”); printf(“tt 1.價格低于%d的個數(shù)n”,j); printf(“tt 2.價格高于%d的個數(shù)n”,j); scanf(“%d”,&k); if(k==1) { for(s=h->next;s->next!=NULL;s=s->next) if(s->price n++; printf(“車票價格低于%d的個數(shù)有%d個.n”,j,n); } else { for(s=h->next;s->next!=NULL;s=s->next) if(s->price>j) n++; printf(“車票價格低于%d的個數(shù)有%d個.n”,j,n); } break; case 2: printf(“請輸入您要統(tǒng)計剩余票數(shù)的數(shù)量:”); scanf(“%d”,&j); printf(“tt 請選擇低于或高于所輸票數(shù):n”); printf(“tt 1.票數(shù)低于%d的個數(shù)n”,j); printf(“tt 2.票數(shù)高于%d的個數(shù)n”,j); scanf(“%d”,&k); if(k==1) { for(s=h->next;s->next!=NULL;s=s->next) if(s->amount n++; printf(“剩余票數(shù)低于%d的個數(shù)有%d個.n”,j,n); } else { for(s=h->next;s->next!=NULL;s=s->next) if(s->amount>j) n++; printf(“剩余票數(shù)高于%d的個數(shù)有%d個.n”,j,n); } break; case 0:break; } } //保存用戶和密碼到文件2.txt中// void save_user(){ char file[10]=“2.txt”;FILE *fp;char name[20];char pwd[10];fp=fopen(“2.txt”,“at+”);if(fp==NULL){ printf(“n寫文件出錯,按任意鍵退出.n”); getchar();exit(1);} printf(“請輸入用戶名:”); scanf(“%s”,name);printf(“請輸入密碼:”); scanf(“%s”,pwd); fprintf(fp,“%s %sn”,name,pwd); getchar(); fclose(fp); printf(“用戶注冊成功!n”);} //檢驗用戶和密碼是否匹配// int check(char *name,char *pwd){ char name1[20];char pwd1[10];FILE *fp;char file[10]=“2.txt”;if((fp=fopen(“2.txt”,“rt”))==NULL){ printf(“讀文件出錯,按任意鍵退出!n”); getchar(); exit(1);} while(!feof(fp)){ fscanf(fp,“%s %s”,name1,pwd1); if(strcmp(name1,name)==0&&strcmp(pwd1,pwd)==0) return 1;} return 0;} //數(shù)據(jù)排序// void sort(struct Node *h){ struct Node *s,*p,*m,*n;int t,t1,t2,t3;char s1[20];char s2[10]; printf(“車票價格由小到大排序如下:n”);for(s=h->next;s->next!=NULL;s=s->next)for(p=s->next;p->next!=NULL;p=p->next) if(s->price>p->price) { t1=s->num;s->num=p->num;p->num=t1; t2=s->price;s->price=p->price;p->price=t2; t3=s->amount;s->amount=p->amount;p->amount=t3; strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1); strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2); } print(h);printf(“nn剩余車票數(shù)量由多到少排序如下:n”);for(s=h->next;s->next!=NULL;s=s->next) for(p=s->next;p->next!=NULL;p=p->next) if(s->amount amount) { t1=s->num;s->num=p->num;p->num=t1; t2=s->price;s->price=p->price;p->price=t2; t3=s->amount;s->amount=p->amount;p->amount=t3; strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1); strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2); } print(h);} void main(){ struct Node *head,*p;int i,j,k;head=(struct Node *)malloc(sizeof(struct Node));head->next=NULL;char name[20];char pwd[10];printf(“n***************歡迎進入火車票管理系統(tǒng)******************n”);printf(“tt 1.用戶登錄n”);printf(“tt 2.用戶注冊n”);printf(“tt 0.退出系統(tǒng) n”);printf(“請輸入所選序號:”);scanf(“%d”,&k); switch(k){ case 1: printf(“請輸入用戶名:”); scanf(“%s”,name); printf(“請輸入密碼:”); scanf(“%s”,pwd); if(check(name,pwd)) { printf(“密碼正確.n”); do { printf(“nntt*********************歡迎進入火車票管理系統(tǒng)***********************n”); printf(“tt 1.錄入火車票信息tt 2.添加火車票信息n”); printf(“tt 3.修改火車票信息tt 4.刪除火車票信息n”); printf(“tt 5.打印火車票信息tt 6.查詢火車票信息n”); printf(“tt 7.統(tǒng)計火車票信息tt 8.火車票銷售排行n”); printf(“tt 0.退出系統(tǒng)n”); printf(“請輸入您要進入菜單的序號(0-8):”); scanf(“%d”,&i); switch(i) { case 1: printf(“請錄入火車票信息nn”); head=creat(); save(head); head=read(); break; case 2: append(); break; case 3: printf(“請輸入您要修改的火車票的編號:”); scanf(“%d”,&j); change(head,j); save(head); break; case 4: head=read(); if(delete(head)) { printf(“已正確刪除!n”); save(head); } else printf(“要刪除的結點不存在!n”); break; case 5: head=read(); print(head); break; case 6: printf(“請輸入您要查詢火車票的編號(以0結束):”); scanf(“%d”,&j); { p=find(head); printf(“編號 起點和終點 出發(fā)時間 車票價格 剩余票數(shù):n”); printf(“%d %10s %5s %10d %6dn”,p->num,p->name,p->time,p->price,p->amount); printf(“請繼續(xù)輸入序號(以0結束):”); scanf(“%d”,&j); } break; case 7: head=read();count(head);break; case 8: sort(head);break; case 0: printf(“************************用!*****************************n”);break; } }while(i!=0); } else printf(“密碼錯誤或用戶名不存在.n”); break;case 2:save_user();break;case 0:break;} 謝 謝 使 目錄 前言..................................................................................................................2 概要設計..................................................................................................................3 1.1 數(shù)據(jù)結構設計...........................................................................................3 2.1 算法設計...................................................................................................3 2.1.1 建立鏈表的算法..............................................................................3 2.1.2 鏈表插入一個元素的算法..............................................................3 2.1.3 鏈表刪除一個元素的算法..............................................................3 3.1 ADT描述..................................................................................................4 4.1 詳細設計…………………………………………… ……………………………… 4 4.1.1 數(shù)據(jù)存儲結構……………………………… ……………………………… 4.4.1.2 主要偽代碼…… …………………… ……………………………………… 4 軟件測試..................................................................................................................7 心得體會................................................................................................................11 源代碼...................................................................................................................12 參考文獻………………………………………………………………………...21 前言 數(shù)據(jù)結構是計算機程序設計的重要理論技術基礎,它不僅是計算機學科的核心課程,而且已經(jīng)成為其他理工專業(yè)的熱門選修課。 隨著計算機科學的技術和發(fā)展,計算機的功能和運算速度不斷地提高,其應用于信息處理的范圍日益擴大。與之相應的,計算機的加工處理對象也從簡單的數(shù)據(jù)發(fā)展到一般的符號,進而發(fā)展到更復雜的數(shù)據(jù)結構。數(shù)據(jù)結構是計算機程序設計的重要理論技術基礎,數(shù)據(jù)結構的表示和操作都涉及到算法,如何描述數(shù)據(jù)的結構和討論有關的算法,又涉及到程序設計語言。因此,它不僅是計算機學科的核心課程,而且已經(jīng)成為其他理工專業(yè)的熱門選修課。我們通過對這門基礎課程的學習,要學會分析研究計算機加工的數(shù)據(jù)結構的特性,以便為應用涉及的數(shù)據(jù)選擇適合的邏輯結構,儲存結構及其相應的算法,并初步掌握算法時間分析和空間分析的技術。通過實際操作去了解數(shù)據(jù)結構原理,練習編寫代碼的能力,以及抽象能力。 從課程性質上講,“數(shù)據(jù)結構”是一門專業(yè)技術基礎課。它的要求是學會分析研究計算機加工的數(shù)據(jù)結構的特性,以便為應用涉及的數(shù)據(jù)選擇適當?shù)倪壿嫿Y構,存儲結構及相應的算法,并初步掌握算法的時間分析和空間分析的技術。另一方面,數(shù)據(jù)結構的學習過程也是復雜程序設計的訓練過程,要求編寫的程序結構清楚和正確易讀,符合軟件工程的規(guī)范。 概要設計 1.1 數(shù)據(jù)結構設計 采用鏈式儲存結構。typedef struct LNode{ ElemType data;struct LNode *next;}LNode,*LinkList;2.1 算法設計 2.1.1 建立鏈表的算法 (1)算法思想分析 首先從表尾到表頭逆向建立單鏈表,然后再建立的單鏈表基礎上進行對鏈表上的元素進行查詢,刪除,插入的操作。(2)要點描述 首先建立一個帶頭結點的單鏈表,通過申請內存,先建立一個空鏈表。然后結點的插入,建立一個有多個結點的鏈表。在進行查詢等操作。(3)時間和空間復雜度分析 程序的時間復雜度為O(n)。 2.1.2 鏈表插入一個元素的算法 (1)算法思想分析 要生成一個新數(shù)據(jù)域為X的結點,然后插入在單鏈表中。(2)要點描述 在鏈表中插入結點只需要修改指針。若要在第 i 個結點之前插入元素,修改的是第 i-1 個結點的指針。 (3)時間和空間復雜度分析 時間復雜度O(n)2.1.3 鏈表刪除一個元素的算法 (1)算法思想分析 要刪除一個結點,必須修改指針并且釋放空間。(2)要點描述 找到線性表中第i-1個結點,修改其指向后繼的指針。 (3)時間和空間復雜度分析 時間復雜度O(n) 3.1 ADT描述 ADT LinkList{ 數(shù)據(jù)對象:D={ e | e∈LNode } 數(shù)據(jù)關系:R1={ 基本操作: GreateList_L(&L, n) 操作結果:構造了一個長為n的數(shù)據(jù)鏈表 ListDelete_L(&L, i, &e) 初始條件:鏈表L已存在而且非空 操作結果:刪除L的第i個數(shù)據(jù),并且用e返回其值 ListInsert_L(&L, i, e) 初始條件:鏈表L已存在 操作結果: 在L的第i個位置插入數(shù)據(jù)e GetElem(L, i, e) 初始條件:鏈表L已存在 操作結果:用e返回L中的第i個數(shù)據(jù) }ADT LinkList 4.1 詳細設計 4.1.1數(shù)據(jù)存儲結構設計 采用單鏈式線性表實現(xiàn) 4.1.2 主要偽代碼 Status GetElem(LinkList L, int i, ElemType *e){ int j=0;int d;LinkList p = L;while(p&&jnext;j++; } if(!p || j > i)return ERROR;printf(“您要查詢的元素是:n”);d=p->data;printf(“%d”,d);printf(“n”);} void InitList(LinkList *L){ *L =(LinkList)malloc(sizeof(struct LNode));if(!*L)exit(OVERFLOW);(*L)->next = NULL;} Status ListInsert(LinkList L, int i, ElemType e){ int j = 0;LinkList p = L, s;while(p && j < i-1){ p = p->next;j++;} if(!p|| j > i-1)return ERROR;s =(LinkList)malloc(sizeof(struct LNode));s->data = e;s->next = p->next;p->next = s;return OK;} Status ListDelete(LinkList L, int i, ElemType *e){ int j = 0;LinkList p = L, q;while(p->next && j < i-1){ p = p->next; j++;} if(!p->next || j > i-1)return ERROR;q = p->next;p->next = q->next;*e = q->data;free(q);return OK;} void ListTraverse(LinkList L, void(*vi)(ElemType)){ LinkList p = L->next;while(p){ vi(p->data);p = p->next;} printf(“n”);} void ListPrint(LinkList L){ LinkList p = L->next;while(p){ printf(“%d ”, p->data);p = p->next;} printf(“n”);} void printInt(int data){ printf(“%d ”, data);}.軟件測試 圖一(主界面) 圖二(插入學生信息) 圖三(顯示所有學生信息) 圖四(查詢個人信息) 圖五(統(tǒng)計信息) 圖六(修改信息) 圖七(保存數(shù)據(jù)) 圖八(刪除信息) 心得體會 通過本程序的設計,我對數(shù)據(jù)結構作了以下總結:要解決一道程序題必須先要認真捕捉改程序中的有用信息,找出解決方法。先規(guī)劃好,程序需要什么樣的數(shù)據(jù)結構,什么函數(shù),對程序有什么要求。然后從整體把握對程序設計進行分工,相應地把程序分成若干模塊,具體實現(xiàn)各部分實行相應的功能。一個程序要順利地進行設計,一是要對程序的功能有全面的了解,如果漏了某些部分,都會使得這個程序調試不出來或者是令該程序沒有達到預想的效果。其次,在程序的編譯中,必須注重程序設計過程中的細節(jié),像單鏈表的程序,就要理解鏈表的概念,理解鏈表的數(shù)據(jù)特點,要清楚知道數(shù)據(jù)域和指針域的作用,否則,很容易會浪費大量時間在檢測錯誤上面。要說到解題的思考方向,如果要總結成規(guī)律,我認為要靈活的進行方法的設計,通過不同的方法來實現(xiàn)不同的功能,如通過結點的插入來實現(xiàn)鏈表的創(chuàng)建。同時應該注意各種語句的選擇,要先預想好需要什么樣的語句來實現(xiàn)函數(shù)定義,盡量簡單快捷地完成,避免出錯。 要規(guī)范面向對象程序設計師的書寫協(xié)管,在這次課程設計中,我們再次感受到,規(guī)范的程序書寫,可以更好的進行后期的差錯補漏。還應該注意各種面向對象語言語法的運用,例如繼承的方法,都要嚴格按照語法來進行,否則很容易就會出現(xiàn)錯誤,甚至嚴重影響課程設計的進度。 源代碼 #include “stdio.h” #include “stdlib.h” #include “string.h” int shoudsave=0;// struct student { char num[10];//學號 char name[20]; char sex[4]; int cgrade; int mgrade; int egrade; int totle; int ave; char neartime[10];//最近更新時間 }; typedef struct node { struct student data; struct node *next;}Node,*Link; int menu(){ char m[3]; int n; printf(“ ************************歡迎進入學生成績管理系統(tǒng)******************************nn”); printf(“t歡迎使用本學生管理系統(tǒng),本系統(tǒng)將為您提供歷史學生信息查詢,學生成績信息管理功能。n”); printf(“********************************************************************************”); printf(“t1輸入學生資料ttttt2刪除學生資料n”); printf(“t3查詢學生資料ttttt4修改學生資料n”); printf(“t5顯示學生資料ttttt6統(tǒng)計學生成績n”); printf(“t7保存學生資料n”); printf(“ttplease choose a operation(1-7):n”); printf(“*********************************************************************** *********n”); scanf(“%s”,m); n=atoi(m); return(n);} void printstart(){ printf(“---------n”);} void Wrong(){ printf(“n=====>提示:輸入錯誤!n”);} void Nofind(){ printf(“n=====>提示:沒有找到該學生!n”);} void printc()// 本函數(shù)用于輸出中文 { printf(“學號t 姓名 性別 英語成績 數(shù)據(jù)庫成績 數(shù)據(jù)結構成績 總分平均分n”);} void printe(Node *p)//本函數(shù)用于輸出英文 { printf(“%-12s%stt%st%dtt%dt%dt%dt %dn”,p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave);} Node* Locate(Link l,char findmess[],char nameornum[])//該函數(shù)用于定位連表中符合要求的接點,并返回該指針 { Node *r; if(strcmp(nameornum,“num”)==0)//按學號查詢 { r=l->next; while(r!=NULL) { if(strcmp(r->data.num,findmess)==0) return r; r=r->next; } } else if(strcmp(nameornum,“name”)==0)//按姓名查詢 { r=l->next; while(r!=NULL) { if(strcmp(r->data.name,findmess)==0) return r; r=r->next; } } return 0;} void Add(Link l)//增加學生 { Node *p,*r,*s; char num[10]; r=l; s=l->next; while(r->next!=NULL) r=r->next;//將指針置于最末尾 while(1) { printf(“請你輸入學號(以'0'返回上一級菜單:)”); scanf(“%s”,num); if(strcmp(num,“0”)==0) break; while(s) { if(strcmp(s->data.num,num)==0) { printf(“=====>提示:學號為'%s'的學生已經(jīng)存在,若要修改請你選擇'4 修改'!n”,num); printstart(); printc(); printe(s); printstart(); printf(“n”); return; } s=s->next; } p=(Node *)malloc(sizeof(Node)); strcpy(p->data.num,num); printf(“請你輸入姓名:”); scanf(“%s”,p->data.name); getchar(); printf(“請你輸入性別:”); scanf(“%s”,p->data.sex); getchar(); printf(“請你輸入數(shù)據(jù)結構成績:”); scanf(“%d”,&p->data.cgrade); getchar(); printf(“請你輸入數(shù)據(jù)庫成績:”); scanf(“%d”,&p->data.mgrade); getchar(); printf(“請你輸入英語成績:”); scanf(“%d”,&p->data.egrade); getchar(); p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade; p->data.ave=p->data.totle / 3; //信息輸入已經(jīng)完成p->next=NULL; r->next=p; r=p; shoudsave=1; } } void Qur(Link l)//查詢學生 { char findmess[20]; Node *p; if(!l->next) { printf(“n=====>提示:沒有資料可以查詢!n”); return; } printf(“請你輸入要查找的學號:”); scanf(“%s”,findmess); p=Locate(l,findmess,“num”); if(p) { printf(“tttt查找結果n”); printstart(); printc(); printe(p); printstart(); } else Nofind();} void Del(Link l)//刪除 { Node *p,*r; char findmess[20]; if(!l->next) { printf(“n=====>提示:沒有資料可以刪除!n”); return; } printf(“n=====>確定進行刪除操作請按 1,按其他按鍵退出該操作nnnn”); if(menu()==1) { printf(“請你輸入要刪除的學號:”); scanf(“%s”,findmess); p=Locate(l,findmess,“num”); if(p) { r=l; while(r->next!=p) r=r->next; r->next=p->next; free(p); printf(“n=====>提示:該學生已經(jīng)成功刪除!n”); shoudsave=1; } else Nofind(); } else exit;} void Modify(Link l)//修改函數(shù) { Node *p; char findmess[20]; if(!l->next) { printf(“n=====>提示:沒有資料可以修改!n”); return; } printf(“請你輸入要修改的學生學號:”); scanf(“%s”,findmess); p=Locate(l,findmess,“num”); if(p) { printf(“請你輸入新學號(原來是%s):”,p->data.num); scanf(“%s”,p->data.num); printf(“請你輸入新姓名(原來是%s):”,p->data.name); scanf(“%s”,p->data.name); getchar(); printf(“請你輸入新性別(原來是%s):”,p->data.sex); scanf(“%s”,p->data.sex); printf(“請你輸入新的數(shù)據(jù)結構成績(原來是%d分):”,p->data.cgrade); scanf(“%d”,&p->data.cgrade); getchar(); printf(“請你輸入新的數(shù)據(jù)庫成績(原來是%d分):”,p->data.mgrade); scanf(“%d”,&p->data.mgrade); getchar(); printf(“請你輸入新的英語成績(原來是%d分):”,p->data.egrade); scanf(“%d”,&p->data.egrade); p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade; p->data.ave=p->data.totle/3; printf(“n=====>提示:資料修改成功!n”); shoudsave=1; } else Nofind(); } void Disp(Link l)//顯示函數(shù) { int count=0; Node *p; p=l->next; if(!p) { printf(“n=====>提示:沒有資料可以顯示!n”); return; } printf(“tttt顯示結果n”); printstart(); printc(); printf(“n”); while(p) { printe(p); p=p->next; } printstart(); printf(“n”);} void Tongji(Link l)//統(tǒng)計函數(shù) { Node *pm,*pe,*pc,*pt,*pa;//用于指向分數(shù)最高的接點 Node *r=l->next; if(!r) { printf(“n=====>提示:沒有資料可以統(tǒng)計!n”); return; } pm=pe=pc=pt=pa=r; while(r!=NULL) { if(r->data.cgrade>=pc->data.cgrade) pc=r; if(r->data.mgrade>=pm->data.mgrade) pm=r; if(r->data.egrade>=pe->data.egrade) pe=r; if(r->data.totle>=pt->data.totle) pt=r; if(r->data.ave>=pa->data.ave) pa=r; r=r->next; } printf(“------------------------------統(tǒng)計結果-n”); printf(“總分最高者:t%s %d分n”,pt->data.name,pt->data.totle); printf(“平均分最高者:t%s %d分n”,pa->data.name,pa->data.ave); printf(“英語最高者:t%s %d分n”,pe->data.name,pe->data.egrade); printf(“數(shù)據(jù)庫最高者:t%s %d分n”,pm->data.name,pm->data.mgrade); printf(“數(shù)據(jù)結構最高者:t%s %d分n”,pc->data.name,pc->data.cgrade); printstart();} void Save(Link l)//保存函數(shù) { FILE* fp; Node *p; int flag=1,count=0; fp=fopen(“c:student”,“wb”); if(fp==NULL) { printf(“n=====>提示:重新打開文件時發(fā)生錯誤!n”); exit(1); } p=l->next; while(p) { if(fwrite(p,sizeof(Node),1,fp)==1) { p=p->next; count++; } else { flag=0; break; } } if(flag) { printf(“n=====>提示:文件保存成功.(有%d條記錄已經(jīng)保存.)n”,count); shoudsave=0; } fclose(fp);} void main(){ Link l;//連表 FILE *fp;//文件指針 char ch; char jian; int count=0; Node *p,*r; l=(Node*)malloc(sizeof(Node)); l->next=NULL; r=l; fp=fopen(“C:student”,“rb”); if(fp==NULL) { fp=fopen(“C:student”,“wb”); exit(0); } printf(“n=====>提示:文件已經(jīng)打開,正在導入記錄......n”); while(!feof(fp)) { p=(Node*)malloc(sizeof(Node)); if(fread(p,sizeof(Node),1,fp))//將文件的內容放入接點中 { p->next=NULL; r->next=p; r=p;//將該接點掛入連中 count++; } } fclose(fp);//關閉文件 printf(“n=====>提示:記錄導入完畢,共導入%d條記錄.n”,count); for(;;) { switch(menu()) { case 1:Add(l);break;//增加學生 case 2:Del(l);break;//刪除學生 case 3:Qur(l);break;//查詢學生 case 4:Modify(l);break;//修改學生 case 5:Disp(l);break;//顯示學生 case 6:Tongji(l);break;//統(tǒng)計學生 case 7:Save(l);break;//保存學生 default: Wrong(); getchar(); break; } } } 參考文獻 《數(shù)據(jù)結構(C語言版)》----------------清華大學出版社 嚴蔚敏 吳偉民 編著 《C語言程序設計》------------------------中國鐵道出版社 丁峻嶺 余堅 編著 《數(shù)據(jù)結構》 課程設計報告書 一.課程設計題目 圖書借閱管理系統(tǒng) 二.課程設計內容 實現(xiàn)圖書管理信息系統(tǒng)的設計 (一)管理員功能: ①登錄:輸入管理員密碼,若密碼錯誤則不得執(zhí)行管理員操作。 ②添加新書:增加新的圖書資料,同時需檢查新書的圖書編號是否已存在于原圖書資料中,若已存在則應取消添加或提示重新輸入。 ③修改圖書:通過編號查詢該圖書資料,若該編號存在,則顯示已有信息,允許修改,否則提示 無該圖書信息。 ④刪除圖書:通過編號查詢該圖書資料,若該編號存在,則顯示已有信息,允許刪除,否則提示無該圖書信息。刪除對象包括該圖書資料以及“圖書狀態(tài)”和“借閱人”中與此書相關的所有記錄。 ⑤查找圖書: A.按書號查找:通過編號查詢該圖書資料,若該編號存在,則顯示圖書信息,否則提示無該圖書信息。 B.按書名查找:通過書名查詢該圖書資料,若該編號存在,則顯示圖書信息,否則提示無該圖書信息。 C.按作者查找:通過作者查詢該圖書資料,若該編號存在,則顯示圖書信息,否則提示無該圖書信息。 ⑥查看全部圖書:顯示所有圖書資料,包括書號、書名、作者、狀態(tài)和借閱人等信息。 ⑦修改讀者:通過姓名查詢該讀者信息,若該姓名存在,則顯示已有信息,允許修改,否則提示無該讀者信息。 ⑧刪除讀者:通過姓名查詢該讀者信息,若該姓名存在,則顯示已有信息,允許刪除,否則提示無該讀者信息。刪除對象包括該讀者姓名、班級、電話以及“圖書狀態(tài)”和“借閱人”中與此人相關的所有記錄。 ⑨查看所有讀者:顯示所有讀者資料,包括姓名、班級、電話等信息。 (二)讀者功能: ①登錄:輸入讀者姓名,若未經(jīng)注冊則不得進入讀者服務界面。②注冊:新用戶經(jīng)注冊方可登錄系統(tǒng)并使用讀者服務功能。③借書: A.輸入所需圖書編號,判斷該編號是否存在,若不存在則提示重新輸入。 B.若所輸入圖書編號存在,則判斷該書籍是否已被借出,若已借則不允許執(zhí)行借書操作。 C.若所輸入圖書未借,則提示輸入讀者姓名,判斷所輸入讀者姓名是否存在,若不存在則提示重新輸入。D.若讀者姓名存在,則允許執(zhí)行借書操作。 E.借書處理包括在“圖書狀態(tài)”中加上“已借”標記。④還書: A.輸入所借書編號,判斷該編號是否存在,若不存在則提示重新輸入。B.判斷該書是否已借出,若未借則不允許執(zhí)行還書操作。C.借書處理包括在“圖書狀態(tài)”中加上“未借”標記。⑤查詢圖書: A.按書號查找:通過編號查詢該圖書資料,若該編號存在,則顯示圖書信息,否則提示無該圖書信息。 B.按書名查找:通過書名查詢該圖書資料,若該編號存在,則顯示圖書信息,否則提示無該圖書信息。 C.按作者查找:通過作者查詢該圖書資料,若該編號存在,則顯示圖書信息,否則提示無該圖書信息。 ⑥查看借閱情況:輸入讀者姓名,若該姓名不存在則提示重新輸入,若該姓名存在則顯示該讀者借閱信息。 三.算法設計 1.流程圖(部分示例) ①管理員登錄 ②讀者登錄 ③讀者借閱圖書 ④管理員或讀者按書名查找圖書 2.代碼實現(xiàn) #include #include //將字符串BookFile替換原文件中的宏名稱book #define ReaderFile “reader.txt” using namespace std;//**********讀者結構體,用于存放讀者基本信息********** class Reader { private: char rname[10]; char rclass[10]; char rtel[10];public: Reader()//構造函數(shù),實現(xiàn)數(shù)據(jù)初始化 {strcpy(rname,“");strcpy(rclass,”“);strcpy(rtel,”“);} //初始化 void set_rname(char *rn)//設置讀者姓名 {strcpy(rname,rn);} //將rn的字符復制給rname char * get_rname()//獲得讀者姓名 {return rname;} //返回rname的值 void set_rclass(char *rc)//設置班級 {strcpy(rclass,rc);} char *get_rclass() //獲得班級 {return rclass;} void set_rtel(char *rt) //設置電話 {strcpy(rtel,rt);} char *get_rtel() //獲得電話 {return rtel;} void copy(Reader m)//讀者Reader結構體copy函數(shù) {strcpy(rname,m.get_rname());strcpy(rclass,m.get_rclass());strcpy(rtel,m.get_rtel());} };//*********讀者管理結構體,實現(xiàn)對讀者用戶的管理********* class ReaderManage { Reader readers[50];//定義讀者結構體數(shù)組對象,最多50位 int length; void readFile() //讀取讀者文件函數(shù) { ifstream fread;//定義輸入文件流對象fread length=0; //計數(shù) if(!fread) {cout<<”讀取磁盤文件失敗!“< fread.open(ReaderFile,ios::binary);//使文件流與文件關聯(lián),以二進制方式打開 int i=0; while((fread.read((char *)&readers[i],sizeof(Reader))))//計算并返回所占字節(jié)數(shù) i++; length=i; fread.close();//關閉磁盤文件 } void saveFile()//保存讀者文件 { ofstream fwrite; //定義輸出文件流對象fwrite if(!fwrite) {cout<<”文件保存失敗!n“;exit(1);} fwrite.open(ReaderFile,ios::binary);//使文件流與文件關聯(lián),以二進制方式打開 fwrite.write((char *)readers,length*sizeof(Reader));//計算并返回所占字節(jié)數(shù) fwrite.close();//關閉磁盤文件 } public: ReaderManage()//構造函數(shù),讀取讀者文件 {readFile();} ~ReaderManage()//析構函數(shù),保存讀者文件 {saveFile();} void add_reader(Reader r)//添加讀者用戶函數(shù) { if(length>=50) {cout<<”對不起,注冊人數(shù)已滿!n“;return;} readers[length].copy(r);//調用讀者結構體copy函數(shù) length++;} int search(char *rn)//按姓名查找讀者函數(shù) { for(int i=0;i if(strcmp(readers[i].get_rname(),rn)==0)//判斷是否相等 return i; return-1; } bool revise(Reader reader,int i)//修改讀者資料函數(shù) { readers[i].copy(reader);//調用讀者結構體cpoy函數(shù) return true;} void delete_reader(int index)//刪除讀者函數(shù) { for(int i=index;i readers[i].copy(readers[i+1]);//用后面的地址覆蓋前面的地址 length--;} bool log_reader(Reader r)//登錄時判斷讀者用戶是否存在函數(shù),相當于遍歷 { for(int i=0;i if(strcmp(readers[i].get_rname(),r.get_rname())==0) return true; return false; } void reg_reader()//注冊用戶函數(shù) { char n[10]; //姓名name char c[10]; //班級class char t[10]; //電話tel Reader reader; cout<<”nt請輸入您的姓名:“; cin>>n; cout<<”nt請輸入您的所在班級:“; cin>>c; cout<<”nt請輸入您的聯(lián)系電話:“; cin>>t; reader.set_rname(n);//調用讀者結構體設置讀者姓名函數(shù) reader.set_rclass(c);//調用讀者結構體設置讀者班級函數(shù) reader.set_rtel(t);//調用讀者結構體設置聯(lián)系電話函數(shù) add_reader(reader);//調用讀者結構體添加讀者函數(shù) cout<<”nt注冊成功!“;} void show_reader()//不帶參的顯示讀者信息函數(shù) { cout<<”n ☆☆☆☆☆☆☆☆☆☆☆☆讀★★者★★信★★息★★列★★表☆☆☆☆☆☆☆☆☆☆☆nn“; cout<<”姓名ttt班級ttt電話n“; for(int i=0;i { cout< cout<<”ttt“< cout<<”ttt“< } } void show_reader(int i)//帶參的顯示讀者信息函數(shù) { cout<<”n ☆☆☆☆☆☆☆☆☆☆☆☆讀★★者★★信★★息★★列★★表☆☆☆☆☆☆☆☆☆☆nn“; cout<<”姓名ttt班級ttt電話n“; cout< cout<<”ttt“< cout<<”ttt“< } }; //********書籍結構體,用于存放書籍基本信息******** class Book { private: char bnum[10]; //書號 char bname[20]; //書名 char bauthor[10]; //作者 char rname[10]; //借閱人 public: int tag; //狀態(tài)標記判斷指標(已借/在架)Book() //構造函數(shù)初始化信息 {strcpy(bnum,”“);strcpy(bname,”“);strcpy(bauthor,”“);strcpy(rname,”“);tag=0;} void set_bnum(char *no) //設置書號 {strcpy(bnum,no);} //把字符串no中的字符復制到字符串bnum中 char *get_bnum() //獲得書號 { return bnum;} //返回值為bnum void set_bname(char *n) //設置書名 {strcpy(bname,n);} char *get_bname() //獲得書名 {return bname;} void set_bauthor(char *a) //設置作者 {strcpy(bauthor,a);} char * get_bauthor() //獲得作者姓名 {return bauthor;} void set_tag(int t) //狀態(tài)設置 {tag=t;} void set_rname(char *rn) //設置借閱人 {strcpy(rname,rn);} char *get_rname() //獲得借閱人姓名 {return rname;} void show_tag() //顯示圖書狀態(tài) { if(tag==1) {cout<<”已借“;} else {cout<<”在架“;} } void copy(Book m) //Book結構體copy函數(shù) { strcpy(bnum,m.get_bnum()); //將獲得書號函數(shù)里的返回值里的字符復制到bnum strcpy(bname,m.get_bname()); strcpy(bauthor,m.get_bauthor()); tag=0; } };//**********書籍管理結構體,實現(xiàn)管理書籍功能********** class BookManage { private: Book books[100]; //定義Book書籍結構體數(shù)組對象,最大容量100本 int length; void saveFile() //保存書籍文件 { ofstream fwrite;//定義輸出文件流ofstream結構體對象fwrite if(!fwrite) {cout<<”文件保存失敗!n“;exit(1); } fwrite.open(BookFile,ios::binary);//使文件流與文件建立關聯(lián),以二進制方式打開 fwrite.write((char *)books,length*sizeof(Book));//計算并返回所占字節(jié)數(shù) fwrite.close(); //關閉磁盤文件 } void readFile() //讀取書籍文件 { ifstream fread;//定義輸入文件流對象fread length=0; if(!fread) {cout<<”文件讀取失敗!“< Book temp; //定義書籍結構體臨時對象temp int i=0; fread.open(BookFile ,ios::binary);//使文件流與文件建立關聯(lián),以二進制方式打開 while((fread.read((char *)&books[i],sizeof(Book)))) i++; //每讀完一次,指針后移一位,直到不能讀出正確結果為止 length=i; fread.close();} public: BookManage() //構造函數(shù),讀取磁盤文件 { length=0; //從頭讀起,計數(shù),最大100 readFile(); //調用讀取文件函數(shù) } ~BookManage() //析構函數(shù),保存磁盤文件 { saveFile(); //調用保存文件函數(shù) } bool add(Book book) //添加圖書資料 { if(length>=100) { cout<<”對不起,儲存已滿,您無法繼續(xù)添加!n“; return-1; } else { books[length].copy(book);//調用書籍結構體copy函數(shù) length++; return(length-1); } } int bnu_search(char *bnu) //按書號查找書籍 { for(int i=0;i if(strcmp(books[i].get_bnum(),bnu)==0)//字符串比較,判斷是否相等 return i; return-1; } int bna_search(char *bna) //按書名查找書籍 { for(int i=0;i if(strcmp(books[i].get_bname(),bna)==0)//判斷 return i; return-1; } int bau_search(char *bau)//按作者查找書籍 { for(int i=0;i if(strcmp(books[i].get_bauthor(),bau)==0)//判斷 return i; return-1; } int rn_search(char *rn)//按借閱人查找書籍 { for(int i=0;i if(strcmp(books[i].get_rname(),rn)==0)//判斷 return i; return-1; } void revise(Book book,int i)//帶參的修改書籍函數(shù) {books[i].copy(book);} //調用Book結構體copy函數(shù) void deleteBook(int index)//帶參的刪除書籍資料函數(shù) { for(int i=index;i books[i].copy(books[i+1]);//用后面的地址將當前的地址覆蓋 length--; } void show_book()//不帶參的顯示書籍函數(shù) { cout<<” ☆☆☆☆☆☆☆☆☆☆☆書★★籍★★信★★息★★列★★表☆☆☆☆☆☆☆☆☆☆☆nn“; cout<<”書號“; cout.width(10); cout<<”書名“; cout.width(10); cout<<”作者“; cout.width(17); cout<<”狀態(tài)“; cout.width(25); cout<<”借閱人n“; for(int i=0;i { cout< cout.width(10); cout< cout.width(10); cout< cout.width(20); books[i].show_tag(); cout.width(20); cout< } } void show_book(int i)//帶參的顯示書籍函數(shù) { cout<<” ☆☆☆☆☆☆☆☆☆☆☆書★★籍★★信★★息★★列★★表☆☆☆☆☆☆☆☆☆☆☆nn“; cout<<”書號“; cout.width(10); cout<<”書名“; cout.width(10); cout<<”作者“; cout.width(17); cout<<”狀態(tài)“; cout.width(25); cout<<”借閱人n“; cout< cout.width(10); cout< cout.width(10); cout< cout.width(20); books[i].show_tag(); cout.width(20); cout< } void br_book()//讀者借、還書功能函數(shù) { ReaderManage reader; int choi;//選擇 int a; //圖書狀態(tài)指標(1已借2在架) char sh[10];//需要還書的書號 char dz[10];//讀者姓名 cin>>sh; int index=bnu_search(sh);//定義一個變量等于查找到的書號 if(index>=0) { if(books[index].tag==1)//已借 a=1; if(books[index].tag==0)//在架 a=2; switch(a) { case 1: cout<<”nt該書目前狀態(tài)為<已借出>n“; cout<<”nt請選擇您需要的服務:1:還書 0:返回n“; cin>>choi; cin.ignore();//清除以回車結束的輸入緩沖區(qū)的內容,消除上一次輸入對下一次輸入的影響 if(choi!=1&&choi!=0) { cout<<”nt操作有誤,請重新選擇(0/1)!n“; return; } else if(choi==1) { cout<<”nt請輸入您的姓名:“; cin>>dz; cin.ignore(); int index1=reader.search(dz); if(index1==-1) { cout<<”nt對不起,系統(tǒng)無該讀者記錄,新用戶請先注冊!“; return; } else { strcpy(dz,”“);//將讀者姓名已空白代替 books[index].set_rname(dz);//借閱人一項變?yōu)榭瞻?/p> books[index].set_tag(0);//圖書狀態(tài)變?yōu)樵诩?/p> cout<<”nt還書成功!“< } }break; case 2: cout<<”nt該書目前狀態(tài)為<在館>n“; cout<<”nt請您需要的服務:1:借書 0:返回n“; cin>>choi; cin.ignore(); if(choi!=1&&choi!=0) { cout<<”nt操作有誤,請重新選擇(0/1)!n“; return; } else if(choi==1) { cout<<”nt請輸入您的姓名:“; cin>>dz; int index1=reader.search(dz); if(index1==-1) { cout<<”nt對不起,系統(tǒng)無改讀者記錄,新用戶請先注冊!“; return; } else { books[index].set_rname(dz); books[index].set_tag(1); cout<<”nt借書成功!“< } }break; } } else { cout<<”nt對不起,系統(tǒng)無該書記錄!“;} } bool in(int i)//判斷圖書是否在架函數(shù) { if(books[i].tag==1) return false; return true; } };//**********菜單結構體,實現(xiàn)界面引導********** class Menu { BookManage bm; //定義書籍管理結構體對象bm ReaderManage rm; //定義讀者管理結構體對象rm Reader r; //定義讀者結構體對象r private: int choice; //菜單序號選擇 int key; //管理員密碼 public: void header() //頁眉 { system(”color fd“); cout<<”t ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 圖 書 借 閱 管 理 系 統(tǒng) ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“;} void log_menu() //主界面登錄菜單 { system(”color fd“); header(); cout<<”nttt< 請 選 擇 登 錄 身 份 >n“; cout<<”ntttt1: 管理員nntttt2: 讀 者nntttt0: 退 出nt您的選擇是: “;choice: cin>>choice; cin.ignore(); //清除以回車結束的輸入緩沖區(qū)的內容,消除上一次輸入對下一次輸入的影響 switch(choice) { case 1: log_admin(); //管理員登錄,需密碼驗證 break; case 2: reader_lr(); //讀者登錄或注冊 break; case 0: exit(1); //退出系統(tǒng) break; default: cout<<”nt您的操作有誤,請重新選擇(0→2): “; goto choice; } } void admin_menu() //管理員管理菜單 { system(”cls“); system(”color f9“); cout<<”t ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 系★統(tǒng)★管★理★界★面 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 1: 添加新的圖書資料 2: 修改已有圖書資料 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 3: 刪除已有圖書資料 4: 查找已有圖書資料 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 5: 查看所有圖書資料 6: 修改讀者用戶信息 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 7: 刪除讀者用戶信息 8: 查看所有讀者信息 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 9: 返回系統(tǒng)登錄界面 0: 退出圖書管理系統(tǒng) ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; cout<<”nt請選擇您需要的服務序號: “;choice: cin>>choice; cin.ignore(); switch(choice) { case 1: //添加圖書 addBook(); bm.~BookManage(); break; case 2: //修改圖書 reviseBook(); bm.~BookManage(); break; case 3: //刪除圖書 delBook(); bm.~BookManage(); break; case 4: //查找圖書 absearch_menu(); break; case 5: //查看全部圖書 bm.show_book(); cout<<”nt顯示完畢!“; system(”pause“); admin_menu(); break; case 6: //修改讀者 reviseReader(); rm.~ReaderManage(); break; case 7: //刪除讀者 delReader(); rm.~ReaderManage(); break; case 8: //查看所有讀者 rm.show_reader(); cout<<”nt顯示完畢!“; system(”pause“); admin_menu(); break; case 9: //返回系統(tǒng)登錄界面 system(”cls“); log_menu(); case 0: //退出系統(tǒng) exit(1); break; default: cout<<”nt您的操作有誤,請重新選擇(0→7): “; goto choice; } } void reader_menu() //讀者服務菜單 { system(”cls“); system(”color f9“); cout<<”t ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 讀★者★服★務★界★面 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 1: 借 閱 圖 書 2: 歸 還 圖 書 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 3: 查 找 圖 書 4: 借 閱 情 況 ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ 5: 返 上 一 級 0: 退 出 系 統(tǒng) ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆ ☆n“; cout<<”t ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; cout<<”nt請選擇您需要的服務序號: “;choice: cin>>choice; cin.ignore(); switch(choice) { case 1: cout<<”nt請輸入您欲借閱的圖書書號: “; bm.br_book(); bm.~BookManage(); break; case 2: cout<<”nt請輸入您欲歸還的圖書書號: “; bm.br_book(); bm.~BookManage(); break; case 3: bsearch_menu(); break; case 4: rn_search(); break; case 5: system(”cls“); reader_lr(); break; case 0: exit(1); break; default: cout<<”nt您的操作有誤,請重新選擇(0→5): goto choice; } } void bsearch_menu() //讀者查找書籍菜單 { system(“color f2”); cout<<“ttt☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n”;cout<<“ttt☆ ☆n”; cout<<“ttt☆ 書★籍★查★找★界★面 ☆n”; cout<<“ttt☆ ☆n”; cout<<“ttt☆ 1: 按 書號查找 ☆n”; cout<<“ttt☆ ☆n”; cout<<“ttt☆ 2: 按 書名查找 ☆n”; cout<<“ttt☆ ☆n”; cout<<“ttt☆ 3: 按 作者查找 ☆n”; cout<<“ttt☆ ☆n”; cout<<“ttt☆ 4: 返 回 上 級 ☆n”; cout<<“ttt☆ ☆n”; cout<<“ttt☆ 0: 退 出 系 統(tǒng) ☆n”;cout<<“ttt☆ ☆n”; cout<<“ttt☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n”; int bsc;//book search choice cout<<“nt請輸入您需要的服務序號: ”; cin>>bsc; cin.ignore(); switch(bsc){ case 1: bnu_search(); break; case 2: bna_search(); “; break; case 3: bau_search(); break; case 4: reader_menu(); break; case 5: exit(1); default: cout<<”nt操作有誤,請重新選擇<0→4>“; break; } } void absearch_menu() //管理員查找書籍菜單 { system(”color f2“); cout<<”ttt☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆ 書★籍★查★找★界★面 ☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆ 1: 按 書號查找 ☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆ 2: 按 書名查找 ☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆ 3: 按 作者查找 ☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆ 4: 返 回 上 級 ☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆ 0: 退 出 系 統(tǒng) ☆n“; cout<<”ttt☆ ☆n“; cout<<”ttt☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n“; int bsc;//book search choice cout<<”nt請輸入您需要的服務序號: “; cin>>bsc; cin.ignore(); switch(bsc) { case 1: bnu_search(); break; case 2: bna_search(); break; case 3: bau_search(); break; case 4: admin_menu(); break; case 5: exit(1); default: cout<<”nt操作有誤,請重新選擇<0→4>“; break; } } void reader_lr()//讀者登錄或注冊菜單 { system(”cls“); header(); system(”color f1“); cout<<”nttt< 讀 者 登 錄 界 面 >n“; cout<<”nttt 1: 登 錄t2: 注 冊nnttt 3: 返 回t0: 退 出nt您的選擇是: “; int rlc;//reader login choice choice: cin>>rlc; cin.ignore(); switch(rlc) { case 1: //已注冊讀者登錄 cout<<”nt請輸入您的姓名: “; char rn[10];//reader name cin>>rn; r.set_rname(rn); if(rm.log_reader(r)) { cout<<”nt登錄成功!“; system(”pause“); reader_menu(); } else cout<<”nt登錄失敗!新用戶請先注冊!“; system(”pause“); reader_lr(); break; case 2: //新用戶注冊 rm.reg_reader(); rm.~ReaderManage(); system(”pause“); reader_lr(); break; case 3: system(”cls“); log_menu(); case 0: exit(1); break; default: cout<<”nt您的操作有誤,請重新選擇(0→2): goto choice; } } void log_admin() //管理員登錄 { cout<<“nt請輸入管理員密碼:”; int key; cin>>key; if(key==14125) { cout<<“nt登錄成功!nnt”; system(“pause”); admin_menu(); //管理員操作菜單 } else {cout<<“nt登錄失敗!原因是密碼錯誤!n”; cout<<“nt請選擇 <1:返回上一級菜單 0:退出系統(tǒng)> cin>>choice; cin.ignore(); switch(choice) ”;“; { case 1: system(”cls“); log_menu(); break; case 0: exit(1); break; default: cout<<”nt操作有誤,請重新選擇(0/1)“; } } } void rn_search() //按書號查找函數(shù) { char rn[10];//reader name cout<<”nt請輸入您的姓名:“; cin>>rn; int index=bm.rn_search(rn); if(index>=0) bm.show_book(index); else cout<<”nt對不起,系統(tǒng)無您的借閱記錄!“; cout<<”nt“; system(”pause“); reader_menu(); } void bnu_search() //按書號查找函數(shù) { char bnu[10]; cout<<”nt請輸入您要查找的書號:“; cin>>bnu; int index=bm.bnu_search(bnu); if(index>=0) bm.show_book(index); else cout<<”nt對不起,系統(tǒng)無該書籍記錄!“; cout<<”nt“; system(”pause“); bsearch_menu(); } void bna_search() //按書名查找函數(shù) { cout<<”nt請輸入您要查找的書名:“< char bna[20]; cin>>bna; int index=bm.bna_search(bna); if(index>=0) bm.show_book(index); else cout<<”nt對不起,系統(tǒng)無該書籍記錄!“; cout<<”nt“; system(”pause“); bsearch_menu(); } void bau_search() //按作者查找函數(shù) { cout<<”nt請輸入您要查找的作者:“< char bau[10]; cin>>bau; int index=bm.bau_search(bau); if(index>=0) bm.show_book(index); else cout<<”nt對不起,系統(tǒng)無該書籍記錄!“; cout<<”nt“; system(”pause“); bsearch_menu(); } void addBook() //新增書籍函數(shù) { char numb[10]; //書號 char nameb[20]; //書名 char authorb[10];//作者 Book book; //書籍結構體對象book int tag1;input1:cout<<”nt請輸入新書書號:“; cin>>numb; int index=bm.bnu_search(numb); if(index==-1) { cout<<”nt系統(tǒng)目前尚無該書記錄,您可以繼續(xù)操作!n“; cout<<”nt請輸入新書書名:“; cin>>nameb; cout<<”nt請輸入新書作者:“; cin>>authorb; tag1=0; book.set_bnum(numb); book.set_bname(nameb); book.set_bauthor(authorb); book.set_tag(tag1); bm.add(book); cout<<”nt恭喜您!新書資料錄入成功!“; } else { cout<<”nt對不起,該書號紀錄已存在!請重新輸入!“; goto input1; } system(”pause“); admin_menu();} void reviseBook() //修改書籍函數(shù) { char numb[10]; char nameb[20]; char authorb[10]; Book book; int index=-1;input2: cout<<”nt請輸入您要修改書籍的書號:“; cin>>numb; index=bm.bnu_search(numb); if(index==-1) { cout<<”nt對不起,您輸入的書號不存在,請重新輸入!“< goto input2; return; } bool sta=bm.in(index);//判斷是否在館 if(sta) { cout<<”nt該書目前處于<在架>狀態(tài),您可以繼續(xù)操作!“; cout<<”nt請輸入更改后的書名:“; cin>>nameb; cout<<”nt請輸入更改后的作者:“; cin>>authorb; book.set_bnum(numb); book.set_bname(nameb); book.set_bauthor(authorb); bm.revise(book,index); cout<<”nt恭喜您!書籍資料修改成功!“;} else cout<<”nt該書已被借出,請于讀者歸還后再進行相關操作!“< system(”pause“); admin_menu(); } void delBook() //刪除書籍函數(shù) { char numb[10]; cout<<”nt請輸入您要刪除書籍的書號:“; cin>>numb; int index=bm.bnu_search(numb); if(index>=0) { cout<<”nt您確定要刪除該書記錄嗎(y/n) “; char y; cin>>y; if(y=='y'||y=='Y') { bm.deleteBook(index); cout<<”nt刪除成功!“; } else cout<<”nt刪除取消!“; } else cout<<”nt對不起,系統(tǒng)沒有該圖書記錄!“; system(”pause“); admin_menu(); } void reviseReader()//修改讀者用戶函數(shù) { char namer[10]; Reader reader;get1: cout<<”nt請輸入你要修改讀者用戶的姓名: “; cin>>namer; int index=rm.search(namer); if(index==-1) { cout<<”nt對不起,系統(tǒng)無改讀者記錄,重新輸入請按1,返回請按0: int a; cin>>a;get2: cin.ignore(); switch(a) { case 1: goto get1; break; case 0: system(“pause”); admin_menu(); break; default: cout<<“nt您的操作有誤,請重新選擇(0/1): ”; goto get2; } return; } “; else { char namer[10]; char classr[10]; char telr[10]; Reader reader; cout<<”nt請輸入更改后的讀者姓名:“; cin>>namer; cout<<”nt請輸入更改后的班級名稱:“; cin>>classr; cout<<”nt請輸入更改后的聯(lián)系電話:“; cin>>telr; reader.set_rname(namer); reader.set_rclass(classr); reader.set_rtel(telr); cout<<”nt是否真的修改該記錄(y/n?):“; char y; cin>>y; if(y=='y'||y=='Y') { rm.revise(reader,index); cout<<”nt修改成功!n“; } else cout<<”nt操作取消!n“; } system(”pause“); admin_menu(); } void delReader() //刪除讀者用戶函數(shù) { cout<<”nt請輸入您要刪除的讀者用戶的姓名:“< char namer[10]; cin>>namer; int index=rm.search(namer); if(index>=0) { cout<<”nt是否真的刪除該記錄(y/n?): “; char y; cin>>y; if(y=='y'||y=='Y') { rm.delete_reader(index); cout<<”nt刪除成功!n“; } else cout<<”nt操作取消!n“; } else cout<<”nt對不起,系統(tǒng)尚無該讀者記錄!“; system(”pause“); admin_menu(); } };//********主函數(shù)******** int main(){ system(”color fd“);Menu menu;//定義菜單結構體對象menu cout<<”ttt 文計091-1 龐麗萍 200990514125 “;cout<<”ttt 當前日期:2011-6-22 星期三“;cout<<”ttt 當前時間:08:00 “;cout<<”nttt ★ 歡 “;cout<<”★ 迎 “; } cout<<”★ 進 “;cout<<”★ 入 ★nn";menu.log_menu();return 0;四.程序正確性驗證 1主頁面 2.管理員登陸頁面 3.圖書添加 五.課程設計過程中出現(xiàn)的問題、原因及解決方法 1.用戶登錄密碼時,不能使用暗碼來保護密碼。(未解決成功)2.每個界面都能退出到主頁面,而不是直接跳出系統(tǒng)。(以解決)3.書名字長太多影響書籍、作者、數(shù)量等之間的一一對應關系。(為解決成功) 六.課程設計的主要收獲 通過這幾天學習設計圖書信息管理系統(tǒng),認識到一切看似簡單的事情只有自己動手做,明白了其中的原理才會更好的把它變成自己的東西。圖書管理系統(tǒng)主要用到了文件的輸入輸出,以及利用簡單的順序查找知識,加深了對數(shù)據(jù)結構的理解與應用。對于這次的課程設計,首先,許多知識不僅僅只是課本上的,需要我們自己去圖書館或網(wǎng)上收集資料。其次,編程過程終遇到各種錯誤,需要我們耐心分析,討論,最后得出正確結果。第三,程序無錯,進行調試,然后進行加工修改優(yōu)化。 七.對今后課程設計的建議 希望老師可以分小組做課程設計,題目可以相應的增大難度。分組可以鍛煉團對合作能力,隊員可以相互討論解決問題,彼此能夠激發(fā)更多的思想,做出更完善的作品,同時也防止了互相拷貝的情況,讓每個同學都積極的參與到設計中,而不是坐享其成。第三篇:C語言課程設計火車票系統(tǒng)源代碼
第四篇:數(shù)據(jù)結構實驗報告(報告+C語言源代碼)
第五篇:圖書管理系統(tǒng) 數(shù)據(jù)結構 課程設計