




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、成都信息工程學院計算機系課程實驗報告實驗課程:C語言程序設計2實驗項目:職工管理系統指導教師: 李莉麗學生姓名:桂柯 學生學號:2010051102班 級:計科3班實驗地點: 5201實驗時間:2011 年 5月 11日 點 點實驗成績:評閱老師:李莉麗(說明:實驗報告必須包含下面的每項內容,根據實驗情況認真填寫,封面必須打印或復印(A4紙),書寫上機實驗報告內容的紙張也用A4紙,最后從側面裝訂)一【上機實驗目的】通過親自設計程序,可以令我們熟悉c語言操作,更熟練的掌握c語句。初步體會編程的過程,為將來的程序深入學習打下基礎和培養興趣。二【實驗環境】TC2.0三【上機實驗內容】職工信息管理系統
2、要求:職工信息包括職工號,姓名,性別,年齡,學歷,工資,住址,電話等(職工號不相等)。試設計一職工信息管理系統,使之能夠提供下列功能: (1)系統以菜單方式工作 (2)職工信息錄入功能(職工信息用文件保存) (3)職工信息瀏覽功能 (4)職工信息查詢功能,查詢方式: 1)按學歷查詢 2)按職工號查詢 (5) 職工信息刪除,修改功能(可選項)。思路:(1)錄入并向文件里保存數據的實現思路:C語言并沒有提供由鍵盤輸入數據直接錄入文件的功能,只有內存變量向文件寫入數據的功能,而由鍵盤輸入數據可以到內存變量,因此實現此部分功能時,應當由鍵盤將數據放入變量,再由變量寫入文件。這里錄入要求不采用書上例子,
3、它是用結構體數組,我們要求只用一個結構體實現。先將一個人的信息放入結構體,將這個結構體數據寫入文件后,再將下一個人的信息繼續放在這個結構體中,再將這個結構體寫入文件。這樣節省內存空間。計算并修改文件里數據的實現思路:這部分的功能的實現應當先將文件的數據讀到變量當中,在變量當中完成計算,再將數據寫入文件。如果只是修改文件的數據,并不計算,則可以直接定位到文件中相應的位置,寫入數據,則把原來的數據覆蓋以完成修改。(2)向文件里追加數據的實現思路:文件本身提供了這項功能,只要以 “a”的方式打開就行。然后向文件寫入的數據,直接放在文件末尾。(3)查找文件里是否有某項數據的實現思路:C語言并沒有提供判
4、斷文件內容的功能,必須將文件內容讀到變量里再進行判斷。實際的查找可能是在大量的數據里的查找,高效的查找是折半查找(下學期的數據結構專門講這一內容),折半的前提是排序,因此需先對排序后的文件讀出,以折半方式查找。(這要要求,是希望鞏固折半查找與排序兩個重要算法,至于它的時空效率是否高,可以學完數據結構知識后自己再判斷)。(4)根據要求顯示文件里的某些數據或全部數據的實現思路:C語言沒有提供將文件內容顯示的功能,所以需要將文件內容讀到變量里,再顯示變量。(5)在文件里插入或刪除某項數據的實現思路:C語言同樣沒有直接提供該項功能,因此必須借由內存變量完成。由以前的知識知道,在大量的數據里刪除一個數,
5、用數組表示不合適,因為涉及到大量的數據的移動,用鏈表是合適的,效率高(關于這一點,在數據結構這門課有詳細的講解)。因此完成這部分操作要求用鏈表實現,先將文件里的數據讀出組織成鏈表,在鏈表上完成插入與刪除后,再將鏈表中的數據寫入文件。(6)按某個數據項進行排序生成排序文件的實現思路:排序是在數組里實現。因此先要將文件里的數據讀到數組里,將數組排完序后,再將數據寫入文件(一般寫入一個新文件)。四【上機調試程序流程圖】1.顯示主菜單(以如下程序作為介紹)2. 添加職工信息3. 瀏覽職工信息。4. 查詢職工信息。4(1)根據標號查詢職工信息。(2) 根據姓名查詢職工信息。(3) 根據年齡查詢職工信息。
6、5. 修改職工信息。瀏覽修改后的職工信息。6. 刪除職工信息。瀏覽刪除后的職工信息。7. 用鏈表添加職工信息。瀏覽添加后的職工信息。五【上機調試中出現的錯誤信息、錯誤原因及解決辦法】1. 光條菜單的錯誤:剛開始只能用鍵盤上的英文字母控制光條上下移動,想用上下箭頭的ASCII代碼,結果錯誤不能上下移動解決方法:使用鍵盤掃描碼。2、瀏覽函數 scan()在調試和鏈接的時候都沒有出現錯誤提示,但在運行的時候出現了問題。寫入指定位置的文件,打開后總會有亂碼。與c語言課本上的例題對照后發現,我寫的fopen("wenjian","ab")沒有指定文件的存儲類型。解
7、決方法:在文件名wenjian后面加上.txt后即可。3.瀏覽函數scan()遇到的問題雖然不大,但解決起來很麻煩。理想的運行結果是美觀整齊。即下面的職工信息分別與第一個printf輸出的中文項目提示對齊。解決方法:不斷修改空格個數,不斷運行察看效果。4.程序運行后菜單界面不消失解決方法:使用清屏函數,是每次運行后界面還原。5.功能函數運行完后會跳出界面,直接退出。解決方法:在每個功能函數的后面加如返回值。六【上機調試后的源程序及還存在的問題】源程序:#include<stdio.h>#include<conio.h>#define ESC 0x1b/*鍵盤掃描碼*/#
8、define ENTER 0x0d#define UP 0x48#define DOWN 0x50#define LEFT 0x4b#define RIGHT 0x4dtypedef struct /*定義存放職工信息的類型*/int No;int age;char name20;WORKERBASIC;typedef struct workerWORKERBASIC workerinfo;struct worker *next;WORKER;void cleanscreen()/*清屏*/int i,j;gotoxy(1,1);textbackground(RED);for(i=1;i<
9、;=25;i+)for(j=1;j<=80;j+)cprintf(" ");clrscr();void Initial()/*定義初始化程序的函數*/int i,j;char list720="1. Add",/*菜單名*/"2. Scan","3. Edit","4. Modify","5. Delete","6. Into","7. Exit"cleanscreen();textbackground(YELLOW);for(i
10、=1;i<=14;i+)/*畫窗口*/for(j=1;j<=80;j+)if (i=1|i=14|j<=2|(j>=79)cprintf(" ");else if (i>=3&&i<=12&&j>=5&&j<=76)cprintf(" ");elseprintf(" ");gotoxy(32,4);textcolor(BLACK);cprintf("W E L C O M E !");gotoxy(31,14);cpri
11、ntf("Worker System - BY GK");for(i=0;i<7;i+)/*顯示菜單*/gotoxy(34,i+6);cprintf("%s",listi);void PrintPause()/*暫停*/printf("Press any key to go on . . .");getch();void Guangtiao(int flag)/*定義畫光條的函數*/char list720="1. Add","2. Scan","3. Edit",&q
12、uot;4. Modify","5. Delete","6. Into","7. Exit"gotoxy(34,flag+5);textcolor(WHITE);textbackground(BLACK);cprintf("%s",listflag-1);textcolor(LIGHTGRAY);int Add()/*添加信息*/WORKERBASIC new;FILE *fp=NULL;char ch='y'while(ch='y')cleanscreen();print
13、f("Please input the worker's No. ");scanf("%d",&new.No);getchar();printf("Please input the worker's age: ");scanf("%d",&new.age);getchar();printf("Please input the worker's name: ");scanf("%s",);getchar();if(fp=f
14、open("wenjian.txt","ab")=NULL)fp=fopen("wenjian.txt","wb");if(fwrite(&new,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();fclose(fp);printf("whether go on(y or n):");scanf("%c",&ch);return 0;int Scan()/*查看
15、信息*/WORKERBASIC load;FILE *fp=NULL;int n=0;cleanscreen();if(fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;printf("-n");printf("No.tworker'sagetworker'namen");while(!feof(fp)if (fread(&load,sizeof
16、(WORKERBASIC),1,fp)=1)n+;elsebreak;printf("%dt%dtt%sn",load.No,load.age,);printf("-n");printf("total:%dn",n);fclose(fp);PrintPause();return 0;void exchange(WORKERBASIC *max,WORKERBASIC *min)/*調換兩個職工在數組中的位置*/WORKERBASIC t;strcpy(,max->name);t.No=max->
17、;No;strcpy(max->name,min->name);max->No=min->No;strcpy(min->name,);min->No=t.No;int EditByNo(WORKERBASIC *F1,int n)/*按編號查找*/int i,j,k,num,high,low,mid,flag=0;for(i=0;i<n-1;i+)/*排序*/k=i;for(j=i+1;j<n;j+)if (F1+k*sizeof(WORKERBASIC)->No) > (F1+j*sizeof(WORKERBASIC)-
18、>No)k=j;if (k!=i)exchange(F1+i*sizeof(WORKERBASIC),(F1+k*sizeof(WORKERBASIC);/*此顯示信息只為說明職工已按編號排序,該部分可刪除*/printf("-n");printf("No.tworker'agettworker'namen");for(i=0;i<n;i+)printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->
19、age,(F1+i*sizeof(WORKERBASIC)->name);printf("-n");printf("nPlease input a number you want to search: ");scanf("%d",&num);getchar();low=0;/*折半法*/high=n-1;while(low<=high)&&(flag=0)mid=(low+high)/2;if (num>(F1+mid*sizeof(WORKERBASIC)->No)low=mid+1;
20、else if (num<(F1+mid*sizeof(WORKERBASIC)->No)high=mid-1;else if (num=(F1+mid*sizeof(WORKERBASIC)->No)flag=1;if (flag=0)printf("can not find the worker!n");elseprintf("-n");printf("No.tworker'agettworker'namen");printf("%dt%dttt%sn",(F1+mid*siz
21、eof(WORKERBASIC)->No,(F1+mid*sizeof(WORKERBASIC)->age,(F1+mid*sizeof(WORKERBASIC)->name);printf("-n");PrintPause();return 0;int EditByName(WORKERBASIC *F1,int n)/*按姓名查找*/char str20;int i,j,k,flag=0,low,high,mid;for(i=0;i<n-1;i+)/*排序*/k=i;for(j=i+1;j<n;j+)if (strcmp( (F1+k*si
22、zeof(WORKERBASIC)->name, (F1+j*sizeof(WORKERBASIC)->name )>0)k=j;if (k!=i)exchange(F1+i*sizeof(WORKERBASIC),(F1+k*sizeof(WORKERBASIC);/*此顯示信息只為說明職工已按名字排序,該部分可刪除*/printf("-n");printf("No.tworker'agettworker'namen");for(i=0;i<n;i+)printf("%dt%dttt%sn",
23、(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->age,(F1+i*sizeof(WORKERBASIC)->name);printf("-n");printf("nPlease input a worker you want to exactly search: ");scanf("%s",str);getchar();low=0;/*折半法*/high=n-1;while(low<=high)&&(flag=0)mid=(lo
24、w+high)/2;if (strcmp( str, (F1+mid*sizeof(WORKERBASIC)->name )>0)low=mid+1;else if (strcmp( str, (F1+mid*sizeof(WORKERBASIC)->name )<0)high=mid-1;else if (strcmp( str, (F1+mid*sizeof(WORKERBASIC)->name )=0)flag=1;if (flag=0)printf("can not find the worker!n");elseprintf(&quo
25、t;-n");printf("No.tworker'agettworker'namen");printf("%dt%dttt%sn",(F1+mid*sizeof(WORKERBASIC)->No,(F1+mid*sizeof(WORKERBASIC)->age,(F1+mid*sizeof(WORKERBASIC)->name);printf("-n");PrintPause();return 0;int EditByage(WORKERBASIC *F1,int n)/*按年齡查找*/i
26、nt a,flag=0,i;printf("-n");printf("No.tworker'agettworker'namen");for(i=0;i<n;i+)printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBASIC)->age,(F1+i*sizeof(WORKERBASIC)->name);printf("-n");printf("nPlease input work
27、er'age: ");scanf("%d",&a);getchar();for(i=0;i<n;i+)if (a=(F1+i*sizeof(WORKERBASIC)->age)if(flag=0)printf("-n");printf("No.tworker'agettworker'namen");flag=1;printf("%dt%dttt%sn",(F1+i*sizeof(WORKERBASIC)->No,(F1+i*sizeof(WORKERBAS
28、IC)->age,(F1+i*sizeof(WORKERBASIC)->name);if (flag=0)printf("can not find the worker!n");elseprintf("-n");PrintPause();return 0;int Edit()/*查找*/WORKERBASIC load,*F1;int n=0,key=0,i;FILE *fp=NULL;cleanscreen();if (fp=fopen("wenjian.txt","rb")=NULL)printf(
29、"can not find wenjian!n");PrintPause();return 0;while(!feof(fp)/*統計職工的數量*/if (fread(&load,sizeof(WORKERBASIC),1,fp)=1)n+;fclose(fp);if (n=0)printf("No information of worker!n");PrintPause();return 0;F1=(WORKERBASIC *)malloc(n*sizeof(WORKERBASIC);/*按職工的人數申請內存空間*/fp=fopen("
30、;wenjian.txt","rb");for(i=0;i<n;i+)if (fread(F1+i*sizeof(WORKERBASIC),sizeof(WORKERBASIC),1,fp)!=1)printf("read failed!n");PrintPause();return 0;fclose(fp);while(key!=ESC)clrscr();printf("Please select search method:n");printf("-n");printf("1t=By
31、No.n");printf("2t=By Name.n");printf("3t=By age.n");printf("Esct=Exitn");printf("-n");printf("Press 1 2 3 or Esc?n");key=getch();switch(key)case '1':key=EditByNo(F1,n);break;case '2':key=EditByName(F1,n);break;case '3':ke
32、y=EditByage(F1,n);break;free(F1);return 0;int Modify()/*修改*/WORKER *head=NULL,*t=NULL,*load=NULL;FILE *fp=NULL;char str20;int num;cleanscreen();load=(WORKER *)malloc(sizeof(WORKER);if(fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return
33、 0;head=t=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)printf("load failed!n");PrintPause();return 0;while(!feof(fp)load=(WORKER *)malloc(sizeof(WORKER);t->next=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)break;t=load;t->next=NULL;/*創建鏈表完成*/fclose(fp);t=head;Scan();printf(&q
34、uot;nWhich worker do you want to modify?n");printf("Please input the No. age. or name: ");scanf("%s",str);getchar();num=atoi(str);/*把字符串轉換為長整型數*/if (strcmp(t->,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nPlease input the new No
35、. ");scanf("%d",&t->workerinfo.No);getchar();printf("nPlease input the new age. ");scanf("%d",&t->workerinfo.age);getchar();printf("nPlease input the new name: ");scanf("%s",t->);getchar();printf("Modify Succ
36、ess!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();break;free(head);fclose(fp);PrintPause();return 0;/*存入文件(目
37、標在首地址)*/while(t->next!=NULL)t=t->next;if (strcmp(t->,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nPlease input the new No. ");scanf("%d",&t->workerinfo.No);getchar();printf("nPlease input the new age. ");scanf("
38、;%d",&t->workerinfo.age);getchar();printf("nPlease input the new name: ");scanf("%s",t->);getchar();printf("Modify Success!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t
39、->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();break;/*存入文件(當不在首地址的時候)*/free(head);fclose(fp);PrintPause();return 0;printf("Input Wrong!n");PrintPause();return 0;int Delete()/*刪除*/WORKER *head=NULL,*t=NULL,*load=NULL,*b=N
40、ULL;FILE *fp=NULL;int num;char str20,ch;cleanscreen();load=(WORKER *)malloc(sizeof(WORKER);if(fp=fopen("wenjian.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;head=t=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)printf("load failed!n"
41、;);PrintPause();return 0;while(!feof(fp)load=(WORKER *)malloc(sizeof(WORKER);t->next=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)break;t=load;t->next=NULL;fclose(fp);t=head;Scan();printf("nWhich worker do you want to delete?n");printf("Please input the No. age or name: &quo
42、t;);scanf("%s",str);getchar();num=atoi(str);if (strcmp(t->,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nDo you really want to delete %s?(Press 'y' to sure)n",t->);if (ch=getch()!='y')return 0;head=t->n
43、ext;free(t);printf("Delete Success!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();break;free(head);fclos
44、e(fp);PrintPause();return 0;while(t->next!=NULL)t=b;t=t->next;if (strcmp(t->,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nDo you really want to delete %s?(Press 'y' to sure)n",t->);if (ch=getch()!='y')return
45、0;b->next=t->next;free(t);printf("Delete Success!n");fp=fopen("wenjian.txt","wb");t=head;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();br
46、eak;free(head);fclose(fp);PrintPause();return 0;b=b->next;printf("Input Wrong!n");PrintPause();return 0;int Into() /*鏈表插入*/WORKER *head=NULL,*t=NULL,*load=NULL,*b=NULL,*a=NULL;FILE *fp=NULL;int num;char str20,ch;cleanscreen();load=(WORKER *)malloc(sizeof(WORKER);if(fp=fopen("wenjia
47、n.txt","rb")=NULL)printf("can not find wenjian!n");PrintPause();return 0;head=t=load;if (fread(load,sizeof(WORKERBASIC),1,fp)!=1)printf("load failed!n");PrintPause();return 0;while(!feof(fp)load=(WORKER *)malloc(sizeof(WORKER);t->next=load;if (fread(load,sizeof(
48、WORKERBASIC),1,fp)!=1)break;t=load;t->next=NULL;fclose(fp);t=head;Scan();a=(WORKER *)malloc(sizeof(WORKER);printf("ninput new worker's No.");scanf("%d",&a->workerinfo.No);getchar();printf("nPlease input the new age. ");scanf("%d",&a->worke
49、rinfo.age);getchar();printf("input new worker's name:");scanf("%s",a->);getchar();printf("nwhere do you want to add?n");printf("Please input the No. age. or name: ");scanf("%s",str);getchar();num=atoi(str);if (strcmp(t->worke
50、,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nDo you really want to add before %s?(Press 'y' to sure)n",t->);if (ch=getch()!='y')return 0;a->next=t;printf("Into Success!n");fp=fopen("wenjian.txt",&qu
51、ot;wb");head=t=a;if (head!=NULL)fwrite(t,sizeof(WORKERBASIC),1,fp);while(t->next!=NULL)t=t->next;if(fwrite(t,sizeof(WORKERBASIC),1,fp)!=1)printf("add failed!n");PrintPause();break;free(head);fclose(fp);PrintPause();return 0;while(t->next!=NULL)t=t->next;if (strcmp(t->,str)=0)|(num=t->workerinfo.No)|(num=t->workerinfo.age)printf("nDo you really want to add after %s?(Press 'y
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025四川資源集團招聘134人查看職位筆試參考題庫附帶答案詳解
- 2025新入職員工安全培訓考試試題答案匯編
- 2025年新入員工安全培訓考試試題(突破訓練)
- 2025版權授權合同范本-網站作品授權協議模板
- 2025玉米購銷合同全書
- 2025深圳市建筑設計合同
- 專利代理委托協議
- 2025年民間融資的居間服務合同范本
- 2025年城市公寓租賃合同
- 2025年家居棉品合作協議書
- 2025-2030中國寵物行業市場發展分析及發展趨勢與投資前景預測報告
- AGC-AVC培訓課件教學課件
- 山洪災害防御知識課件
- 決勝新高考·四川名優校聯盟2025屆高三4月聯考英語+答案
- 境外道路貨物運輸應急預案
- 中考英語讀寫綜合練習
- 混凝土供應保證方案 2
- 慢性阻塞性肺疾病入院記錄模板-病歷書寫
- 新疆維吾爾自治區和田地區各縣區鄉鎮行政村村莊村名居民村民委員會明細及行政區劃代碼
- 軟件測試技術課程教學大綱
- 液壓與氣壓傳動完整版課件
評論
0/150
提交評論