实验二 用C语言编写简单程序
#include<stdio.h> int main(void) { double r,s; double cylinder(double r); printf("Enter r:"); scanf("%lf",&r); if(r<=0){ printf("输入错误"); } else if(r>0){ s=3.1415926*r*r; printf("s=%.3f\n",s); } return 0; }
#include<stdio.h> int main(void) { int i,n; double r,h,v; double cylinder(double r,double h); printf("Enter n:"); scanf("%d",&n); for(i=1;i<=n;i++){ printf("Enter r and h:"); scanf("%lf%lf",&r,&h); if((r<=0)||(h<=0)){ printf("输入错误,重新输入"); } else{ 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; }
#include<stdio.h> int main(void) { int i,n; 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; printf("y=%lf\n",y); } else{ y=(x-50)*0.58+50*0.53; printf("y=%lf\n",y); } } return 0; }
#include<stdio.h> int main(void) { int i,m; double x,y; double fact(double x); double cylinder(double x); printf("Enter m:"); scanf("%d",&m); for(i=1;i<=m;i++){ printf("Enter x:"); scanf("%lf",&x); if(x<=0){ printf("输入错误,重新输入"); } else if(x<=50){ y=fact(x); printf("y=%lf\n",y); } else{ y=cylinder(x); printf("y=%lf\n",y); } } return 0; } double fact(double x) { double result; result=0.53*x; return result; } double cylinder(double x) { double result; result=0.53*50+(x-50)*0.58; return result; }