1.添加客户
2.修改客户
3.删除客户
4.客户列表
5.退 出
#define _CRT_SECURE_NO_WARNINGS #include运行结果#include #include struct Customer { int id;//编号 int age;//年龄 char name[10];//名字 char gender;//f/m char phone[16];//电话号码 char email[20];//邮箱 }; int loop = 1;//控制是否退出主菜单 char key; int customerNum = 0;//表示当前客户数量 //客户结构体数组 struct Customer customers[100];//最多有100个数组 //显示Customer变量的信息 void getCustomerInfo(struct Customer* customer) { printf("n%dt%st%ct%dt%st%s", (*customer).id, (*customer).name,(*customer).gender, (*customer).age, (*customer).phone, (*customer).email); } void add() { customers[customerNum].id = customerNum + 1; printf("n----------------添加客户------------"); printf("n姓名: "); scanf("%s", customers[customerNum].name); getchar(); printf("n性别: "); scanf("%c", &customers[customerNum].gender); getchar(); printf("n年龄: "); scanf("%d", &customers[customerNum].age); getchar(); printf("n电话: "); scanf("%s", customers[customerNum].phone); getchar(); printf("n邮箱: "); scanf("%s", customers[customerNum].email); getchar(); printf("n----------------添加完成------------"); customerNum++; } //根据输入的id返回index, int findIndex(int id) { int index = -1; int i ; for (i = 0; i < customerNum; i++) { if (customers[i].id == id) {//找到 index = i; } } return index; } //接收要删除的客户id,返回1,说明删除成功 int del(int id) { int index = findIndex(id); int i; if (index == -1) {//不存在这个对应id客户 return 0; } else { //将编号为id的客户移除,后一个往前覆盖 for (i = index+1; i < customerNum; i++) { customers[i - 1] = customers[i]; customers[i - 1].id--;//id减一 } customerNum--; return 1; } } //显示删除界面 void delView() { int id=0; printf("n----------------删除客户-------------------"); printf("n请输入删除客户编号(-1退出): "); scanf("%d", &id); getchar(); if (id == -1) { printf("n你放弃了删除!"); return; } printf("n确认是否删除(Y/N): "); char choice; scanf("%c", &choice); getchar(); if (choice == 'Y') { if (!del(id)) { printf("n删除失败,id不存在"); } else { printf("n删除成功!"); } } } //显示客户信息列表 void listCustomers() { int i = 0; printf("n--------------客户列表------------"); printf("n编号t姓名t性别t年龄t电话t邮箱"); for (i = 0; i < customerNum; i++) { getCustomerInfo(&customers[i]); } } //显示主菜单 void mainMenu() { do { printf("n--------------客户信息管理软件------------"); printf("n--------------1.添加客户------------"); printf("n--------------2.修改客户------------"); printf("n--------------3.删除客户------------"); printf("n--------------4.客户列表------------"); printf("n--------------5.退 出------------"); printf("n请选择输入1_5: "); scanf("%c", &key); getchar(); switch (key) { case '1': //printf("添加客户"); add(); break; case '2': //修改该用户时,先查找是否存在,如果找到该用户,先显示该用户信息,id不能修改 //待完善........... printf("修改客户"); break; case '3': //printf("删除客户"); delView(); break; case '4': listCustomers(); break; case '5': char choice1; do { printf("n确认是否退出(Y/N): "); scanf("%c", &choice1); getchar(); } while (choice1 != 'Y' && choice1 != 'N'); if (choice1 == 'Y') { loop = 0; } break; default: printf("n输入错误,请重新输入!"); break; } } while (loop); printf("n你退出了系统!"); getchar(); } int main() { mainMenu();//调用函数,显示菜单 return 0; }
注:以上内容仅为个人学习笔记



