gpt4 book ai didi

c++ - 使用光标操作打印垂直直方图 C++[ncurses][解决了 w/o ncurses]

转载 作者:行者123 更新时间:2023-11-28 07:37:25 25 4
gpt4 key购买 nike

我以前问过这样的问题,但它有点误导,因为我没有包括打印顺序。自从我了解了整个概念的变化后,我认为再问一次会更合适。

 #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.

并且必须完全按照该顺序完成。此外,每列的打印必须使用单独的函数完成。

第一个循环:

*

*

第二个循环:

    *

* *

* *

最后一个循环:

    *

* *

* * *

*编辑:*显然还有另一种解决方案,无需任何光标操作。我的老师建议我应该首先将字符存储在一个字符指针中,然后打印该字符逐行指针内存。效果很好。

最佳答案

这是一个 curses 程序可以做到这一点

#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;
}

关于c++ - 使用光标操作打印垂直直方图 C++[ncurses][解决了 w/o ncurses],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16496639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com