C++,不需要修改队列中的(随机)对象,对象指针
C++, modification of (random) object in a queue not desired, object pointors
我已经创建了一个对象指针队列(感谢向量库)。
std::vector<Polygon*>queue;
我的class多边形是所有其他多边形的母亲class,基本上它的目的是画一个形状。所以多边形正在绘制一个多边形,矩形是一个矩形... * 和 # 都是随机选择的。它实际上非常有效。
所以我想创建几个这样的绘图并将它们放入队列中,当程序结束时显示所有形状(使用 FIFO)。起初它似乎有效,但挖掘了一下我意识到绘图的不同之处。
例如矩形仍然是矩形,具有相同的尺寸,但是*和#的模式会不同。所以这意味着图形没有被保存,但是它的参数已经被保存了。队列正在重建这些。
我想做的是保存相同的形状,我做错了什么?
/*
* File: main.cpp
* Author: vfortineau
*
* Created on may 12nd 2015, 10:25
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include "Polygon.h"
#include "Rectangle.h"
#include "Square.h"
#include "IsoscelesTriangle.h"
#include "ReversedTriangle.h"
#include "Diamond.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int choice;
int _height;
int _width;
int _posi;
int i=0;
Polygon* polymorph;
std::vector<Polygon*>queue;
while (1)
{
cout << "\n\tPlease type one of the following command :\n" << endl;
cout << "\t********************************************\n"<<\
"\t* 1 - Upright isosceles triangle *\n" << \
"\t* 2 - Inverted isosceles triangle *\n" << \
"\t* 3 - Rectangle *\n" << \
"\t* 4 - Square *\n"<< \
"\t* 5 - Diamond *\n"<< \
"\t* 6 - Polygone *\n" << \
"\t* 0 - Exit Program *\n" << \
"\t********************************************" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new IsoscelesTriangle(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 2:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new ReversedTriangle(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 3:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the size of the width (please type an integer) : \n";
cin >> _width;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Rectangle(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 4:
cout << "Choose the size of the sides (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Square(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 5:
cout << "Choose the size of the vertical diagonal (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Diamond(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 6:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the size of the width (please type an integer) : \n";
cin >> _width;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Polygon(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 0:
while(not queue.empty())
{
i++;
cout << endl << "\t Figure " << i << endl;
cout << "\t---------\n\n";
queue[0]->drawShape();
queue.erase(queue.begin());
}
return 0;
}
}
return 0;
}
我加入了 drawShape() 函数的一个版本作为示例,但我不认为问题出在此处。
void IsoscelesTriangle::drawShape()
{
srand(time(NULL));
int random=0;
for(int i=0; i<height; i++)
{
position();
for(int j=0; j<height-(i+1); j++)
{
std::cout << " ";
}
for (int k=0; k<(2*i+1); k++)
{
random=rand()%2;
if (random==0) std::cout << "* ";
else std::cout << "# ";
}
std::cout << std::endl;
}
}
height 是 Polygon 的受保护成员,position 是 Polygon 的 public 函数。
for (int k=0; k<(2*i+1); k++)
{
random=rand()%2;
if (random==0) std::cout << "* ";
else std::cout << "# ";
}
每次调用 drawShape()
都会执行,并生成一个新的随机模式 * 和 #。如果您希望它保持一致,则需要在构造对象时生成一次模式,将其存储在某个地方,然后 drawShape()
查看该预生成的模式。
IsoscelesTriangle::drawShape()
的行为是随机的。如果你在建筑中扔一枚硬币并永远使用它,让它成为你的对象的一部分。
例如,
class IsoscelesTriangle
{
//stuff
private:
bool useStar;
};
并在IsoscelesTriangle
的构造函数中初始化useStar
:
IsoscelesTriangle::IsoscelesTriangle(int height, int pos)
{
srand(time(NULL));
useStar = (rand()%2 == 0);
}
最后,将 IsoscelesTriangle::drawShape()
中的 random == 0
替换为布尔值 useStar
。
我已经创建了一个对象指针队列(感谢向量库)。
std::vector<Polygon*>queue;
我的class多边形是所有其他多边形的母亲class,基本上它的目的是画一个形状。所以多边形正在绘制一个多边形,矩形是一个矩形... * 和 # 都是随机选择的。它实际上非常有效。
所以我想创建几个这样的绘图并将它们放入队列中,当程序结束时显示所有形状(使用 FIFO)。起初它似乎有效,但挖掘了一下我意识到绘图的不同之处。
例如矩形仍然是矩形,具有相同的尺寸,但是*和#的模式会不同。所以这意味着图形没有被保存,但是它的参数已经被保存了。队列正在重建这些。
我想做的是保存相同的形状,我做错了什么?
/*
* File: main.cpp
* Author: vfortineau
*
* Created on may 12nd 2015, 10:25
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include "Polygon.h"
#include "Rectangle.h"
#include "Square.h"
#include "IsoscelesTriangle.h"
#include "ReversedTriangle.h"
#include "Diamond.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int choice;
int _height;
int _width;
int _posi;
int i=0;
Polygon* polymorph;
std::vector<Polygon*>queue;
while (1)
{
cout << "\n\tPlease type one of the following command :\n" << endl;
cout << "\t********************************************\n"<<\
"\t* 1 - Upright isosceles triangle *\n" << \
"\t* 2 - Inverted isosceles triangle *\n" << \
"\t* 3 - Rectangle *\n" << \
"\t* 4 - Square *\n"<< \
"\t* 5 - Diamond *\n"<< \
"\t* 6 - Polygone *\n" << \
"\t* 0 - Exit Program *\n" << \
"\t********************************************" << endl;
cin >> choice;
switch(choice)
{
case 1:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new IsoscelesTriangle(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 2:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new ReversedTriangle(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 3:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the size of the width (please type an integer) : \n";
cin >> _width;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Rectangle(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 4:
cout << "Choose the size of the sides (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Square(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 5:
cout << "Choose the size of the vertical diagonal (please type an integer) : \n";
cin >> _height;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Diamond(_height, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 6:
cout << "Choose the size of the height (please type an integer) : \n";
cin >> _height;
cout << "Choose the size of the width (please type an integer) : \n";
cin >> _width;
cout << "Choose the position (please type an integer) : \n ";
cin >> _posi;
polymorph = new Polygon(_height, _width, _posi);
polymorph->drawShape();
queue.push_back(polymorph);
break;
case 0:
while(not queue.empty())
{
i++;
cout << endl << "\t Figure " << i << endl;
cout << "\t---------\n\n";
queue[0]->drawShape();
queue.erase(queue.begin());
}
return 0;
}
}
return 0;
}
我加入了 drawShape() 函数的一个版本作为示例,但我不认为问题出在此处。
void IsoscelesTriangle::drawShape()
{
srand(time(NULL));
int random=0;
for(int i=0; i<height; i++)
{
position();
for(int j=0; j<height-(i+1); j++)
{
std::cout << " ";
}
for (int k=0; k<(2*i+1); k++)
{
random=rand()%2;
if (random==0) std::cout << "* ";
else std::cout << "# ";
}
std::cout << std::endl;
}
}
height 是 Polygon 的受保护成员,position 是 Polygon 的 public 函数。
for (int k=0; k<(2*i+1); k++)
{
random=rand()%2;
if (random==0) std::cout << "* ";
else std::cout << "# ";
}
每次调用 drawShape()
都会执行,并生成一个新的随机模式 * 和 #。如果您希望它保持一致,则需要在构造对象时生成一次模式,将其存储在某个地方,然后 drawShape()
查看该预生成的模式。
IsoscelesTriangle::drawShape()
的行为是随机的。如果你在建筑中扔一枚硬币并永远使用它,让它成为你的对象的一部分。
例如,
class IsoscelesTriangle
{
//stuff
private:
bool useStar;
};
并在IsoscelesTriangle
的构造函数中初始化useStar
:
IsoscelesTriangle::IsoscelesTriangle(int height, int pos)
{
srand(time(NULL));
useStar = (rand()%2 == 0);
}
最后,将 IsoscelesTriangle::drawShape()
中的 random == 0
替换为布尔值 useStar
。