2014-04-12 51 views
0

enter image description here我剛剛完成通過這個帕斯卡三角項目和輸出是不正確的三角形,任何人有任何想法,爲什麼發生這種情況,間距看起來有關的路要走爲什麼它不是一個完美的三角形C++?

#include <string> 
#include <iostream> 
using namespace std; 
int factorial(int numPar); //the factorial function will recieve a number, and return that number's factorial 
int pascalNum(int rowPar, int elementPar); 
int main(){ 
    //declarations 
    string strOutput = ""; 
    int userNum; 
    int rowCounter = 0; 
    int elementCounter = 0; 
    int columnsPerRow = 1; 
    int spaces; 
    int initialSpaces; 
    int counter = 0; 
    //get user input 
    cout << "Enter an integer from 1 to 10: "; 
    cin >> userNum; 
    while (userNum > 10 || userNum < 1){ 
     cout << "Invalid entry: " << endl; 
     cin >> userNum; 
    } 
    initialSpaces = userNum + 4; //needed to make the triangle an isoscoles, and keep it away from left 
    //calculations 
    while ((rowCounter + 1) <= userNum){ 
     for (counter = initialSpaces; counter > 0; counter--){ 
      strOutput = strOutput + " "; 
     } 
     while (elementCounter < columnsPerRow){ 
      strOutput = strOutput + to_string(pascalNum(rowCounter, elementCounter)); 
      if (pascalNum(rowCounter, elementCounter) < 10){ 
       spaces = 3; 
      } 
      else if (pascalNum(rowCounter, elementCounter) < 100){ 
       spaces = 2; 
      } 
      else if (pascalNum(rowCounter, elementCounter) < 1000){ 
       spaces = 1; 
      } 
      while (spaces > 0){ 
       strOutput = strOutput + " "; 
       spaces = spaces - 1; 
      } 
      elementCounter = elementCounter + 1; 
     } 

     cout << strOutput << endl; 
     columnsPerRow = columnsPerRow + 1; 
     elementCounter = 0; 
     rowCounter = rowCounter + 1; 
     //initialSpaces--; //this makes there be less and less space until the triangle starts 
     strOutput = ""; 
    } 

    system("pause>nul"); 
    return 0; 
} 
int factorial(int numPar) { 
    //declarations 
    int counter = 1; 
    int numResult = 1; 
    int initial = numPar; 
    if (numPar > 1){ 
     while (counter <= numPar){ 
      numResult = numResult * counter; 
      counter++; 
     } 
     return numResult; 
    } 
    else 
     return 1; 
} 
int pascalNum(int rowPar, int elementPar){ 
    int answer; 
    answer = factorial(rowPar)/(factorial(elementPar) * factorial(rowPar - elementPar)); 
    return answer; 
} 
+5

如果你想描述你想要的代碼做什麼,並且具體描述它與當前的做法有什麼不同,它會幫助你得到更好的答案。 – Edward

+0

我想要一個完美的三角形,這是有點傾斜不完美,我只是想弄清楚如何從cmd複製輸出 –

+0

任何機會,你可以添加你當前的輸出? – Deduplicator

回答

2

右它只是排列會那麼,你正在討論佈局錯誤。首先你必須弄清楚你的號碼單元佈局。你有這樣做的代碼的開始,但你把所有額外的空間在右邊。

你可能想要的是以中心爲中心的東西。這意味着,對於最多三位數字,您需要左側的一個填充空間,而對於一個數字,您需要兩側都有空格。總體而言,你希望單元之間有一個空間。所以,你的填充代碼更改爲:

for(int elementCounter = 0; elementCounter < columnsPerRow; 
     elementCounter = elementCounter + 1){ 
     // Don't Repeat Yourself 
     int num = pascalNum(rowCounter, elementCounter); 
     string numStr = to_string(num); 
     if (num < 10){ 
      numStr = numStr + " "; 
     } 
     if (num < 100){ 
      numStr = string(" ") + numStr; 
     } 
     strOutput += numStr; 
    } 

現在,你知道你的代碼有三種可能的數字和一個填充空間,細胞抽出它應該是什麼樣子的一個小的測試案例:

### 
    ### ### 
### ### ### 

現在看圖案,並且lo每行左側有兩個縮進空格,或總計爲2 * (9 - r),其中r0變爲9。因此解決您的外(行)循環,並擺脫initialSpaces東西:

while ((rowCounter + 1) <= userNum){ 
    for (counter = 2 * (9 - rowCounter); counter > 0; counter--){ 
     strOutput = strOutput + " "; 
    } 
    // ... continue as above ... 

,這應該解決的事情。故事的道德:

不要忘了制定你的預期輸出格式。

如果必須使用方格紙。