问题二十五:用结构体编写程序,求3个长方柱(Bulk)的体积和表面积。

/**************************************************************
*****AUTHER:liuyongshui
*******DATE:2013\4\7
***LANGUAGE:C
***QUESTION:用结构体编写程序,求3个长方柱(Bulk)的体积和表面积。
**********************************************8*****************/

#include <stdio.h>


struct bulk
{
      float length;
      float width;
      float heigth;
};

void volume(struct bulk b[]); //原函数的申明
void areas(struct bulk b[]);

int main()
{
        int i;
        struct bulk BULK[3];
        
        for(i=0; i<3; i++)
        {
             printf("第%d个长方体的长 宽 高:\n", i+1);
             scanf("%f %f %f", &BULK[i].length, &BULK[i].width, &BULK[i].heigth);
        }

        volume(BULK);   //传递结构体
        areas(BULK);

        return 0;
}

//函数定义
void volume(struct bulk b[])//计算体积
{
     int i;

     for(i=0; i<3; i++)
     {
         printf("第%d个长方体的体积%f\n", i+1, b[i].length* b[i].width* b[i].heigth);
     }
}

void areas(struct bulk b[])//计算过表面积
{
    int i;

    for(i=0; i<3; i++)
    {
        printf("第%d个长方体的表面积%f\n", i+1, 2*(b[i].length* b[i].width +
                                                 b[i].heigth* b[i].width +
                                                 b[i].length* b[i].heigth));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,计算圆柱体的表面积体积需要使用圆柱体的半径和高,可以使用以下公式进行计算: 圆柱体的表面积 = 2 * π * r * h + 2 * π * r^2 圆柱体的体积 = π * r^2 * h 其中,π是一个常数,约等于3.14159。 现在我们可以编写一个名为cylinder()的函数,该函数的参数为圆柱体的半径和高,返回值为一个包含表面积体积结构体。具体实现如下: ```c #include <stdio.h> #define PI 3.14159 struct Cylinder { double surface_area; double volume; }; struct Cylinder cylinder(double r, double h) { struct Cylinder result; result.surface_area = 2 * PI * r * h + 2 * PI * r * r; result.volume = PI * r * r * h; return result; } int main() { double r = 3.0, h = 5.0; struct Cylinder c = cylinder(r, h); printf("The surface area of the cylinder is: %.2f\n", c.surface_area); printf("The volume of the cylinder is: %.2f\n", c.volume); return 0; } ``` 该程序使用了一个结构体Cylinder来保存圆柱体的表面积体积,同时定义了一个常量PI来表示π的值。在函数cylinder()中,根据上述公式计算圆柱体的表面积体积,并将结果保存在一个类型为Cylinder的结构体中,最后将该结构体作为函数的返回值。在主函数中,调用cylinder()函数,并输出计算结果。 运行该程序,可以得到以下输出结果: ``` The surface area of the cylinder is: 150.80 The volume of the cylinder is: 141.37 ``` 表面积保留两位小数,体积也保留两位小数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值