用C语言的结构体编写汽车配件管理系统该系统主要包括管理员用户和普通用户。 其中管理员模块功能主要包括(管理、录入、产品详细信息、信息提交、修改); 普通用户模块功能主要包括( 账号注册、产品信息预览、搜索产品信息、产品排序、用户端查看)。 系统的内容通过文件储存,并且该系统可以读取文件来载入客户账号以及产品信息。给该系统的文件保存和载入编写代码,代码详细一点,只需实现所说功能即可。】
时间: 2024-03-18 15:41:22 浏览: 87
汽车配件管理系统
好的,以下是用C语言的结构体编写汽车配件管理系统的代码,包括文件保存和载入功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERS 100
#define MAX_PRODUCTS 100
//定义用户结构体
typedef struct {
char username[50];
char password[50];
int isAdmin;
} User;
//定义产品结构体
typedef struct {
char name[50];
char description[100];
float price;
} Product;
//定义全局变量
User users[MAX_USERS];
Product products[MAX_PRODUCTS];
int numUsers = 0;
int numProducts = 0;
//函数声明
void saveUsersToFile();
void saveProductsToFile();
void loadUsersFromFile();
void loadProductsFromFile();
void registerUser();
void viewProducts();
void searchProducts();
void sortProducts();
void viewUserProducts();
void addProduct();
void editProduct();
void deleteProduct();
int main() {
char choice;
//载入用户和产品信息
loadUsersFromFile();
loadProductsFromFile();
//循环显示菜单,根据用户选择执行不同的操作
while (1) {
printf("\n");
printf("Welcome to the Car Parts Management System!\n");
printf("Please choose an option:\n");
printf("\n");
printf("1. Register\n");
printf("2. View Products\n");
printf("3. Search Products\n");
printf("4. Sort Products\n");
printf("5. View My Products\n");
printf("\n");
if (users[0].isAdmin) {
printf("6. Add Product\n");
printf("7. Edit Product\n");
printf("8. Delete Product\n");
printf("\n");
}
printf("9. Exit\n");
printf("\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1':
registerUser();
break;
case '2':
viewProducts();
break;
case '3':
searchProducts();
break;
case '4':
sortProducts();
break;
case '5':
viewUserProducts();
break;
case '6':
if (users[0].isAdmin) {
addProduct();
} else {
printf("You do not have permission to perform this action.\n");
}
break;
case '7':
if (users[0].isAdmin) {
editProduct();
} else {
printf("You do not have permission to perform this action.\n");
}
break;
case '8':
if (users[0].isAdmin) {
deleteProduct();
} else {
printf("You do not have permission to perform this action.\n");
}
break;
case '9':
//保存用户和产品信息到文件并退出程序
saveUsersToFile();
saveProductsToFile();
printf("Exiting...\n");
exit(0);
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
//将用户信息保存到文件
void saveUsersToFile() {
FILE *fp;
int i;
fp = fopen("users.txt", "w");
if (fp != NULL) {
for (i = 0; i < numUsers; i++) {
fprintf(fp, "%s %s %d\n", users[i].username, users[i].password, users[i].isAdmin);
}
fclose(fp);
}
}
//将产品信息保存到文件
void saveProductsToFile() {
FILE *fp;
int i;
fp = fopen("products.txt", "w");
if (fp != NULL) {
for (i = 0; i < numProducts; i++) {
fprintf(fp, "%s %s %.2f\n", products[i].name, products[i].description, products[i].price);
}
fclose(fp);
}
}
//从文件中载入用户信息
void loadUsersFromFile() {
FILE *fp;
char line[100];
char *token;
fp = fopen("users.txt", "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp) != NULL) {
token = strtok(line, " ");
strcpy(users[numUsers].username, token);
token = strtok(NULL, " ");
strcpy(users[numUsers].password, token);
token = strtok(NULL, " ");
users[numUsers].isAdmin = atoi(token);
numUsers++;
}
fclose(fp);
}
}
//从文件中载入产品信息
void loadProductsFromFile() {
FILE *fp;
char line[100];
char *token;
fp = fopen("products.txt", "r");
if (fp != NULL) {
while (fgets(line, sizeof(line), fp) != NULL) {
token = strtok(line, " ");
strcpy(products[numProducts].name, token);
token = strtok(NULL, " ");
strcpy(products[numProducts].description, token);
token = strtok(NULL, " ");
products[numProducts].price = atof(token);
numProducts++;
}
fclose(fp);
}
}
//用户注册
void registerUser() {
char username[50];
char password[50];
printf("Enter a username: ");
scanf("%s", username);
//检查用户名是否已存在
for (int i = 0; i < numUsers; i++) {
if (strcmp(users[i].username, username) == 0) {
printf("That username is already taken.\n");
return;
}
}
printf("Enter a password: ");
scanf("%s", password);
//创建新用户
strcpy(users[numUsers].username, username);
strcpy(users[numUsers].password, password);
users[numUsers].isAdmin = 0;
printf("User registered.\n");
numUsers++;
}
//查看所有产品信息
void viewProducts() {
printf("Products:\n");
for (int i = 0; i < numProducts; i++) {
printf("%d. %s - %s - $%.2f\n", i+1, products[i].name, products[i].description, products[i].price);
}
}
//搜索产品信息
void searchProducts() {
char term[50];
int found = 0;
printf("Enter search term: ");
scanf("%s", term);
for (int i = 0; i < numProducts; i++) {
if (strstr(products[i].name, term) != NULL || strstr(products[i].description, term) != NULL) {
printf("%d. %s - %s - $%.2f\n", i+1, products[i].name, products[i].description, products[i].price);
found = 1;
}
}
if (!found) {
printf("No results found.\n");
}
}
//按价格排序产品信息
void sortProducts() {
int i, j;
Product temp;
for (i = 0; i < numProducts-1; i++) {
for (j = 0; j < numProducts-i-1; j++) {
if (products[j].price > products[j+1].price) {
temp = products[j];
products[j] = products[j+1];
products[j+1] = temp;
}
}
}
printf("Products sorted by price.\n");
}
//查看用户自己添加的产品信息
void viewUserProducts() {
int found = 0;
printf("Your Products:\n");
for (int i = 0; i < numProducts; i++) {
if (strcmp(users[0].username, products[i].name) == 0) {
printf("%d. %s - %s - $%.2f\n", i+1, products[i].name, products[i].description, products[i].price);
found = 1;
}
}
if (!found) {
printf("You have not added any products.\n");
}
}
//添加产品信息
void addProduct() {
char name[50];
char description[100];
float price;
printf("Enter product name: ");
scanf("%s", name);
printf("Enter product description: ");
scanf("%s", description);
printf("Enter product price: ");
scanf("%f", &price);
//创建新产品
strcpy(products[numProducts].name, name);
strcpy(products[numProducts].description, description);
products[numProducts].price = price;
printf("Product added.\n");
numProducts++;
}
//编辑产品信息
void editProduct() {
int choice;
char description[100];
float price;
viewProducts();
printf("Enter product number to edit: ");
scanf("%d", &choice);
if (choice < 1 || choice > numProducts) {
printf("Invalid choice.\n");
return;
}
printf("Enter new description: ");
scanf("%s", description);
printf("Enter new price: ");
scanf("%f", &price);
//更新选定产品的信息
strcpy(products[choice-1].description, description);
products[choice-1].price = price;
printf("Product updated.\n");
}
//删除产品信息
void deleteProduct() {
int choice;
viewProducts();
printf("Enter product number to delete: ");
scanf("%d", &choice);
if (choice < 1 || choice > numProducts) {
printf("Invalid choice.\n");
return;
}
//移动数组中的元素来删除选定的产品信息
for (int i = choice-1; i < numProducts-1; i++) {
products[i] = products[i+1];
}
numProducts--;
printf("Product deleted.\n");
}
```
以上代码实现了一个基本的汽车配件管理系统,包括管理员模块和普通用户模块。其中,数据保存和载入功能通过文件来实现,分别保存了用户和产品的信息。在程序运行时,会先从文件中加载这些信息,然后在用户进行操作时对其进行修改,最后再将修改后的信息保存回文件中。
阅读全文