通訊錄
實現一個通訊錄
通訊錄可以用來存儲1000個人的信息,每個人的信息包括:姓名、性別、年齡、電話、住址
提供方法
添加聯繫人信息
刪除指定聯繫人信息
查找指定聯繫人信息
修改指定聯繫人信息
顯示所有聯繫人信息
清空所有聯繫人
以名字排序所有聯繫人
定義結構體
typedef struct contactNode{ //定義聯繫人結構體
char name[15];
char sex[15];
int age;
char number[20];
char address[100];
};
contactNode nodes[1000]; //全局定義1000個聯繫人的數組用於存放聯繫人
int countIndex = 0; //用於控制數組下標
交互界面
void menu(){
while (1){
printf("\n==========通訊錄==========\n||");
printf("0.退出系統 |\n||");
printf("1.添加聯繫人信息 |\n||");
printf("2.刪除指定聯繫人信息 |\n||");
printf("3.查找指定聯繫人信息 |\n||");
printf("4.修改指定聯繫人信息 |\n||");
printf("5.顯示所有聯繫人信息 |\n||");
printf("6.清空所有聯繫人 |\n||");
printf("7.以名字排序所有聯繫人 |\n");
printf("==========================\n");
int choice;
printf("\n請輸入您的選擇:");
scanf("%d", &choice);
switch (choice)
{
default:
printf("輸入不合法!\n");
break;
case 0:
printf("byebye!!\n");
return;
case 1:
addContactInformation();
break;
case 2:
deleteSpecifiedContactInformation();
break;
case 3:
findSpecifiedContactInformation();
break;
case 4:
modifySpecifiedContactInformation();
break;
case 5:
displayAllContactInformation();
break;
case 6:
emptyAllContacts();
break;
case 7:
sortContactsByName();
break;
}
}
}
1.添加聯繫人信息
void addContactInformation(){
printf("請輸入你要添加的聯繫人信息\n");
printf("姓名:");
scanf("%s", &nodes[countIndex].name);
printf("性別:");
scanf("%s", &nodes[countIndex].sex);
printf("年齡:");
scanf("%d", &nodes[countIndex].age);
printf("電話號碼:");
scanf("%s", &nodes[countIndex].number);
printf("地址:");
scanf("%s", &nodes[countIndex].address);
countIndex++;
printf("聯繫人添加成功!\n");
}
2.刪除指定聯繫人信息
void deleteSpecifiedContactInformation(){
printf("請輸入你要刪除的聯繫人信息\n");
char deleteName[15] ; //用於存儲需要刪除的聯繫人姓名
char deleteNumber[20]; //用於存儲需要刪除的聯繫人電話號碼
printf("姓名:");
scanf("%s", &deleteName);
printf("電話號碼:");
scanf("%s",&deleteNumber);
int tempIndex = -1; //用於存儲需要刪除的聯繫人在數組中的的下標
int i;
for (i = 0; i < countIndex; i++) //尋找需要刪除的聯繫人
{
if (strcmp(deleteName, nodes[i].name) == 0 && strcmp(deleteNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("無此聯繫人!\n");
}
else{
for (int j = tempIndex; j < countIndex - 1; j++){ //將該聯繫人之後所有聯繫人前移一個位置,將需要刪除的聯繫人覆蓋
strcpy(nodes[j].name, nodes[j + 1].name);
strcpy(nodes[j].sex, nodes[j + 1].sex);
nodes[j].age = nodes[j + 1].age;
strcpy(nodes[j].number, nodes[j + 1].number);
strcpy(nodes[j].address, nodes[j + 1].address);
}
countIndex--; //聯繫人總數減1
printf("刪除成功!");
}
}
3.查找指定聯繫人信息
void findSpecifiedContactInformation(){
printf("請輸入你要查詢的聯繫人信息\n");
char findName[15]; //用於存儲需要查詢的聯繫人姓名
char findNumber[20]; //用於存儲需要查詢的聯繫人電話號碼
printf("姓名:");
scanf("%s", &findName);
printf("電話號碼:");
scanf("%s", &findNumber);
int tempIndex = -1; //用於存儲需要刪除的聯繫人在數組中的的下標
int i;
for (i = 0; i < countIndex; i++) //尋找需要刪除的聯繫人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("無此聯繫人!\n");
}
else{
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[tempIndex].name, nodes[tempIndex].sex, nodes[tempIndex].age, nodes[tempIndex].number, nodes[tempIndex].address);
printf("-----------------------------------------------------------------------------------------\n");
printf("=========================================================================================\n");
printf("查詢成功!\n");
}
}
4.修改指定聯繫人信息
void modifySpecifiedContactInformation(){
printf("請輸入你要修改的聯繫人信息\n");
char findName[15]; //用於存儲需要查詢的聯繫人姓名
char findNumber[20]; //用於存儲需要查詢的聯繫人電話號碼
printf("待修改聯繫人姓名:");
scanf("%s", &findName);
printf("待修改聯繫人電話號碼:");
scanf("%s", &findNumber);
int tempIndex = -1; //用於存儲需要修改的聯繫人在數組中的的下標
int i;
for (i = 0; i < countIndex; i++) //尋找需要修改的聯繫人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("無此聯繫人!\n");
}
else{
printf("修改後的姓名:");
scanf("%s", &nodes[tempIndex].name);
printf("修改後的性別:");
scanf("%s", &nodes[tempIndex].sex);
printf("修改後的年齡:");
scanf("%d", &nodes[tempIndex].age);
printf("修改後的電話號碼:");
scanf("%s", &nodes[tempIndex].number);
printf("修改後的地址:");
scanf("%s", &nodes[tempIndex].address);
printf("修改成功!");
}
}
5.顯示所有聯繫人信息
void displayAllContactInformation(){
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
for (int i = 0; i < countIndex; i++)
{
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[i].name, nodes[i].sex, nodes[i].age, nodes[i].number, nodes[i].address);
printf("-----------------------------------------------------------------------------------------\n");
}
printf("=========================================================================================\n");
}
6.清空所有聯繫人
void emptyAllContacts(){
countIndex = 0;
printf("通訊錄已清空!\n");
}
清空後的通訊錄:
7.以名字排序所有聯繫人
void sortContactsByName(){
for (int i = 0; i < countIndex; i++)
{
for (int j = i; j < countIndex; j++){
if (strcmp(nodes[i].name, nodes[j].name) > 0){
contactNode tempnode;
strcpy(tempnode.name, nodes[i].name);
strcpy(tempnode.sex, nodes[i].sex);
tempnode.age = nodes[i].age;
strcpy(tempnode.number, nodes[i].number);
strcpy(tempnode.address, nodes[i].address);
strcpy(nodes[i].name, nodes[j].name);
strcpy(nodes[i].sex, nodes[j].sex);
nodes[i].age = nodes[j].age;
strcpy(nodes[i].number, nodes[j].number);
strcpy(nodes[i].address, nodes[j].address);
strcpy(nodes[j].name, tempnode.name);
strcpy(nodes[j].sex, tempnode.sex);
nodes[j].age = tempnode.age;
strcpy(nodes[j].number, tempnode.number);
strcpy(nodes[j].address, tempnode.address);
}
}
}
printf("已升序排列!\n");
}
main函數
int main(){
menu();
system("pause");
return 0;
}
所有源代碼
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#pragma warning(disable:4996)
typedef struct contactNode{ //定義聯繫人結構體
char name[15];
char sex[15];
int age;
char number[20];
char address[100];
};
contactNode nodes[1000]; //全局定義1000個聯繫人的數組用於存放聯繫人
int countIndex = 0; //用於控制數組下標
void addContactInformation(){
printf("請輸入你要添加的聯繫人信息\n");
printf("姓名:");
scanf("%s", &nodes[countIndex].name);
printf("性別:");
scanf("%s", &nodes[countIndex].sex);
printf("年齡:");
scanf("%d", &nodes[countIndex].age);
printf("電話號碼:");
scanf("%s", &nodes[countIndex].number);
printf("地址:");
scanf("%s", &nodes[countIndex].address);
countIndex++;
printf("聯繫人添加成功!\n");
}
void deleteSpecifiedContactInformation(){
printf("請輸入你要刪除的聯繫人信息\n");
char deleteName[15] ; //用於存儲需要刪除的聯繫人姓名
char deleteNumber[20]; //用於存儲需要刪除的聯繫人電話號碼
printf("姓名:");
scanf("%s", &deleteName);
printf("電話號碼:");
scanf("%s",&deleteNumber);
int tempIndex = -1; //用於存儲需要刪除的聯繫人在數組中的的下標
int i;
for (i = 0; i < countIndex; i++) //尋找需要刪除的聯繫人
{
if (strcmp(deleteName, nodes[i].name) == 0 && strcmp(deleteNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("無此聯繫人!\n");
}
else{
for (int j = tempIndex; j < countIndex - 1; j++){ //將該聯繫人之後所有聯繫人前移一個位置,將需要刪除的聯繫人覆蓋
strcpy(nodes[j].name, nodes[j + 1].name);
strcpy(nodes[j].sex, nodes[j + 1].sex);
nodes[j].age = nodes[j + 1].age;
strcpy(nodes[j].number, nodes[j + 1].number);
strcpy(nodes[j].address, nodes[j + 1].address);
}
countIndex--; //聯繫人總數減1
printf("刪除成功!");
}
}
void findSpecifiedContactInformation(){
printf("請輸入你要查詢的聯繫人信息\n");
char findName[15]; //用於存儲需要查詢的聯繫人姓名
char findNumber[20]; //用於存儲需要查詢的聯繫人電話號碼
printf("姓名:");
scanf("%s", &findName);
printf("電話號碼:");
scanf("%s", &findNumber);
int tempIndex = -1; //用於存儲需要刪除的聯繫人在數組中的的下標
int i;
for (i = 0; i < countIndex; i++) //尋找需要刪除的聯繫人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("無此聯繫人!\n");
}
else{
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[tempIndex].name, nodes[tempIndex].sex, nodes[tempIndex].age, nodes[tempIndex].number, nodes[tempIndex].address);
printf("-----------------------------------------------------------------------------------------\n");
printf("=========================================================================================\n");
printf("查詢成功!\n");
}
}
void modifySpecifiedContactInformation(){
printf("請輸入你要修改的聯繫人信息\n");
char findName[15]; //用於存儲需要查詢的聯繫人姓名
char findNumber[20]; //用於存儲需要查詢的聯繫人電話號碼
printf("待修改聯繫人姓名:");
scanf("%s", &findName);
printf("待修改聯繫人電話號碼:");
scanf("%s", &findNumber);
int tempIndex = -1; //用於存儲需要修改的聯繫人在數組中的的下標
int i;
for (i = 0; i < countIndex; i++) //尋找需要修改的聯繫人
{
if (strcmp(findName, nodes[i].name) == 0 && strcmp(findNumber, nodes[i].number) == 0){
tempIndex = i;
break;
}
}
if (tempIndex == -1){
printf("無此聯繫人!\n");
}
else{
printf("修改後的姓名:");
scanf("%s", &nodes[tempIndex].name);
printf("修改後的性別:");
scanf("%s", &nodes[tempIndex].sex);
printf("修改後的年齡:");
scanf("%d", &nodes[tempIndex].age);
printf("修改後的電話號碼:");
scanf("%s", &nodes[tempIndex].number);
printf("修改後的地址:");
scanf("%s", &nodes[tempIndex].address);
printf("修改成功!");
}
}
void emptyAllContacts(){
countIndex = 0;
printf("通訊錄已清空!\n");
}
void displayAllContactInformation(){
printf("=========================================================================================\n");
printf("|Name\t|Sex\t|Age\t|Tel\t\t\t|Address\t\t\t\n");
printf("-----------------------------------------------------------------------------------------\n");
for (int i = 0; i < countIndex; i++)
{
printf("|%s\t|%s\t|%d\t|%s\t\t\t|%s\t\t\t\n", nodes[i].name, nodes[i].sex, nodes[i].age, nodes[i].number, nodes[i].address);
printf("-----------------------------------------------------------------------------------------\n");
}
printf("=========================================================================================\n");
}
void sortContactsByName(){
for (int i = 0; i < countIndex; i++)
{
for (int j = i; j < countIndex; j++){
if (strcmp(nodes[i].name, nodes[j].name) > 0){
contactNode tempnode;
strcpy(tempnode.name, nodes[i].name);
strcpy(tempnode.sex, nodes[i].sex);
tempnode.age = nodes[i].age;
strcpy(tempnode.number, nodes[i].number);
strcpy(tempnode.address, nodes[i].address);
strcpy(nodes[i].name, nodes[j].name);
strcpy(nodes[i].sex, nodes[j].sex);
nodes[i].age = nodes[j].age;
strcpy(nodes[i].number, nodes[j].number);
strcpy(nodes[i].address, nodes[j].address);
strcpy(nodes[j].name, tempnode.name);
strcpy(nodes[j].sex, tempnode.sex);
nodes[j].age = tempnode.age;
strcpy(nodes[j].number, tempnode.number);
strcpy(nodes[j].address, tempnode.address);
}
}
}
printf("已升序排列!\n");
}
void menu(){
while (1){
printf("\n==========通訊錄==========\n||");
printf("0.退出系統 |\n||");
printf("1.添加聯繫人信息 |\n||");
printf("2.刪除指定聯繫人信息 |\n||");
printf("3.查找指定聯繫人信息 |\n||");
printf("4.修改指定聯繫人信息 |\n||");
printf("5.顯示所有聯繫人信息 |\n||");
printf("6.清空所有聯繫人 |\n||");
printf("7.以名字排序所有聯繫人 |\n");
printf("==========================\n");
int choice;
printf("\n請輸入您的選擇:");
scanf("%d", &choice);
switch (choice)
{
default:
printf("輸入不合法!\n");
break;
case 0:
printf("byebye!!\n");
return;
case 1:
addContactInformation();
break;
case 2:
deleteSpecifiedContactInformation();
break;
case 3:
findSpecifiedContactInformation();
break;
case 4:
modifySpecifiedContactInformation();
break;
case 5:
displayAllContactInformation();
break;
case 6:
emptyAllContacts();
break;
case 7:
sortContactsByName();
break;
}
}
}
int main(){
menu();
system("pause");
return 0;
}