“商场信息管理系统”
前言
本文为大一小白期末课程设计作业,如果有同样的课设的小伙伴希望能给你有所帮助,如果产生了一点帮助期望你能给我一个点赞或关注谢谢。
一、项目描述
商场信息管理系统是一个模拟商场管理(如:万达广场、吾悦广场等)。该系统分为2个模块,管理员模块和游客模块。首先实现注册和登录模块;管理员模块是实现商场店铺的管理,采用树来存储商场店铺信息,通过分类来处理,如服装店、餐饮点,然后服装店又分为童装、女装等;游客模块是实现知道商场内所有店铺的信息,输入熟悉店铺名称看商场内有没有等等
二、功能实现
1. 注册功能的实现,账号和密码存储在内存中;
代码如下:
void saveAccountsToFile(Account* accounts, int count, char* filename)
{//账号存入文件函数
FILE* file = fopen(filename, "w");
if (file == NULL)
{
printf("无法打开文件 %s\n", filename);
return;
}
int i;
for (i = 0; i < count; i++)
{
fprintf(file, "%s %s\n", accounts[i].account, accounts[i].password);
}
fclose(file);
}
void loadAccountsFromFile(Account* accounts, int count, char* filename)
{//读取文件中的信息
FILE* file = fopen(filename, "r");
if (file == NULL)
{
printf("无法打开文件 %s\n", filename);
return;
}
int i;
for (i = 0; i < count; i++)
{
fscanf(file, "%s %s", accounts[i].account, accounts[i].password);
}
fclose(file);
}
void inputAccountsFromUser(Account* accounts, int count)
{//注册用户
int i;
for (i=0; i < count; i++)
{
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
printf("<\t请输入账号:\t\t\t<\n" );
scanf("%s", accounts[i].account);
printf("<\t请输入密码:\t\t\t<\n");
scanf("%s", accounts[i].password);
printf("<\t注册成功!\t\t\t<\n\n\n");
}
}
此处使用的文件为accounts.txt,使用时注意要先创建一个accounts.txt文件。
2.登录模块的实现
代码如下:
int login(Account* accounts, int count) //登录系统
{
char inputAccount[20];
char inputPassword[20];
int i,j,k;
int loggedIn = 0; // 登录状态标志变量,默认为登录失败
for (i = 0; i < count; i++)
{
printf("<\t请输入账号:\t\t\t<\n");
scanf("%s", inputAccount);
printf("<\t请输入密码:\t\t\t<\n");
scanf("%s", inputPassword);
for(j=0;j<100;j++)
{
if(strcmp(inputAccount, accounts[j].account) == 0 && strcmp(inputPassword, accounts[j].password) == 0)
{
loggedIn = 1; // 将登录状态标志变量设置为登录成功
printf("<\t密码正确!登陆成功!\t\t<\n");
printf("<\t请输入你的选项:\t\t<\n");
printf("<\t3.管理者模式:\t\t\t<\n");
printf("<\t4.游客模式:\t\t\t<\n:");
scanf("%d",&j);
if(j==3)
{
TreeNode* root = NULL;
// 添加商品信息
Product product1 = {"苹果", 6.5, "水果"};
insertProduct(&root, &product1);
Product product2 = {"香蕉", 3.0, "水果"};
insertProduct(&root, &product2);
Product product3 = {"橙子", 4.0, "水果"};
insertProduct(&root, &product3);
Product product4 = {"笔记本电脑", 5000.0, "电子产品"};
insertProduct(&root, &product4);
Product product5 = {"手机", 3000.0, "电子产品"};
insertProduct(&root, &product5);
printf("<\t欢迎进入管理者模式!\t\t<\n");
printf("<\t现有商品有:\t\t\t<\n");
printProducts(root);
printf("<\t请输入你的功能:\t\t<\n");
printf("<\t1.添加新商品:\t\t\t<\n");
printf("<\t2.删除新商品:\t\t\t<\n");
scanf("%d",&k);
if(k==1)
{
Product newProduct;//定义新的节点
inputProduct(&newProduct);//写入商品信息
insertProduct(&root, &newProduct);//插入到树中并排序
// 打印商品列表
printProducts(root);
}
if(k==2)
{
// 删除商品信息
char deleteName[50];
findProduct2(root);
printf("<\t请再输入一次\t\t\t<\n") ;
scanf("%s", deleteName);
root = deleteProduct(root, deleteName);
printf("<\t删除后的商品列表:\t\t<\n");
// 打印修改后的商品列表
printProducts(root);
}
}
else if(j==4)
{
TreeNode* root = NULL;
guestMode(root);
}
else
{
printf("<\t输入错误!将返回上一菜单!请重试!\t\t\t\n");
return 0;
}
}
}
if (!loggedIn) // 如果没有找到匹配的账号和密码,即为登录失败
printf("登录失败,请检查您的账号和密码。\n");
}
}
其中使用了许多别的函数,如删除函数,查找函数,打印函数等等,下面会写到。
3.游客模式的实现
代码如下:
// 游客模式
void guestMode(TreeNode* root) {
// 添加初始商品
Product product1 = {"苹果", 6.5, "水果",0};
insertProduct(&root, &product1);
Product product2 = {"香蕉", 3.0, "水果",0};
insertProduct(&root, &product2);
Product product3 = {"橙子", 4.0, "水果",0};
insertProduct(&root, &product3);
Product product4 = {"笔记本电脑", 5000.0, "电子产品",0};
insertProduct(&root, &product4);
Product product5 = {"手机", 3000.0, "电子产品",0};
insertProduct(&root, &product5);
while (1) {
int choice;
printf("\n<\t请选择您的操作:\t\t<\n");
printf("<\t0.返回登录页面\t\t\t<\n");
printf("<\t1.查看所有商品\t\t\t<\n");
printf("<\t2.搜索商品\t\t\t<\n");
printf("<\t3.评价商品\t\t\t<\n");
printf("<\t请输入您的选择:\t\t<\n");
scanf("%d", &choice);
if (choice == 1)
{
printf("<\t\n商品清单:\t\t\t<\n");
printProducts(root);
}
else if (choice == 2)
{
findProduct1(root);
}
else if (choice == 0) {
break;
}
else if(choice==3)
{
rateProductMenu(root);
}
else
{
printf("<\t无效的选项!请重新输入。\t\t<\n");
}
}
}
4.其中的一些重要函数
代码如下:
void insertProduct(TreeNode** root, Product* product) //排序树
{//插入商品并且分类
if (*root == NULL) //判空
{//如果空
//创建一个新的树节点作为根节点,并将商品信息复制到节点的 product 字段中。
*root = (TreeNode*)malloc(sizeof(TreeNode));
(*root)->product = *product;
(*root)->left = NULL;//定义为空
(*root)->right = NULL;
}
else
{//比较商品名称
if (product->name< (*root)->product.name)
{// 若商品名称小于当前节点的名称,则继续在左子树中插入
insertProduct(&((*root)->left), product);
}
else if (product->name> (*root)->product.name)
{//如上
insertProduct(&((*root)->right), product);
}
else
{
// 商品名称相同,根据商品分类决定插入到左子树或右子树
if (product->category <(*root)->product.category)
{
insertProduct(&((*root)->left), product);
} else
{
insertProduct(&((*root)->right), product);
}
}
}
}
void inputProduct(Product* product) //输入商品
{//管理者管理商品函数与下方循环连用
printf("请输入商品名称:");
scanf("%s", product->name);
printf("请输入商品价格:");
scanf("%f", &(product->price));
printf("请输入商品分类:");
scanf("%s", product->category);
}
void printProducts(TreeNode* root)
{ //打印商品 用于给游客看商品
// 添加商品信息
if (root != NULL)
{
printProducts(root->left);
printf("商品分类:%s\n", root->product.category);
printf("商品名称:%s\n", root->product.name);
printf("商品价格:%.2f\n", root->product.price);
printf("商品评分:%d\n", root->product.rating);
printf("\n");
printProducts(root->right);
}
}
TreeNode* findMin(TreeNode* node) {
if (node->left == NULL) {
return node;
} else {
return findMin(node->left);
}
}
TreeNode* deleteProduct(TreeNode* root, char* name) {
if (root == NULL) {
return root;
}
int cmp = strcmp(name, root->product.name);
if (cmp < 0) {
root->left = deleteProduct(root->left, name);
} else if (cmp > 0) {
root->right = deleteProduct(root->right, name);
} else {
if (root->left == NULL && root->right == NULL) {
// 叶子节点,直接删除
free(root);
root = NULL;
} else if (root->left == NULL) {
// 只有右子树,用右子树替换当前节点
TreeNode* temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL) {
// 只有左子树,用左子树替换当前节点
TreeNode* temp = root;
root = root->left;
free(temp);
} else {
// 左右子树都存在,找到右子树中最小的节点替换当前节点
TreeNode* minNode = findMin(root->right);
root->product = minNode->product;
root->right = deleteProduct(root->right, minNode->product.name);
}
}
return root;
}
TreeNode* searchProduct(TreeNode* root, char* name) //二叉搜索树查找函数的第一部分
{
if (root == NULL || strcmp(name, root->product.name) == 0)
{//如果结点到头了或者找到了 则直接返回当前节点
return root;
}
if (strcmp(name, root->product.name) < 0)
//如果给定的商品名称小于当前节点的商品名称,则继续在左子树中递归搜索
{
return searchProduct(root->left, name);
}
else
{
return searchProduct(root->right, name);
}
}
void findProduct1(TreeNode* root) //查找函数的第二部分 //游客版
{
char name[50];
printf("请输入要查找的商品名称:");
scanf("%s", name);
TreeNode* result = searchProduct(root, name);//调用第一部分
if (result != NULL)
{
printf("找到该商品:\n");
printf("<\t商品名称:%s\t\t\t<\n", result->product.name);
printf("<\t商品价格:%.2f\t\t\t<\n", result->product.price);
printf("<\t商品分类:%s\t\t\t<\n", result->product.category);
printf("<\t商品评价:%d\t\t\t<\n", result->product.rating);
}
else
{
printf("未找到该商品。\n");
}
}
void findProduct2(TreeNode* root) //查找函数的第二部分 //管理者版
{
char name[50];
printf("<\t请输入要查找的商品名称:\t<\n");
scanf("%s", name);
TreeNode* result = searchProduct(root, name);//调用第一部分
if (result != NULL)
{
printf("<\t找到该商品:\t\t\t<\n");
}
else
{
printf("<\t未找到该商品。\t\t\t<\n");
}
}
void rateProduct(TreeNode* root, const char* name, int rating)
{
TreeNode* node = searchProduct(root, name);
if (node != NULL)
{
node->product.rating = rating;
printf("<\t成功评价商品。\t\t\t<\n");
}
else
{
printf("<\t未找到该商品。\t\t\t<\n");
}
}
void rateProductMenu(TreeNode* root) //游客评价函数
{
char name[50];
int rating;
printf("<\t请输入要评价的商品名称:\t\t<\n");
scanf("%s", name);
printf("<\t请输入评分(0-5):\t\t\t<\n");
scanf("%d", &rating);
rateProduct(root, name, rating);
}
int menu(Account *accounts[])
{
int num;
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
printf("<\t欢迎使用商场信息管理系统!\t<\n");
printf("<\t请输入您的选项:\t\t<\n");
printf("<\t1.注册新用户:\t\t\t<\n");
printf("<\t2.用户登录:\t\t\t<\n");
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
scanf("%d",&num);
switch(num)//switch选择1 2
{
case 1:
inputAccountsFromUser(accounts, 1);
return;
case 2:
login(accounts, 1);
break;
default:
puts("输入错误,请重试");
return;
}
}
三、源代码
代码如下:
#include <stdio.h>
#include <string.h>
typedef struct
{//用户数组结构体//登录注册
char account[20];
char password[20];
} Account;
typedef struct
{//商品树,管理者
char name[50];
float price;
char category[50]; // 添加商品分类字段
int rating;
} Product;
typedef struct TreeNode //树结构体
{
Product product;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
void insertProduct(TreeNode** root, Product* product) //排序树
{//插入商品并且分类
if (*root == NULL) //判空
{//如果空
//创建一个新的树节点作为根节点,并将商品信息复制到节点的 product 字段中。
*root = (TreeNode*)malloc(sizeof(TreeNode));
(*root)->product = *product;
(*root)->left = NULL;//定义为空
(*root)->right = NULL;
}
else
{//比较商品名称
if (product->name< (*root)->product.name)
{// 若商品名称小于当前节点的名称,则继续在左子树中插入
insertProduct(&((*root)->left), product);
}
else if (product->name> (*root)->product.name)
{//如上
insertProduct(&((*root)->right), product);
}
else
{
// 商品名称相同,根据商品分类决定插入到左子树或右子树
if (product->category <(*root)->product.category)
{
insertProduct(&((*root)->left), product);
} else
{
insertProduct(&((*root)->right), product);
}
}
}
}
void inputProduct(Product* product) //输入商品
{//管理者管理商品函数与下方循环连用
printf("请输入商品名称:");
scanf("%s", product->name);
printf("请输入商品价格:");
scanf("%f", &(product->price));
printf("请输入商品分类:");
scanf("%s", product->category);
}
void printProducts(TreeNode* root)
{ //打印商品 用于给游客看商品
// 添加商品信息
if (root != NULL)
{
printProducts(root->left);
printf("商品分类:%s\n", root->product.category);
printf("商品名称:%s\n", root->product.name);
printf("商品价格:%.2f\n", root->product.price);
printf("商品评分:%d\n", root->product.rating);
printf("\n");
printProducts(root->right);
}
}
TreeNode* findMin(TreeNode* node) {
if (node->left == NULL) {
return node;
} else {
return findMin(node->left);
}
}
TreeNode* deleteProduct(TreeNode* root, char* name) {
if (root == NULL) {
return root;
}
int cmp = strcmp(name, root->product.name);
if (cmp < 0) {
root->left = deleteProduct(root->left, name);
} else if (cmp > 0) {
root->right = deleteProduct(root->right, name);
} else {
if (root->left == NULL && root->right == NULL) {
// 叶子节点,直接删除
free(root);
root = NULL;
} else if (root->left == NULL) {
// 只有右子树,用右子树替换当前节点
TreeNode* temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL) {
// 只有左子树,用左子树替换当前节点
TreeNode* temp = root;
root = root->left;
free(temp);
} else {
// 左右子树都存在,找到右子树中最小的节点替换当前节点
TreeNode* minNode = findMin(root->right);
root->product = minNode->product;
root->right = deleteProduct(root->right, minNode->product.name);
}
}
return root;
}
TreeNode* searchProduct(TreeNode* root, char* name) //二叉搜索树查找函数的第一部分
{
if (root == NULL || strcmp(name, root->product.name) == 0)
{//如果结点到头了或者找到了 则直接返回当前节点
return root;
}
if (strcmp(name, root->product.name) < 0)
//如果给定的商品名称小于当前节点的商品名称,则继续在左子树中递归搜索
{
return searchProduct(root->left, name);
}
else
{
return searchProduct(root->right, name);
}
}
void findProduct1(TreeNode* root) //查找函数的第二部分 //游客版
{
char name[50];
printf("请输入要查找的商品名称:");
scanf("%s", name);
TreeNode* result = searchProduct(root, name);//调用第一部分
if (result != NULL)
{
printf("找到该商品:\n");
printf("<\t商品名称:%s\t\t\t<\n", result->product.name);
printf("<\t商品价格:%.2f\t\t\t<\n", result->product.price);
printf("<\t商品分类:%s\t\t\t<\n", result->product.category);
printf("<\t商品评价:%d\t\t\t<\n", result->product.rating);
}
else
{
printf("未找到该商品。\n");
}
}
void findProduct2(TreeNode* root) //查找函数的第二部分 //管理者版
{
char name[50];
printf("<\t请输入要查找的商品名称:\t<\n");
scanf("%s", name);
TreeNode* result = searchProduct(root, name);//调用第一部分
if (result != NULL)
{
printf("<\t找到该商品:\t\t\t<\n");
}
else
{
printf("<\t未找到该商品。\t\t\t<\n");
}
}
// 游客模式
void guestMode(TreeNode* root) {
// 添加初始商品
Product product1 = {"苹果", 6.5, "水果",0};
insertProduct(&root, &product1);
Product product2 = {"香蕉", 3.0, "水果",0};
insertProduct(&root, &product2);
Product product3 = {"橙子", 4.0, "水果",0};
insertProduct(&root, &product3);
Product product4 = {"笔记本电脑", 5000.0, "电子产品",0};
insertProduct(&root, &product4);
Product product5 = {"手机", 3000.0, "电子产品",0};
insertProduct(&root, &product5);
while (1) {
int choice;
printf("\n<\t请选择您的操作:\t\t<\n");
printf("<\t0.返回登录页面\t\t\t<\n");
printf("<\t1.查看所有商品\t\t\t<\n");
printf("<\t2.搜索商品\t\t\t<\n");
printf("<\t3.评价商品\t\t\t<\n");
printf("<\t请输入您的选择:\t\t<\n");
scanf("%d", &choice);
if (choice == 1)
{
printf("<\t\n商品清单:\t\t\t<\n");
printProducts(root);
}
else if (choice == 2)
{
findProduct1(root);
}
else if (choice == 0) {
break;
}
else if(choice==3)
{
rateProductMenu(root);
}
else
{
printf("<\t无效的选项!请重新输入。\t\t<\n");
}
}
}
void rateProduct(TreeNode* root, const char* name, int rating)
{
TreeNode* node = searchProduct(root, name);
if (node != NULL)
{
node->product.rating = rating;
printf("<\t成功评价商品。\t\t\t<\n");
}
else
{
printf("<\t未找到该商品。\t\t\t<\n");
}
}
void rateProductMenu(TreeNode* root) //游客评价函数
{
char name[50];
int rating;
printf("<\t请输入要评价的商品名称:\t\t<\n");
scanf("%s", name);
printf("<\t请输入评分(0-5):\t\t\t<\n");
scanf("%d", &rating);
rateProduct(root, name, rating);
}
void saveAccountsToFile(Account* accounts, int count, char* filename)
{//账号存入文件函数
FILE* file = fopen(filename, "w");
if (file == NULL)
{
printf("无法打开文件 %s\n", filename);
return;
}
int i;
for (i = 0; i < count; i++)
{
fprintf(file, "%s %s\n", accounts[i].account, accounts[i].password);
}
fclose(file);
}
void loadAccountsFromFile(Account* accounts, int count, char* filename)
{//读取文件中的信息
FILE* file = fopen(filename, "r");
if (file == NULL)
{
printf("无法打开文件 %s\n", filename);
return;
}
int i;
for (i = 0; i < count; i++)
{
fscanf(file, "%s %s", accounts[i].account, accounts[i].password);
}
fclose(file);
}
void inputAccountsFromUser(Account* accounts, int count)
{//注册用户
int i;
for (i=0; i < count; i++)
{
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
printf("<\t请输入账号:\t\t\t<\n" );
scanf("%s", accounts[i].account);
printf("<\t请输入密码:\t\t\t<\n");
scanf("%s", accounts[i].password);
printf("<\t注册成功!\t\t\t<\n\n\n");
}
}
int login(Account* accounts, int count) //登录系统
{
char inputAccount[20];
char inputPassword[20];
int i,j,k;
int loggedIn = 0; // 登录状态标志变量,默认为登录失败
for (i = 0; i < count; i++)
{
printf("<\t请输入账号:\t\t\t<\n");
scanf("%s", inputAccount);
printf("<\t请输入密码:\t\t\t<\n");
scanf("%s", inputPassword);
for(j=0;j<100;j++)
{
if(strcmp(inputAccount, accounts[j].account) == 0 && strcmp(inputPassword, accounts[j].password) == 0)
{
loggedIn = 1; // 将登录状态标志变量设置为登录成功
printf("<\t密码正确!登陆成功!\t\t<\n");
printf("<\t请输入你的选项:\t\t<\n");
printf("<\t3.管理者模式:\t\t\t<\n");
printf("<\t4.游客模式:\t\t\t<\n:");
scanf("%d",&j);
if(j==3)
{
TreeNode* root = NULL;
// 添加商品信息
Product product1 = {"苹果", 6.5, "水果"};
insertProduct(&root, &product1);
Product product2 = {"香蕉", 3.0, "水果"};
insertProduct(&root, &product2);
Product product3 = {"橙子", 4.0, "水果"};
insertProduct(&root, &product3);
Product product4 = {"笔记本电脑", 5000.0, "电子产品"};
insertProduct(&root, &product4);
Product product5 = {"手机", 3000.0, "电子产品"};
insertProduct(&root, &product5);
printf("<\t欢迎进入管理者模式!\t\t<\n");
printf("<\t现有商品有:\t\t\t<\n");
printProducts(root);
printf("<\t请输入你的功能:\t\t<\n");
printf("<\t1.添加新商品:\t\t\t<\n");
printf("<\t2.删除新商品:\t\t\t<\n");
scanf("%d",&k);
if(k==1)
{
Product newProduct;//定义新的节点
inputProduct(&newProduct);//写入商品信息
insertProduct(&root, &newProduct);//插入到树中并排序
// 打印商品列表
printProducts(root);
}
if(k==2)
{
// 删除商品信息
char deleteName[50];
findProduct2(root);
printf("<\t请再输入一次\t\t\t<\n") ;
scanf("%s", deleteName);
root = deleteProduct(root, deleteName);
printf("<\t删除后的商品列表:\t\t<\n");
// 打印修改后的商品列表
printProducts(root);
}
}
else if(j==4)
{
TreeNode* root = NULL;
guestMode(root);
}
else
{
printf("<\t输入错误!将返回上一菜单!请重试!\t\t\t\n");
return 0;
}
}
}
if (!loggedIn) // 如果没有找到匹配的账号和密码,即为登录失败
printf("登录失败,请检查您的账号和密码。\n");
}
}
int menu(Account *accounts[])
{
int num;
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
printf("<\t欢迎使用商场信息管理系统!\t<\n");
printf("<\t请输入您的选项:\t\t<\n");
printf("<\t1.注册新用户:\t\t\t<\n");
printf("<\t2.用户登录:\t\t\t<\n");
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
scanf("%d",&num);
switch(num)//switch选择1 2
{
case 1:
inputAccountsFromUser(accounts, 1);
return;
case 2:
login(accounts, 1);
break;
default:
puts("输入错误,请重试");
return;
}
}
int main() {
Account accounts[100];
loadAccountsFromFile(accounts, 100, "accounts.txt");
// 输出读取到的账号信息
int i,a;
/*for (i = 0; i < 100; i++)
{
printf("账号:%s 密码:%s\n", accounts[i].account, accounts[i].password);
}*/
// 将账号信息存入文件
saveAccountsToFile(accounts, 100, "accounts.txt");
// 用户输入账号密码
for(a=0;a<100;a++)
menu(accounts);
return 0;
}
总结
以上就是今天要讲的内容,代码很多不完善的地方,欢迎斧正!