一.课程设计题目与要求(包括题目与系统功能要求)
A.<1>设计如下类,其功能和部分成员如下:
Object:抽象类,所有的物体都有价值(profit)属性;
Point:点的位置;
Line(线段),Rectangle,Cuboid, Square,Cube,Circle,Cylinder.
<2>功能:能够实现上述物体的移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息(cout<<编号、面积、容积和价值)等操作,且所有物品的对象实现自动编号。
<3>提示如下:
1.移动: Line类对象移动其中点;
Rectangle、Square和Circle:移动重心;
Cubiod、Cube和Cylinder: 移动底面重心。
2.放大和缩小:以倍数为参数,进行相应组件的放大和缩小。
3.默认比较方式:
Line:比较长度;
Rectangle、Square和Circle:比较面积;
Cubiod、Cube和Cylinder: 比较体积。
注:同维度(或不同维度)空间内的不同类物体之间可进行大小比较。相等返回0,小于返回-1、大于返回1。
B.再设计一个容器类(Container),容器具有最大容量属性。
<1>功能:能容纳以上定义的各种物品(当然指的是Cylinder,Cube和Cuboid),实现添加一个物品(addItem),移除容器里的一个物品(removeItem),重载[].
<2>附加功能:
1.不改变物品在容器中的位置,把物品的id按照排序结果(根据物品某一关键字)返回 ;
2.给定一定数量的物品(假设物品的总容量超过容器最大容量),挑选部分装入容器中,设计算法实现所装物品的总价值最大。
二.需求分析
1.问题描述:
①——本次c++实践主要的目的是为了熟悉继承和派生,从设计的要求可知,要有一个最底层的抽象基类即Object类,然后就是多个类之间的关系,Point继承Object——然后Line继承Point,添加length新成员;Circle继承Point,添加新成员R;Square继承Point,添加L1新成员;Rectangle继承Point,添加L1,L2新成员;最后Cylinder继承Circle,添加height新成员;Cube继承Square,添加high新成员;Cuboid继承Rectangle,添加high新成员。这样就完成了继承与派生类的设计。
②——除了这些类,还必须有操作,否则就没有意义了,要实现上述物体的move,zoomin,zoomout,compare,打印物品信息(cout<<编号、面积、容积和价值)等操作,这里要用到运行时的多态,即需要基类的指针,将在下一条详细讲,这里要做的是函数成员的设计,这要求在每个类(包括基类)要有同名函数,即(overriding a virtual function),返回值类型,参数类型等都得一样,而且基类的要是virtual function。
③——再设计一个容器类(Container),所有物品的对象实现自动编号。这里的容器要用vector来做,我们在Container类中定义vector<Point *> Po数据成员(既可实现自动编号又可实现运行时的多态)。至于其它操作的设计与②相似,也可在Container类中实现。
2.系统环境、运行要求:Visual Studio 2012/2013 等c++编译器。
三.概要设计(包括系统流程设计、系统模块设计)
系统主流程:
系统分流程:
1.添加物品:
2.移动物品:
3.放大或缩小物品:
4.删除信息:
5.比较大小:
6.打印信息:
继承与派生的关系:
四.详细设计(包括类的函数成员和数据成员设计、界面设计(见问题提示)、及其它模块设计与实现)
1.先来看继承与派生类的数据成员和函数成员的设计,首先是继承与派生的关系和添加新数据成员:
//base class
class Object{
protected:
double Profit;//价值属性,是每个物体都有的
public:
Object() :Profit(0){}
Object(double a) :Profit(a){}
virtual ~Object(){}//析构函数最好都用virtual
virtual void print() = 0;//pure virtual function,you can not define an object
};
class Point :public Object{
protected:
double x, y, z;// 3D,作为Point继承了Object,坐标为新成员
......};
class Line :virtual public Point{
protected:
double length;//Line继承Point,添加长度为新成员,派生类一定要有自己的新成员
......};
class Circle :virtual public Point{
protected:
double R;//作为Circle继承了Object,添加半径为新成员
......};
class Cylinder :public Circle{
private:
double height;//作为Cylinder继承了Circle,添加高度height为新成员
......};
class Square :public Point{
protected:
double R;//Square继承Point,添加1条边R作为新成员
......};
class Cube :public Square{
protected:
double High;//Cube继承Square,添加高High作为新成员
......};
class Rectangle :public Point{
protected:
double R1, R2;//Rectangle继承Point,添加2条边R1,R2作为新成员
......};
class Cuboid :public Rectangle{
protected:
double High;//Cuboid继承Rectangle,添加高High作为新成员
......};
其次是函数成员的设计:这里以Point的为例:
class Point :public Object{
protected:
double x, y, z;// 3D,作为Point继承了Object,坐标为新成员
public:
Point():Object(){
x = y = z = 0;
}//派生类的构造函数,注意基类成员的赋值问题
Point(Point & c):Object(c){
x = c.x;
y = c.y;
z = c.z;
}//显示的复制构造函数
Point(double a, double b, double c, double d);//重载的构造函数
void getPoint();//得到点的坐标
virtual double getlength(){ return 0; }//返回长度,没有为0,有就返回
virtual double getarea(){ return 0; }//返回面积,没有为0,有就返回
virtual double getvolume(){ return 0; }//返回体积,没有为0,有就返回
virtual void movexyz();//移动坐标
virtual void zoom();//放大或缩小
virtual void print();//打印物品信息
virtual ~Point(){}//类自己的析构函数
};
注意到这里的所有用于操作的成员函数都是virtual,因为这是用于派生的类,而我们要实现运行时的多态,故在所有派生类中都有同名函数,即(overriding a virtual function),返回值类型,参数类型等都得一样,当基类对象指针指向派生类对象时,实现运行时的多态完成要做的事。
2.再来看容器类的设计:
//operate class
class Container{
private:
vector<Point*> Po;//指向基类指针类型的容器
public:
void Addobject();//添加物品
void Moveposition();//移动物体的位置
void Zoominorout();//放大或缩小物体
void Compare();//比较物体的大小
void Print();//打印物体的信息
void Deleteobject();//删除容器里的具有体积的物品
Point* & operator[](int i);//重载[]
};
部分成员函数如下:
//删除函数:
double a;//a是体积
cin >> a;
for (vector<Point*>::iterator iter = Po.begin(); iter != Po.end();) {
if ((*iter)->getvolume() == a) {
iter = Po.erase(iter);//使用了迭代器vector<Point*>::iterator iter
}
else {
iter++;
}
//移动函数:
void Container::Moveposition(){
int a = 1;
while (a){
int b;
cout << "请输入需要移动的序号:" << endl;
cin >> b;
Po[b]->print();
Po[b]->movexyz();
Po[b]->print();
cout << "是否要继续?继续请按1,否请按0:" << endl;
cin >> a;
}
}
//打印函数:
for (int i = 0; i<(int)Po.size(); i++){
cout << i << " ";
Po[i]->print();//用一个for循环即可打印全部的内容
}
//添加操作的部分代码:
case 8:{
cout << "请输入长方体的数据(单价,坐标,长,宽,高):" << endl;
cin >> p >> x >> y >> z >> l >> l1 >> l2;
Cuboid cub1(p, x, y, z, l, l1, l2);
Point s(p, x, y, z), *S;
S = &cub1;
if (Vc + S->getvolume() <= 50){
pi[b] = &s;
Po.push_back(pi[b]);
cub[b] = cub1;
Po[i] = &cub[b];
i++;
b++;
}
else cout << "容器已满" << endl;
}break;
3.界面设计:
void showmenu(){
cout << "\t" << "\t" << "\t" << "0.退出此管理系统" << endl;
cout << "\t" << "\t" << "\t" << "1.添加物体的信息" << endl;
cout << "\t" << "\t" << "\t" << "2.移动物体的位置" << endl;
cout << "\t" << "\t" << "\t" << "3.放大或缩小物体" << endl;
cout << "\t" << "\t" << "\t" << "4.比较物体的大小" << endl;
cout << "\t" << "\t" << "\t" << "5.打印物体的信息" << endl;
cout << "\t" << "\t" << "\t" << "6.删除物体的信息" << endl;
}
Container ope;//定义类对象
switch (x){
case 0:quit = true; cout << "已退出!" << endl; break;
case 1:ope.Addobject(); break;
case 2:ope.Moveposition(); break;
case 3:ope.Zoominorout(); break;
case 4:ope.Compare(); break;
case 5:ope.Print(); break;
case 6:ope.Deleteobject(); break;
}
主函数显示功能菜单,供用户选择操作。每步操作之前,都要显示菜单,在主函数中调用类的方法,在主函数中定义类对象。
首先:定义显示菜单(不是类的成员函数):void showmenu()//显示菜单
其次:在while中调用showmenu()实现每步操作之前,都要显示菜单,这样就形成了主界面!!!
五.测试(包括对各功能模块的测试)
1.主界面设计:进入管理系统,按任意键继续,接下来是功能的选项,共7个,选择进行测试:
2.键入1:添加物品信息,添加点(无体积,不占容器体积,只是有一个编号):
3.键入2,添加一条线段(无体积,不占容器体积,只是有一个编号):
4.继续添加circle,square,cuboid等,后面用这些添加的元素进行测试:
(注意它们的单价,点为0,线为1,面为2,体积的为3):
5.容器具有最大容量属性:
6.键入2,测试移动物体位置的功能,只需重新输入中心坐标:
7.键入3,测试放大或缩小物体的功能,以倍数为参数,进行相应组件的放大和缩小,如果是个点的话,会有相应的安全性判断,即"sorry,it's a point,you can't zoomin or zoomout it!":
8.键入4,测试比较的功能,同维度(或不同维度)空间内的不同类物体之间可进行大小比较,若是同维度的,则比较相应的体积(或面积或长度):
9.如若不是同维度的,则比较顺序为体积->面积->长度:
10.键入5,测试print功能,可以选择<1>print all:
11.<2>print single:
12.键入6,测试删除物体信息的功能,由于考虑到container类最大容量的功能及容纳以上定义的各种物品(当然指的是Cylinder,Cube和Cuboid),实现添加一个物品(addItem),移除容器里的一个物品(removeItem)等,这里是按照物品的体积来删除,如下:
13.删除后打印界面,3,4被删除,剩下0,1,2号:
14.键入0,退出管理系统:
六.结论(对系统开发的总结,设计的亮点,存在的不足,需要改进的地方)
本学期较上学期来说做了更多的课程设计,有了更多的实践经验,对于此次的设计,我感觉知识的综合能力较强,毕竟c++已经学习了一个学期,对于类的设计也不能仅仅局限于一个或俩个,要学会从分散到集中,从少到多。本次上机的主要目的是熟悉继承与派生(inheritance and polymorphism),这是c++的核心内容,也是比较难掌握的,但总的来说,这次上机实践我的收获还是很大的,它找到了我的错误和不足之处。
对于这个面向对象的程序设计,我个人认为这个程序的优点在于:1.在做的过程中使用了多文件的结构,这是我第一次比较完整和系统的在自己的探索下完成的多文件设计,便于编译和改错;2.类的设计,在程序中共有10个类,它们之间存在继承与派生的关系,每个类都有自己独立的数据成员和函数成员,声明和定义都是分开的,都是按照类的标准设计来做的,虽然不是很完善;3.还有就是实现了运行时的多态,在Container类中用了vector<Point*> Po,即指向基类的指针容器,这在程序中起到了至关重要的作用,也是我想了好久才想到的。
在缺点方面,都有点不好意思说了:首先是在继承与派生时对概念弄得不是很清晰,逻辑上出现了一些错误,再加上理解题意出了问题,导致派生关系弄得不对或复杂了,比如Cylinder的派生,应用Circle再添一个height就行了,而我却弄成了Circle和Line,后来在老师你的提醒下课后才改正过来;还有就是关于继承类的构造函数没有写好,忽略了关键点,即基类成员和新增成员的使用和关系,不过也改正了过来;还有就是有功能没有实现,像给定一定数量的物品,假设物品的总容量超过容器最大容量,挑选部分装入容器中,设计算法实现所装物品的总价值最大。等等,总的来说,程序还存在很大的改进空间,我会在今后的学习中慢慢改进,让程序更完美。
C++课本的教学知识已经教完了,但对于我来说还差得很远很远,光靠这一年的时间是远远不够的,需要更多的知识灌溉和实践,C++之路曼曼其修远兮,吾将上下而求索。要成为一名合格的程序员还有许多的路要走,我会一直坚持下去!谢谢老师一年来经常给予指导和教诲,收获颇多,并希望一直如此。
七.附录(程序源代码)
//头文件.h部分
#ifndef Object_H
#define Object_H
#include<iostream>
#include<vector>
const double PI = 3.14;
using namespace std;
//base class
class Object{
protected:
double Profit;
public:
Object() :Profit(0){}
Object(double a) :Profit(a){}
virtual ~Object(){}
virtual void print() = 0;//pure virtual function,you can not define an object
};
#endif
//******保存在Object.h
#ifndef Point_H
#define Point_H
#include<iostream>
#include<vector>
#include"Object.h"
using namespace std;
class Point :public Object{
protected:
double x, y, z;// 3D
public:
Point():Object(){
x = y = z = 0;
}
Point(Point & c):Object(c){
x = c.x;
y = c.y;
z = c.z;
}
Point(double a, double b, double c, double d);
void getPoint();
virtual double getlength(){ return 0; }
virtual double getarea(){ return 0; }
virtual double getvolume(){ return 0; }
virtual void movexyz();
virtual void zoom();
virtual void print();
virtual ~Point(){}
};
#endif
//******保存在Point.h
#ifndef Line_H
#define Line_H
#include<iostream>
#include<vector>
#include"Point.h"
using namespace std;
class Line :virtual public Point{
protected:
double length;
public:
Line():Point(){
length = 0;
}
Line(Line & c) :Point(c){
length= c.length;
}
Line(double a, double b, double c, double d, double e);
double getlength();
double getvolume(){ return 0; }
double getarea(){ return 0; }
void movexyz();
void zoom();
void print();
virtual ~Line(){}
};
#endif
//******保存在Line.h
#ifndef Circle_H
#define Circle_H
#include<iostream>
#include<vector>
#include"Point.h"
using namespace std;
class Circle :virtual public Point{
protected:
double R;
public:
Circle():Point(){
R = 0;
}
Circle(Circle & c) :Point(c){
R = c.R;
}
Circle(double a, double b, double c, double d, double e);
double getarea();
double getlength(){ return 0; }
double getvolume(){ return 0; }
void movexyz();
void zoom();
void print();
virtual ~Circle(){}
};
#endif
//******保存在Circle.h
#ifndef Cylinder_H
#define Cylinder_H
#include<iostream>
#include<vector>
#include"Circle.h"
using namespace std;
class Cylinder :public Circle{
private:
double height;
public:
Cylinder():Circle(){
height = 0;
}
Cylinder(Cylinder & c):Circle(c){
height = c.height;
}
Cylinder(double a, double b, double c, double d, double e,double f);
double getvolume();
double getlength(){ return 0; }
double getarea(){ return 0; }
void movexyz();
void zoom();
void print();
virtual~Cylinder(){}
};
#endif
//******保存在Cylinder.h
#ifndef Square_H
#define Square_H
#include<iostream>
#include<vector>
#include"Point.h"
using namespace std;
class Square :public Point{
protected:
double R;
public:
Square();
Square(Square & c) :Point(c){
R = c.R;
}
Square(double p, double x1, double x2, double x3, double r);
double getarea();
double getlength(){ return 0; }
double getvolume(){ return 0; }
void movexyz();
void zoom();
void print();
virtual ~Square(){};
};
#endif
//******保存在Square.h
#ifndef Rectangle_H
#define Rectangle_H
#include<iostream>
#include<vector>
#include"Point.h"
using namespace std;
class Rectangle :public Point{
protected:
double R1, R2;
public:
Rectangle();
Rectangle(Rectangle & c) :Point(c){
R1 = c.R1;
R2 = c.R2;
}
Rectangle(double p, double x1, double x2, double x3, double r1, double r2);
double getarea();
double getlength(){ return 0; }
double getvolume(){ return 0; }
void movexyz();
void zoom();
void print();
virtual ~Rectangle(){};
};
#endif
//******保存在Rectangle.h
#ifndef Cube_H
#define Cube_H
#include<iostream>
#include<vector>
#include"Square.h"
using namespace std;
class Cube :public Square{
protected:
double High;
public:
Cube();
Cube(double p, double x1, double x2, double x3, double r, double high);
Cube(Cube & c) :Square(c){
High = c.High;
}
double getarea(){ return 0; }
double getlength(){ return 0; }
double getvolume();
void movexyz();
void zoom();
void print();
virtual ~Cube(){};
};
#endif
//******保存在Cube.h
#ifndef Cuboid_H
#define Cuboid_H
#include<iostream>
#include<vector>
#include"Rectangle.h"
using namespace std;
class Cuboid :public Rectangle{
protected:
double High;
public:
Cuboid();
Cuboid(Cuboid & c):Rectangle(c){
High = c.High;
}
Cuboid(double p, double x1, double x2, double x3, double r1, double r2, double high);
double getvolume();
double getlength(){ return 0; }
double getarea(){ return 0; }
void movexyz();
void zoom();
void print();
virtual ~Cuboid(){};
};
#endif
//******保存在Cuboid.h
#ifndef Container_H
#define Container_H
#include<iostream>
#include<vector>
#include"Line.h"
#include"Cylinder.h"
#include"Cube.h"
#include"Cuboid.h"
using namespace std;
//operate class
class Container{
private:
vector<Point*> Po;
public:
void Addobject();
void Moveposition();
void Zoominorout();
void Compare();
void Print();
void Deleteobject();
Point* & operator[](int i);
};
#endif
//******保存在Container.h
//.cpp部分
#include"Point.h"
Point::Point(double a, double b, double c, double d) :Object(a), x(b), y(c), z(d){
}
void Point::getPoint(){
cout << "x坐标:" << x << " " << "y坐标:" << y << " " << "z坐标:" << z << endl;
}
void Point::movexyz(){
double a, b, c;
cout << "请重新输入坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Point::zoom(){
cout << "sorry,it's a point,you can't zoomin or zoomout it!" << endl;
}
void Point::print(){
cout << "point:" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << endl;
}
//******保存在Point.cpp
#include"Line.h"
Line::Line(double a, double b, double c, double d, double e) :Point(a*e, b, c, d), length(e){
};
double Line::getlength(){
return length;
}
void Line::movexyz(){
double a, b, c;
cout << "请重新输入中点坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Line::zoom(){
cout << "it's a line,please input the times you want to zoominorout!" << endl;
double a;
cin >> a;
length = a*length;
}
void Line::print(){
cout << "line:" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "长度:" << length << endl;
}
//******保存在Line.cpp
#include"Circle.h"
Circle::Circle(double a, double b, double c, double d, double e) :Point(a*PI*e*e, b, c, d), R(e){
};
double Circle::getarea(){
return(PI*R*R);
}
void Circle::movexyz(){
double a, b, c;
cout << "请重新输入圆心坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Circle::zoom(){
cout << "it's a circle,please input the times you want to zoominorout!" << endl;
double a;
cin >> a;
R = a*R;
}
void Circle::print(){
cout << "circle:" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "面积:" << PI*R*R << endl;
}
//******保存在Circle.cpp
#include"Cylinder.h"
Cylinder::Cylinder(double a, double b, double c, double d, double e, double f) :Circle(a,b,c,d,e),height(f){
Profit = 3 * getvolume();
}
double Cylinder::getvolume(){
return(PI*R*R*height);
}
void Cylinder::movexyz(){
double a, b, c;
cout << "请重新输入底面圆心坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Cylinder::zoom(){
cout << "it's a cylinder,please input the times you want to zoominorout!" << endl;
double a, b;
cin >> a >> b;
height = a*height;
R = b*R;
}
void Cylinder::print(){
cout << "cylinder:" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "体积:" << getvolume() << endl;
}
//******保存在Cylinder.cpp
#include"Square.h"
Square::Square() :Point(){
R = 0;
}
Square::Square(double p, double x1, double x2, double x3, double r) :Point(p*r*r, x1, x2, x3), R(r){
}
double Square::getarea(){
return(R*R);
}
void Square::movexyz(){
double a, b, c;
cout << "请重新输入底面中心坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Square::zoom(){
cout << "it's a square,please input the times you want to zoominorout!(side length)" << endl;
double a;
cin >> a;
R = a*R;
}
void Square::print(){
cout << "square:" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "面积:" << R*R << endl;
}
//******保存在Square.cpp
#include"Rectangle.h"
Rectangle::Rectangle() :Point(){
R1 = R2 = 0;
}
Rectangle::Rectangle(double p, double x1, double x2, double x3, double r1, double r2) :Point(p*r1*r2, x1, x2, x3), R1(r1), R2(r2){
}
double Rectangle::getarea(){
return(R1*R2);
}
void Rectangle::movexyz(){
double a, b, c;
cout << "请重新输入底面中心坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Rectangle::zoom(){
cout << "it's a Rectangle,please input the times you want to zoominorout!(side length1,length2)" << endl;
double a, b;
cin >> a >> b;
R1 = a*R1;
R2 = b*R2;
}
void Rectangle::print(){
cout << "rectangle:" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "面积:" << getarea() << endl;
}
//******保存在Rectangle.cpp
#include"Cube.h"
Cube::Cube():Square(){
High = 0;
}
Cube::Cube(double p, double x1, double x2, double x3, double r, double high) :Square(p*high*high*high, x1, x2, x3, r), High(high){
}
double Cube::getvolume(){
return(High*High*High);
}
void Cube::movexyz(){
double a, b, c;
cout << "请重新输入底面中心坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Cube::zoom(){
cout << "it's a Cube,please input the times you want to zoominorout!(side length)" << endl;
double a;
cin >> a;
R = a*R;
High = a*High;
}
void Cube::print(){
cout << "cube" << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "体积:" << getvolume() << endl;
}
//******保存在Cube.cpp
#include"Cuboid.h"
Cuboid::Cuboid():Rectangle(){
High = 0;
}
Cuboid::Cuboid(double p, double x1, double x2, double x3, double r1, double r2, double high) :Rectangle(p, x1, x2, x3, r1, r2), High(high){
Profit = p*R1*R2*High;
}
double Cuboid::getvolume(){
return(R1*R2*High);
}
void Cuboid::movexyz(){
double a, b, c;
cout << "请重新输入底面中心坐标:" << endl;
cin >> a >> b >> c;
x = a;
y = b;
z = c;
}
void Cuboid::zoom(){
cout << "it's a Cuboid,please input the times you want to zoominorout!(side length1,length2,High)" << endl;
double a, b, c;
cin >> a >> b >> c;
R1 = a*R1;
R2 = b*R2;
High = c*High;
}
void Cuboid::print(){
cout << "cuboid" << " " << " " << "价值:" << Profit << " " << "坐标:" << "(" << x << "," << y << "," << z << ")" << " " << "体积:" << getvolume() << endl;
}
//******保存在Cuboid.cpp
#include"Container.h"
Point *pi[20];
Point po[20];
Line li[20];
Circle ci[20];
Cylinder cy[20];
Square sq[20];
Rectangle re[20];
Cuboid cub[20];
Cube cu[20];
const double V = 50;
double Vc = 0;
void Container::Addobject(){
int a = 1, b = 0, i = 0;
double p, x, y, z, l, l1, l2;
while (a){
for (int j = 0; j<(int)Po.size(); j++){
Vc += Po[j]->getvolume();
}
if (Vc < 50){
cout << "请选择要添加的物品,1-点,2-线,3-圆,4-圆柱,5-正方形,6-长方形,7-正方体,8-长方体:" << endl;
int j;
cin >> j;
switch (j){
case 1:{
cout << "请输入点的数据(单价,坐标):" << endl;
cin >> p >> x >> y >> z;
Point po1(p, x, y, z);
pi[b] = &po1;
Po.push_back(pi[b]);
po[b] = po1;
Po[i] = &po[b];
i++;
b++;
}break;
case 2:{
cout << "请输入线的数据(单价,中点坐标,长度):" << endl;
cin >> p >> x >> y >> z >> l;
Line li1(p, x, y, z, l);
Point s(p, x, y, z);
pi[b] = &s;
Po.push_back(pi[b]);
li[b] = li1;
Po[i] = &li[b];
i++;
b++;
}break;
case 3:{
cout << "请输入圆的数据(单价,圆心坐标,半径):" << endl;
cin >> p >> x >> y >> z >> l;
Circle ci1(p, x, y, z, l);
Point s(p, x, y, z);
pi[b] = &s;
Po.push_back(pi[b]);
ci[b] = ci1;
Po[i] = &ci[b];
i++;
b++;
}break;
case 4:{
cout << "请输入圆柱的数据(单价,底面圆心坐标,圆半径,高):" << endl;
cin >> p >> x >> y >> z >> l >> l1;
Cylinder cy1(p, x, y, z, l, l1);
Point s(p, x, y, z),*S;
S = &cy1;
pi[b] = &s;
if (Vc + S->getvolume() <= 50){
Po.push_back(pi[b]);
cy[b] = cy1;
Po[i] = &cy[b];
i++;
b++;
}
else cout << "容器已满" << endl;
}break;
case 5:{
cout << "请输入正方形的数据(单价,坐标,边):" << endl;
cin >> p >> x >> y >> z >> l;
Square sq1(p, x, y, z, l);
Point s(p, x, y, z);
pi[b] = &s;
Po.push_back(pi[b]);
sq[b] = sq1;
Po[i] = &sq[b];
i++;
b++;
}break;
case 6:{
cout << "请输入长方形的数据(单价,坐标,长,宽):" << endl;
cin >> p >> x >> y >> z >> l >> l1;
Rectangle re1(p, x, y, z, l, l1);
Point s(p, x, y, z);
pi[b] = &s;
Po.push_back(pi[b]);
re[b] = re1;
Po[i] = &re[b];
i++;
b++;
}break;
case 7:{
cout << "请输入正方体的数据(单价,坐标,底面边,高(与底面边相等)):" << endl;
cin >> p >> x >> y >> z >> l >> l1;
Cube cu1(p, x, y, z, l, l1);
Point s(p, x, y, z), *S;
S = &cu1;
if (Vc + S->getvolume() <= 50){
pi[b] = &s;
Po.push_back(pi[b]);
cu[b] = cu1;
Po[i] = &cu[b];
i++;
b++;
}
else cout << "容器已满" << endl;
}break;
case 8:{
cout << "请输入长方体的数据(单价,坐标,长,宽,高):" << endl;
cin >> p >> x >> y >> z >> l >> l1 >> l2;
Cuboid cub1(p, x, y, z, l, l1, l2);
Point s(p, x, y, z), *S;
S = &cub1;
if (Vc + S->getvolume() <= 50){
pi[b] = &s;
Po.push_back(pi[b]);
cub[b] = cub1;
Po[i] = &cub[b];
i++;
b++;
}
else cout << "容器已满" << endl;
}break;
}
cout << "是否要继续,继续请按1,否按0" << endl;
cin >> a;
}
else { cout << "容器已满" << endl; break; }
}
}
void Container::Moveposition(){
int a = 1;
while (a){
int b;
cout << "请输入需要移动的序号:" << endl;
cin >> b;
Po[b]->print();
Po[b]->movexyz();
Po[b]->print();
cout << "是否要继续?继续请按1,否请按0:" << endl;
cin >> a;
}
}
void Container::Zoominorout(){
int i = 1;
while (i){
cout << "请输入想要放大或缩小的物体序号:" << endl;
int a;
cin >> a;
Po[a]->print();
Po[a]->zoom();
Po[a]->print();
cout << "是否要继续?继续请按1,否请按0:" << endl;
cin >> i;
}
}
void Container::Compare(){
cout << "请输入需要比较的2个物品的编号:" << endl;
int a, b;
cin >> a >> b;
if (Po[a]->getvolume() != 0 && Po[b]->getvolume() != 0){
if (Po[a]->getvolume()>Po[b]->getvolume())
cout << a << "物体空间大小" << ">" << b << "物体空间大小" << endl;
else if (Po[a]->getvolume() == Po[b]->getvolume())
cout << a << "物体空间大小" << "=" << b << "物体空间大小" << endl;
else cout << a << "物体空间大小" << "<" << b << "物体空间大小" << endl;
}
else if (Po[a]->getvolume() == 0 && Po[b]->getvolume() == 0){
if (Po[a]->getarea() != 0 && Po[b]->getarea() != 0){
if (Po[a]->getarea()>Po[b]->getarea())
cout << a << "物体平面大小" << ">" << b << "物体平面大小" << endl;
else if (Po[a]->getarea() == Po[b]->getarea())
cout << a << "物体平面大小" << "=" << b << "物体平面大小" << endl;
else cout << a << "物体平面大小" << "<" << b << "物体平面大小" << endl;
}
else if (Po[a]->getarea() == 0 && Po[b]->getarea() == 0){
if (Po[a]->getlength()>Po[b]->getlength()) cout << a << "物体" << "长于" << b << "物体" << endl;
else if (Po[a]->getlength() == Po[b]->getlength()) cout << a << "物体" << "等于" << b << "物体" << endl;
else cout << a << "物体" << "小于" << b << "物体" << endl;
}
else{
if (Po[b]->getarea() == 0) cout << a << "物体平面大小" << ">" << b << "物体大小" << endl;
else cout << a << "物体大小" << "<" << b << "物体平面大小" << endl;
}
}
else{
if (Po[b]->getvolume() == 0) cout << a << "物体空间大小" << ">" << b << "物体空间大小" << endl;
else cout << a << "物体空间大小" << "<" << b << "物体空间大小" << endl;
}
}
void Container::Print(){
cout << "print all or single,all-1,single-2" << endl;
int a;
cin >> a;
if (a == 1){
for (int i = 0; i<(int)Po.size(); i++){
cout << i << " ";
Po[i]->print();
}
}
else if (a == 2){
cout << "please input the number you want to print:" << endl;
int b;
cin >> b;
cout << b << " ";
Po[b]->print();
}
else cout << "input wrong" << endl;
}
void Container::Deleteobject(){
int i = 1;
while (i){
cout << "the volume you want to delete" << endl;
double a;
cin >> a;
for (vector<Point*>::iterator iter = Po.begin(); iter != Po.end();) {
if ((*iter)->getvolume() == a) {
iter = Po.erase(iter);
}
else {
iter++;
}
}
cout << "do you want to continue? 1-continue,0-end" << endl;
cin >> i;
}
}
Point* & Container::operator[](int i){
if (i<0 || i >= (int)Po.size())
cout << "下标出界" << endl;
else return Po[i];
}
//******保存在Container.cpp
#include<iostream>
#include<vector>
#include<algorithm>
#include"Container.h"
using namespace std;
void showmenu(){
cout << "\t" << "\t" << "\t" << "0.退出此管理系统" << endl;
cout << "\t" << "\t" << "\t" << "1.添加物体的信息" << endl;
cout << "\t" << "\t" << "\t" << "2.移动物体的位置" << endl;
cout << "\t" << "\t" << "\t" << "3.放大或缩小物体" << endl;
cout << "\t" << "\t" << "\t" << "4.比较物体的大小" << endl;
cout << "\t" << "\t" << "\t" << "5.打印物体的信息" << endl;
cout << "\t" << "\t" << "\t" << "6.删除物体的信息" << endl;
}
int main(){
int x, i = 0;
bool quit = false;
for (i = 0; i<3; i++)
cout << "\t" << "\t" << "\t" << "******************************" << endl;
cout << "\t" << "\t" << "\t" << "【 欢迎进入物品信息管理系统】" << endl;
for (i = 0; i<3; i++)
cout << "\t" << "\t" << "\t" << "******************************" << endl;
Container ope;
system("pause");
while (!quit){
showmenu();
cin >> x;
switch (x){
case 0:quit = true; cout << "已退出!" << endl; break;
case 1:ope.Addobject(); break;
case 2:ope.Moveposition(); break;
case 3:ope.Zoominorout(); break;
case 4:ope.Compare(); break;
case 5:ope.Print(); break;
case 6:ope.Deleteobject(); break;
}
}
return 0;
}
//******保存在main.cpp