[英]Printing a vertical histogram with cursor manipulation C++[ncurses][Solved w/o ncurses]
我之前曾问过这样的问题,但是由于我不包括印刷订单,这有点误导人了。因为我了解了整个概念的变化,因此我认为再次提出这个问题会更加适当。
#include <iostream>
using namespace std;
int main()
{
int a, b, c, i;
cin >> a >> b >> c;
for ( i = 0; i < a; i++)
cout << "*" << endl;
for ( i = 0; i < b; i++)
cout << "*" << endl;
for ( i = 0; i < c; i++)
cout << "*" << endl;
}
我知道输出与以下内容相同:
for ( i = 0; i < a + b + c; i++ ){
cout << "*" << endl;
}
所以对于2 3 1我得到:
*
*
*
*
*
*
我想要的是:
*
* *
* * * //Horizontal distance between 2 shapes don't matter.
并且必须完全按照该顺序进行。每列的打印也必须使用单独的功能。
第一循环:
*
*
第二循环:
*
* *
* *
最后循环:
*
* *
* * *
* 编辑: *显然还有另一种解决方案可以完全不使用游标进行操作。我的老师建议我应该先将字符存储在char指针中,然后逐行打印该char指针内存。 。
这是一个可以做的诅咒程序
#include <iostream>
#include <curses.h>
using namespace std;
int main(int argc, char** argv)
{
int a,b,c,i;
cin >> a >> b >> c;
initscr(); // initialise curses
int rows, cols;
getmaxyx(stdscr, rows, cols); // get screen size
for (i=0; i<a; i++) {
mvprintw(rows - 1 - i, 0, "*"); // plot at bottom column 0
}
for (i=0; i<b; i++) {
mvprintw(rows - 1 - i, 1, "*"); // plot at bottom column 1
}
for (i=0; i<c; i++) {
mvprintw(rows - 1 - i, 2, "*"); // plot at bottom column 2
}
refresh(); // update screen
getch(); // exit when key is pressed
endwin(); // exit curses
return 0;
}
您无法按照自己的方式进行操作。 由于您无法垂直输出到控制台,因此您需要一次打印一条水平线。
因此,首先,您需要找出totalLines
(总共为a
, b
和c
的最大值)中总共需要多少行。 然后,您应该遍历每行。
在行迭代中,您需要在正确的位置打印出正确数量的*
s。 是否需要为a
画点的条件是a >= totalLines - line
(其中, line
是当前线,从第一行的0开始)。 与b
和c
相似,因此您将需要3个具有这些条件的if
语句,每个条件都打印出空格或*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.