50

请大佬讲解一下这段代码的大概思路。。。课程设计想要参考一下

#include //指示编译器将文件iostream中的代码嵌入到该程序中该指令所在的地方

using namespace std;

#include //包含文件流头文件
#define BUFSIZE 50 //定义BUFSIZE 的值为50
class mGuestroom;

template

class cNode //节点
{
public:
T data;
cNode *next; //到下一个节点
};

template //创建模板
class tList
{
public: //定义成员函数
tList();
void Create(); //创建链表
bool Empty() const; //判断链表是否为空
void Insert(const T e); //从尾部插入一个元素
cNode* GetcNode(int i); //返回第i个节点
bool Find(const T e); //在链表中查找某个值
~tList(); //销毁链表,析构
cNode *head; //头节点
};

template
tList::tList() //类的成员函数的实现
{
head = new cNode; //头节点
head->next = NULL; //以null为尾节点
}

template
void tList::Create()
{
cNode *p;
p = head;
p->next = NULL;
}

template
bool tList::Empty() const
{
return (head->next == NULL);
}

template
void tList::Insert(const T e) //插入
{
cNode *p, *q;
p = head;
q = new cNode;
q->data = e;
while (p->next)
{
p = p->next;
}
p->next = q;
q->next = NULL;
}

template
cNode* tList::GetcNode(int i) //返回第i个节点
{
int k = 0;
cNode *p;
p = head;
while (p && k < i)
{
p = p->next;
++k;
}
return p;
}

template
bool tList::Find(const T e) //在链表中查找某个值
{
bool flag = false;
cNode *p;
p = head->next;
while (p)
{
if (p->data == e)
{
flag = true;
break;
}
p = p->next;
}
return flag;
}

template
tList::~tList()
{
cNode *p;
while (head)
{
p = head->next;
delete head;
head = p;
}
}

class mGuestroom
{
public:
char ID[BUFSIZE]; //客房编号
char Type[BUFSIZE]; //客房类型
char Book[BUFSIZE]; //提前定房日期
char Price[BUFSIZE]; //客房价格
char Function[BUFSIZE]; //客房主要内容
int StayTime; //入住天数有效期
bool flag;//标志
mGuestroom()//构造函数初始化
{
memset(ID, 0, BUFSIZE); memset(Type, 0, BUFSIZE); memset(Book, 0, BUFSIZE); memset(Function, 0, BUFSIZE);
memset(Price, 0, BUFSIZE); StayTime = 0; flag = false;
} //memset:把char类型转换成ASCII值
};

void ReadData(tList *tListGue)//读取数据库
{
char buf[BUFSIZE];
ifstream ifile("Guestroom.txt", ios_base::in); //创建一个输出文件流对象
if (!ifile)
{
cerr << "读取时打开文件出错!" << endl;
}
mGuestroom Guestroom;
while (ifile.getline(buf, BUFSIZE)) //遍历文本,读取数据
{
ifile.getline(buf, BUFSIZE);
strcpy(Guestroom.ID, buf);//读取客房编号
memset(buf, 0, BUFSIZE);

    ifile.getline(buf, BUFSIZE);//读取客房类型
    strcpy(Guestroom.Type, buf);
    memset(buf, 0, BUFSIZE);

    ifile.getline(buf, BUFSIZE);//读取提前定房日期
    strcpy(Guestroom.Book, buf);
    memset(buf, 0, BUFSIZE);

    ifile.getline(buf, BUFSIZE);//读取价格
    strcpy(Guestroom.Price, buf);
    memset(buf, 0, BUFSIZE);

    ifile.getline(buf, BUFSIZE);//读取客房主要内容
    strcpy(Guestroom.Function, buf);
    memset(buf, 0, BUFSIZE);

    ifile.getline(buf, BUFSIZE);//读取入住有效期
    Guestroom.StayTime = atoi(buf);
    memset(buf, 0, BUFSIZE);

    Guestroom.flag = true;
    tListGue->Insert(Guestroom);
}
cout << "读取数据成功!\n\n";
ifile.close();//关闭文件
return;

}

void AddtheGuestroom(tList *tListGue)// 输入客房函数
{
mGuestroom tempGuestroom; //定义临时客房
cout << "下面开始添加客房信息\n";
cout << "请输入入住的客房编号:";
cin >> tempGuestroom.ID; //读入客房编号
cout << "请输入客房类型:";
cin >> tempGuestroom.Type; //读入客房类型
cout << "请输入客人定房日期:";
cin >> tempGuestroom.Book; //读入定房日期
cout << "请输入客房价格:";
cin >> tempGuestroom.Price; //读入价格
cout << "请输入客房的主要内容:";
cin >> tempGuestroom.Function; //读入主要内容
cout << "请输入客房入住有效期:";
cin >> tempGuestroom.StayTime; //读入入住有效期
tempGuestroom.flag = true;
tListGue->Insert(tempGuestroom);
cout << "添加成功!\n";
system("pause"); //pause:停顿,终止
system("cls");
}

void SaveData(tList *tListGue) //保存数据
{
int i = 0;
ofstream ofile("Guestroom.txt", ios_base::out); //创建一个输入文件流对象
if (!ofile)
{
cerr << "保存时打开文件出错!" << endl;
}
cNode *p;
p = tListGue->head->next;
while (p)
{
//从内存中读取数据,存到外盘
if (p->data.flag)
{
ofile << "\n";
ofile << (p->data).ID << "\n";
ofile << (p->data).Type << "\n";
ofile << (p->data).Book << "\n";
ofile << (p->data).Price << "\n";
ofile << (p->data).Function << "\n";
ofile << (p->data).StayTime << "\n";
}
p = p->next;
}
cout << "保存数据成功!\n";
ofile.close();
system("pause");
system("cls");
return;
}

void ModifytheGuestroom(tList *tListGue) //修改客房信息
{
char id[BUFSIZE];
cout << "\n请输入你要查找的客房编号:";
cin >> id; //读取客房编号
bool flag = false;
cNode *p;
p = tListGue->head->next;
while (p)
{
if (strcmp(p->data.ID, id) == 0 && p->data.flag)
{
flag = true;
break;
}
p = p->next;
}
if (flag == true)
{//显示屏幕
cout << "找到了,该客房的原始信息是:\n";
cout << "\n\n客房编号:" << (p->data).ID << "\t客房类型:" << (p->data).Type << "\n\t\t定房日期:" << (p->data).Book << "\n\t\t价格:" << (p->data).Price;
cout << "\n\t\t客房的主要内容:" << (p->data).Function << "\n\t\t入住有效期:" << (p->data).StayTime;

    cout << "\n下面开始修改该客房的信息:\n";
    mGuestroom tempGuestroom;            //定义临时客房
    cout << "请输入客房的客房编号:";
    cin >> tempGuestroom.ID;               //读入客房编号
    cout << "请输入客房的客房类型:";
    cin >> tempGuestroom.Type;             //读入客房类型
    cout << "请输入客人的定房日期:";
    cin >> tempGuestroom.Book;             //读入定房日期
    cout << "请输入客房的客房价格:";
    cin >> tempGuestroom.Price;            //读入价格
    cout << "请输入客房的客房主要内容:";
    cin >> tempGuestroom.Function;         //读入客房的主要内容
    cout << "请输入客人入住客房有效期:";
    cin >> tempGuestroom.StayTime;         //读入入住有效期
    tempGuestroom.flag = true;
    p->data = tempGuestroom;
    cout << "修改成功!";
}
cout << endl;
system("pause");
system("cls");
return;

}

void DeletetheGuestroom(tList *tListGue) //删除客房信息
{
char id[BUFSIZE];
cout << "\n请输入你要查找的客房编号:";
cin >> id; //读取客房编号
bool flag = false;
int count = 0;
cNode *p;
p = tListGue->head->next;
while (p)
{
if (strcmp(p->data.ID, id) == 0 && p->data.flag)
{
count++;
flag = true;
break;
}
p = p->next;
}
if (flag == false) cout << "对不起,你所查找的客房延长了住房日期!\n";
else

{
count--;
cout << "找到了,该客房的原始信息是:\n";
cout << "\n\n客房编号:" << (p->data).ID << "\t客房类型:" << (p->data).Type << "\n\t\t定房日期:" << (p->data).Book << "\n\t\t价格:" << (p->data).Price;
cout << "\n\t\t客房的主要内容:" << (p->data).Function << "\n\t\t入住有效期:" << (p->data).StayTime;
cout << endl;
(p->data).flag = false;
cout << "该客房的信息删除成功!\n";
}
cout << endl;
system("pause");
system("cls");
return;
}

void FindtheremainGuestroomById(tList *tListGue)//根据客房编号查找
{
char id[BUFSIZE];
cout << "\n请输入你要查找的客房编号:";
cin >> id; //读取客房编号
bool flag = false;
cNode *p;
p = tListGue->head->next;
while (p)
{
if (strcmp(p->data.ID, id) == 0 && p->data.flag)
{
flag = true;
break;
}
p = p->next;
}

if (flag == false)     cout << "对不起,没有该标号的客房!\n";   //如果找不到
else                                                                 //如果找到了
{
    cout << "\n找到了,该客房的信息是:\n";
    cout << "\n\n客房编号:" << (p->data).ID << "\t客房类型:" << (p->data).Type << "\n\t\t定房日期:" << (p->data).Book << "\n\t\t价格:" << (p->data).Price;
    cout << "\n\t\t主要内容:" << (p->data).Function << "\n\t\t入住有效期:" << (p->data).StayTime;
}
cout << endl;
system("pause");
system("cls");                       //清屏
return;

}

void FindtheremainGuestroomByType(tList *tListGue)//根据客房类型查找客房
{
char type[BUFSIZE];
cout << "\n请输入你要查找的客房类型:";
cin >> type; //读取客房类型
bool flag = false;
cNode *p;
p = tListGue->head->next;
while (p)
{
if (strcmp(p->data.Type, type) == 0 && p->data.flag)
{
flag = true;
break;
}
p = p->next;
}

if (flag == false)     cout << "对不起,没有该类型的客房!\n";     //如果满了
else                                                      //如果找到了
{
    cout << "\n找到了,该客房信息是:\n";
    cout << "\n\n客房编号:" << (p->data).ID << "\t客房类型:" << (p->data).Type << "\n\t\t定房日期:" << (p->data).Book << "\n\t\t价格:" << (p->data).Price;
    cout << "\n\t\t客房的主要内容:" << (p->data).Function << "\n\t\t入住有效期:" << (p->data).StayTime;
}
cout << endl;
system("pause");
system("cls");
return;

}

void FindtheremainGuestroomByPrice(tList *tListGue)//根据价格查找客房
{
char price[BUFSIZE];
cout << "\n请输入你要查找的剩余客房的价格:";
cin >> price; //读取价格
bool flag = false;
cNode *p;
p = tListGue->head->next;
while (p)
{
if (strcmp(p->data.Price, price) == 0 && p->data.flag)
{
flag = true;
cout << "\n\n客房编号:" << (p->data).ID << "\t客房类型:" << (p->data).Type << "\n\t\t定房日期:" << (p->data).Book << "\n\t\t价格:" << (p->data).Price;
cout << "\n\t\t客房的主要内容:" << (p->data).Function << "\n\t\t入住有效期:" << (p->data).StayTime;
}
p = p->next;
}
if (flag == false) cout << "对不起,没有你所要求价格的客房!\n"; //如果找不到
cout << endl;
system("pause");
system("cls");
return;
}

void FindtheremainGuestroom(tList *tListGue)//查找客房
{
system("cls");
while (1)
{
cout << "\n\n\n\n";
cout << " 1:按客房编号查询\n\n";
cout << " 2:按客房类型查询\n\n";
cout << " 3:按价格查询\n\n";
cout << " 4:退出查询\n\n";
cout << "你选择:";
int choose = 0;
cin >> choose;
switch (choose) //根据用户选择
{
case 1:FindtheremainGuestroomById(tListGue); //按客房编号查询
break;
case 2:FindtheremainGuestroomByType(tListGue); //按客房类型查询
break;
case 3:FindtheremainGuestroomByPrice(tListGue); //按价格查询
break;
case 4:
system("cls");
return; //退出查询
default:break;
}
}

return;

}

void RanktheremainGuestroom(tList *tListGue) //排序剩余客房
{
cNode *p, *q;
mGuestroom remainGuestroom;
for (p = tListGue->head->next; p != NULL; p = p->next)
{
for (q = p; q != NULL; q = q->next)
{
if (p->data.StayTime > q->data.StayTime)
{
remainGuestroom.flag = p->data.flag;
strcpy(remainGuestroom.ID, p->data.ID); //复制
strcpy(remainGuestroom.Type, p->data.Type);
strcpy(remainGuestroom.Book, p->data.Book);
strcpy(remainGuestroom.Price, p->data.Price);
strcpy(remainGuestroom.Function, p->data.Function);
remainGuestroom.StayTime = p->data.StayTime;

            p->data.flag = q->data.flag;
            strcpy(p->data.Function, q->data.Function);
            strcpy(p->data.ID, q->data.ID);
            strcpy(p->data.Type, q->data.Type);
            strcpy(p->data.Book, q->data.Book);
            p->data.StayTime = q->data.StayTime;

            q->data.flag = remainGuestroom.flag;
            strcpy(q->data.Function, remainGuestroom.Function);
            strcpy(q->data.ID, remainGuestroom.ID);
            strcpy(q->data.Type, remainGuestroom.Type);
            strcpy(q->data.Book, remainGuestroom.Book);
            q->data.StayTime = remainGuestroom.StayTime;
        }
    }
}

}

void OutputtheremainGuestroom(tList *tListGue) //输出剩余房间函数
{
cNode *p;
p = tListGue->head->next;
while (p)
{
if (p->data.flag)
{
cout << "\n\n客房编号:" << (p->data).ID << "\t客房类型:" << (p->data).Type << "\n\t\t提前定房日期:" << (p->data).Book << "\n\t\t价格:" << (p->data).Price;
cout << "\n\t\t主要内容:" << (p->data).Function << "\n\t\t入住有效期:" << (p->data).StayTime;
}
p = p->next;
}
cout << endl;
system("pause"); //停止
system("cls"); //清屏
return;
}

int main()
{
tList tListGue;
tListGue.Create(); //创建链表
ReadData(&tListGue);
while (1)
{
cout << "\n\n 客房管理系统 \n\n";
cout << " ---------------------------------- \n\n";
cout << " 1.客房信息的录入 \n\n";
cout << " 2:客房信息保存\n\n";
cout << " 3.客房信息的修改 \n\n";
cout << " 4.客房信息的删除 \n\n";
cout << " 5.剩余客房信息的查询 \n\n";
cout << " 6.剩余客房信息的排序 \n\n";
cout << " 7.剩余客房信息的输出 \n\n";
cout << " 8.退 出 系 统 \n\n";
cout << " 请选择:";
int choose = 0;
cin >> choose;
switch (choose) //用户的选择
{
case 1:AddtheGuestroom(&tListGue); break; //输入客房信息

case 2:SaveData(&tListGue); break; //客房信息信息保存
case 3:ModifytheGuestroom(&tListGue); break; //修改客房信息
case 4:DeletetheGuestroom(&tListGue); break; //删除客房信息
case 5:FindtheremainGuestroom(&tListGue); break; //查询剩余客房信息
case 6:RanktheremainGuestroom(&tListGue); break; //排序剩余客房信息
case 7:OutputtheremainGuestroom(&tListGue); break; //输出剩余客房信息
case 8:exit(0); //退出
default: break;
}
}
return 0;
}

查看全部
weixin_48666605
Alan、long
2020/07/03 13:49
  • c++
  • 开发语言
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复