IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 酒店管理系统(c++课程设计) -> 正文阅读

[C++知识库]酒店管理系统(c++课程设计)

这是一个用c++编写的一个课程设计的代码:酒店管理系统

大概实现了一下操作:对订单进行增删查改,对订单文件保存,对订单数据保存进入ordering.dat,对订单文件的销毁。

如有不足,请指正~~

Ordering.h

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;

class Ordering
{
    public:
        Ordering();
        Ordering(string , string , string , string , string);
        virtual ~Ordering();

        string GetId() { return id; }
        string GetName() { return name; }
        friend ostream& operator<<(ostream& , const Ordering& );
        friend istream& operator>>(istream& , Ordering& );


    private:
        string name;//姓名
        string tel;//电话号码
        string time;//订餐时间
        string remarks;//备注
        string id;//房间号
};

Hotel.h

#include "Ordering.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;

class Hotel 
{
public:
    Hotel();
    ~Hotel();
    bool AddOrder(Ordering);
    bool DeleteOrder(string);
    bool FindOrder(string);
    void DispAll();
    void SaveToFile();
    void ReadFromFile();
    void DestroyOrder();
    int cnt;//人数

private:
    Ordering addr[50];
   
};

Hotel.cpp

#include"Hotel.h"
#include<iostream>
#include<string>
#include<cstring>
#include <fstream>
#include<istream>
#include<vector>
using namespace std;
int N = 50;
Hotel::Hotel()
{
    cnt = 0;
}

Hotel::~Hotel()
{
    
}

bool Hotel::AddOrder(Ordering ord) {
    int i;
    for (i = 0; i < cnt; i++) {
        if (addr[i].GetId()==ord.GetId()) {
            cout << "这个已经存在,无法加入!" << endl;
            return false;
        }
    }
    if (cnt < N && i == cnt){
        addr[cnt] = ord;
        cnt++;
        cout << "订单添加成功." << endl;
        return true;
    }
    else  return false;
}

void Hotel::DispAll() {
    Ordering ord;
    cout << " 房间号" << "\t姓名" << "\t\t电话号码" << "\t\t预定时间" << "\t备注" << endl;
    for (int i = 1; i <= 54; i++)
        cout << "=";
    cout << endl;
    for (int i = 0; i < cnt; i++) {
        ord= addr[i];
        cout << ord;
    }
}

bool Hotel::DeleteOrder(string id) {
    int i;
    for (i = 0; i < cnt; i++) {
        if (addr[i].GetId()==id) {
            int j = i;
            for (; j < cnt; j++) {
                addr[j] = addr[j + 1];
            }
            cout << "这个订单的信息已经删除!" << endl;
            cnt--;
            return true;
        }
    }
    if (i == cnt) {
        cout << "这个订单没有找到,无法删除" << endl;
        return false;
    }
    return false;
}

bool Hotel::FindOrder(string id) {
    int i;
    Ordering ord;
    for (i = 0; i < cnt; i++) {
        ord = addr[i];
        if (ord.GetId()==id) {
            cout << "订单信息找到!" << endl;
            cout << " 房间号" << "\t姓名" << "\t\t电话号码" << "\t\t预定时间" << "\t备注" << endl;
            for (int i = 1; i <= 54; i++)
                cout << "=";
            cout << endl;
            //cout << sta.id << '\t' << sta.name << '\t' << sta.department << '\t' << sta.wage << '\t' << sta.position << endl;
            cout << ord;
            return true;
        }
    }
    if (i == cnt) {
        cout << "订单信息没有找到!" << endl;
        return false;
    }
    return false;
}

void Hotel::SaveToFile()
{
    ofstream ofs("ordering.dat", ios::binary | ios::out | ios::trunc);
    if (!ofs)
    {
        cerr << "Write file error." << endl;
        return;
    }
    for (int i = 0; i < cnt; i++)
        ofs.write((char*)&addr[i], sizeof(addr[i]));
    ofs.close();
    cout << "文件保存完成!" << endl;
}

void Hotel::ReadFromFile() {
    ifstream ifs("odering.dat", ios::binary | ios::in);
    if (!ifs) {
        cerr << "Open the file error." << endl;
        cout << "请创建文件:ordering.dat,开始创建..." << endl;
        ofstream ofs("odering.dat", ios::binary | ios::trunc);
        ofs.close();
        cout << "空文件创建完成!" << endl;
    }
    else {
        ifs.seekg(0, ios::end);
        long filelen = ifs.tellg();
        cnt = filelen / sizeof(Ordering);
        cout << "文件已打开,请注意修改后及时保存!!" << endl;
        ifs.seekg(0, ios::beg);
        for (int i = 0; i < cnt; i++)
            ifs.read((char*)&addr[i], sizeof(addr[i]));
        ifs.close();
    }
}

void Hotel::DestroyOrder()
{
    char ch;
    cout << "危险!!你确认要清空说有订单信息?(y/n):";

    while ((ch = getchar()) != '\n')
        continue;
    cin.get(ch);
    if (ch == 'y' || ch == 'Y')
    {
        ofstream ofs("ordering.dat", ios::binary | ios::trunc);
        ofs.close();
        cout << "订单信息已清空!" << endl;
        cnt = 0;
    }
}

Ordering.cpp

#include<iostream>
#include<string>
#include<cstring>
#include"Ordering.h"
#include<vector>

Ordering::Ordering()
{
	this->id = "";
	this->name = "";
	this->remarks = "";
	this->tel = "";
	this->time = "";
}
Ordering::Ordering(string name, string tel, string time, string remarks, string room)
{
	this->id = id;
	this->name = name;
	this->remarks = remarks;
	this->tel = tel;
	this->time = time;
}
Ordering::~Ordering(){}

ostream& operator<<(ostream& o, const Ordering& ord) {
	o << ord.id << '\t' << ord.name << "\t\t" << ord.tel << '\t' << ord.time << '\t' << ord.remarks << endl;
	return o;
}
istream& operator>>(istream& in, Ordering& ord) {
	cout << "请输入房间号:";
	in >> ord.id;
	cout << "请输入姓名:";
	in >> ord.name;
	cout << "请输入电话号码:";
	in >> ord.tel;
	cout << "请输入订餐时间:";
	in >> ord.time;
	cout << "请输入订餐备注:";
	in >> ord.remarks;
	return in;
}

main.cpp

#include"Hotel.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
const int NUM = 30;
const char password[] = "wslxc";
Hotel *hol = new Hotel();

void login() {
    int c = 3, count = 1;
    char key[NUM];
    cout << "请输入登录密码(您有三次机会):";
    cin >> key;
    while (strcmp(key, password) != 0) {
        if (count < 3)
            cout << "密码错误,您还有" << --c << "次机会,请重新输入:";
        if (count >= 3)
        {
            cout << "**抱歉,输入错误三次,退出系统**\n";
            exit(0);
        }
        cin >> key;
        count++;
    }
    cout << "密码正确,欢迎使用!\n\n";
    system("pause");
}

void ShowMainMenu()
{
    system("cls");
    cout << "请选择您的操作:" << endl;
    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 Welcome()
{
    cout << endl;
    cout << "     $-------------------------------------------------$" << endl;
    cout << "     |                                                 |" << endl;
    cout << "     |      **欢迎进入酒店订餐信息管理程序 **          | " << endl;
    cout << "     |                                                 |" << endl;
    cout << "     @-------------------------------------------------@" << endl;
}


void SelectFun(int sel) {
    Ordering *order=new Ordering();
    string id;
    switch (sel) {
    case 1:
        hol->ReadFromFile();
        system("pause");
        break;
    case 2:
        cout << "请输入要添加的订餐信息:" << endl;
        cin >> *order;
        hol->AddOrder(*order);
        system("pause");
        break;
    case 3:
        cout << "请输入要查找的房间号:";
        cin >> id;
        hol->FindOrder(id);
        system("pause");
        break;
    case 4:
        cout << "所有的订餐信息如下:" << endl;
        hol->DispAll();
        system("pause");
        break;
    case 5:
        cout << "请输入要删除的房间号:";
        cin >> id;
        hol->DeleteOrder(id);
        system("pause");
        break;
    case 6:
        hol->SaveToFile();
        system("pause");
        break;
    case 7:
        hol->DestroyOrder();
        system("pause");
        break;
    case 0:
        break;
    default:
    {
        cout << "输入错误,请重新输入!" << endl;
        system("pause");
    }
    }
}


int main()
{
    int sel = 1;
    Welcome();
    login();
    while (sel)
    {
        ShowMainMenu();
        cout << "订餐总人数:" << hol->cnt << ",请输入选择功能(0-7):";
        cin >> sel;
        cout << endl;
        SelectFun(sel);
    }
    delete hol;
    return 0;

}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-07 13:40:26  更:2021-10-07 13:42:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 2:05:59-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码