c语言构建简单的手机通讯录源代码
时间: 2023-03-24 15:04:00 浏览: 97
以下是一个简单的 C 语言手机通讯录源代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CONTACTS 100
struct Contact {
char name[50];
char phone[20];
};
struct Contact contacts[MAX_CONTACTS];
int num_contacts = ;
void add_contact() {
if (num_contacts >= MAX_CONTACTS) {
printf("通讯录已满,无法添加新联系人。\n");
return;
}
struct Contact new_contact;
printf("请输入联系人姓名:");
scanf("%s", new_contact.name);
printf("请输入联系人电话:");
scanf("%s", new_contact.phone);
contacts[num_contacts] = new_contact;
num_contacts++;
printf("联系人已添加。\n");
}
void list_contacts() {
if (num_contacts == ) {
printf("通讯录为空。\n");
return;
}
printf("姓名\t\t电话\n");
for (int i = ; i < num_contacts; i++) {
printf("%s\t\t%s\n", contacts[i].name, contacts[i].phone);
}
}
int main() {
while (1) {
printf("请选择操作:\n");
printf("1. 添加联系人\n");
printf("2. 查看通讯录\n");
printf("3. 退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_contact();
break;
case 2:
list_contacts();
break;
case 3:
printf("再见!\n");
exit();
default:
printf("无效的选择。\n");
}
}
return ;
}
阅读全文