【问题标题】:Printing a circle using arrow symbols使用箭头符号打印圆形
【发布时间】:2015-09-16 18:56:51
【问题描述】:

我一直在努力使用箭头符号以及delay();Sleep(); 绘制圆形图案,例如在打印 之后它会延迟大约几秒钟,然后打印 等等。这会给人一个画圈的印象。像

        ↑

   ←         →

        ↓

我尝试到处搜索,到目前为止我发现如何阅读箭头键VM_KEYDOWN documentation。可悲的是,这不是我想要的。请帮忙?附言。我知道我没有发布任何“富有成效的尝试”,那是因为我没有,所以不要生气:X 任何帮助将不胜感激。 :)

更新: 我试图用这次失败的尝试打印箭头。

#include <iostream>
#include <string>
int main() { 
    std::wstring s(L"←→↑↓"); 
    std::wcout << s << "\n"; 
}

UPDATED-2* 所以我设法用这个打印符号:

#include <iostream>
using namespace std;
int main()
{
  char left,right,up,down;

  up = 24;
  down = 25;
  left = 27;
  right = 26;

  cout << up;
  cout << down;
  cout << left;
  cout << right;
  cout << "\n";
  system("PAUSE");  
  return 0;
}

但是现在我需要知道如何使用上面打印的序列。

工作尝试

#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;
int main()
{
    char left, right, up, down;

    up = 24;
    down = 25;
    left = 27;
    right = 26;
    cout << setw(10);
    cout << up;
    cout << endl;
    cout << setw(20);
    Sleep(1000);
    cout << right;

    cout << endl;
    cout << endl;
    cout << endl;

    cout << setw(10);
    Sleep(1000);
    cout << down;
    Sleep(1000);

    cout << left;

    system("PAUSE");
    return 0;
}

但这显然不准确/不高效。

【问题讨论】:

  • 您想在图形窗口还是终端中执行此操作?无论哪种方式,您都需要一个外部库,例如用于终端操作的 nCurses。
  • @Quentin 请详细说明?我打算在控制台上执行此操作(如果有任何意义)什么样的外部库?我在这里读过一些关于 nCurses 的文章:linuxquestions.org/questions/programming-9/… 如果那是正确的
  • 那么 nCurses(Windows IIRC 上的 PDCurses)就是您所需要的。文档是herekeypad() 是箭头键功能所需要的。
  • @Quentin keypad() 只允许如何读取密钥对吗?如果是这样,我确实知道,但是我该如何画它们呢?这就是问题所在。请在这件事上给予我帮助!! :(
  • 所以你只想打印箭头 symbols ?如果是这种情况,您应该删除关于 keys 的部分。这可能与终端中使用的编码和/或字体有关。

标签: c++ visual-studio-2013 geometry symbols


【解决方案1】:

很高兴看到这种努力。我做了一些更改并添加了一个函数来将光标位置设置到您打印 的同一行。试试这个:

    #include <iostream>
    #include <iomanip>
    #include <windows.h>
    #include <conio.h>
    using namespace std;
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD CursorPosition;
    void gotoXY(int, int);

    int main(){
    char left, right, up, down;
    up = 24;
    down = 25;
    left = 27;
    right = 26;
    cout << setw(20) << up<< endl << endl << endl<< setw(25);
    Sleep(1000);
    cout << right << endl<< endl<< endl << setw(20);
    Sleep(1000);
    cout << down;
    Sleep(1000);
    cout << endl;
    gotoXY(0, 2 + (1));
    cout << setw(15) << left<< endl;
    _getch();
    return 0;
   }
    void gotoXY(int x, int y)
    {
        CursorPosition.X = x;
        CursorPosition.Y = y;
        SetConsoleCursorPosition(console, CursorPosition);
    }

我承认它也没有那么高效,但它确实有效并且应该会有所帮助 你会更好地理解/前进。

【讨论】:

  • 这正是我想要的,我知道下一步该做什么了。非常感谢你!!
  • 您可以通过删除多个 cout 来缩短您的代码。喜欢cout &lt;&lt; setw(20) &lt;&lt; up &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl &lt;&lt; setw(25);
  • @Undefined 我很高兴它有帮助:)
猜你喜欢
  • 1970-01-01
  • 2016-02-21
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
相关资源
最近更新 更多