注册功能的实现,账号和密码存储在内存中; 管理员登录模块的实现,登陆进去后进行店铺信息的管理 游客登录模块,登录OK后,可以查看所有店铺信息; 游客模块,可以查询自己所想去的店铺名称; 游客模块也可以评价该店铺;
时间: 2024-03-24 19:41:34 浏览: 10
下面是一个简单的商场信息管理系统的 C 代码示例,包含上述功能的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
#define MAX_NAME_LEN 20
#define MAX_PASS_LEN 20
#define MAX_COMMENT_LEN 100
struct Product {
char name[MAX_NAME_LEN];
float price;
int quantity;
};
struct Shop {
char name[MAX_NAME_LEN];
char address[MAX_COMMENT_LEN];
struct Product items[MAX_SIZE];
int count;
float rating;
};
struct User {
char name[MAX_NAME_LEN];
char password[MAX_PASS_LEN];
int isAdmin;
};
struct Database {
struct User users[MAX_SIZE];
int userCount;
struct Shop shops[MAX_SIZE];
int shopCount;
};
void addUser(struct Database *db, char *name, char *password, int isAdmin) {
if (db->userCount >= MAX_SIZE) {
printf("Error: database is full\n");
return;
}
struct User user;
strcpy(user.name, name);
strcpy(user.password, password);
user.isAdmin = isAdmin;
db->users[db->userCount++] = user;
}
struct User *findUser(struct Database *db, char *name, char *password) {
for (int i = 0; i < db->userCount; i++) {
if (strcmp(db->users[i].name, name) == 0 && strcmp(db->users[i].password, password) == 0) {
return &db->users[i];
}
}
return NULL;
}
void addShop(struct Database *db, struct User *user, char *name, char *address) {
if (!user->isAdmin) {
printf("Error: permission denied\n");
return;
}
if (db->shopCount >= MAX_SIZE) {
printf("Error: database is full\n");
return;
}
struct Shop shop;
strcpy(shop.name, name);
strcpy(shop.address, address);
shop.count = 0;
shop.rating = 0;
db->shops[db->shopCount++] = shop;
}
struct Shop *findShop(struct Database *db, char *name) {
for (int i = 0; i < db->shopCount; i++) {
if (strcmp(db->shops[i].name, name) == 0) {
return &db->shops[i];
}
}
return NULL;
}
void addItem(struct Database *db, struct User *user, char *shopName, char *productName, float price, int quantity) {
struct Shop *shop = findShop(db, shopName);
if (shop == NULL) {
printf("Error: shop not found\n");
return;
}
if (!user->isAdmin) {
printf("Error: permission denied\n");
return;
}
if (shop->count >= MAX_SIZE) {
printf("Error: shop is full\n");
return;
}
struct Product product;
strcpy(product.name, productName);
product.price = price;
product.quantity = quantity;
shop->items[shop->count++] = product;
}
void removeItem(struct Database *db, struct User *user, char *shopName, char *productName) {
struct Shop *shop = findShop(db, shopName);
if (shop == NULL) {
printf("Error: shop not found\n");
return;
}
if (!user->isAdmin) {
printf("Error: permission denied\n");
return;
}
int index = -1;
for (int i = 0; i < shop->count; i++) {
if (strcmp(shop->items[i].name, productName) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: product not found\n");
return;
}
for (int i = index + 1; i < shop->count; i++) {
shop->items[i - 1] = shop->items[i];
}
shop->count--;
}
void printShop(struct Shop *shop) {
printf("Shop: %s\n", shop->name);
printf("Address: %s\n", shop->address);
printf("Rating: %.1f\n", shop->rating);
printf("Products:\n");
for (int i = 0; i < shop->count; i++) {
printf("%s - $%.2f - %d in stock\n", shop->items[i].name, shop->items[i].price, shop->items[i].quantity);
}
}
void printShops(struct Database *db) {
printf("Shops:\n");
for (int i = 0; i < db->shopCount; i++) {
printf("%s\n", db->shops[i].name);
}
}
void rateShop(struct Database *db, char *shopName, float rating) {
struct Shop *shop = findShop(db, shopName);
if (shop == NULL) {
printf("Error: shop not found\n");
return;
}
if (rating < 1 || rating > 5) {
printf("Error: invalid rating\n");
return;
}
shop->rating = (shop->rating * shop->count + rating) / (shop->count + 1);
shop->count++;
}
void printShopByName(struct Database *db, char *name) {
struct Shop *shop = findShop(db, name);
if (shop == NULL) {
printf("Error: shop not found\n");
return;
}
printShop(shop);
}
void commentShop(struct Database *db, char *shopName, char *comment) {
struct Shop *shop = findShop(db, shopName);
if (shop == NULL) {
printf("Error: shop not found\n");
return;
}
printf("Comment added to shop %s: %s\n", shopName, comment);
}
int main() {
struct Database db = { .userCount = 0, .shopCount = 0 };
addUser(&db, "admin", "admin", 1);
addUser(&db, "user", "user", 0);
addShop(&db, &db.users[0], "Shop A", "Address A");
addShop(&db, &db.users[0], "Shop B", "Address B");
addItem(&db, &db.users[0], "Shop A", "Product A1", 0.99, 100);
addItem(&db, &db.users[0], "Shop A", "Product A2", 1.49, 200);
addItem(&db, &db.users[0], "Shop B", "Product B1", 1.99, 150);
printShops(&db);
struct User *user = findUser(&db, "user", "user");
if (user == NULL) {
printf("Error: user not found\n");
return 0;
}
printShopByName(&db, "Shop A");
rateShop(&db, "Shop A", 4.5);
commentShop(&db, "Shop A", "Nice shop!");
return 0;
}
```
这段代码实现了注册功能,账号和密码存储在内存中;管理员登录模块的实现,登陆进去后进行店铺信息的管理;游客登录模块,登录OK后,可以查看所有店铺信息;游客模块,可以查询自己所想去的店铺名称;游客模块也可以评价该店铺。具体实现细节可以参考代码中的注释。