编写一个计算简单平面周长的程序,提示用户输入不同的值计算不同形状的周长。 提示用户输入1时计算圆形的周长。提示用户输入圆形的半径,计算圆形周长,并屏幕输出。

编写一个计算简单平面周长的程序,提示用户输入不同的值计算不同形状的周长。
提示用户输入1时计算圆形的周长。提示用户输入圆形的半径,计算圆形周长,并屏幕输出。


#include <iostream>
using namespace std;

const double PI = 3.14159; // 圆周率

int main() {
    int shape;
    double radius, circumference;

    cout << "请输入要计算周长的形状:" << endl;
    cout << "1. 圆形" << endl;
    cout << "2. 其他形状(暂不支持)" << endl;
    cout << "选择形状(输入对应数字):";
    cin >> shape;

    switch (shape) {
        case 1:
            cout << "请输入圆形的半径:";
            cin >> radius;
            circumference = 2 * PI * radius;
            cout << "圆形的周长为:" << circumference << endl;
            break;
        default:
            cout << "对不起,暂不支持该形状的周长计算。" << endl;
    }

    return 0;
}

引用 皆我百晓生 小程序回复内容作答:

#include <iostream>
using namespace std;

int main() {
    int shape;
    cout << "请输入要计算的形状(1代表圆形):";
    cin >> shape;

    if (shape == 1) {
        double radius;
        cout << "请输入圆形的半径:";
        cin >> radius;

        double circumference = 2 * 3.14159 * radius;
        cout << "圆形的周长为:" << circumference << endl;
    } else {
        cout << "未识别的形状,请重新输入!" << endl;
    }

    return 0;
}

【以下回答由 GPT 生成】

我将使用C++编写一个计算简单平面周长的程序。

首先,我将使用一个无限循环,让程序一直运行,直到用户选择退出。

在循环内部,我将输出一个菜单给用户,让他们选择计算不同形状的周长。

接下来,我将使用switch语句根据用户的选择执行不同的操作。

如果用户选择1,表示计算圆形的周长,我将提示用户输入圆的半径,并使用下面的公式计算周长:

周长 = 2 * π * 半径

最后,我将输出计算得到的周长。

这是完整的C++代码:

```cpp
#include <iostream>
using namespace std;

int main() {
    while (true) {
        cout << "请选择要计算的形状:" << endl;
        cout << "1. 计算圆形的周长" << endl;
        cout << "0. 退出程序" << endl;

        int choice;
        cin >> choice;

        switch(choice) {
            case 1:
                double radius;
                cout << "请输入圆的半径:";
                cin >> radius;

                double perimeter = 2 * 3.14159 * radius;
                cout << "圆的周长为:" << perimeter << endl;
                break;
            case 0:
                cout << "程序已退出。" << endl;
                return 0;
            default:
                cout << "无效的选择,请重新选择。" << endl;
        }
    }
}

这个程序会一直运行,直到用户选择退出。

用户可以选择计算圆形的周长,程序会要求输入圆的半径,然后计算周长并输出。

用户还可以选择退出程序。

备注:该程序假设用户输入的半径为合法的数字,没有进行错误处理。在实际开发中,可能需要添加一些错误处理的代码。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^