2013-10-15 90 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