生活随笔
收集整理的這篇文章主要介紹了
制作一个简单的通讯录
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
制作一個簡單的通訊錄:
一.總體方針
1.test.c
測試
2.contact.c
實現(xiàn)函數(shù)的功能
3.contact.h
聲明函數(shù)
二.通訊錄的主要內(nèi)容簡介
1.存放1000個好友信息
名字
電話
性別
住址
年齡
2.增加好友信息
3.刪除指定名字的好友信息
4.查找好友信息
5.修改好友信息
6.打印好友信息
7.排序
三.制作
1.test.c
測試
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{printf("************************************\n");printf("*****1.add 2.del *****\n");printf("*****3.search 4.modify *****\n");printf("*****5.show 6.sort *****\n");printf("*****0.exit *****\n");printf("************************************\n");}
int main()
{int input
= 0;struct Contact con
;InitContact(&con
);do{menu();printf("請選擇:>");scanf("%d",&input
);switch (input
){case ADD
:AddContact(&con
);break;case DEL
:DelContact(&con
);break;case SEARCH
:SearchContct(&con
);break;case MODIFY
:ModfyContact(&con
);break;case SHOW
:ShowContact(&con
);break;break;case EXIT
:printf("退出通訊錄\n");break;default:printf("輸入錯誤\n");break;}}while(input
);return 0;
}
2.contact.c
實現(xiàn)函數(shù)的功能
#define _CRT_SECURE_NO_WARNINGS 1#include"contact.h"void InitContact(struct Contact
* ps
)
{memset(ps
->data
, 0, sizeof(ps
->data
));ps
->size
= 0;
}
void AddContact(struct Contact
* ps
)
{if (ps
->size
== MAX
){printf("通訊錄已滿,無法增加\n");}else{printf("請輸入名字:>");scanf("%s", ps
->data
[ps
->size
].name
);printf("請輸入年齡:>");scanf("%d", &(ps
->data
[ps
->size
].age
));printf("請輸入性別:>");scanf("%s", ps
->data
[ps
->size
].sex
);printf("請輸入電話:>");scanf("%s", ps
->data
[ps
->size
].tele
);printf("請輸入地址:>");scanf("%s", ps
->data
[ps
->size
].addr
);ps
->size
++;printf("添加成功\n");}
}void ShowContact(const struct Contact
* ps
)
{if (ps
->size
== 0){printf("通訊錄為空\n");}else{int i
= 0;printf("%-20s\t%-4s\t%-5\t%-12s\t%-20s\n", "名字", "年齡", "性別", "電話", "地址");for (i
= 0; i
< ps
->size
; i
++){printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",ps
->data
[i
].name
,ps
->data
[i
].age
,ps
->data
[i
].sex
,ps
->data
[i
].tele
,ps
->data
[i
].addr
);}}
}static int FindByName(const struct Contact
* ps
, char name
[MAX_NAME
])
{int i
= 0;for (i
= 0; i
< ps
->size
; i
++){if (0 == strcmp(ps
->data
[i
].name
, name
))return i
;return -1;}
}void DelContact(struct Contact
* ps
)
{char name
[MAX_NAME
];printf("請輸入刪除人的名字:>");scanf("%s", name
);int pos
= FindByName(ps
,name
);if (pos
== -1){printf("需要刪除的人不存在\n");}else{int j
= 0;for (j
= pos
; j
< ps
->size
-1; j
++){ps
->data
[j
] = ps
->data
[j
+ 1];}ps
->size
--;printf("刪除成功\n");}}void SearchContct(const struct Contact
* ps
)
{char name
[MAX_NAME
];printf("請輸入需要查找讓你的名字:>");scanf("%s", name
);int pos
= FindByName(ps
, name
);if (pos
== -1){printf("要查找的人不存在\n");}else{printf("%-20s\t%-4s\t%-5\t%-12s\t%-20s\n", "名字", "年齡", "性別", "電話", "地址");printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",ps
->data
[pos
].name
,ps
->data
[pos
].age
,ps
->data
[pos
].sex
,ps
->data
[pos
].tele
,ps
->data
[pos
].addr
);}
} void ModfyContact(struct Contact
* ps
)
{char name
[MAX_NAME
];printf("請輸入需要修改人的名字");scanf("%s", name
);int pos
= FindByName(ps
, name
);if (pos
== -1){printf("需要修改的人信息不存在\n");}else{printf("請輸入名字:>");scanf("%s", ps
->data
[pos
].name
);printf("請輸入年齡:>");scanf("%d", &(ps
->data
[pos
].age
));printf("請輸入性別:>");scanf("%s", ps
->data
[pos
].sex
);printf("請輸入電話:>");scanf("%s", ps
->data
[pos
].tele
);printf("請輸入地址:>");scanf("%s", ps
->data
[pos
].addr
);printf("修改完成\n");}
}
3.contact.h
聲明函數(shù)
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30enum Option
{EXIT
,ADD
,DEL
,SEARCH
,MODIFY
,SHOW
};struct PeoInfo
{char name
[MAX_NAME
];int age
;char sex
[MAX_SEX
];char tele
[MAX_TELE
];char addr
[MAX_ADDR
];
};
struct Contact
{struct PeoInfo data
[MAX
];int size
;
};
void InitContact(struct Contact
* ps
);
void AddContact(struct Contact
* ps
);
void ShowContact(const struct Contact
* ps
);
void DelContact(struct Contact
* ps
);
void SearchContct(const struct Contact
* ps
);
void ModfyContact(struct Contact
* ps
);
注:個人結(jié)合老師上課內(nèi)容對結(jié)構(gòu)體的認(rèn)識,本人萌新可能有許多不正到之處,望周正。
總結(jié)
以上是生活随笔為你收集整理的制作一个简单的通讯录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。