C語言小專案 通訊錄

2021-09-22 08:00:50 字數 3296 閱讀 2176

通訊錄全部採用c語言實現,用鍊錶實現增加、刪除、修改、查詢等功能,還有命令解析函式(將輸入分解成主命令、姓名、**),聯絡人資訊是儲存在檔案中,每次程式執行和結束時都會讀取檔案中的資訊。

節點裡定義的都是指標,增加新節點時要開闢新的記憶體,刪除節點時要記得及時釋放記憶體,防止記憶體溢位。

標頭檔案:標頭檔案中是巨集定義和函式原型宣告。

#ifndef _main_h_

#define _main_h_

#include

#include

#include

#include

typedef

struct nodenode,

*pnode;

#define add 1

#define delete 2

#define find 3

#define list 4

#define modify 5

#define quit 6

#define help 7

pnode creat_head()

;//建立頭結點

void

display

(const pnode head)

;void

add(

const pnode head,

char

*name,

char

*telephone)

;//增加聯絡人

intanalyse

(char

*buf,

char

*cmd,

char

*name,

char

*telephone)

;//解析指令

void

delete

(const pnode head,

char

*name)

;//刪除聯絡人

pnode find

(pnode head,

char

*name)

;//查詢聯絡人

void

modify

(pnode head,

char

*name,

char

*telephone)

;//修改聯絡人

void

help()

;//顯示操作資訊

intread_list

(pnode head)

;//程式執行時,將儲存在檔案中的聯絡人資訊讀取出來

void

write_list

(pnode head)

;//程式結束時,將鍊錶中的聯絡人寫入檔案中儲存起來

#endif

main.c檔案:主要是程式的執行流程,**簡潔明瞭。

#include

"main.h"

intmain()

;char cmd[20]

=;char name[20]

=;char telephone[20]

=;help()

; pnode head =

creat_head()

;//建立頭結點,不含有效資訊

read_list

(head)

;//將檔案中儲存的資料讀取出來

int i;

while(1

)}return0;

}

list.c檔案:此檔案中是各函式的定義。

#include

"main.h"

pnode creat_head()

void

display

(const pnode head)

}void

add(

const pnode head,

char

*name,

char

*telephone)

intanalyse

(char

*buf,

char

*cmd,

char

*name,

char

*telephone)

else

}else

//判斷此次輸入的是哪條指令,並返回相應的值if(

strcmp

(cmd,

"add")==

0)elseif(

strcmp

(cmd,

"delete")==

0)elseif(

strcmp

(cmd,

"find")==

0)elseif(

strcmp

(cmd,

"list")==

0)elseif(

strcmp

(cmd,

"modify")==

0)elseif(

strcmp

(cmd,

"quit")==

0)elseif(

strcmp

(cmd,

"help")==

0)else

}void

delete

(const pnode head,

char

*name)

}pnode find

(pnode head,

char

*name)if(

strcmp

(pnode-

>name, name)==0

)else

}void

modify

(pnode head,

char

*name,

char

*telephone)

}void

help()

void

write_list

(pnode head)

}int

read_list

(pnode head)

//一次讀取一行,即乙個聯絡人的資訊

while

(fgets

(buf,

sizeof

(buf)

,fp)

!=null

)*p =0;

add(head, buf, p+1)

;}}

C語言通訊錄小專案

include include include include 定義節點 struct node 建立頭節點 int init struct node pheader return 0 bzero pnode,sizeof struct node 清理堆空間 pnode name 20 對於字元陣列...

C語言小專案 通訊錄系統

專案要求 實現乙個通訊錄 通訊錄可以用來儲存1000個人的資訊,每個人的資訊包括 姓名 性別 年齡 住址 提供方法 新增聯絡人資訊 刪除指定聯絡人資訊 查詢指定聯絡人資訊 修改指定聯絡人資訊 顯示所有聯絡人資訊 清空所有聯絡人 以名字排序所有聯絡人 這個系統我們分為三個部分來實現 contact.h...

通訊錄小專案

可以儲存1000人的資訊,個人資訊包括姓名 住址 年齡 性別。提供方法 1 新增聯絡人資訊 2 刪除指定聯絡人資訊 3 查詢指定聯絡人資訊 4 修改指定聯絡人資訊 5 顯示所有聯絡人資訊 6 清空所有聯絡人資訊 7 以名字排序所有聯絡人 1 從檔案讀取和向檔案寫入資訊 fopen fclose fs...