当前位置 博文首页 > Jason's Blog:练习13—几何求算

    Jason's Blog:练习13—几何求算

    作者:[db:作者] 时间:2021-09-01 10:34

    题目

    设圆半径 r=1.5,圆柱高 h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。取小数点后2位数字,请编写程序。

    解题步骤

    (1)变量、常量定义;
    (2)函数定义;
    (3)函数调用;
    (4)输出结果;

    C语言

    #include <stdio.h>
    #include <math.h>
    #define PI 3.14
    
    float CirclePerimeter(float radius)
    {
        return 2 * PI * radius;
    }
    
    float CircleArea(float radius)
    {
        return PI * pow(radius, 2);
    }
    
    float SphereSurfaceArea(float radius)
    {
        return 4 * PI * pow(radius, 2);
    }
    
    float SphereVolume(float radius)
    {
        return 4 / 3 * PI * pow(radius, 3);
    }
    
    float CylinderVolume(float radius, float height)
    {
        return PI * pow(radius, 2) * height;
    }
    
    int main()
    {
        float r = 1.5, h = 3;
        printf("CirclePerimeter=%.2f\n", CirclePerimeter(r));
        printf("CircleArea=%.2f\n", CircleArea(r));
        printf("SphereSurfaceArea=%.2f\n", SphereSurfaceArea(r));
        printf("SphereVolume=%.2f\n", SphereVolume(r));
        printf("CylinderVolume=%.2f\n", CylinderVolume(r, h));
        return 0;
    }
    

    说明

    1. 对需求进行拆分,在一个个函数中 “逐个击破”,“分而治之”思想;
    2. 使用C语言中<math.h>头文件下的数学函数pow()计算次方,例如pow(x,y)表示 x 的 y 次方;
    cs
    下一篇:没有了