1、主要要注意 修改功能:
char buff[20];printf("姓名(%s):", customer[index].name);fgets(buff, sizeof(buff), stdin);if (buff[0] != '\n'){sscanf(buff, "%s", customer[index].name);}
当在进行读取用户输入时,如果用户进行回车操作,这样就会导致这次读取的姓名就为\n,但是对于实际来说肯定不行,此时我们使用fgets()函数,将输入流中的数据读取的我们开辟的一个缓冲区中,判断读取到的第一个字符是不是\n,如果是,则不进行修改,如果不是,再进行修改,使用sscanf()函数来格式化到name属性。
#include <stdio.h>
#include <stdbool.h>
//
int key;
char loop = 1;
int cust_count;
typedef struct
{int id;char name[20];int age;char sex;char phone[20];char email[20];
} Customer;Customer customer[100];void show_header()
{printf("================客户管理系统===============\n");printf(" 1、添加客户\n");printf(" 2、修改客户\n");printf(" 3、删除客户\n");printf(" 4、客户列表\n");printf(" 5、退出(请选择1-5:\n");
}void add()
{printf("\n===============添加客户=================\n");customer[cust_count].id = cust_count + 1;printf("姓名:\n");scanf("%s", customer[cust_count].name);getchar();printf("年龄:\n");scanf("%d", &customer[cust_count].age);getchar();printf("性别:\n");scanf("%c", &customer[cust_count].sex);getchar();printf("手机号:\n");scanf("%s", customer[cust_count].phone);getchar();printf("邮箱:\n");scanf("%s", customer[cust_count].email);getchar();cust_count++;
}
void get_customer(Customer *customer)
{printf("%s\t%d\t%c\t%s\t\t\t%s\n", customer->name, customer->age, customer->sex,customer->phone, customer->email);
}int get_index(int id)
{int index = -1;for (int i = 0; i < cust_count; i++){if (customer[i].id = id){index = id;}}return index;
}bool edit(int id)
{int index = get_index(id);if (index == -1){return false;}printf("\n===============修改客户=================\n");char buff[20];printf("姓名(%s):", customer[index].name);fgets(buff, sizeof(buff), stdin);if (buff[0] != '\n'){sscanf(buff, "%s", customer[index].name);}printf("年龄(%d):", customer[index].age);fgets(buff, sizeof(buff), stdin);if (buff[0] != '\n'){sscanf(buff, "%s", &customer[index].age);}printf("性别(%c):", customer[index].sex);fgets(buff, sizeof(buff), stdin);if (buff[0] != '\n'){sscanf(buff, "%c", &customer[index].sex);}printf("电话(%s):", customer[index].phone);fgets(buff, sizeof(buff), stdin);if (buff[0] != '\n'){sscanf(buff, "%s", customer[index].phone);}printf("邮箱(%s):", customer[index].email);fgets(buff, sizeof(buff), stdin);if (buff[0] != '\n'){sscanf(buff, "%s", customer[index].email);}return true;
}void edit_view()
{int id;char choice = 'n';printf("\n===============修改客户=================\n");printf("\n===============请输入要修改的客户编号(-1.退出)=================\n");scanf("%d", &id);getchar();if (id == -1){printf("\n===============您放弃这次修改操作=================\n");}printf("\n===============是否确认修改(y/n)=================\n");scanf("%c", &choice);getchar();if (choice == 'n'){return;}printf("%s", edit(id) ? "修改成功\n" : "修改失败\n");
}bool del(int id)
{int index = get_index(id);if (index == -1){return false;}for (int i = index; i < cust_count; i++){customer[i] = customer[i + 1];customer[i].id = i + 1;}cust_count--;return true;
}void del_view()
{int id;char choice = 'n';printf("\n===============删除客户=================\n");printf("\n===============请输入要删除的客户编号(-1.退出)=================\n");scanf("%d", &id);getchar();if (id == -1){printf("\n===============您放弃这次删除操作=================\n");}printf("\n===============是否确认删除(y/n)=================\n");scanf("%c", &choice);getchar();if (choice == 'n'){return;}printf("%s", del(id) ? "删除成功\n" : "删除失败\n");
}void list()
{if (cust_count == 0){printf("当前客户为0,请先添加客户信息后再来查询");return;}printf("\n===============客户列表=================\n");printf("姓名\t年龄\t性别\t手机号\t\t\t邮箱\n");for (int i = 0; i < cust_count; i++){get_customer(&(customer[i]));}
}
void quit()
{char choice;do{printf("确认是否退出y/n:\n");scanf("%c", &choice);getchar();} while (choice != 'y' && choice != 'n');if (choice == 'y'){loop = 0;}
}void show_main()
{ // 主界面函数do{show_header();scanf("%d", &key);getchar();switch (key){case 1:add();break;case 2:edit_view();break;case 3:del_view();break;case 4:list();break;case 5:quit();break;default:printf("输入有误,请重新输入:\n");break;}} while (loop);
}int main()
{show_main();return 0;
}