2013-10-15 91 views
0

的面積和周長,我想制定如下方案,並正在尋求建議:計算幾何形狀

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

One run of the program will look like this: 
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) >Q 

#include<iostream.h> 
#include<conio.h> 
void main() 
{ 
//clear the screen. 
clrscr(); 
//declare variable type int 
int a,peri; 
//Input the side and save it in 'a' 
cout<<"Enter the side of square"<<endl; 
cin>>a; 
//calculate perimeter and save it in 'peri' 
peri=4*a; 
//show the output 'peri' 
cout<<"Perimeter of square is "<<peri; 
//get character 
getch(); 
} 

這在C或C++中看起來如何?

+0

你熟悉的'while'和'之開關語句?這些對你做這個家庭作業是有用的。 – Floris

+0

switch語句不太好,這只是練習題。 – user2880024

+0

嗨!請看看我如何編輯你的文章,使其更加平易近人。關於這個問題本身,這個廣場看起來是一個好的開始。嘗試實施詢問用戶現在想要的形狀,如果有任何問題提出挑戰,請告訴我們。 –

回答

1

下面的代碼片段給你一些建議:

printf("enter S, C, R, or Q (to quit):\n"); 
while((c = lower(getc())) != 'q') { 
    switch(c) { 
    case 'r': 
     // get input for rectangle 
     break; 
    case 's': 
     // ditto for square 
     break; 
    case 'c': 
     // ditto for circle 
     break; 
    else: 
     // deal with "unexpected input" 
    } 
    printf("enter S, C, R, or Q (to quit):\n"); 
} 

這應該讓你開始...

1

你需要有些東西,

  • 的main()函數返回int
  • 你想爲形狀選擇單個字符,
  • 焦炭形狀; cin >>形狀將做你想做的
  • 你將需要或者前言cin,cout和endl與std ::
  • 或者你需要聲明,使用namespace std;
  • 你將需要測試針對C,R,S,Q你的形狀進入,
  • 你想要一個外循環,以檢查「Q」是否進入
  • 你想只允許大寫? (您可能需要使用TOUPPER)

這裏是一個骨架,

#include <iostream> 
#include <conio> 
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 
     } 
     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); 
} 
+0

錯過了使用'switch'的絕好機會 - 但是對於新手程序員來說,這裏有很多有用的信息。 – Floris

+0

同意,切換是正確的答案,但新手需要看if-else-if模式;試圖幫助新程序員理解。 – ChuckCottrill

+0

謝謝,但它只是要求只有形狀。例如當我輸入S時,它不會要求一方。 – user2880024