[英]How can I change my hanoi tower output display vertically?
I'm making hanoi tower for my assignment. 我正在为我的任务准备河内塔。 I asked my professor to check my code before submitting it.
我要求教授在提交代码之前先进行检查。 My professor said he would give me a high score if I printed my display vertically, but I don't know how to change it to vertical.
我的教授说,如果我垂直打印显示器,他会给我高分,但是我不知道如何将其更改为垂直。 How can I change it to vertical?
如何将其更改为垂直? and also the towers are out in reverse, how can I get it back?
而且塔倒转了,我该如何找回它?
MyStack is in the header that the professor make it for us MyStack在教授为我们制作的标题中
struct MyStack{
int tos; //top of stack
char s[MAXSTACK]; //MAXSTACK is 100.
}
void createS(MyStack &S) {
S.tos = -1;
}
void pushS(MyStack &S, char item) {
S.s[++S.tos] = item;
}
char popS(MyStack& S) {
return(S.s[S.tos--]);
}
bool isEmptyS(MyStack& S) {
if (S.tos == -1)
return (true);
return false;
}
This is the main. 这是主要的。
const static int TOWER_CNT=3;
void printTower(MyStack tower[TOWER_CNT]) {
MyStack tempStack; // temporary stacks to temporarily store tower values
createS(tempStack); // reset temporary stack
char tempNum;
cout << " ===============================" << endl;
for (int i = 0; i < TOWER_CNT; i++) { // display all tower.
cout << " Tower " << i + 1 << ": ";
while (!isEmptyS(tower[i])) {
pushS(tempStack, popS(tower[i])); // Put it upside down in a temporary stack because it has to be printed from the beginning of the tower.
}
while (!isEmptyS(tempStack)) { // Print out the value of the temporary stack and put it back into the tower.
tempNum = popS(tempStack);
//cout << (int)tempNum << " "; // print by number.
//cout << " ";
change(tempNum); // change number into *
cout << "\n ";
pushS(tower[i], tempNum);
}
cout << endl;
}
cout << " ===============================" << endl;
system("pause");
}
You need to retrieve some global information from your towers. 您需要从塔中检索一些全局信息。 then you might do:
那么您可以这样做:
int maxHeight(const MyStack (&towers)[TOWER_CNT])
{
auto res = 0;
for (const auto& tower : towers) {
// Alternatively, you might take the sum, for fixed size towers.
res = std::max(res, tower.tos + 1);
}
return res;
}
int baseWidth(const MyStack (&towers)[TOWER_CNT])
{
auto res = 0;
for (const auto& tower : towers) {
auto base = ((tower.tos == -1) ? 1 : tower.s[0]);
res = std::max(res, base);
}
return res;
}
void printTowers(const MyStack (&towers)[TOWER_CNT]) {
auto height = maxHeight(towers);
auto base = baseWidth(towers);
for ([[maybe_unused]]const auto& _ : towers) {
std::cout << std::string((base) / 2, ' ')
<< std::string(1, '^')
<< std::string((base - 1) / 2, ' ') << " ";
}
std::cout << std::endl;
for ([[maybe_unused]]const auto& _ : towers) {
std::cout << std::string((base) / 2, ' ')
<< std::string(1, '|')
<< std::string((base - 1) / 2, ' ') << " ";
}
std::cout << std::endl;
for (int j = 0; j != height; ++j) {
for (const auto& tower : towers) {
auto plate_index = height - j - 1;
auto count = ((plate_index < tower.tos + 1)
? tower.s[plate_index] // You might use (2 * tower.s[plate_index] + 1)
// For larger but better display.
: 0);
// -1 there are to handle even count for display
std::cout << std::string(base / 2 - count / 2, ' ')
<< std::string(count / 2, '*')
<< std::string(1, count == 1 ? '*' : '|')
<< std::string((count - 1) / 2, '*')
<< std::string((base - 1) / 2 - (count - 1) / 2, ' ') << " ";
}
std::cout << std::endl;
}
for ([[maybe_unused]]const auto& _ : towers) {
std::cout << std::string(base, '=') << " ";
}
std::cout << std::endl;
}
With output similar to: 输出类似于:
^ ^ ^
| | |
*|* | |
***|*** * **|**
****|**** *****|***** ******|******
============= ============= =============
That also handles even number, but display is not fine IMO. 这也可以处理偶数,但显示效果不佳。
Fill free to adapt to your look&feel. 免费填充以适应您的外观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.