📄 09_04.cpp
字号:
// 09_04.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "container.h"
#include <iostream>
#include <list>
using namespace std;
// 容器管理类
class containerManager{
protected:
list<container*> m_lstContainers;
container* CreateContainer(int type, int val1 = 0, int val2 = 0);
public:
void Insert();
void Remove();
void RemoveAll();
void ShowInfo();
};
containerManager manager; // 容器管理对象
int nOperation = 0; // 当前选择的操作
static int nContainerID = 0; // 当前的最大容器号,确保容器号不重复
// 根据输入参数创建不同类型的对象
container* containerManager::CreateContainer(int type, int val1, int val2)
{
switch(type){
case 1:
return new cube(++nContainerID, val1);
break;
case 2:
return new sphere(++nContainerID, val1);
break;
case 3:
return new cylinder(++nContainerID, val1, val2);
break;
default:
return NULL;
break;
}
}
// 添加新容器
void containerManager::Insert()
{
int type = 0;
int val1 = 0;
int val2 = 0;
while(type < 1 || type > 3){
cout << "请输入容器类型: 1. 立方体 2. 球体 3. 圆柱体" << endl;
cin >> type;
}
switch(type){
case 1:
while(val1 <= 0){
cout << "请输入立方体边长: ";
cin >> val1;
}
break;
case 2:
while(val1 <= 0){
cout << "请输入球体半径: ";
cin >> val1;
}
break;
case 3:
while(val1 <= 0){
cout << "请输入圆柱体底面半径: ";
cin >> val1;
}
while(val2 <= 0){
cout << "请输入圆柱体高度: ";
cin >> val2;
}
break;
default:
break;
}
// 在链表尾插入新节点
container* pc = CreateContainer(type, val1, val2);
m_lstContainers.push_back(pc);
}
// 删除指定容器号的容器
void containerManager::Remove()
{
int id = 0;
while(id <= 0){
cout << "请输入要删除的容器号: ";
cin >> id;
}
// 遍历链表查找该容器
list<container*>::iterator itFind = m_lstContainers.begin();
while(itFind != m_lstContainers.end()){
if((*itFind)->GetID() == id){ // 找到该容器
itFind = m_lstContainers.erase(itFind);
cout << "succeeed: 删除容器号为" << id <<"的容器成功." << endl;
return;
}
itFind++;
}
// 没找到该容器
cout << "error: 没找到容器号为" << id << "的容器." << endl;
}
// 删除所有容器
void containerManager::RemoveAll()
{
// 释放各节点
list<container*>::iterator itFind = m_lstContainers.begin();
while(itFind != m_lstContainers.end()){
delete (*itFind);
itFind++;
}
// 清空链表
m_lstContainers.clear();
cout << "succeed: 删除所有容器完毕." << endl;
}
// 显示所有容器的信息
void containerManager::ShowInfo()
{
if(m_lstContainers.size() == 0){
cout << "error: 目前没有任何容器." << endl;
}
list<container*>::iterator itFind = m_lstContainers.begin();
while(itFind != m_lstContainers.end()){
(*itFind)->Show(); // 根据指向的不同调用不能类域的Show函数
itFind++;
}
}
void PrintMenu()
{
cout << endl << endl;
cout << "1. 添加新容器" << endl;
cout << "2. 删除指定容器" << endl;
cout << "3. 删除所有容器" << endl;
cout << "4. 显示容器信息" << endl;
cout << "5. 退出系统" << endl;
cout << "请选择您所需的操作: ";
}
int main(int argc, char* argv[])
{
cout << "容器管理系统 version 1.0" << endl;
cout << "王 涛 计算机与信息技术学院 北京交通大学" << endl;
while( nOperation != 5 ){
// 让用户选择操作
while( nOperation < 1 || nOperation > 5){
PrintMenu();
cin >> nOperation;
}
// 根据功能选择进行不同操作
switch( nOperation ){
case 1: manager.Insert(); break;
case 2: manager.Remove(); break;
case 3: manager.RemoveAll(); break;
case 4: manager.ShowInfo(); break;
default: break;
}
// 复位操作选择
if( nOperation != 5 ){
nOperation = 0;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -