用C语言实现函数功能 初始化 欢迎界面 用户角色选择 登录验证入口 顾客登录验证 管理员登录验证 顾客功能模块入口 管理员功能模块入口 顾客功能主菜单 管理员功能主菜单 管理顾客信息 增加顾客信息 删除顾客信息 修改顾客信息 查询顾客信息 删除药品信息 修改药品信息 查询药品信息 增加药品销售信息 删除药品销售信息 修改药品销售信息 查询药品销售信息 查询个人信息 修改个人信息 退出系统函数 载入客户信息 保存客户信息 查看商品销售信息
时间: 2024-03-19 14:40:40 浏览: 114
C语言函数,很使用的,以界面出现
好的,以下是用C语言实现以上功能的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义全局变量,保存客户信息
typedef struct {
char name[20];
char password[20];
char role[10];
} Customer;
Customer customers[100];
int customerCount = 0;
// 声明函数
void init();
void welcome();
char* chooseRole();
void login();
void customerLogin();
void adminLogin();
void customerMenu();
void adminMenu();
void manageCustomer();
void addCustomer();
void deleteCustomer();
void modifyCustomer();
void queryCustomer();
void deleteProduct();
void modifyProduct();
void queryProduct();
void addSales();
void deleteSales();
void modifySales();
void querySales();
void viewProfile();
void modifyProfile();
void saveCustomer();
void loadCustomer();
void viewSales();
// 主函数
int main() {
init();
welcome();
char* role = chooseRole();
if (strcmp(role, "customer") == 0) {
customerLogin();
customerMenu();
} else if (strcmp(role, "admin") == 0) {
adminLogin();
adminMenu();
} else {
printf("Invalid role!\n");
}
return 0;
}
// 初始化客户信息
void init() {
loadCustomer();
}
// 欢迎界面
void welcome() {
printf("Welcome to our system!\n");
}
// 选择用户角色
char* chooseRole() {
printf("Please choose your role:\n");
printf("1. Customer\n");
printf("2. Admin\n");
int choice;
scanf("%d", &choice);
if (choice == 1) {
return "customer";
} else if (choice == 2) {
return "admin";
} else {
printf("Invalid choice!\n");
return "";
}
}
// 登录验证入口
void login() {
char name[20];
char password[20];
printf("Please enter your name:\n");
scanf("%s", name);
printf("Please enter your password:\n");
scanf("%s", password);
for (int i = 0; i < customerCount; i++) {
if (strcmp(name, customers[i].name) == 0 && strcmp(password, customers[i].password) == 0) {
printf("Login successful!\n");
return;
}
}
printf("Invalid name or password!\n");
}
// 顾客登录验证
void customerLogin() {
printf("Customer login:\n");
login();
}
// 管理员登录验证
void adminLogin() {
printf("Admin login:\n");
login();
}
// 顾客功能模块入口
void customerMenu() {
while (1) {
printf("Please choose your operation:\n");
printf("1. View profile\n");
printf("2. Modify profile\n");
printf("3. View sales\n");
printf("4. Exit\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
viewProfile();
break;
case 2:
modifyProfile();
break;
case 3:
viewSales();
break;
case 4:
saveCustomer();
exit(0);
default:
printf("Invalid choice!\n");
}
}
}
// 管理员功能模块入口
void adminMenu() {
while (1) {
printf("Please choose your operation:\n");
printf("1. Manage customer\n");
printf("2. Manage product\n");
printf("3. Manage sales\n");
printf("4. Exit\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
manageCustomer();
break;
case 2:
printf("Manage product\n");
break;
case 3:
printf("Manage sales\n");
break;
case 4:
saveCustomer();
exit(0);
default:
printf("Invalid choice!\n");
}
}
}
// 管理顾客信息
void manageCustomer() {
while (1) {
printf("Please choose your operation:\n");
printf("1. Add customer\n");
printf("2. Delete customer\n");
printf("3. Modify customer\n");
printf("4. Query customer\n");
printf("5. Exit\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
addCustomer();
break;
case 2:
deleteCustomer();
break;
case 3:
modifyCustomer();
break;
case 4:
queryCustomer();
break;
case 5:
return;
default:
printf("Invalid choice!\n");
}
}
}
// 增加顾客信息
void addCustomer() {
if (customerCount == 100) {
printf("Customer count has reached the maximum!\n");
return;
}
Customer customer;
printf("Please enter the name:\n");
scanf("%s", customer.name);
printf("Please enter the password:\n");
scanf("%s", customer.password);
strcpy(customer.role, "customer");
customers[customerCount++] = customer;
printf("Successfully added a new customer!\n");
}
// 删除顾客信息
void deleteCustomer() {
char name[20];
printf("Please enter the name of the customer you want to delete:\n");
scanf("%s", name);
for (int i = 0; i < customerCount; i++) {
if (strcmp(name, customers[i].name) == 0) {
for (int j = i + 1; j < customerCount; j++) {
customers[j - 1] = customers[j];
}
customerCount--;
printf("Successfully deleted the customer!\n");
return;
}
}
printf("Customer not found!\n");
}
// 修改顾客信息
void modifyCustomer() {
char name[20];
printf("Please enter the name of the customer you want to modify:\n");
scanf("%s", name);
for (int i = 0; i < customerCount; i++) {
if (strcmp(name, customers[i].name) == 0) {
printf("Please enter the new password:\n");
scanf("%s", customers[i].password);
printf("Successfully modified the customer!\n");
return;
}
}
printf("Customer not found!\n");
}
// 查询顾客信息
void queryCustomer() {
char name[20];
printf("Please enter the name of the customer you want to query:\n");
scanf("%s", name);
for (int i = 0; i < customerCount; i++) {
if (strcmp(name, customers[i].name) == 0) {
printf("Name: %s, Password: %s, Role: %s\n", customers[i].name, customers[i].password, customers[i].role);
return;
}
}
printf("Customer not found!\n");
}
// 删除药品信息
void deleteProduct() {
printf("Delete product\n");
}
// 修改药品信息
void modifyProduct() {
printf("Modify product\n");
}
// 查询药品信息
void queryProduct() {
printf("Query product\n");
}
// 增加药品销售信息
void addSales() {
printf("Add sales\n");
}
// 删除药品销售信息
void deleteSales() {
printf("Delete sales\n");
}
// 修改药品销售信息
void modifySales() {
printf("Modify sales\n");
}
// 查询药品销售信息
void querySales() {
printf("Query sales\n");
}
// 查询个人信息
void viewProfile() {
printf("View profile\n");
}
// 修改个人信息
void modifyProfile() {
printf("Modify profile\n");
}
// 退出系统函数
void exitSystem() {
printf("Exit system\n");
}
// 载入客户信息
void loadCustomer() {
FILE* file = fopen("customers.txt", "r");
if (file == NULL) {
return;
}
while (!feof(file)) {
Customer customer;
fscanf(file, "%s %s %s", customer.name, customer.password, customer.role);
if (strcmp(customer.role, "customer") == 0) {
customers[customerCount++] = customer;
}
}
fclose(file);
}
// 保存客户信息
void saveCustomer() {
FILE* file = fopen("customers.txt", "w");
for (int i = 0; i < customerCount; i++) {
fprintf(file, "%s %s %s\n", customers[i].name, customers[i].password, customers[i].role);
}
fclose(file);
}
// 查看商品销售信息
void viewSales() {
printf("View sales\n");
}
```
以上是一个简单的客户管理系统的实现,其中包括顾客和管理员两种角色,顾客可以查看和修改个人信息,以及查看销售信息;管理员可以管理客户信息,药品信息和销售信息。代码中使用了结构体来保存客户信息,使用文件来保存客户信息,方便数据的存储和读取。
阅读全文