2017-04-06 32 views
1

我写了一个代码,使用星号打印一个数字 - 无论是消极的还是积极的。我的程序从用户的输入中反转数字,然后将它切成数字逐个打印,但不在同一行中。这里是我的代码:在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位数字? p.s.如果有任何简单的方法(只使用循环,当然(if-else)),不要写完整的代码,只提示。

+2

当心,功课来了。 :P –

+1

构建成3个'std :: string',最后打印出来。 – Jarod42

+0

@斯巴达克斯的确如此:P,它是一项功课,但我不是要求解决方案:D –

回答

0

这是暗示,而不是打印的明星直接,尽量用std::string(或字符数组)

例如,这是我将存储的星方式:

s[0][0]="***"; 
s[0][1]="* *"; 
s[0][2]="***"; 

然后使用for迭代s,以便可以打印所有数字的第一行,然后是第二行,然后是第三行。

1

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

设置变量和:

使循环从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: 

    *** *** *** 
    ***  * *** 
    ***  *  * 
+0

谢谢,我不应该使用数组,但我会找到一种方式,而不使用它们。 –

+0

您可以使用3个变量作为顶部中间和底部的数字,而不是数组。 – emrebaris

+0

cout << topof8 <<“\ t”; cout << topof7 <<“\ t”; cout << topof9 <<“\ t”; cout << middleof8 <<“\ t”; cout << middleof7 <<“\ t”; cout << middleof9 <<“\ t”; cout << bottomof8 <<“\ t”; cout << bottomof7 <<“\ t”; cout << bottomof9 <<“\ t”; – emrebaris

0

而不是一次打印三条线。对于每个数字打印只是第一行(中间有空格)。然后重复中间和最后一行的过程。只有在行结束后才打印endl。

因此,我们需要首先以高效的方式指定数组的样子。我们将创建char数组。

char top_line[10][4] = {"***", " *", "** ", "***", "* *", " **", "* ", "***", "***", "***" }; 
char middle_line[10][4] = {"* *", " *", " * ", " **", "***", " * ", "***", " *", "***", "***" }; 
char bottom_line[10][4] = {"***", " *", " **", "***", " *", "** ", "***", " *", "***", " *" }; 
cout <<"Enter a number:"; 
int n; 
cin >> n; 
int n_copy = n; 
// We count the number of digits in n; 
int digits = 1; 
while(n) { 
    n/=10; 
    digits *= 10; 
} 
digits = digits/10; 
n = n_copy; 
stringstream line1, line2, line3; 
while (n) { 
    int d = n/digits; 
    n = n % digits; 
    digits /= 10; 
    line1 << top_line[d] << " "; 
    line2 << middle_line[d] << " "; 
    line3 << bottom_line[d] << " "; 
} 
cout << line1.str() << endl << line2.str() << endl << line3.str() << endl; 

演示:Ideone

+0

我在想那个,但我相信它很丑。不管怎样,谢谢! –

+0

您可以创建常量数组,因此您无需编写大量条件即可。让我把它放在答案中。 –

+0

@PaulR我失去了踪迹。将解决它。 –