2010-03-04 3 views
0

사용자 입력 옵션을 사용하여 사다리꼴을 만들려고합니다. 내 코드가 최선의 방법은 아닐지도 모르지만 지금까지는 작동합니다! 내 문제는 내가 사다리꼴의 기지 출력 창 왼쪽을 만질 필요합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 다음 동작이 아니라 전체 라인 폭을 설정 setw사용자가 입력 한 문자를 사용하여 사다리꼴 만들기. (Console App)

#include <iostream> 
#include <iomanip> 
#include <cmath> 

using namespace std; 

int main() 
{ 
    int topw, height, width, rowCount = 0, temp; 
    char fill; 

    cout << "Please type in the top width: "; 
    cin >> topw; 

    cout << "Please type in the height: "; 
    cin >> height; 

    cout << "Please type in the character: "; 
    cin >> fill; 

    width = topw + (2 * (height - 1)); 
    cout<<setw(width); 

    for(int i = 0; i < topw;i++) 
    { 
     cout << fill; 
    } 
    cout << endl; 
    rowCount++; 
    width--; 

    temp = topw + 1; 

    while(rowCount < height) 
    { 
     cout<<setw(width); 

     for(int i = 0; i <= temp; i++) 
     { 
      cout << fill; 
     } 
     cout << endl; 

     rowCount++; 
     width--; 
     temp = temp +2; 
    } 
} 
+0

인가 :

또한, 일부 중복 코드를 시도있을 것 같습니다? – Xorlev

+0

"최고의 사다리꼴"은 무엇을 의미합니까? –

답변

1

. 따라서 단일 한 wout < < 채우기의 너비는 값으로 설정됩니다. 이것은 패딩 (padding)을 제공하지만 최종 행의 경우 setw를 0으로 설정해야합니다. 이 숙제

int main() 
{ 
int topw, height, width, rowCount = 0, temp; 
char fill; 

cout << "Please type in the top width: "; 
cin >> topw; 

cout << "Please type in the height: "; 
cin >> height; 

cout << "Please type in the character: "; 
cin >> fill; 

width = height; 
cout<<setw(width); 

temp = topw; 

while(rowCount < height) 
{ 
    cout<<setw(width); 

    for(int i = 0; i < temp; i++) 
    { 
     cout << fill; 
    } 
    cout << endl; 

    rowCount++; 
    width--; 
    temp = temp +2; 
} 
} 
관련 문제