0

在此处输入图像描述我刚刚完成了这个帕斯卡三角形项目,输出不是一个正确的三角形,任何人都知道为什么会这样,间距看起来很正确,只是对齐方式偏离了

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

1 回答 1

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)总共有两个缩进空格,r09。相应地修复您的外部(行)循环并摆脱这些initialSpaces东西:

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

这应该可以解决问题。故事的道德启示:

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

必要时使用方格纸。

于 2014-04-12T04:14:33.840 回答