我的输出应该是四个三角形和一个金字塔。我设法得到了四个三角形,但无法弄清楚金字塔。任何帮助都会很棒。(我还必须使用 setw 和 setfill)。
输出是一个左对齐的三角形,然后左对齐倒置。右对齐三角形,然后右对齐三角形倒置。
这是我当前的输出:
#include <iostream>
#include <iomanip>
using namespace std;
//setw(length)
//setfill(char)
int height; //Number of height.
int i;
int main()
{
cout << "Enter height: ";
cin >> height;
//upside down triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n"; //line break between
//rightside up triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//right aligned triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(i-height) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//upside down/ right aligned triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill (' ') << setw(height-i+1) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//PYRAMID
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(height-i*3) << " "; //last " " is space between
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
}//end of main