【问题标题】:print a filled square in console在控制台中打印一个实心方块
【发布时间】:2014-10-18 09:29:06
【问题描述】:

我需要使用我的 C++ 程序(1cm x 1cm 大小)在 Linux 终端中打印一个实心正方形。我尝试使用 ASCII 254 (■),但在终端中它打印为垃圾字符。我不确定如何使用 c++ 打印扩展的 ASCII 字符。这是我尝试打印扩展 ASCII 的两种方法。但没有成功。

第一种方法

for(int i=128; i< 255; i++ )
{
 std::cout << static_cast<char>(i) << std::endl;
}

第二种方法

unsigned char temp = 'A'
for(int i=65; i< 255; i++ )
{
 std::cout << temp++ << std::endl;
 std::wcout << temp << std::endl;
}

有什么建议或替代想法吗?

【问题讨论】:

标签: c++ linux ascii


【解决方案1】:

尝试使用 unicode cout &lt;&lt; "\u25A0";

http://www.fileformat.info/info/unicode/category/So/list.htm

【讨论】:

    【解决方案2】:

    或者尝试一下:

    std::cout << (char)254u;
    

    【讨论】:

      【解决方案3】:

      试试这个:

      char t = -2;
      cout << t;
      

      【讨论】:

        【解决方案4】:

        就像 Sebastian Kuczyński 建议的那样,我们可以用它来做很棒的东西,比如条形图、主机图等。它非常酷。

        代码

        printf("\n\nHistogram of Float data\n");
            for (i = 1; i <= bins; i++)
            {
                count = hist[i];
                printf("0.%d |", i - 1);
                for (j = 0; j < count; j++)
                {
                    printf("%c", (char)254u);
                }
                printf("\n");
            }
        

        输出

        Histogram of Float data
        0.0 |■■■■■■■■■■■■■■■■■■■■■■
        0.1 |■■■■■■■■■■■■■■■■
        0.2 |■■■■■
        0.3 |■■■■■■■■■■■■■■
        0.4 |■■■■■■■■
        0.5 |■■■■■■■■■■■■■■■■
        0.6 |■■■■■■■■■■
        0.7 |■■■■■■■
        0.8 |■■■■■■■■■■■■■■■
        0.9 |■■■■■■■
        

        【讨论】:

          【解决方案5】:

          要获得你想要的输出,试试这个:

          #include<iostream>
          #include<windows.h>
          using namespace std;
          
          void setconsolecolor(int textColor, int bgColor) 
          {
          SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + 
          (bgColor * 16)));
          }
          
          int main()
          {
          cout<<"The chunk of Blocks in the colors : \n";
          cout<<"                                    ";
          setconsolecolor(0,9);
          cout<<"   ";
          setconsolecolor(0,4);
          cout<<"   ";
          setconsolecolor(0,8);
          cout<<"   \n";
          setconsolecolor(0,0);
          return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-02
            • 1970-01-01
            • 1970-01-01
            • 2012-08-18
            • 2016-11-21
            • 2013-01-27
            相关资源
            最近更新 更多