[技術討論] C++問題請教- 平行四邊型

大家好, 小弟係一個學緊C++既新手, 係寫一個小程式當中出現左小小問題, 希望各位師兄指教

呢個係我想要既效果,

This program will output a parallelogram.
How long do you want each side to be? 5
Please enter the character you want it to be made of: #
#
##
###
####
#####
   ####
      ###
        ##
          #

但係我寫出黎既效果就係咁

How long do you want each side to be?5
Please enter the character you want it to be made of:#
#
##
###
####
#####
# # # #
  #  #  #
   #   #
    #

我諗係我用SETFILL同SETW出左問題, 但係我試左好耐都搞唔掂, 希望各位指點指點!, 謝謝 (仲有係我既LOOP 寫得有冇問題? 有冇野要改進?)

呢度係我寫既CODE...


#include <iostream>
#include <iomanip>
using namespace std;


int main()

{

   int n;
   int r;
   int c;
   char w;
   
   cout << "How long do you want each side to be?" ;
   cin >> n;
   
   cout << "Please enter the character you want it to be made of:" ;
   cin >> w;

for (r = 1; r <= n; ++r)      
{
    for (c = 1; c <= r; ++c)     
        cout << w;
    cout << endl;
}


for (r = n - 1; r >= 1; --r)  
{
    for (c = 1; c <= r; ++c)     
        cout << setw(n-r+1) << right << w;
      cout << endl;
}
}

你響個loop入面setw, 咁咪粒粒character唔同width
提示, 唔好用setw, 請自己計翻要出幾多個space響前面, 就loop幾多次cout << " ";

TOP

令我諗起CS2331 ge日子lol

TOP

本帖最後由 梁志健 於 2014-3-18 19:38 編輯
你響個loop入面setw, 咁咪粒粒character唔同width
提示, 唔好用setw, 請自己計翻要出幾多個space ...
KinChungE 發表於 2014-3-18 18:37


師兄, 可唔可以用簡單例子講解一下...?
又或者係LOOP入邊用printf又得唔得呢?

你講個計返空格O個個我明明地, 但係我唔知點執行

TOP

師兄, 可唔可以用簡單例子講解一下...?
又或者係LOOP入邊用printf又得唔得呢?

你講個計返空格O個個我明 ...
梁志健 發表於 2014-3-18 19:31


假設要出
****
 ***
  **
   *

就用以下code:
int size = 4;

for (int row = 0; row < size; row++) {
    for (int pos = 0; pos < size; pos++) {
        if (pos < row) cout << " ";
        else cout << "*";
    }
    cout << endl;
}

TOP

唔該師兄, 俾少少時間我消化下先...感激

TOP

出到了, 唔該師兄, 原來要改左我原來個LOOP.
你個下半三角係首先出長方型, 再用個IF ELSE去出返D空格.
技術上我仲係唔係好明, 但係原理上我開始明明地.
唔該晒師兄, 等我再慢慢研究下先..

TOP

我既完成品, 多謝各位!

How long do you want each side to be?5
Please enter the character you want it to be made of:@

@
@@
@@@
@@@@
@@@@@
    @@@@
        @@@
            @@
                 @
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;


  4. int main()

  5. {

  6.    int n;
  7.    int r;
  8.    int c;
  9.    char w;
  10.    
  11.    cout << "How long do you want each side to be?" ;
  12.    cin >> n;
  13.    
  14.    cout << "Please enter the character you want it to be made of:" ;
  15.    cin >> w;

  16. for (r = 0; r < n; r++)      
  17. {
  18.     for (c = 0; c < r; c++)     
  19.         cout << w;
  20.     cout << endl;
  21. }

  22. for (r = 0; r < n; r++)  

  23. {

  24.     for (c = 0; c < n; c++)
  25. {
  26.    if (c < r) cout << " ";
  27.    else cout << w;
  28.    
  29. }
  30. cout << endl;

  31. }
  32. }
複製代碼

TOP

我既完成品, 多謝各位!

How long do you want each side to be?5
Please enter the character you want it ...
梁志健 發表於 2014-3-18 20:48

我既完成品, 岩岩得閒無野做,整埋個有 角度既比你


比你參考下 , 遲啲你都要 學 array 架喇 !
  1. //============================================================================
  2. // Name        : testing.cpp
  3. // Author      :
  4. // Version     :
  5. // Copyright   : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================

  8. #include <iostream>
  9. using namespace std;

  10. #include <iostream>
  11. #include <iomanip>
  12. #include <math.h>
  13. using namespace std;

  14. #define PI 3.14159265

  15. int main()

  16. {
  17.         int L_SIDE_LENGTH;
  18.         int UP_SIDE_LENGTH;
  19.         int ANGEL;
  20.         char symbol;
  21.    cout << "How long do you want each left/right side to be?" ;
  22.    cin >> L_SIDE_LENGTH;


  23.    cout << "How long do you want each top/bottom side to be?" ;
  24.    cin >> UP_SIDE_LENGTH;


  25.    cout << "what is the angel between two side?" ;
  26.    cin >> ANGEL;


  27.    // enlarge ten time to be more precise
  28.    int ENLARGE =1;
  29.    int possible_width = L_SIDE_LENGTH *L_SIDE_LENGTH *ENLARGE *ENLARGE ;
  30.    int possible_height = UP_SIDE_LENGTH *UP_SIDE_LENGTH  *ENLARGE *ENLARGE;
  31.    int map[possible_width][possible_height];

  32.    for(int i =0; i<possible_height ; i++)
  33.                       for(int j=0 ; j < possible_width ; j++)
  34.                                          map[i][j] = 0;

  35.    for(int i =0; i< L_SIDE_LENGTH*ENLARGE; i++){
  36.            int y = i;
  37.            int x = round((i)/cos(ANGEL* PI / 180.0));
  38.            // The left side
  39.            // start from top-left corner
  40.            map[i][0] = 1;

  41.            // The top side
  42.            map[y][x] = 1;

  43.            // The Bottm side
  44.            // parallelogram property in this case (transpose height)
  45.            map[y+(L_SIDE_LENGTH*ENLARGE)][x] = 1;

  46.            // the right side (transpose height and width)
  47.            int transpose_x =  round(( (L_SIDE_LENGTH*ENLARGE))/cos(ANGEL* PI / 180.0)) ;
  48.            int transpose_y =  (L_SIDE_LENGTH*ENLARGE);
  49.            map[i+transpose_y][transpose_x] = 1;
  50.    }
  51.    cout << "Please enter the character you want it to be made of:";
  52.    cin >> symbol;
  53.    for(int i =0; i<possible_height ; i++){
  54.            int row_start = -1;
  55.            int row_end = -1;
  56.                       for(int j=0 ; j < possible_width ; j++){
  57.                                          if(map[i][j] == 1){
  58.                                                             if(row_start == -1)
  59.                                                                                row_start =j;
  60.                                                             else if(row_end == -1)
  61.                                                                                row_end = j;

  62.                                          }
  63.                       }
  64.                       // fill in the bounded area
  65.                       for(int k = row_start ; k< row_end;k++)
  66.                                          map[i][k] = 1;
  67.    }

  68.    for(int i =0; i<possible_height ; i++){
  69.                       for(int j=0 ; j < possible_width ; j++){
  70.                                          if(map[i][j] == 0)
  71.                                                             cout << " ";
  72.                                          else
  73.                                                             cout << symbol;

  74.                       }
  75.                       cout << endl;
  76.    }

  77. }
複製代碼

TOP

樓上真係好得閒 , 同埋好無聊,
咁得閒, 不如寫下啲有實際用途小程式, 貢獻呢個社群

TOP