用C语言的结构体编写汽车配件管理系统该系统主要包括管理员用户和普通用户。其中管理员模块功能主要包括(管理、录入、产品详细信息、信息提交、修改);普通用户模块功能主要包括( 账号注册、产品信息预览、搜索产品信息、产品排序、用户端查看)。系统的内容通过文件储存,并且该系统可以读取文件来载入客户账号以及产品信息。给该系统的文件保存和载入编写代码
时间: 2023-11-26 08:03:53 浏览: 46
以下是一个简单的汽车配件管理系统的代码示例,其中包含了管理员和普通用户的功能以及文件保存和载入功能:
```c
#include <stdio.h>
#include <string.h>
// 定义汽车配件结构体
typedef struct {
char name[20];
char brand[20];
int price;
} AutoPart;
// 定义用户结构体
typedef struct {
char username[20];
char password[20];
int isAdmin;
} User;
// 保存汽车配件信息到文件
void saveAutoPartsToFile(AutoPart parts[], int count) {
FILE *file = fopen("autoparts.dat", "wb");
fwrite(parts, sizeof(AutoPart), count, file);
fclose(file);
}
// 从文件中载入汽车配件信息
int loadAutoPartsFromFile(AutoPart parts[]) {
FILE *file = fopen("autoparts.dat", "rb");
int count = 0;
if (file != NULL) {
while (fread(&parts[count], sizeof(AutoPart), 1, file)) {
count++;
}
fclose(file);
}
return count;
}
// 保存用户信息到文件
void saveUsersToFile(User users[], int count) {
FILE *file = fopen("users.dat", "wb");
fwrite(users, sizeof(User), count, file);
fclose(file);
}
// 从文件中载入用户信息
int loadUsersFromFile(User users[]) {
FILE *file = fopen("users.dat", "rb");
int count = 0;
if (file != NULL) {
while (fread(&users[count], sizeof(User), 1, file)) {
count++;
}
fclose(file);
}
return count;
}
// 用户注册
void registerUser(User users[], int *count) {
User user;
printf("Username: ");
scanf("%s", user.username);
printf("Password: ");
scanf("%s", user.password);
user.isAdmin = 0;
users[*count] = user;
(*count)++;
saveUsersToFile(users, *count);
}
// 用户登录
int loginUser(User users[], int count, char *username, char *password) {
for (int i = 0; i < count; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
return i;
}
}
return -1;
}
// 管理员录入汽车配件信息
void inputAutoPart(AutoPart parts[], int *count) {
AutoPart part;
printf("Name: ");
scanf("%s", part.name);
printf("Brand: ");
scanf("%s", part.brand);
printf("Price: ");
scanf("%d", &part.price);
parts[*count] = part;
(*count)++;
saveAutoPartsToFile(parts, *count);
}
// 管理员查看汽车配件信息
void viewAutoParts(AutoPart parts[], int count) {
for (int i = 0; i < count; i++) {
printf("%s %s %d\n", parts[i].name, parts[i].brand, parts[i].price);
}
}
// 普通用户查看汽车配件信息
void previewAutoParts(AutoPart parts[], int count) {
for (int i = 0; i < count; i++) {
printf("%s %s\n", parts[i].name, parts[i].brand);
}
}
// 普通用户搜索汽车配件信息
void searchAutoParts(AutoPart parts[], int count, char *keyword) {
for (int i = 0; i < count; i++) {
if (strstr(parts[i].name, keyword) || strstr(parts[i].brand, keyword)) {
printf("%s %s %d\n", parts[i].name, parts[i].brand, parts[i].price);
}
}
}
// 普通用户按价格排序汽车配件信息
void sortAutoPartsByPrice(AutoPart parts[], int count) {
for (int i = 0; i < count-1; i++) {
for (int j = i+1; j < count; j++) {
if (parts[i].price > parts[j].price) {
AutoPart temp = parts[i];
parts[i] = parts[j];
parts[j] = temp;
}
}
}
}
int main() {
int autoPartCount = 0;
AutoPart autoParts[100];
int userCount = 0;
User users[100];
// 载入数据
autoPartCount = loadAutoPartsFromFile(autoParts);
userCount = loadUsersFromFile(users);
// 用户登录
char username[20];
char password[20];
int currentUserIndex = -1;
while (currentUserIndex == -1) {
printf("Username: ");
scanf("%s", username);
printf("Password: ");
scanf("%s", password);
currentUserIndex = loginUser(users, userCount, username, password);
if (currentUserIndex == -1) {
printf("Invalid username or password, please try again.\n");
}
}
// 根据用户类型执行不同的操作
if (users[currentUserIndex].isAdmin) {
int choice;
do {
printf("1. Input auto part\n");
printf("2. View auto parts\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
inputAutoPart(autoParts, &autoPartCount);
break;
case 2:
viewAutoParts(autoParts, autoPartCount);
break;
case 3:
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 3);
} else {
int choice;
do {
printf("1. Preview auto parts\n");
printf("2. Search auto parts\n");
printf("3. Sort auto parts by price\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
previewAutoParts(autoParts, autoPartCount);
break;
case 2:
char keyword[20];
printf("Enter keyword: ");
scanf("%s", keyword);
searchAutoParts(autoParts, autoPartCount, keyword);
break;
case 3:
sortAutoPartsByPrice(autoParts, autoPartCount);
viewAutoParts(autoParts, autoPartCount);
break;
case 4:
break;
default:
printf("Invalid choice, please try again.\n");
}
} while (choice != 4);
}
return 0;
}
```
注意:此代码示例仅用于演示目的,实际应用中可能存在安全漏洞和性能问题,需要根据实际情况进行优化和改进。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)