C语言函数求图形面积

124 阅读1分钟
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//函数原型
void calcCircle();    //计算圆的面积
void calcRectangle(); //计算矩形面积
/*
* 计算圆的面积(函数实现)
*/
void calcCircle()
{
    double radius, s;//半径和面积
    printf("请输入圆半径: ");
    scanf("%lf", &radius);
    //圆的面积=3.14乘以半径的平方
    s = 3.14 * pow(radius, 2);
    printf("半径为%.2lf的圆面积为: %.2lf\n", radius, s);
}
/*
* 计算矩形的面积(函数实现)
*/
void calcRectangle()
{
    double width,height; //矩形的宽和高
    double s;            //矩形的面积
    printf("请输入矩形的宽和高:");
    scanf("%lf%lf", &width, &height);
    s = width * height;
    printf("矩形的面积为: %.2lf", s);
}

int main()
{
    /*
    三种图形的面积计算公式如下:
    圆:s = PI * r * r;
    矩形:s = width * height;
    三角形:s = width * height / 2;
    使用函数分别实现三种图形的面积计算,打印计算结果
    */
    //书写函数的步骤:完成函数三要素
    //误区:不要一开始就去考虑如何完成某个功能
    calcCircle();       //调用计算圆面积的函数
    calcRectangle();    //调用矩形的面积

    //作为一个程序员,要学会偷懒
    //需要返回值吗?
    //函数名是什么?
    //需要给这个函数参数吗?
    return 0;
}