c++ - 在 C++ 中使用星号绘制数字

标签 c++

我写了一个代码,它使用星号打印一个数字——无论是负数还是正数。我的程序从用户输入中反转数字,然后将其切成数字并逐个打印,但不在同一行中。这是我的代码:

#include <iostream>

using namespace std;

int main() {

    int n, i = 0, reverse = 0, digit;

    cout << "Enter an integer:";
    cin >> n;
    cout << endl;


    if(n < 0){
     cout << "--"<<endl;
     n = n - 2*n;
     }

    while(n > 0){

        reverse = reverse*10 + n%10;

        n = n / 10;

    }

   while(reverse > 0){
        digit = reverse % 10;
        reverse = reverse / 10;


    if(digit == 0){ 
        cout << "***"<<endl;
        cout << "* *"<<endl; 
        cout << "***"<<endl;
        cout << endl;
        }
    if(digit == 1){
        cout << " * "<<endl;
        cout << "** "<<endl;
        cout << "***"<<endl;
        cout << endl;
        }
    if(digit == 2){
        cout << "** "<<endl;
        cout << " * "<<endl;
        cout << " **"<<endl;
        cout << endl;
        }
    if(digit == 3){ 
        cout << "***"<<endl;
        cout << " **"<<endl;
        cout << "***"<<endl;
        cout << endl;
        }
    if(digit == 4){
        cout << "* *"<<endl;
        cout << "***"<<endl;
        cout << "  *"<<endl;
        cout << endl;
        }
    if(digit == 5){
        cout << " **"<<endl;
        cout << " * "<<endl;
        cout << "** "<<endl;
        cout << endl;
        }
    if(digit == 6){
        cout << "*  "<<endl;
        cout << "***"<<endl;
        cout << "***"<<endl;
        cout << endl;
        }
    if(digit == 7){
        cout << "***"<<endl;
        cout << "  *"<<endl;
        cout << "  *"<<endl;
        cout << endl;
        }
    if(digit == 8){
        cout << "***"<<endl;
        cout << "***"<<endl;
        cout << "***"<<endl;
        cout << endl;
        }
    if(digit == 9){
        cout << "***"<<endl;
        cout << "***"<<endl;
        cout << "  *"<<endl;
        cout << endl;
        }

    }   
}

我很好奇,如果有任何可能的方法可以在同一行中打印数字,或者如果不是任何数字,是否可以使用固定数字,例如:3 位数字或 4-数字? 附:如果有任何简单的方法(仅使用循环,当然还有 (if-else)),请不要编写完整的代码,只需提示即可。

最佳答案

您需要的变量: digCount:你有多少位数, digits[digCount]:数字大小的整数数组。

设置变量并:

从 0 到 digCount 做一个循环。在这个循环中,首先写下所有数字的顶部,然后是中间,最后是底部。为了表明我的意思:

假设你有 879:

for(int i = 0; i < digCount; i++)
{
    cout<< topofDigit[i] << \t;
}

cout << "\n";

for(int i = 0; i < digCount; i++)
{
    cout<< middleofDigit[i] << \t;
}

cout << "\n";

for(int i = 0; i < digCount; i++)
{
    cout<< bottomofDigit[i] << \t;
}

cout << "\n";

我希望它能给你一个意见。为了说明更多,这个程序首先会写:

    ***    ***    ***

Then:

    ***      *    ***

And finally:

    ***      *      *

Final output:

    ***    ***    ***
    ***      *    ***
    ***      *      *

关于c++ - 在 C++ 中使用星号绘制数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43248992/

相关文章:

c# - 如何将二进制文件头示例代码(C++/C#)转换为Python?

c++ - 仅在 QTabWidget 的事件选项卡上关闭按钮

嵌入许多简单变量和一个嵌套结构的结构的 C++ 深度复制..(memcpy?)

c++ - 在 header 之间调用时,模板实例化无法匹配重载流运算符的参数列表

c++ - makefile 运行它编译的代码

C++ - 需要左值作为赋值的左操作数

c++ - Qt5Qmld.dll缺失 : could not start the simple qt application because Qt5Qmld. dll缺失

c++ - Visual Studio 字体和颜色

c++ - libc++:为什么关闭后流仍然很好

c++ - 钻石方形算法创建对角线切割