Some_221123 2022-05-07 20:36 采纳率: 100%
浏览 261
已结题

PTA 实验10-1 圆形体体积计算器,通过不了,找不到问题

实验10-1 圆形体体积计算器
分数 20
作者 张高燕
单位 浙江大学

输入格式:
在每次计算之前,要求输出如下界面:

1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
然后从标准输入读进一个整数指令。

输出格式:
如果读入的指令是1或2或3,则执行相应的体积计算;如果是其他整数,则程序结束运行。

当输入为1时,在计算球体体积之前,打印Please enter the radius:,然后读入球体半径,完成计算;
当输入为2时,在计算圆柱体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算;
当输入为3时,在计算圆锥体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算。
计算结果在一行内输出,保留小数点后两位。

输入样例:
1
2
3
2.4 3
0
输出样例:
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius:
33.51
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius and the height:
18.10
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:

这是我的代码,但是找不到问题呀,看看吧。

#include <stdio.h>
#include <math.h>
#define Pi 3.1415926535
void print(void);
void select(int option);
void ball(void);
void cylinder(void);
void cone(void);
int main(void) {
    int option;
    while (1) {
        print();
        scanf("%d", &option);
        if (option < 1 || option > 3) {
            break;
        }
        select(option);
    }
    return 0;
}
void print(void) {
    printf("1-Ball\n");
    printf("2-Cylinder\n");
    printf("3-Cone\n");
    printf("Other-Exit\n");
    printf("Please enter your command:");
}
void select(int option) {
    switch (option) {
        case 1:
            ball();
            break;
        case 2:
            cylinder();
            break;
        case 3:
            cone();
            break;
    }
}
void ball(void) {
    double r, v;
    printf("Please enter the radius:\n");
    scanf("%lf", &r);
    v = 4 * Pi * pow(r, 3) / 3;
    printf("%.2f\n", v);
}
void cylinder(void) {
    double r, h, v;
    printf("Please enter the radius and the height:\n");
    scanf("%lf %lf", &r, &h);
    v = Pi * pow(r, 2) * h;
    printf("%.2f\n", v);
}
void cone(void) {
    double r, h, v;
    printf("Please enter the radius and the height:\n");
    scanf("%lf %lf", &r, &h);
    v = Pi * pow(r, 2) * h / 3;
    printf("%.2f\n", v);
}
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-05-11 12:35
    关注

    改这样试试:

    #include <stdio.h>
    //#include <math.h>
    #include <stdlib.h>
    #define Pi 3.1415926535
    void print(void);
    void select(int option);
    void ball(void);
    void cylinder(void);
    void cone(void);
    int main(void) {
        int option;
        while (1) {
            print();
            scanf("%d", &option);
            //if (option < 1 || option > 3) {
            //    break;
            //}
            //select(option);
            switch (option) {
            case 1:
                ball();
                break;
            case 2:
                cylinder();
                break;
            case 3:
                cone();
                break;
            default:exit(0);
            }
        }
        return 0;
    }
    void print(void) {
        printf("1-Ball\n");
        printf("2-Cylinder\n");
        printf("3-Cone\n");
        printf("Other-Exit\n");
        printf("Please enter your command:");
    }
    void select(int option) {
        switch (option) {
        case 1:
            ball();
            break;
        case 2:
            cylinder();
            break;
        case 3:
            cone();
            break;
        }
    }
    void ball(void) {
        double r, v;
        printf("Please enter the radius:\n");
        scanf("%lf", &r);
        v = 4.0 * Pi * r * r * r / 3.0;  //v = 4 * Pi * pow(r, 3) / 3;
        printf("%.2f\n", v);
    }
    void cylinder(void) {
        double r, h, v;
        printf("Please enter the radius and the height:\n");
        scanf("%lf %lf", &r, &h);
        v = Pi * r * r * h;    //v = Pi * pow(r, 2) * h;
        printf("%.2f\n", v);
    }
    void cone(void) {
        double r, h, v;
        printf("Please enter the radius and the height:\n");
        scanf("%lf %lf", &r, &h);
        v = Pi * r * r * h / 3.0; //v = Pi * pow(r, 2) * h / 3;
        printf("%.2f\n", v);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月16日
  • 创建了问题 5月7日

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭