0
我剛剛完成通過這個帕斯卡三角項目和輸出是不正確的三角形,任何人有任何想法,爲什麼發生這種情況,間距看起來有關的路要走爲什麼它不是一個完美的三角形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;
}
如果你想描述你想要的代碼做什麼,並且具體描述它與當前的做法有什麼不同,它會幫助你得到更好的答案。 – Edward
我想要一個完美的三角形,這是有點傾斜不完美,我只是想弄清楚如何從cmd複製輸出 –
任何機會,你可以添加你當前的輸出? – Deduplicator