为什么它不是一个完美的三角形C ++?

时间:2014-04-12 03:31:22

标签: c++ pascals-triangle

enter image description here我刚刚完成了这个pascal三角形项目并且输出不是一个正确的三角形,任何人都知道为什么会发生这种情况,间距看起来正确它只是对齐的方式

#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;
}

1 个答案:

答案 0 :(得分: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; 
    }

既然您知道您的代码具有三个可能的数字和一个填充空间的单元格,请为小型测试用例绘制它应该是什么样子:

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

现在看看模式,并且每行左侧有两个缩进空格,或总共2 * (9 - r),其中r0变为9 。相应地修复外部(行)循环并删除initialSpaces内容:

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

那应该解决问题。故事的道德:

不要忘记绘制预期的输出格式。

如果必须,请使用方格纸。