سؤال

enter image description hereI just finished going through this pascal triangle project and the the output is not a proper triangle , anyone have any idea why this is happening , the spacing looks about right its just the alignment that's way off

#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;
}
هل كانت مفيدة؟

المحلول

Well, you're going about the layout wrong. First you've got to work out your number cell layout. You've got the start of code that does this, but you put all the extra spaces on the right.

What you probably want is something that's centered. Which means that, for a maximum of three digits, you'll want the one padding space on the left, and for one digit, you want a space on either side. And overall you want one space between cells. So your padding code changes to:

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

Now that you know your code has cells with three possible digits and one padding space, draw out what it should look like for a small test case:

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

Now look at the pattern, and lo, there are two indent spaces on the left per row, or 2 * (9 - r) total, where r goes from 0 to 9. Fix your outer (row) loop accordingly and get rid of the initialSpaces stuff:

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

And that should fix things. Moral of the story:

Don't forget to draw up your expected output format.

Use graph paper if you have to.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top