2017-05-03 3 views
-2

C++で次のパターンを開発したいと思います。高さと幅のあるC++スターパターン

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


printf("Enter trunk height: "); 
scanf("%d", &trunk_height); 
printf("Enter trunk width: "); 
scanf("%d", &trunk_width); 
printf("Enter leaves width: "); 
scanf("%d", &leaves_width); 

TRUNCの高さは= 5
が幅を残し9.
TRUNC幅は11

である私はいくつかのコードを記述したが動作していません。私の問題は、truncの高さを検出する方法と、ユーザーから情報を取得するコードを書く方法です。

int main() 
{ 
    int i, j; 
    int x = 5; 
    int y = 1; 

    for(j = 1; j <= 5; j++) 
    { 
     for(i = 1; i <= x; i++) 
     { 
      cout << " "; 
     } 
     x--; 

     for(i = 1; i <= y; i++) 
     { 
      cout << "*"; 
     } 
     y += 2; 

     cout << endl; 
    } 
} 
+1

だからあなたの質問は何ですか? –

答えて

0

コードを以下試してみてください。

#include<iostream> 

using namespace std; 

int main() { 
    int height = 9, width = 5, leave = 11; 

    // Print tree 
    int stars = 1; 
    for(int row = (leave + 2 - 1)/2; row >= 1 ; row--){ 
     cout<<string(row, ' ')<<string(stars, '*')<<endl; 
     stars += 2; 
    } 

    // Print trunk 
    int pos = (leave - width)/2 +1; 
    for(int row = 0; row<height; row++){ 
     cout<<string(pos, ' ')<<string(width, '*')<<endl; 
    } 
} 

出力:

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

Ideone link