来到CSDN的第一天
自己觉得不容易的一串代码
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double calcCircle(double);
double validate(double);
int num;
double calacate(double, double);
double calcRectangle(double, double);
double radius = 0;
double width = 0;
double height = 0;
double s = 0;
int choice;
int main()
{
printf(“1、圆\n”);
printf(“2、矩形\n”);
printf(“3、三角形\n”);
printf(“本系统支持三种图形的面积,请选择:\n”);
scanf_s("%d", &choice);
switch (choice)
{
case 1:
printf("请输入圆半径,我来计算圆的面积:");
scanf_s("%lf", &radius);
radius = validate(radius);
s = calcCircle(radius);
break;
case 2:
printf("请输入矩形宽和高,我来计算矩形的面积:");
scanf_s("%lf%lf", &width, &height);
s = calcRectangle(width, height);
break;
case 3:
printf("请输入三角形的底和高,我来计算三角形的面积:");
scanf_s("%lf%lf", &width, &height);
s = calacate(width, height);
break;
default:
printf("本系统只支持3种图形,请在1-3中选择!");
}
printf("图形的面积为:%.2lf\n", s);
return 0;
}
double calcCircle(double radius)
{
printf("%.2lf\n", radius);
double s = 3.14 * pow(radius, 2);
return s;
}
//验证输入的double型数据
double validate(double num)
{
return num > 0 ? num : 1; //如果num>0,会返回一个非零值,表示真
}
double calcRectangle(double width, double height)
{
printf("%.2lf%.2lf\n", width, height);
return width * height;
}
double calacate(double width, double height)
{
printf("%.2lf%.2lf\n", width, height);
return width * (height / 2);
}
不喜勿喷