通讯录源代码

 http://bestbbs.5d6d.com/thread-44-1-1.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言通讯录源代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_CONTACTS 100 struct contact { char name[50]; char phone[15]; }; struct contact contacts[MAX_CONTACTS]; int num_contacts = 0; void add_contact() { if (num_contacts >= MAX_CONTACTS) { printf("Sorry, the address book is full.\n"); return; } struct contact new_contact; printf("Enter name: "); fgets(new_contact.name, 50, stdin); new_contact.name[strcspn(new_contact.name, "\n")] = '\0'; // remove newline character printf("Enter phone number: "); fgets(new_contact.phone, 15, stdin); new_contact.phone[strcspn(new_contact.phone, "\n")] = '\0'; // remove newline character contacts[num_contacts++] = new_contact; printf("New contact added.\n"); } void list_contacts() { printf("Contact list:\n"); for (int i = 0; i < num_contacts; i++) { printf("%s: %s\n", contacts[i].name, contacts[i].phone); } } void search_contacts() { char search_term[50]; printf("Enter search term: "); fgets(search_term, 50, stdin); search_term[strcspn(search_term, "\n")] = '\0'; // remove newline character printf("Search results:\n"); for (int i = 0; i < num_contacts; i++) { if (strstr(contacts[i].name, search_term) != NULL) { printf("%s: %s\n", contacts[i].name, contacts[i].phone); } } } int main() { char input[10]; while (1) { printf("Enter command (add, list, search, or quit): "); fgets(input, 10, stdin); input[strcspn(input, "\n")] = '\0'; // remove newline character if (strcmp(input, "add") == 0) { add_contact(); } else if (strcmp(input, "list") == 0) { list_contacts(); } else if (strcmp(input, "search") == 0) { search_contacts(); } else if (strcmp(input, "quit") == 0) { printf("Goodbye!\n"); return 0; } else { printf("Invalid command.\n"); } } } ``` 这个程序使用结构体来存储联系人的姓名和电话号码,并提供了添加、列出、搜索和退出功能。请注意,此代码示例可能并不完美,仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值