2013-10-18 36 views
0

計算幾何形狀的面積和 周長。首先要求用戶輸入代表形狀爲 的字母。我們用C表示圓,R表示矩形,S表示方形。
用戶選擇形狀後,程序會相應地提示相應的形狀的適當的 尺寸。例如,如果用戶選擇了一個正方形,該程序將要求一方。如果它是一個圓圈,程序將要求半徑。如果 它是一個矩形,它會詢問長度和寬度。 收到適當的尺寸後,程序將計算所需形狀的面積和周長,並將其打印在屏幕上。再次,代碼 將要求另一封信。如果用戶輸入'Q',則程序終止。如何獲得幾何形狀的面積和周長的工作?

該方案的運行將是這樣的:

Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) 
>S 
Please enter the side of the square 
> 8 
The area is 64 and the perimeter is 32 
Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) 
>R 
Please enter the width of the rectangle 
> 5 
Please enter the length of the rectangle 
> 7 
The area is 35 and the perimeter is 24 
Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) 

這是我迄今所做的,但不知道爲什麼我不能得到它要求的側方當我按S.

我做了什麼得到的是:

Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) 

併除外問:我輸入什麼都它重複了同樣的問題。 Q只是停止,但任何其他包機輸入將要求Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit)

發生了什麼事情?在測試之前,從

if (shape == 'R') 

#include <iostream> 

using namespace std; 
int main() 
{ 
    //clear the screen. 
    //clrscr(); 
    //declare variable type int 
    char shape = 'N'; //none 
    int area, perimeter; 
    while(shape != 'Q') 
    { 
     cout<<"Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit) >"<<endl; 
     //get shape choice 
     cin>>shape; 

     if(shape == 'C') 
     { 
     int radius; 
     //Circle, radius 
     cout<<"Please enter the radius >"<<endl; 
     } 
     else if(shape == 'S') 
     { 
     int side; 
     //Input the side 
     cout<<"Please enter the side of the square >"<<endl; 
     //Square, side 
     cin>>side; 
     //calculate perimeter and save it in 'peri' 
     perimeter=4*side; 
     //show the output 'perimeter' 
     cout<<"Perimeter of square is "<<perimeter<<endl; 
     } 
     else if(shape == 'R') 
     { 
     int width,length; 
     //Rectangle, width,length 
     } 
    } 
    return(0); 
} 
+3

你輸入的'還是'S'? –

+0

正如@ScottMermelstein所說,一個可能的原因可能是您以小寫形式輸入選項,而代碼期望爲大寫形式。 – heretolearn

+0

是的,你是寫我不得不輸入大寫。但我怎麼能讓它首先要求比周界更大的區域? –

回答

2

改變你的條件語句要

if (shape == 'R' || shape == 'r') 

或者,將其改爲全部爲1的情況下:

cin >> shape; 
shape = std::toupper(shape); 
if (shape == 'R') // already know it is between A-Z, so we can just check the uppercase 
+0

謝謝,但我有一個問題找到圓形和矩形區域和參數。 –

+0

也我怎麼會要求的廣場是因爲我確實得到廣場的參數? 請輸入形狀(C:圓形,S:方形,R:矩形Q:退出)> s 請輸入正方形> 正方形的周長爲8 –

+0

在您發佈的代碼中,詢問這些參數(也就是說,沒有'cin >>半徑;'語句 - 與矩形塊相同)。就像你在廣場上做的那樣,你必須要求你想要的參數。 –