我创建了一个对象指向器的队列(感谢矢量库)。
std::vector<Polygon*>queue;我的班级Polygon是所有其他人的母班,基本上它的目的是绘制一个形状。 因此,多边形绘制一个多边形,矩形是一个矩形......随机选择*和#。 它实际上完美无缺。
所以我想创建几个这些图纸并将它们放入队列中,当程序结束时显示所有形状(使用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的受保护成员,并定位Polygon的公共函数。
输出的一个例子
I've created a queue (thanks to vector library) of object pointors.
std::vector<Polygon*>queue;My class Polygon is the mother class of all the others, basicaly the aim of it is to draw a shape. So the polygon is drawing a polygon, the rectangle a rectangle... with both * and # chosen at random. It actually perfectly works.
So I wanted to create several of these drawings and put them in a queue and when the program ends display all the shapes (using a FIFO). At first it seems to works, but diging a bit I realized that the drawing where different.
For example the rectangle will still be a rectangle, with the same dimension, but the patern of * and # will be different. So it means that the figure has not been saved, but its parameters have been. And the queue is rebuilding those.
What I would like to do is to save the same shape, what did I do wrong ?
/* * 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; }I'm joining as an example a version of a drawShape() function but I don't think the problem is coming from here.
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 is a protected member of Polygon, and position a public function of Polygon.
An example of output
最满意答案
for (int k=0; k<(2*i+1); k++) { random=rand()%2; if (random==0) std::cout << "* "; else std::cout << "# "; }每次调用drawShape()都会执行,并生成一个新的随机模式*和#。 如果希望它保持一致,则需要在构造对象时生成一次模式,将其存储在某处,并让drawShape()查看该预生成的模式。
for (int k=0; k<(2*i+1); k++) { random=rand()%2; if (random==0) std::cout << "* "; else std::cout << "# "; }Every time you call drawShape() this gets executed, and generates a new random pattern of * and #. If you want that to be consistent, you need to generate the pattern once when the object is constructed, store it somewhere, and have drawShape() look at that pregenerated pattern.
C ++,修改不需要的队列中的(随机)对象,对象指向者(C++, modification of (random) object in a queue not desired, object pointors)我创建了一个对象指向器的队列(感谢矢量库)。
std::vector<Polygon*>queue;我的班级Polygon是所有其他人的母班,基本上它的目的是绘制一个形状。 因此,多边形绘制一个多边形,矩形是一个矩形......随机选择*和#。 它实际上完美无缺。
所以我想创建几个这些图纸并将它们放入队列中,当程序结束时显示所有形状(使用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的受保护成员,并定位Polygon的公共函数。
输出的一个例子
I've created a queue (thanks to vector library) of object pointors.
std::vector<Polygon*>queue;My class Polygon is the mother class of all the others, basicaly the aim of it is to draw a shape. So the polygon is drawing a polygon, the rectangle a rectangle... with both * and # chosen at random. It actually perfectly works.
So I wanted to create several of these drawings and put them in a queue and when the program ends display all the shapes (using a FIFO). At first it seems to works, but diging a bit I realized that the drawing where different.
For example the rectangle will still be a rectangle, with the same dimension, but the patern of * and # will be different. So it means that the figure has not been saved, but its parameters have been. And the queue is rebuilding those.
What I would like to do is to save the same shape, what did I do wrong ?
/* * 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; }I'm joining as an example a version of a drawShape() function but I don't think the problem is coming from here.
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 is a protected member of Polygon, and position a public function of Polygon.
An example of output
最满意答案
for (int k=0; k<(2*i+1); k++) { random=rand()%2; if (random==0) std::cout << "* "; else std::cout << "# "; }每次调用drawShape()都会执行,并生成一个新的随机模式*和#。 如果希望它保持一致,则需要在构造对象时生成一次模式,将其存储在某处,并让drawShape()查看该预生成的模式。
for (int k=0; k<(2*i+1); k++) { random=rand()%2; if (random==0) std::cout << "* "; else std::cout << "# "; }Every time you call drawShape() this gets executed, and generates a new random pattern of * and #. If you want that to be consistent, you need to generate the pattern once when the object is constructed, store it somewhere, and have drawShape() look at that pregenerated pattern.
发布评论