先决条件:在C中切换大小写
问题陈述:
使用Switch case编写一个菜单驱动的程序,以计算以下内容:
1.圆的面积
2.平方面积
3.球体面积
还可以使用函数input()和output()来输入和显示相应的值。
// C program to illustrate
// Menu-Driven program
// using Switch-case
#include
int input();
void output(float);
int main()
{
float result;
int choice, num;
printf("Press 1 to calculate area of circle\n");
printf("Press 2 to calculate area of square\n");
printf("Press 3 to calculate area of sphere\n");
printf("Enter your choice:\n");
choice = input();
switch (choice) {
case 1: {
printf("Enter radius:\n");
num = input();
result = 3.14 * num * num;
printf("Area of sphere=");
output(result);
break;
}
case 2: {
printf("Enter side of square:\n");
num = input();
result = num * num;
printf("Area of square=");
output(result);
break;
}
case 3: {
printf("Enter radius:\n");
num = input();
result = 4 * (3.14 * num * num);
printf("Area of sphere=");
output(result);
break;
}
default:
printf("wrong Input\n");
}
return 0;
}
int input()
{
int number;
scanf("%d", &number);
return (number);
}
void output(float number)
{
printf("%f", number);
}
输出:
Press 1 to calculate area of circle
Press 2 to calculate area of square
Press 3 to calculate area of sphere
Enter your choice:
1
Enter radius:
5
Area of circle=78.5
相关文章:
- 关于C中switch语句的有趣事实
- C程序的输出|套装30(开关盒)
- 在C / C++中的切换情况下使用范围
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。