2013-10-17 66 views
-5

如何为此创建伪代码? 开发应用程序来计算面积和几何形状的周长。首先要求用户输入代表形状为 的字母。我们用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) >Q 

#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); 
} 
+1

堆栈溢出不会为你做你的工作。请告诉我们你已经尝试过什么,失败等等。 –

+0

这是我迄今为止所做的,但不知道如何使用模块。 – user2880024

+0

好吧,“模块”是一个非常通用的术语,没有上下文,很难知道究竟是什么意思。但是我会假设你需要编写单独的类? –

回答

1

创建一个抽象类,形状和三个派生类圆形,矩形和广场。在抽象类中定义三个虚函数:两个用于计算周长和面积,另一个用于输入适当的初始值。

+0

每个样子的代码会怎样? – user2880024

+0

基本上,你从'if'块中取出代码,并将它们放入每个类的相应方法中。if(shape ==“Square”'代码进入'askValues()'和'calculatePerimeter() 'Square'类的功能等。 –

+0

是否有可能让您更详细地展示这一点? – user2880024