使用光标操作打印垂直直方图c++ [ncurses][解决了无ncurses]
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.
必须完全按照这个顺序来做。而且,每一列的打印必须用一个单独的功能来完成。
第一个循环:
*
*
第二个循环:
*
* *
* *
去年循环: *
* *
* * *
*EDIT:*显然还有一个不使用游标操作的解决方案。我的老师建议我应该先将字符存储在一个char指针中,然后逐行打印该char指针内存。
下面是一个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;
}
你不能按你想要的方式来做。您需要一次打印一条水平线,因为您不能垂直输出到控制台。
所以首先你需要找出你总共需要多少行,totalLines
,这是a
, b
和c
的最大值。然后,您应该遍历每一行。
在行迭代中,您需要在正确的位置打印出正确数量的*
s。是否需要为a
画一个点的条件是a >= totalLines - line
(其中line
是当前的线,从0开始为第一条线)。与b
和c
类似,因此您需要3条if
语句,其中每个语句打印空格或*
。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- visual c++,如何获取解决方案目录中的代码
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 使用光标操作打印垂直直方图c++ [ncurses][解决了无ncurses]