#include <stdio.h>
#include <stdlib.h>
double calcCircle(double);
int validate(double);
double calcRectangle(double, double);
int main()
{
double radius;
double width, height;
double s;
int choice;
printf("1、圆\n");
printf("2、矩形\n");
printf("3、三角形\n");
printf("本系统支持计算三种图形的面积,请选择: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("请输入圆半径,我来计算圆的面积:");
do{
scanf("%lf", &radius);
if(!validate(radius))
{
printf("所录入的内容不能为负数, 请重新输入一个正数: ");
}
}while(!validate(radius));
s = calcCircle(radius);
break;
case 2:
printf("请输入矩形的宽和高,我来计算矩形的面积:");
do{
scanf("%lf%lf", &width, &height);
if(!validate(width) || !validate(height))
{
printf("所录入的内容不能为负数, 请重新输入两个正数: ");
}
}while(!validate(width) || !validate(height));
s = calcRectangle(width, height);
break;
case 3:
break;
default:
printf("本系统只支持3种图形, 请在1-3之间选择!");
}
printf("图形的面积为: %.2lf\n", s);
return 0;
}
double calcCircle(double radius)
{
double s = 3.14 * pow(radius, 2);
return s;
}
int validate(double num)
{
return num > 0 ;
}
double calcRectangle(double width, double height)
{
return width * height;
}