我的輸出應該是四個三角形和一個金字塔。我設法得到了四個三角形,但無法弄清金字塔。任何幫助都會很棒。 (我也必須使用setw和setfill)。使用setw建立一個金字塔
輸出是左對齊的三角形,然後左對齊倒置。 右對齊三角形,然後右對齊三角形倒置。
這是我的電流輸出:
#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
它很難理解你想要什麼顯示圖像圖 –
感謝您的建議。我已添加目前的輸出。最後一個三角形應該是金字塔。 – Ace