实验4:在分支循环结构中调用自定义函数

it2024-04-18  10

1、利用循环计算多个圆柱体积。

 

/*计算n个圆柱体体积*/ #include<stdio.h> int main(void) { double r,v,h; double cylinder(double r,double h); printf("Enter r and h:"); scanf("%lf%lf",&r,&h); v=cylinder(r,h); printf("v=%.3f\n",v); return 0; } double cylinder(double r,double h) { double result; result=3.1415926*r*r*h; return result; }

 

2、输出用电量并计算水费。

/*输出用电量并计算应付电费*/ #include<stdio.h> int main(void) { double x,y; printf("Enter x: "); scanf("%.2f",&x); if(x<=0) printf("输入结果错误,请重新输入"); else if(0<x<=50) y=0.53*x; else y=0.53*x+(x-50)*0.05; printf("y=%.2f\n",y); return 0; }

 

3、计算多个用户的电费。

/*计算多户人家的电费*/ #include<stdio.h> int main(void) { int i,n; double x,y; double fee(double x); printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter x:"); scanf("%2f",&x); if(x<=0) printf("输入错误,重新输入"); else{ y=fee(x); } printf("y=%.2f\n",y); } return 0; } double fee(double x) { double y; if(x<=50) { y=0.53*x; } else { y=0.53*x+0.05(x-50); } return y; }

 

转载于:https://www.cnblogs.com/zhangling213549/p/3373506.html

最新回复(0)