用C语言的结构体编写汽车配件管理系统该系统主要包括管理员用户和普通用户,并且该系统可以读取文件来载入客户账号以及产品信息。给该系统的文件保存和载入编写代码。其中管理员模块功能包括(管理、录入、产品详细信息、信息提交、修改);普通用户模块功能包括( 账号注册、产品信息预览、用户端查看)。
时间: 2023-08-03 22:05:46 浏览: 71
好的,下面是用C语言的结构体编写汽车配件管理系统的代码:
首先,我们需要定义一些结构体来存储数据:
```c
// 客户账号结构体
struct customer {
char username[20];
char password[20];
};
// 产品信息结构体
struct product {
char name[50];
int quantity;
float price;
};
// 订单结构体
struct order {
char username[20];
char product_name[50];
int quantity;
float price;
};
```
接下来,我们需要实现文件的保存和载入功能。文件保存功能用于将客户账号和产品信息保存到文件中,文件载入功能用于从文件中读取客户账号和产品信息。
```c
// 将客户账号保存到文件中
void save_customers(struct customer customers[], int count) {
FILE *fp = fopen("customers.txt", "w");
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %s\n", customers[i].username, customers[i].password);
}
fclose(fp);
}
// 从文件中载入客户账号
int load_customers(struct customer customers[]) {
FILE *fp = fopen("customers.txt", "r");
int count = 0;
while (!feof(fp)) {
fscanf(fp, "%s %s\n", customers[count].username, customers[count].password);
count++;
}
fclose(fp);
return count;
}
// 将产品信息保存到文件中
void save_products(struct product products[], int count) {
FILE *fp = fopen("products.txt", "w");
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %d %.2f\n", products[i].name, products[i].quantity, products[i].price);
}
fclose(fp);
}
// 从文件中载入产品信息
int load_products(struct product products[]) {
FILE *fp = fopen("products.txt", "r");
int count = 0;
while (!feof(fp)) {
fscanf(fp, "%s %d %f\n", products[count].name, &products[count].quantity, &products[count].price);
count++;
}
fclose(fp);
return count;
}
```
接下来,我们需要实现管理员模块的功能。管理员模块包括管理、录入、产品详细信息、信息提交和修改功能。
```c
// 管理功能
void admin_menu(struct customer customers[], int customer_count, struct product products[], int product_count) {
int choice;
do {
printf("请选择要进行的操作:\n");
printf("1. 查看客户账号\n");
printf("2. 查看产品信息\n");
printf("3. 录入产品信息\n");
printf("4. 修改产品信息\n");
printf("5. 返回上一级菜单\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("客户账号列表:\n");
for (int i = 0; i < customer_count; i++) {
printf("%s\n", customers[i].username);
}
break;
case 2:
printf("产品信息列表:\n");
for (int i = 0; i < product_count; i++) {
printf("%s 数量:%d 价格:%.2f\n", products[i].name, products[i].quantity, products[i].price);
}
break;
case 3:
printf("请输入要录入的产品信息:\n");
printf("名称:");
scanf("%s", products[product_count].name);
printf("数量:");
scanf("%d", &products[product_count].quantity);
printf("价格:");
scanf("%f", &products[product_count].price);
product_count++;
save_products(products, product_count);
printf("录入成功!\n");
break;
case 4:
printf("请输入要修改的产品名称:");
char name[50];
scanf("%s", name);
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("请输入要修改的产品信息:\n");
printf("名称:");
scanf("%s", products[i].name);
printf("数量:");
scanf("%d", &products[i].quantity);
printf("价格:");
scanf("%f", &products[i].price);
save_products(products, product_count);
printf("修改成功!\n");
break;
}
}
break;
case 5:
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
} while (choice != 5);
}
// 产品详细信息功能
void product_detail(struct product products[], int product_count) {
printf("请输入产品名称:");
char name[50];
scanf("%s", name);
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("产品名称:%s\n", products[i].name);
printf("产品数量:%d\n", products[i].quantity);
printf("产品价格:%.2f\n", products[i].price);
break;
}
}
}
// 信息提交功能
void submit_order(struct order orders[], int order_count, struct product products[], int product_count) {
printf("请输入您的账号:");
char username[20];
scanf("%s", username);
printf("请输入产品名称:");
char name[50];
scanf("%s", name);
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("请输入购买数量:");
int quantity;
scanf("%d", &quantity);
if (quantity <= products[i].quantity) {
orders[order_count].quantity = quantity;
orders[order_count].price = products[i].price * quantity;
strcpy(orders[order_count].username, username);
strcpy(orders[order_count].product_name, name);
products[i].quantity -= quantity;
save_products(products, product_count);
order_count++;
printf("提交成功!\n");
} else {
printf("库存不足,请重新输入购买数量!\n");
}
break;
}
}
}
// 修改功能
void modify_order(struct order orders[], int order_count) {
printf("请输入要修改的订单用户名:");
char username[20];
scanf("%s", username);
for (int i = 0; i < order_count; i++) {
if (strcmp(orders[i].username, username) == 0) {
printf("请输入修改后的购买数量:");
int quantity;
scanf("%d", &quantity);
orders[i].quantity = quantity;
orders[i].price = orders[i].quantity * getPrice(orders[i].product_name);
printf("修改成功!\n");
break;
}
}
}
// 获取产品价格
float getPrice(char name[]) {
struct product products[100];
int product_count = load_products(products);
for (int i = 0; i < product_count; i++) {
if (strcmp(products[i].name, name) == 0) {
return products[i].price;
}
}
return 0.0;
}
```
最后,我们需要实现普通用户模块的功能。普通用户模块包括账号注册、产品信息预览和用户端查看功能。
```c
// 账号注册功能
void register_customer(struct customer customers[], int customer_count) {
printf("请输入账号:");
char username[20];
scanf("%s", username);
printf("请输入密码:");
char password[20];
scanf("%s", password);
strcpy(customers[customer_count].username, username);
strcpy(customers[customer_count].password, password);
customer_count++;
save_customers(customers, customer_count);
printf("注册成功!\n");
}
// 产品信息预览功能
void view_products(struct product products[], int product_count) {
printf("产品信息列表:\n");
for (int i = 0; i < product_count; i++) {
printf("%s 数量:%d 价格:%.2f\n", products[i].name, products[i].quantity, products[i].price);
}
}
// 用户端查看功能
void view_orders(struct order orders[], int order_count) {
printf("您的订单列表:\n");
for (int i = 0; i < order_count; i++) {
printf("产品名称:%s 购买数量:%d 价格:%.2f\n", orders[i].product_name, orders[i].quantity, orders[i].price);
}
}
```
最后,在主函数中我们需要调用上述函数实现整个系统的功能。
```c
int main() {
// 载入客户账号和产品信息
struct customer customers[100];
int customer_count = load_customers(customers);
struct product products[100];
int product_count = load_products(products);
struct order orders[100];
int order_count = 0;
// 进入系统
int choice;
do {
printf("请选择您的身份:\n");
printf("1. 管理员\n");
printf("2. 普通用户\n");
printf("3. 退出系统\n");
scanf("%d", &choice);
switch (choice) {
case 1:
admin_menu(customers, customer_count, products, product_count);
break;
case 2:
int sub_choice;
do {
printf("请选择要进行的操作:\n");
printf("1. 注册账号\n");
printf("2. 查看产品信息\n");
printf("3. 查看订单信息\n");
printf("4. 提交订单\n");
printf("5. 修改订单\n");
printf("6. 返回上一级菜单\n");
scanf("%d", &sub_choice);
switch (sub_choice) {
case 1:
register_customer(customers, customer_count);
break;
case 2:
view_products(products, product_count);
break;
case 3:
view_orders(orders, order_count);
break;
case 4:
submit_order(orders, order_count, products, product_count);
break;
case 5:
modify_order(orders, order_count);
break;
case 6:
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
} while (sub_choice != 6);
break;
case 3:
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
} while (choice != 3);
return 0;
}
```
阅读全文