小宝的实验四-在分支循环结构中调用自定义函数
//计算多个用户的电费 #include<stdio.h> int main() { 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("%lf",&x); if(x<=0) printf("输入错误,重新输入"); else{ y=fee(x); } printf("y=%.3f\n",y); } return 0; } double fee(double x) { double y; if(x<=50) { y=0.53*x; } else { y=26.5+0.58*(x-50); } return y; }
//计算并输出该用户应支付的电费 #include<stdio.h> int main(void) { int n,i; double x,y; printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter x=:"); scanf("%lf",&x); if(x<=0) { printf("输入错误,重新输入"); } else if(x<=50) { y=0.53*x; } else { y=26.5+0.58*(x-50); } printf("y=f(%f)=%.2f\n",x,y); } return 0; }
//利用循环计算多个圆柱体体积 #include<stdio.h> #define PI 3.14 int main() { int h,r,n,i; double v; double cylinder(int r,int h); printf("Enter n=:"); scanf("%d",&n); for(i=1;i<=n;i++){ printf("Enter r and h:\n"); scanf("%d%d",&r,&h); if((r<=0)||(h<=0)) { printf("输入错误,重新输入"); } else{ v=cylinder(r,h); printf("V=%.3f\n",v); } } return 0; } double cylinder(int r,int h) { double result; result=PI*r*r*h; return result; }