西电程序设计C++试题第五题---简单文件数据库-模拟餐馆点餐系统

涉及知识点:文件读写、内存管理、结构体定义、基本数据结构、高级格式化输入输出

要求:编写一个程序模拟餐馆点餐系统。

1)用户分为管理员和用餐者两类,分别显示不同文本格式菜单,通过菜单项对应数字进行选择

2)用餐者菜单包括菜品分类(大类)、菜品列表(含每道菜品的构成信息)、点餐、付费、查询等功能

3)管理员菜单包括菜品信息和用餐者信息录入、修改和删除。菜品信息至少应包括:编号、菜名、类别、价格等;用餐者信息至少应包括:编号、姓名、点餐信息、付费金额、用餐状态(就餐或打包)等。可根据菜品名称或编号进行菜品构成查询;可查询用餐者点餐情况、费用情况;可统计菜品的日点餐量、日营业额等(选做)。命令行参数如下:[restb -a (-u) xxxx] 第一个参数为可执行程序名称;第二个参数为用户身份,-a表示管理员,-u表示用餐者;第三个参数为用餐者名称;

思路:

一,定义菜品结构体,菜单结构体,订单结构体

二,分别写好管理员系统和用户系统和各种函数

三,将菜单内容保存为文件用于读取与修改

#include <iostream>
#include <string>
#include <fstream>

#define Max 1000

using namespace std;

// 管理员菜单
void showAdminMenu()
{
    cout << "*************************" << endl;
    cout << "*****  1、添加菜品  *****" << endl;
    cout << "*****  2、显示菜品  *****" << endl;
    cout << "*****  3、删除菜品  *****" << endl;
    cout << "*****  4、查找菜品  *****" << endl;
    cout << "*****  5、修改菜品  *****" << endl;
    cout << "*****  6、清空菜品  *****" << endl;
    cout << "*****  7、保存数据  *****" << endl;
    cout << "*****  0、退出系统  *****" << endl;
    cout << "*************************" << endl;
}

// 顾客菜单
void showUserMenu()
{
    cout << "*************************" << endl;
    cout << "*****  1、查看菜单  *****" << endl;
    cout << "*****  2、显示订单  *****" << endl;
    cout << "*****  3、删除菜品  *****" << endl;
    cout << "*****  4、结账退出  *****" << endl;
    cout << "*****  0、退出系统  *****" << endl;
    cout << "*************************" << endl;
}

// 菜品结构体
struct Dishes
{
    string Name;  // 菜名
    string No;    // 编号
    string Type;  // 类别
    double Money; // 价格
    int Count;    // 数量
};

// 菜品信息系统结构体
struct Information
{
    struct Dishes dishArray[Max]; // 保存的菜品信息数组
    int Size;                     // 菜品数量
};

struct Menu
{
    struct Dishes m_dishArray[Max]; // 菜单信息数组
    int m_Size;                     // 菜品数量
};

struct Orders
{
    struct Dishes o_dishArray[Max]; // 订单信息数组
    int o_Size;                     // 订单数量
    bool pack = false;              // 是否打包
};

// 管理端
// 读取菜品信息
void readInformation(Information *abs, const string &filename)
{
    ifstream file(filename);
    string line;
    int indexInfo = 0;
    while (getline(file, line))
    {
        size_t pos = 0;
        string token;
        int tokenIndexInfo = 0;
        while ((pos = line.find(',')) != string::npos)
        {
            token = line.substr(0, pos);
            line.erase(0, pos + 1);

            switch (tokenIndexInfo)
            {
            case 0:
                abs->dishArray[indexInfo].Name = token;
                break;
            case 1:
                abs->dishArray[indexInfo].No = token;
                break;
            case 2:
                abs->dishArray[indexInfo].Type = token;
                break;
            case 3:
                abs->dishArray[indexInfo].Money = stod(token);
                break;
            default:
                break;
            }
            tokenIndexInfo++;
        }
        abs->Size++;
        indexInfo++;
    }
    file.close();
}

//  添加菜品信息
void addDish(Information *abs)
{
    // 判断信息系统是否已满
    if (abs->Size >= Max)
    {
        cout << "信息系统已满,无法添加!" << endl;
        return;
    }
    else
    {
        // 编号
        string no;
        cout << "请输入编号: " << endl;
        cin >> no;
        abs->dishArray[abs->Size].No = no;

        // 菜名
        string name;
        cout << "请输入菜名: " << endl;
        cin >> name;
        abs->dishArray[abs->Size].Name = name;

        // 类别
        cout << "请输入类别:  " << endl;
        cout << "1 --- 荤菜" << endl;
        cout << "2 --- 素菜" << endl;
        cout << "3 --- 汤类" << endl;
        cout << "4 --- 饮料" << endl;
        int type = 0;

        while (true)
        {
            cin >> type;
            if (type == 1)
            {
                abs->dishArray[abs->Size].Type = "荤菜";
                break;
            }
            else if (type == 2)
            {
                abs->dishArray[abs->Size].Type = "素菜";
                break;
            }
            else if (type == 3)
            {
                abs->dishArray[abs->Size].Type = "汤类";
                break;
            }
            else if (type == 4)
            {
                abs->dishArray[abs->Size].Type = "饮料";
                break;
            }
            else
            {
                cout << "输入有误,请重新输入!" << endl;
            }
            return;
        }

        // 价格
        cout << "请输入单价:  " << endl;
        double money = 0;
        while (true)
        {
            cin >> money;
            if (money >= 0)
            {
                abs->dishArray[abs->Size].Money = money;
                break;
            }
            cout << "输入有误,请重新输入!" << endl;
            return;
        }

        // 更新信息系统人数
        abs->Size++;

        cout << "添加成功" << endl;

        system("pause");
        system("cls");
    }
}

// 显示菜品信息
void showInformation(Information *abs)
{
    if (abs->Size == 0)
    {
        cout << "信息系统为空" << endl;
    }
    else
    {
        for (int i = 0; i < abs->Size; i++)
        {
            cout << "编号:  " << abs->dishArray[i].No << "\t";
            cout << "菜名:  " << abs->dishArray[i].Name << "\t";
            cout << "类型:  " << abs->dishArray[i].Type << "\t";
            cout << "价格:  " << abs->dishArray[i].Money << endl;
        }
    }
    system("pause");
    system("cls");
}

// 检测菜品是否存在1
int itypeist1(Information *abs, string name)
{
    for (int i = 0; i < abs->Size; i++)
    {
        if (abs->dishArray[i].Name == name)
        {
            return i; // 找到则返回下标
        }
    }
    return -1;
}

// 检测菜品是否存在2
int itypeist2(Menu *menu, string no)
{
    for (int i = 0; i < menu->m_Size; i++)
    {
        if (menu->m_dishArray[i].No == no)
        {
            return i; // 找到则返回下标
        }
    }
    return -1;
}

// 检测菜品是否存在3
int itypeist3(Orders *Orders, string no)
{
    for (int i = 0; i < Orders->o_Size; i++)
    {
        if (Orders->o_dishArray[i].No == no)
        {
            return i; // 找到则返回下标
        }
    }
    return -1;
}

// 删除菜品信息
void deleteDishArray(Information *abs)
{
    cout << "请输入要删除的菜名:  " << endl;
    string name;
    cin >> name;

    int ret = itypeist1(abs, name);
    if (ret == -1)
    {
        cout << "查无此菜!" << endl;
    }
    else // 将菜品信息数据前移,并且信息系统数量减1
    {
        for (int i = ret; i < abs->Size; i++)
        {
            abs->dishArray[i] = abs->dishArray[i + 1];
        }
        abs->Size--;
        cout << "删除成功" << endl;
    }
    system("pause");
    system("cls");
}

// 查找菜品信息
void findDishArray(Information *abs)
{
    cout << "请输入要查找的菜名:  " << endl;
    string name;
    cin >> name;

    int ret = itypeist1(abs, name);
    if (ret == -1)
    {
        cout << "查无此菜!" << endl;
    }
    else
    {
        cout << "编号:  " << abs->dishArray[ret].No << "\t";
        cout << "菜名:  " << abs->dishArray[ret].Name << "\t";
        cout << "类型:  " << abs->dishArray[ret].Type << "\t";
        cout << "价格:  " << abs->dishArray[ret].Money << endl;
    }
    system("pause");
    system("cls");
}

// 修改菜品信息
void modifyDishArray(Information *abs)
{
    cout << "请输入要修改的菜名:  " << endl;
    string name;
    cin >> name;

    int ret = itypeist1(abs, name);
    if (ret == -1)
    {
        cout << "查无此菜!" << endl;
    }
    else
    {
        string name;
        cout << "请输入修改后的菜名:  " << endl;
        cin >> name;
        abs->dishArray[ret].Name = name;

        while (true)
        {
            int type = 0;
            cout << "请输入修改后的菜品种类:  " << endl;
            cout << "1 --- 荤菜" << endl;
            cout << "2 --- 素菜" << endl;
            cout << "3 --- 汤类" << endl;
            cout << "4 --- 饮料" << endl;
            cin >> type;
            if (type == 1)
            {
                abs->dishArray[abs->Size].Type = "荤菜";
                break;
            }
            else if (type == 2)
            {
                abs->dishArray[abs->Size].Type = "素菜";
                break;
            }
            else if (type == 3)
            {
                abs->dishArray[abs->Size].Type = "汤类";
                break;
            }
            else if (type == 4)
            {
                abs->dishArray[abs->Size].Type = "饮料";
                break;
            }
            else
            {
                cout << "输入有误,请重新输入!" << endl;
            }
        }

        cout << "请输入修改后的菜品单价:  " << endl;
        int money;
        cin >> money;
        abs->dishArray[ret].Money = money;

        cout << "修改成功!" << endl;
    }
    system("pause");
    system("cls");
}

// 保存菜品信息
void saveInformation(Information *abs, const string &filename)
{
    ofstream file(filename);

    for (int i = 0; i < abs->Size; i++)
    {
        file << abs->dishArray[i].Name << "," << abs->dishArray[i].No << ","
             << abs->dishArray[i].Type << "," << abs->dishArray[i].Money << ","
             << endl;
    }
    file.close();
}
// 清空信息系统
void cleanInformation(Information *abs)
{
    abs->Size = 0;
    cout << "清空信息系统成功!" << endl;
    system("pause");
    system("cls");
}

// 用户端
// 读取菜单数据
void readMenu(Menu *menu, const string &filename)
{
    ifstream file(filename);
    string line;
    int indexmenu = 0;
    while (getline(file, line))
    {
        size_t pos = 0;
        string token;
        int tokenIndexmenu = 0;
        while ((pos = line.find(',')) != string::npos)
        {
            token = line.substr(0, pos);
            line.erase(0, pos + 1);

            switch (tokenIndexmenu)
            {
            case 0:
                menu->m_dishArray[indexmenu].Name = token;
                break;
            case 1:
                menu->m_dishArray[indexmenu].No = token;
                break;
            case 2:
                menu->m_dishArray[indexmenu].Type = token;
                break;
            case 3:
                menu->m_dishArray[indexmenu].Money = stod(token);
                break;
            default:
                break;
            }
            tokenIndexmenu++;
        }
        menu->m_Size++;
        indexmenu++;
    }
    file.close();
}

// 展示菜单
void showDish(Menu *menu)
{
    if (menu->m_Size == 0)
    {
        cout << "信息系统为空" << endl;
    }
    else
    {
        cout << "荤菜菜单: " << endl;
        for (int i = 0; i < menu->m_Size; i++)
        {
            if ((menu->m_dishArray[i].Type) == "荤菜")
            {
                cout << "编号:  " << menu->m_dishArray[i].No << "\t";
                cout << "菜名:  " << menu->m_dishArray[i].Name << "\t";
                cout << "价格:  " << menu->m_dishArray[i].Money << endl;
            }
        }
        cout << endl;
        cout << "素菜菜单: " << endl;
        for (int i = 0; i < menu->m_Size; i++)
        {
            if ((menu->m_dishArray[i].Type) == "素菜")
            {
                cout << "编号:  " << menu->m_dishArray[i].No << "\t";
                cout << "菜名:  " << menu->m_dishArray[i].Name << "\t";
                cout << "价格:  " << menu->m_dishArray[i].Money << endl;
            }
        }
        cout << endl;
        cout << "汤类菜单: " << endl;
        for (int i = 0; i < menu->m_Size; i++)
        {
            if ((menu->m_dishArray[i].Type) == "汤类")
            {
                cout << "编号:  " << menu->m_dishArray[i].No << "\t";
                cout << "菜名:  " << menu->m_dishArray[i].Name << "\t";
                cout << "价格:  " << menu->m_dishArray[i].Money << endl;
            }
        }
        cout << endl;
        cout << "饮料菜单: " << endl;        
        for (int i = 0; i < menu->m_Size; i++)
        {
            if ((menu->m_dishArray[i].Type) == "饮料")
            {
                cout << "编号:  " << menu->m_dishArray[i].No << "\t";
                cout << "菜名:  " << menu->m_dishArray[i].Name << "\t";
                cout << "价格:  " << menu->m_dishArray[i].Money << endl;
            }
        }
    }

    system("pause");
}

// 点餐
void orderDishes(Menu *menu, Orders *orders)
{
    cout << "请输入菜品编号以订购: " << endl;
    string no;
    cin >> no;
    int ret = itypeist2(menu, no);
    if (ret == -1)
    {
        cout << "查无此菜!" << endl;
    }
    else
    {
        int count = 0;
        cout << "您希望购买多少份" << menu->m_dishArray[ret].Name << ": " << endl;
        cin >> count;
        orders->o_dishArray[orders->o_Size].No = menu->m_dishArray[ret].No;
        orders->o_dishArray[orders->o_Size].Name = menu->m_dishArray[ret].Name;
        orders->o_dishArray[orders->o_Size].Type = menu->m_dishArray[ret].Type;
        orders->o_dishArray[orders->o_Size].Money = (menu->m_dishArray[ret].Money) * (count);
        orders->o_dishArray[orders->o_Size].Count = count;
        orders->o_Size++;
        cout << "购买成功!" << endl;
    }

    system("pause");
    system("cls");
}

// 展示订单
void showOrders(Orders *orders)
{
    double sum = 0;
    if (orders->o_Size == 0)
    {
        cout << "订单为空!" << endl;
    }
    else
    {
        for (int i = 0; i < orders->o_Size; i++)
        {
            cout << "编号:  " << orders->o_dishArray[i].No << "\t";
            cout << "菜名:  " << orders->o_dishArray[i].Name << "\t";
            cout << "类型:  " << orders->o_dishArray[i].Type << "\t";
            cout << "价格:  " << orders->o_dishArray[i].Money << "\t";
            cout << "数量:  " << orders->o_dishArray[i].Count << endl;
            sum += orders->o_dishArray[i].Money;
        }
        cout << "总金额为: " << sum << endl;
    }

    system("pause");
}

// 删除订单
void deleteOrder(Orders *orders)
{
    cout << "请输入要删除的订单内菜品编号: " << endl;
    string no;
    cin >> no;
    int ret = itypeist3(orders, no);
    if (ret == -1)
    {
        cout << "查无此菜!" << endl;
    }
    else // 将菜品信息数据前移,并且信息系统数量减1
    {
        for (int i = ret; i < orders->o_Size; i++)
        {
            orders->o_dishArray[i] = orders->o_dishArray[i + 1];
        }
        orders->o_Size--;
        cout << "删除成功" << endl;
    }
    system("pause");
    system("cls");
}

int main(int argc, char *argv[])
{
    Information abs;
    Orders orders;
    Menu menu;
    string filename = "dishes.txt";
    int op;

    abs.Size = 0;

    int select = 0;

    string user = argv[2];

    if (user == "-a")
    {
        readInformation(&abs, filename);
        while (true)
        {
            showAdminMenu();

            cin >> select;

            switch (select)
            {
            case 1: // 添加
                addDish(&abs);
                break;
            case 2: // 显示
                showInformation(&abs);
                break;
            case 3: // 删除
                deleteDishArray(&abs);
                break;
            case 4: // 查找
                findDishArray(&abs);
                break;
            case 5: // 修改
                modifyDishArray(&abs);
                break;
            case 6: // 清空
                cleanInformation(&abs);
                break;
            case 7: // 保存
                saveInformation(&abs, filename);
            case 0: // 退出系统
                cout << "欢迎下次使用!" << endl;
                return 0;
                break;
            default:
                system("cls");
                break;
            }
        }

        system("pause");

        return 0;
    }
    else if (user == "-u")
    {
        readMenu(&menu, filename);
        while (true)
        {
            showUserMenu();

            cin >> select;

            switch (select)
            {
            case 1: // 显示菜单并点餐
                showDish(&menu);

                orderDishes(&menu, &orders);
                break;
            case 2: // 显示订单
                showOrders(&orders);
                system("cls");
                break;
            case 3: // 删除
                showOrders(&orders);
                deleteOrder(&orders);
                break;
            case 4: // 结账并退出系统
                showOrders(&orders);
                cout << "堂食或者打包: " << endl;
                cout << "1---堂食" << endl;
                cout << "2---打包" << endl;
                cin >> op;
                if (op == 1)
                {
                    orders.pack == 0;
                }
                else if (op == 2)
                {
                    orders.pack == 1;
                }
                else
                {
                    cout << "输入有误,请重新输入!" << endl;
                    system("pause");
                    system("cls");
                    break;
                }
                cout << "欢迎下次使用!" << endl;
                return 0;
                break;
            case 0: // 退出系统
                cout << "欢迎下次使用!" << endl;
                return 0;
                break;
            default:
                system("cls");
                break;
            }
        }
        system("pause");
        return 0;
    }
}

仅供参考

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值