車友信息管理系統(tǒng)詳細編程_第1頁
車友信息管理系統(tǒng)詳細編程_第2頁
車友信息管理系統(tǒng)詳細編程_第3頁
車友信息管理系統(tǒng)詳細編程_第4頁
車友信息管理系統(tǒng)詳細編程_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、先建立四個頭文件 cr.h;prepare.h;file.h;list.h;在D盤建立一個“文件.txt”即可。文件代碼cr.h#ifndef _CR#define _CR#include<stdio.h>struct Carfriendlong VIPnum;char name10;char sex7;char job10;int age2;char brand10;char type7;char colour8;typedef struct Carfriend Type;const int sizeCfr=sizeof(Type); /車友信息所需要的內存空間大小struct

2、nodeType data;struct node *next;const int size=sizeof(struct node); /結點所需要的內存空間的大小#endifPrepare.h#ifndef _PREPARE#define _PREPARE#include"cr.h"#include<stdio.h>#include<string.h>void printHead() /打印表頭函數(shù)printf("%8s%9s%9s%9s%7s%7s%13s%7s%7sn","會員名","昵稱&qu

3、ot;,"性別","職業(yè)","駕齡","年齡","車輛品牌","車款","顏色");void printNode(Type data) /輸出結點所有數(shù)據(jù)域的值int i;printf("%8ld",data.VIPnum);printf("%9s",);printf("%9s",data.sex);printf("%9s",data.job);for(i=0

4、;i<2;i+)printf("%7d",data.agei);printf("%13s",data.brand);printf("%7s",data.type);printf("%7s",data.colour);void readNode(Type *pdata) /根據(jù)提示讀入結點的相關數(shù)據(jù)域的值int i;printf("Input one carfriend's informationn");printf("VIPnum: ");scanf("

5、;%ld",&pdata->VIPnum);printf("name: ");scanf("%s",&pdata->name);printf("sex(Male or Female): ");scanf("%s",&pdata->sex);printf("job: ");scanf("%s",&pdata->job);printf("Input the driving years and the ag

6、e of the carfriendn");for(i=0;i<2;i+)scanf("%d",&pdata->agei);printf("brand of the car: ");scanf("%s",&pdata->brand);printf("type: ");scanf("%s",&pdata->type);printf("colour of the car: ");scanf("%s",&

7、amp;pdata->colour);int endWith(Type data) /輸入結點數(shù)據(jù)時會員名域為0作為結束條件return data.VIPnum=0;int equal(Type data1,Type data2,int condition)if(condition=1)if(strcmp(data1.brand,data2.brand)=0)return 0;elsereturn 1;elseif(condition=2)if(strcmp(data1.sex,data2.sex)=0)return 0;elsereturn 1;elsereturn 1;int lar

8、ger(Type data1,Type data2,int condition)if(condition=1)return data1.age1>data2.age1;return 1;#endiffile.h#include<stdio.h>#include<stdlib.h>#include"list.h"void createFile() /建立初始的數(shù)據(jù)文件Type data;int n,i;FILE *fp;if(fp=fopen("D:文件.txt","w")=NULL) /指定好文件名,以寫

9、入方式打開printf("can not open file cashbox.txt!n"); /若打開失敗,輸出提示信息,退出exit(0);printf("how many records do you want to add?n");scanf("%d",&n);printf("input Carfriends' informationn");for(i=1;i<=n;i+)readNode(&data);fwrite(&data,sizeCfr,1,fp); /將剛才讀

10、入的一條記錄寫入文件printf("n");fclose(fp); /關閉文件struct node *readFile(struct node *head) /將文件中的內容讀出置于單鏈表中Type data;FILE *fp;head=NULL;if(fp=fopen("D:文件.txt","r")=NULL) /以讀的方式打開指定文件printf("can not open file!n"); /如果打開失敗輸出提示信息,退出exit(0);fread(&data,sizeCfr,1,fp); /讀出

11、第一條記錄while(!feof(fp) /文件結束時循環(huán)head=InsertOrder(head,data,1);/從文件中讀出后按年齡順序插入鏈表fread(&data,sizeCfr,1,fp);/再讀出下一條記錄printf("n");fclose(fp); /關閉文件return head; /返回單鏈表的頭指針void saveFile(struct node *head) / 將鏈表中各結點的值依次寫入文件struct node *p=head;FILE *fp;if(fp=fopen("D:文件.txt","w&quo

12、t;)=NULL) /以寫的方式打開指定文件printf("can not open file!n"); /如果打開失敗,輸出提示信息,退出exit(0);if(head=NULL) /如果頭指針為空輸出提示信息,退出printf("nNo Recordn");return ;while(p) /p從head開始,如果鏈表未結束fwrite(&p->next,sizeCfr,1,fp); /則將p所指的結點數(shù)據(jù)值寫入文件中p=p->next; /p指針后移,準備處理下一個結點fclose(fp); /關閉文件list.h#ifndef

13、 _LIST#define _LIST#include"prepare.h" /文件包含,本文件是對結點數(shù)據(jù)域為Type類型的單鏈表進行的處理#include<stdio.h>#include<math.h>struct node *CreateBackward(); /后插法新建鏈表struct node *CreateForward(); /前插法新建鏈表struct node *CreateInsert(); /按序插入法新建鏈表struct node *printList(struct node *head); /鏈表的遍歷struct no

14、de *SearchNode(struct node *head,Type data,int condition); /單鏈表的查找struct node *InsertAfter(struct node *head,Type data); /尾部插入struct node *InsertOrder(struct node *head,Type data,int condition); /有序插入struct node *Delete(struct node *head,Type data); /刪除結點struct node *Recerse(struct node *head); /單鏈表

15、的逆置struct node *printList(struct node *head) /鏈表的遍歷struct node *p;if(head=NULL) /如果鏈表原來為空printf("nNo Recordsn"); /輸出提示信息,然后返回return NULL;for(p=head;p;p=p->next) /p從頭指針開始,每循環(huán)一次向后移1結點位置printNode(p->data); /p非空時執(zhí)行循環(huán)體,調用printNode函數(shù)輸出結點的元素值printf("n");printf("n"); /然后換

16、行return head; /返回頭指針struct node *CreateBackward() /后插法新建鏈表struct node *head,*p,*tail; /tail用來定位于當前鏈表中最后一個結點位置Type data;head=NULL; /鏈表未建時對頭指針和尾指針進行清零處理tail=NULL;printf("Input data end with 0:n");readNode(&data); /調用readNode輸入一個結點的數(shù)據(jù)域的值while(!endWith(data) /endWith(data)函數(shù)值為真時結束鏈表結點的生成p=

17、(struct node *)malloc(size); /利用指針p申請結點的動態(tài)空間p->data=data; /數(shù)據(jù)域賦值p->next=NULL; /新結點的指針域賦值if(head=NULL) /初始鏈為空時,要修改頭文件head=p;else /鏈非空時,將p置于tail所指結點之后tail->next=p;tail=p; /使指向新的鏈尾,便于下次插入readNode(&data); /繼續(xù)讀入下一個結點的數(shù)據(jù)域的值return head; /返回頭指針struct node *CreateForward() /前插法新建鏈表struct node *h

18、ead,*p;Type data;head=NULL; /鏈表未建時對頭指針進行清零處理printf("Input data end with 0:n");readNode(&data); /調用readNode輸入一個結點的數(shù)據(jù)域的值while(!endWith(data) /endWith(data)函數(shù)值為真時結束鏈表結點的生成p=(struct node *)malloc(size); /利用指針p申請結點的動態(tài)空間p->data=data; /數(shù)據(jù)域賦值p->next=head; /新結點的指針域賦值為headhead=p; /修改頭指針rea

19、dNode(&data); /繼續(xù)讀入下一個結點的數(shù)據(jù)域的值return head; /返回頭指針void Revise(struct node *head,char s20,long a)struct node *p;int count=0;if(head=NULL) /如果鏈表原來就為空printf("nNO Recordsn"); /輸出提示信息return ; /返回空指針for(p=head;p;p=p->next)if(p->data .VIPnum=a&&strcmp(p->,s)=0)count=1

20、;readNode(&p->data);break;if(count=0)printf("it is not in the list!n");return;struct node *SearchNode(struct node *head,Type data,int condition) /單鏈表的查找struct node *p=head; /p從head開始int count=0;if(head=NULL) /如果鏈表原來就為空printf("nNO Recordsn"); /輸出提示信息return 0; /返回空指針while(p)

21、if(equal(p->data,data,condition)=0)printHead();printNode(p->data);printf("n");count+;p=p->next;if(count=0)printf("it is not in the list!n");return 0;struct node *InsertAfter(struct node *head,Type data) /尾部插入struct node *p,*p1;p=(struct node*)malloc(size); /利用指針p申請動態(tài)空間p-

22、>data=data; /數(shù)據(jù)域賦值p->next=NULL; /指針域直接賦值為空,因為它是新鏈最后的結點if(head=NULL) /如果鏈表原來為空head=p; /修改頭指針return head; /返回頭指針p1=head; /鏈表原來非空,則指針p1從頭指針開始while(p1->next) /如果指針還沒有指向鏈表的最后一個結點處p1=p1->next; /p1順著鏈向后移動 /循環(huán)停止時,p1指向了鏈表的最后一個結點處p1->next=p; /將新結點連在p1之后return head; /返回頭指針struct node *InsertOrde

23、r(struct node *head,Type data,int condition) /有序插入法struct node *p,*p1,*p2;p2=head;p=(struct node*)malloc(size); /利用指針p申請動態(tài)空間p->data=data; /數(shù)據(jù)域賦值p->next=NULL; /指針域直接賦值為空,以后根據(jù)插入位置再修改if(head=NULL) /原鏈表為空時的插入head=p; /新插入結點成為頭結點return head;/原鏈表不為空時的插入,larger是一個通用函數(shù)while(p2&&larger(p->dat

24、a ,p2->data,condition) /第一參數(shù)大于第二參數(shù),返回真p1=p2; /p1是p2的前趨結點,二者同時后移p2=p2->next;if(head=p2) /如果要在最前面插入,則要修改head指針head=p;else /否則p插在p1的后面p1->next=p;p->next=p2; /p2作為p的后繼結點,即p插在p1和p2之間return head; /返回頭指針struct node *CreateInsert() /按序插入法新建鏈表struct node *head;Type data;head=NULL;printf("Inp

25、ut data end with 0:n");readNode(&data); /調用readNode輸入一個結點的數(shù)據(jù)域的值while(!endWith(data) /endWith(data)函數(shù)值為真時結束鏈表結點的生成head=InsertOrder(head,data,1); /直接InserOrder函數(shù)插入新結點readNode(&data); /繼續(xù)讀入下一個結點的數(shù)據(jù)域的值return head; /返回頭指針struct node *Delete(struct node *head,Type data) /刪除結點struct node *p=he

26、ad,*q=NULL;if(head=NULL) /如果原來鏈表為空,則給出提示信息并返回printf("nNo Recordsn");return head;while(p&&!equal(p->data,data,1) /如果鏈表非空,則從第1個結點開始比較q=p; /如果沒有找到要刪除的結點,則p和q同時向后移p=p->next; /動一個結點位置,q始終是p的前趨if(p) /如果找到需要刪除的結點if(q) /如果刪除的不是第一個結點q->next=p->next; /修改的域,使p的后繼成為q的后繼else /如果刪除的是

27、第一個結點(q保持初值NULL)head=head->next; /修改head指針free(p); /釋放指針p所指向的結點空間else /如果沒有待刪除的點printf("it is not in the list.n"); /則輸出提示信息return head; /返回頭指針struct node *Recerse(struct node *head) /單鏈表的逆置struct node *p=head,*q; /p是當前要處理的結點指針,從原h(huán)ead開始head=NULL; /head置為空值while(p) /用遍歷思想q=p->next; /q記

28、下p的后繼結點,便于下次繼續(xù)處理p->next=head; /修改p的next域,前趨變后趨head=p; /head指向已逆置完成的新的第一個結點處p=q; /p移向原鏈表的下一個結點處,再進行處理return head; /返回頭指針#endif主程序代碼#include<stdio.h>#include<stdlib.h>#include"list.h"#include"file.h"#include"string.h"#include"ctype.h"#include"

29、;windows.h"#include"conio.h"#include"malloc.h"#include"cr.h"void menu() /一級菜單函數(shù)printf("*1. 顯示車友信息 *n");printf("*2. 車友信息管理 *n");printf("*3. 車友人數(shù)統(tǒng)計 *n");printf("*4. 根據(jù)條件查詢 *n");printf("*0. 退出 *n");printf("n"

30、);void menuBase() /車友信息管理的二級菜單printf("*1. 增加車友信息 *n");printf("*2. 刪除車友信息 *n");printf("*3. 修改車友信息 *n");printf("*0. 返回上級菜單 *n");printf("n");void menuCount() /車友人數(shù)統(tǒng)計的二級菜單printf("*1. 車友總人數(shù) *n");printf("*2. 車輛品牌的車友人數(shù) *n");printf("

31、*0. 返回上級菜單 *n");printf("n");void menuSearch() /根據(jù)條件查詢的二級菜單printf("*1. 根據(jù)車款查詢車友信息 *n");printf("*2. 根據(jù)性別查詢車友信息 *n");printf("*0. 返回上級菜單 *n");printf("n");struct node * baseManage(struct node *head) /車友信息管理模塊的實現(xiàn)int choice;Type data;long s1;char s220;

32、do /用dowhile語句可以多次選擇二級菜單menuBase(); /調用二級菜單顯示函數(shù)printf("choice one operation you want to do:n");scanf("%d",&choice); /輸入選擇項switch(choice)case 1: readNode(&data); /首先讀入待插入的車友信息,再插入head=InsertOrder(head,data,1);break;case 2: printf("Input the VIP number deletedn");

33、/讀入會員名,根據(jù)會員名進行刪除(會員名就是一個號,相當于學號)scanf("%d",&data.VIPnum);head=Delete(head,data);break;case 3: printf("P;ease input thr VIP number and his name to be xiugai.n");printf("VIPnum: ");scanf("%ld",&s1);printf("name: ");scanf("%s",&s2)

34、;Revise(head,s2,s1);break;case 0: break;while(choice); /還是回到二級菜單允許再次選擇二級功能return head;int Count(struct node *head) /求總人數(shù)struct node *p;int count=0; /count統(tǒng)計總人數(shù)for(p=head;p;p=p->next)count+;return count;int brandCount(struct node *head,char *s)int count=0;struct node *p;for(p=head;p;p=p->next)i

35、f(strcmp(p->data.brand,s)=0)count+;return count;struct node *countManage(struct node *head) /車友人數(shù)統(tǒng)計模塊的實現(xiàn)int choice,all,brandcount;char s20;domenuCount();printf("choice one operation you want to do:n");scanf("%d",&choice);switch(choice)case 1: printf("the number of the

36、 people is:n");all=Count(head);printf("all=%d",all);printf("n");break;case 2: printf("Please input the brand will be searched:n");scanf("%s",s);brandcount=brandCount(head,s);printf("brandcount=%d",brandcount);printf("n");printf("n

37、");break;case 0: saveFile(head);break;while(choice);return head;struct node *searchManage(struct node *head) /根據(jù)條件查詢模塊的實現(xiàn)int choice;Type data;do /用dowhile語句可以多次選擇二級菜單menuSearch();printf("choice one operation you want to do:n");scanf("%d",&choice); /輸入選擇項switch(choice)cas

38、e 1: printf("Input a VIP's car's brand will be searched:n");scanf("%s",&data.brand); /輸入車款,將根據(jù)車款查詢SearchNode(head,data,choice);printf("n");break;case 2: printf("Input a VIP's sex will be searched:n");scanf("%s",&data.sex); /輸入性別,將根據(jù)性別查詢SearchNode(head,data,choice);printf("n");break;case 0:break;while(choice); /返回二級菜單允許再次選擇二級功能return head; /返回頭指針struct node* runMain(struct node *head,int choice) /該函數(shù)受main調用,根據(jù)選擇項分別調用 /函數(shù)實現(xiàn)各模塊的功能switch(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論