#include<stdio.h> #include<stdlib.h> #include<string.h> int count= 0; int menu(); void add(); void select(); void modify(); void watch(); void clear(); void name_sort(); void del(); struct people { char name[20]; char sex[4]; char age; char tel[15]; char address[20]; }lest[1000]; int main() { while (1) { int sel = menu(); switch (sel) { case 0: return 0; break; case 1: add(); break; case 2: del(); break; case 3: select(); break; case 4: modify(); break; case 5: watch(); break; case 6: clear(); break; case 7:name_sort(); break; default: break; } } system("pause"); return 0; } int menu() { int sel = 0; printf("****************************************\n"); printf("************欢迎使用通讯录**************\n"); printf("****************************************\n"); printf("** 0.exit *****************************\n"); printf("** 1.添加联系人信息 ********************\n"); printf("** 2.删除指定联系人信息 ****************\n"); printf("** 3.查找指定联系人信息 ****************\n"); printf("** 4.修改指定联系人信息 ****************\n"); printf("** 5.显示所有联系人信息 ****************\n"); printf("** 6.清空所有联系人 ********************\n"); printf("** 7.以名字排序所有联系人 **************\n"); printf("****************************************\n"); printf("请输入您的选择\n"); scanf("%d", &sel); if (sel<0 || sel>7) { printf("输入错误请重新输入!\n"); menu(); } return sel; } void add() { printf("请输入姓名:\n"); scanf("%s", &lest[count].name); printf("请输入性别:\n"); scanf("%s", &lest[count].sex); printf("请输入年龄:\n"); scanf("%d", &lest[count].age); printf("请输入电话:\n"); scanf("%s", &lest[count].tel); printf("请输入住址:\n"); scanf("%s", &lest[count].address); count++; printf("添加成功!\n"); } void select() { int i = 0; int j = count; char sel_name[20]; printf("请输入要查找的人:\n"); scanf("%s", sel_name); for (i = 0; i < count; i++) { if (strcmp(lest[i].name, sel_name) == 0) { printf("姓名:%s\n", lest[i].name); printf("性别:%s\n", lest[i].sex); printf("年龄:%d\n", lest[i].age); printf("电话:%s\n", lest[i].tel); printf("住址:%s\n", lest[i].address); break; } if (i == count-1) { printf("不存在要找的人。\n"); } } } void modify() { int i = 0; int j = count; char sel_name[20]; printf("请输入要修改的人:\n"); scanf("%s", sel_name); for (i = 0; i < count; i++) { if (strcmp(lest[i].name, sel_name) == 0) { printf("请输入新的姓名:\n"); scanf("%s", &lest[i].name); printf("请输入新的性别:\n"); scanf("%s", &lest[i].sex); printf("请输入新的年龄:\n"); scanf("%d", &lest[i].age); printf("请输入新的电话:\n"); scanf("%s", &lest[i].tel); printf("请输入新的住址:\n"); scanf("%s", &lest[i].address); } } if (i == count-1) { printf("不存在要找的人。\n"); } } void watch() { int i = 0; int j = count; for (i = 0; i <j; i++) { printf("姓名:%s\n", lest[i].name); printf("性别:%s\n", lest[i].sex); printf("年龄:%d\n", lest[i].age); printf("电话:%s\n", lest[i].tel); printf("住址:%s\n", lest[i].address); } } void clear() { count = 0; } void name_sort() { int i = 0; int j = 0; for (i = 0; i < count - 1; i++) { for (j = 0; j < count - i - 1; j++) { if (strcmp(lest[j].name, lest[j + 1].name)>0) { lest[count + 1] = lest[j]; lest[j] = lest[j + 1]; lest[j + 1] = lest[count + 1]; } } } strcpy(lest[count + 1].name, "0"); strcpy(lest[count + 1].sex, "0"); lest[count + 1].age = 0; strcpy(lest[count + 1].tel, "0"); strcpy(lest[count + 1].address, "0"); watch(); } void del() { int i = 0; int j = count; char sel_name[20]; int b = 0; printf("请输入要删除的人:\n"); scanf("%s", sel_name); for (i = 0; i < count; i++) { if (strcmp(lest[i].name, sel_name) == 0) { for (b = i + 1; b < j; b++) { strcpy(lest[i].name, lest[i + 1].name); strcpy(lest[i].sex, lest[i + 1].sex); lest[i].age = lest[i + 1].age; strcpy(lest[i].tel, lest[i + 1].tel); strcpy(lest[i].address, lest[i + 1].address); } count--; } } }
时间: 2024-12-28 11:04:31