gpt4 book ai didi

c++ - 如何让几何形状的面积和周长起作用?

转载 作者:行者123 更新时间:2023-11-28 03:06:51 25 4
gpt4 key购买 nike

<分区>

计算面积和几何形状的周长。首先要求用户输入一个字母代表形状。我们用 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 之外,我输入的所有内容都会重复相同的问题。 Q 只是停止,但输入的任何其他章程都会要求 Please Enter Shape (C: Circle, S: Square, R: Rectangle Q:quit)

这是怎么回事?

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com