超市管理系统(课设)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//商品和用户最大限量//

#define MAX_USERS 100
#define MAX_ITEMS 100

//声明函数//
void save_users();    //储存管理员信息函数
void load_users();    //读取管理员信息函数
void save_items() ;   //储存商品信息函数 
void load_items();    //读取商品信息函数
void register_user(); //注册函数
int login();          //登录函数
void add_item() ;     //增加商品函数
void remove_item();   //删除商品函数
void sort_items( );   //商品排序函数
void find_items();    //查询商品信息函数
void display_items(); //商品库函数
void modify_item() ;  //修改商品信息函数
void buy_item();      //购物函数

//定义商品结构体

typedef struct {
    char name[50]; // 商品名称
    int quantity; // 数量
    float price; // 单价
} Item;


//定义用户结构体//

typedef struct {
    char username[50]; // 用户名
    char password[50]; // 密码
} User;


//用结构体数组储存用户、商品信息//

User users[MAX_USERS]; // 用户数组
int num_users = 0; // 用户数量

Item stock[MAX_ITEMS]; // 库存数组
int num_items = 0; // 商品数量
Item temp;//临时交换变量


//用户数据文件和商品数据文件//

const char* USER_FILE = "users.dat"; // 用户数据文件
const char* ITEM_FILE = "items.dat"; // 商品数据文件


//用户信息储存模块//

void save_users() {
    FILE* file = fopen(USER_FILE, "w");
    
    for (int i = 0; i < num_users; i++) {
        fprintf(file, "%s %s\n", users[i].username, users[i].password);
    }

    fclose(file);
}


//用户信息调用模块//

void load_users() {
    FILE* file = fopen(USER_FILE, "r");
    

    while (!feof(file)) {
        if (fscanf(file, "%s %s", users[num_users].username, users[num_users].password) != 2) {
            break;
        }
        num_users++;
    }

    fclose(file);
}

//商品信息储存模块//

void save_items() {
    FILE* file = fopen(ITEM_FILE, "w");
    

    for (int i = 0; i < num_items; i++) {
        fprintf(file, "%s %d %.2f\n", stock[i].name, stock[i].quantity, stock[i].price);
    }

    fclose(file);
}

//商品信息读取模块//

void load_items() {
    FILE* file = fopen(ITEM_FILE, "r");
    

    while (!feof(file)) {
        if (fscanf(file, "%s %d %f", stock[num_items].name, &stock[num_items].quantity, &stock[num_items].price) != 3) {
            break;
        }
        num_items++;
    }

    fclose(file);
}

//注册模块//

void register_user() {
    if (num_users >= MAX_USERS) { // 用户数组已满
        printf("\n\t\t注册失败,系统用户已满。\n");
        return;
    }

    char username[50], password[50];

    printf("\n\t\t请输入用户名:");
    scanf("%s", username);

    for (int i = 0; i < num_users; i++) { // 遍历用户数组,查找是否已存在该用户名
        if (strcmp(username, users[i].username) == 0) {
            printf("\n\t\t注册失败,该用户名已被使用。\n");
            return;
        }
    }

    printf("\t\t请输入密码:");
    scanf("%s", password);

    strcpy(users[num_users].username, username);
    strcpy(users[num_users].password, password);

num_users++;

printf("\n\t\t注册成功!\n");
save_users(); // 保存用户数据到文件


}


//登录模块//

int login() {
char username[50], password[50];
    printf("\n\n\n\n\n\n");
    printf("\t\t\t\t管理员登录");
printf("\n\t\t请输入用户名:");
scanf("%s", username);

for (int i = 0; i < num_users; i++) {
    if (strcmp(username, users[i].username) == 0) {
        printf("\t\t请输入密码:");
        scanf("%s", password);

        if (strcmp(password, users[i].password) == 0) {
            printf("\n\t\t登录成功!\n");
            return i; // 返回用户数组下标
        } else {
            printf("\n\t\t密码错误。\n");
            return -1; // 返回登录失败标志
        }
    }
}

printf("\n\t\t该用户名不存在。\n");
return -1; // 返回登录失败标志
}

//进货模块//

void add_item() {
if (num_items >= MAX_ITEMS) { // 商品数组已满
printf("\n\t\t添加商品失败,商品库存已满。\n");
return;
}

char name[50];
int quantity;
float price;

printf("\n\t\t请输入商品名称:");
scanf("%s", name);

for (int i = 0; i < num_items; i++) { // 遍历商品数组,查找是否已存在该商品
    if (strcmp(name, stock[i].name) == 0) {
        printf("\n\t\t添加商品失败,该商品已存在。\n");
        return;
    }
}

printf("\t\t请输入商品数量:");
scanf("%d", &quantity);

printf("\t\t请输入商品单价:");
scanf("%f", &price);

strcpy(stock[num_items].name, name);
stock[num_items].quantity = quantity;
stock[num_items].price = price;
num_items++;

printf("\n添加商品成功!\n");
save_items(); // 保存商品数据到文件


}

//退货模块//

void remove_item() {
char name[50];

printf("\n\t\t请输入要删除的商品名称:");
scanf("%s", name);

for (int i = 0; i < num_items; i++) {
    if (strcmp(name, stock[i].name) == 0) {
        // 删除商品,将最后一个商品覆盖当前位置,数量减一
        stock[i] = stock[num_items-1];
        num_items--;

        printf("\n\t\t删除商品成功!\n");
        save_items(); // 保存商品数据到文件
        return;
    }
}

printf("\n\t\t删除商品失败,该商品不存在。\n");
}

//商品排序模块//
void sort_items() {
    for (int i = 0; i < num_items - 1; i++) {
        int min_index = i;
        for (int j = i + 1; j < num_items; j++) {
            if (stock[j].price < stock[min_index].price) {
                min_index = j;
            }
        }
        if (min_index != i) {
            temp = stock[i];
            stock[i] = stock[min_index];
            stock[min_index] = temp;
        }
    }
}

//查询商品信息模块//
void find_items() {
    char name[50];
    printf("\n\t\t请输入要查询的商品名称:");
    scanf("%s", name);
    for (int i = 0; i < num_items; i++) {
        if (strcmp(name, stock[i].name) == 0) {
            printf("\n\t\t商品名称:%s\n\t\t商品数量:%d\n\t\t商品单价:%.2f\n", stock[i].name, stock[i].quantity, stock[i].price);
            return;
        }
    }
    printf("\n\t\t该商品不存在。\n");
}

//商品库模块//

void display_items() {
printf("\n\t\t商品列表:\n\n");
printf("\t\t名称\t数量\t单价\n");

for (int i = 0; i < num_items; i++) {
    printf("\t\t%s\t%d\t%.2f\n", stock[i].name, stock[i].quantity, stock[i].price);
}

}

//修改商品信息模块//

void modify_item() {
char name[50];
int quantity;
float price;

printf("\n\t\t请输入要修改的商品名称:");
scanf("%s", name);

for (int i = 0; i < num_items; i++) {
    if (strcmp(name, stock[i].name) == 0) {
        printf("\t\t请输入新的商品数量:");
        scanf("%d", &quantity);

        printf("\t\t请输入新的商品单价:");
        scanf("%f", &price);

        stock[i].quantity = quantity;
        stock[i].price = price;


        printf("\n\t\t修改商品成功!\n");
        save_items(); // 保存商品数据到文件
        return;
    }
}

printf("\n\t\t修改商品失败,该商品不存在。\n");
}

//购物模块//

void buy_item() {
char name[50];
int quantity;
float price;

printf("\n\t\t请输入商品名称:");
scanf("%s", name);

for (int i = 0; i < num_items; i++) {
    if (strcmp(name, stock[i].name) == 0) {
        printf("\t\t请输入要购买的商品数量:");
        scanf("%d", &quantity);

        if(quantity<=stock[i].quantity){
        

        stock[i].quantity = stock[i].quantity-quantity;
        
        

        printf("\n\t\t购买成功!\n\t\t总价为:%.2f\n",quantity*stock[i].price);
        save_items(); // 保存商品数据到文件
        return; } else{
        printf("\n\t\t库存不足,购买失败。\n");return;}
        
    }
}

printf("\n\t\t库存不足,购买失败。\n");
}

//主函数模块//

int main() {
load_users(); // 从文件中读取用户数据
load_items(); // 从文件中读取商品数据

int choice, user_index,choice1,choice2,sub_choice;
do {


    printf("\n\n\n\n\n\n");
    printf("\t\t*******************");
    printf("\t\t    超市管理系统   ");
    printf("\t\t*******************");
    printf("\n\t\t请选择你的身份:\n");
    printf("\t\t1.客人\n");
    printf("\t\t2.管理员\n");
    printf("\t\t请选择:");
    scanf("%d", &choice);
    system("cls");
   
    switch(choice){
        
    case 1:
    do{
    printf("\n\n\n\n\n\n");
    printf("\t\t*******************");
    printf("\t\t    WELCOME    ");
    printf("\t\t*******************");
    printf("\n\t\t请选择要进行的操作:\n");
    printf("\t\t1. 查看商品库\n");
    printf("\t\t2. 查找你想要的商品信息\n");
    printf("\t\t3. 请输入你要购买的商品名称\n");
    printf("\t\t4. 退出\n");
    printf("\t\t请选择:");
    scanf("%d", &choice2);
    switch(choice2)
    {
    case 1:
        sort_items( ) ;display_items();system("pause");system("cls");
        break;
    case 2:
        find_items();system("pause");system("cls");
        break;
    case 3:
        buy_item();system("pause");system("cls");
        break;
    case 4:
        break;
    default:
       printf("\n\t\t无效选择,请重新输入。\n");system("pause");system("cls"); break;
    }}while(choice2!=4);system("pause");system("cls");break;
    
    
    case 2:
    

            user_index = login();system("pause");system("cls");
            if (user_index != -1) {
                // 登录成功后进入下一层循环
                
                do {
                    printf("\n\t\t请选择要进行的操作:\n");
                    printf("\t\t1. 增加商品种类\n");
                    printf("\t\t2. 删除商品\n");
                    printf("\t\t3. 浏览商品库(价格由小到大排序)\n");
                    printf("\t\t4. 修改商品信息\n");
                    printf("\t\t5. 查询商品信息\n");
                    printf("\t\t6. 注册新的管理员账号\n");
                    printf("\t\t7. 退出\n");
                    printf("\t\t请选择:");
                    scanf("%d", &sub_choice);

                    switch (sub_choice) {
                        case 1:
                            add_item();system("pause");system("cls");
                            break;
                        case 2:
                            remove_item();system("pause");system("cls");
                            break;
                        case 3:
                            sort_items( ) ;display_items();system("pause");system("cls");
                            break;
                        case 4:
                            modify_item();system("pause");system("cls");
                            break;
                        case 5:
                            find_items();system("pause");system("cls");
                            break;
                        case 6:
                            register_user();system("pause");system("cls");
                            break;
                        case 7:
                            break;
                        default:
                            printf("\n\t\t无效选择,请重新输入。\n");system("pause");system("cls");
                            break;
                    }
                } while (sub_choice != 7);system("pause");system("cls");
            }
            break;
        
        
        
    
    default:
            printf("\n\t\t无效选择,请重新输入。\n");system("pause");system("cls");
            break;
 }}while (sub_choice != 7);

return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1、 一个三维向量类,并定义相应的特殊方法实现两个该类对象之间的加、减运算(要求支持运算符+、-),实现该类对象与标量的乘、除运算(要求支持运算符*、/),以及向量长度的计算(要求使用属性实现)。 2、 编程实现如下功能: (1)定义一个抽象类Shape,在抽象类 Shape中定义求面积 getArea()和周长 getPerimeter()的抽象方法。 (2)分别定义继承抽象类Shape的3个子类即Triangle、Rectangle、和Circle,在这3个子类中重写 Shape中的方法getArea()和 getPerimeter( )。 (3)创建类Triangle、 Rectangle、 Circle的对象,对3个类中的方法进行调用测试。 3、使用第六章(王雷春版)介绍的知识设计一个“书籍出租管理系统”,该系统包括以下功能。 (1) 菜单项“1”:显示书籍(包括书籍名称、价格和借出状态)。 (2) 菜单项“2”:增加书籍(包括书籍名称和价格)。 (3) 菜单项“3”:借出书籍(包括借出书籍名称和借出天数)。 (4) 菜单项“4”:归还书籍(包括归还书籍名称和应付的租书费)。 (5) 菜单项“5”:统计书籍(包括借出书籍册数、未借出书籍册数和总册数)。 (6) 菜单项“-1”:退出系统。 体会利用面向对象编程的思想。 4、设计一个“超市进销存管理系统”,要求如下: (1)系统包括7操作,分别是:1查询所有商品;2添加商品;3修改商品;4.删除商品;5卖出端口;6.汇总;-1.退出系统。 (2)选择操作序号“1”,显示所有商品 (3)选择操作序号“2”,添加商品包括商品名称、数量和进货价格)。 (4)选择操作序号“3”,修改商品 (5)选择操作序号“4”,删除商品 (6)选择操作序号“5”,卖出商品包括商品名称、数量和售出价格)。 (7)选择操作序号“6”,汇总当天卖出商品包括销售商品名称、数量、进货总价、销售总价等。 (8)选择操作序号“-1”,退出系统
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值