使用工具:
Dev-C++ 5.11
题目描述:
编写一系统,计算常见图形(圆、三角形、正方形和矩形)的面积。
要求:
- 实用性强(不懂计算机编程的人也能够使用)。
- 用户根据自己需要选择某类图形、输入相应的数据后,能够正确的计算该图形的面积。
- 能够重复执行上面的操作。
- 当用户把所有图形面积计算完后,能够正常退出
- 当用户数据输错时,能够有相应的错误提示。
参考代码:
#include<stdio.h>
int main()
{
void menu(int n);
int tf=1;
menu(tf);
}
//主函数
void menu(int n)
{
void cir(float n);
void squ(float n);
void rec(float n,float n1);
void tri(float n,float n1);
int choice;
float bj,length,width,bc,bottom,height;
while (n==1)
{
printf("\t--------------Computer shape area!------------\n");
printf("\t1:computer circle area\n");
printf("\t2:computer square area\n");
printf("\t3:computer rectangle area\n");
printf("\t4:computer triangle area\n");
printf("\t0:quit the system\n");
printf("\tInput select 1~4 computer shape area(0:quit system):");
scanf("%d",&choice);
printf("\n");
if(choice==1)
{
printf("\tPlease enter the radius of the circle: ");
scanf("%f",&bj);
printf("\tThe circle area:");
cir(bj);
}
else if(choice==2)
{
printf("\tPlease enter the side length of the square:");
scanf("%f",&bc);
printf("\tThe square area:");
squ(bc);
}
else if(choice==3)
{
printf("\tPlease enter the length and width of the rectangle:\n");
printf("\tInput width of rectangle:");
scanf("%f",&width);
printf("\tInput length of rectangle:");
scanf("%f",&length);
printf("\tThe rectangle area:");
rec(length,width);
}
else if(choice==4)
{
printf("\tPlease enter the bottom and height of the triangle:\n");
printf("\tInput bottom of triangle:");
scanf("%f",&bottom);
printf("\tInput height of triangle:");
scanf("%f",&height);
printf("\tThe triangle area:");
tri(bottom,height);
}
else if(choice==0)
{
printf("\t\tWelcome to use again, bye~~");
break;
}
else
printf("\tSorry, the number you entered is incorrect. Please press the menu to enter the number\n");
}
}
//圆面积
void cir(float n)
{
float pai=3.14,area;
area=n*n*pai;
printf("%.2f\n",area);
}
//正方形面积
void squ(float n)
{
float area;
area=n*n;
printf("%.2f\n",area);
}
// 矩形面积
void rec(float n,float n1)
{
float area;
area=n*n1;
printf("%.2f\n",area);
}
//三角形面积
void tri(float n,float n1)
{
float area;
area=(n*n1)/2;
printf("%.2f\n",area);
}
运行结果:
版权声明:本文为w8862__12原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。