2017-04-06 31 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我失去了蹤跡。將解決它。 –