资源描述:
《c语言程序设计实例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、加减乘除的C程序:#includeintmain(){doublea,b;charc;scanf("%lf%c%lf",&a,&c,&b);switch(c){case'+':printf("%lf%c%lf=%lf",a,c,b,a+b);break;case'-':printf("%lf%c%lf=%lf",a,c,b,a-b);break;case'*':printf("%lf%c%lf=%lf",a,c,b,a*b);break;case'/':printf("%lf%c%lf=%lf",a
2、,c,b,a/b);break;default:printf("error");break;}printf("");return0;}三个数,输出最大的数的C程序:#includeintmain(){inta,b,c;printf("pleaseenterintegers:");scanf("%d%d%d",&a,&b,&c);if(a3、",c);elseprintf("max=%d",a);return0;}死循环:#includeintmain(){intn;for(n=1;n>=1;n++)if(n%3!=0)printf("%d",n);return0;}输出********************的C程序:verygood!*******************#includeintmain(){printf("********************");printf("");printf(
4、"verygood!");printf("*******************");printf("");return0;}ax^2+bx+c=0的c程序:#include#include//程序中要调用求平方根函数sqrt//intmain(){doublea,b,c,disc,x1,x2,p,q,r;scanf("%lf%lf%lf",&a,&b,&c);disc=b*b-4*a*c;if(a!=0)if(disc<0)printf("Thisequationhasn'
5、trealroots",x1,x2);else{p=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf("realroots:x1=%7.2fx2=%7.2f",x1,x2);}if(a==0){r=c/b;x1=r;x2=r;printf("%lf%lf",x1,x2);}return0;}Sinx在[0,pi]的积分:#include#includeusingnamespacestd;intmain(){d
6、oubleS(0);for(doublei=0.00;i<3.15;i+=0.01)S+=sin(i)*0.01;cout<0,y=1;x<0,y=-1}c程序:#includeintmain(){doublex;inty;printf("plesaeenterx:");scanf("%lf",&x);y=0;if(x>=0)if(x>0)y=1;elsey=-1;printf("x=%lf,y=%d",x,y);printf("");r
7、eturn0;}求pi的近似值:#include#includeintmain(){intsign;doublen,term,pi;term=1.0,pi=0.0;n=1.0;sign=1;while(fabs(term)>1e-6){pi=pi+term;n=n+2;sign=-sign;term=sign/n;}pi=pi*4;printf("pi=%10.8f",pi);return0;}#includeintmain(){intd;doublescore
8、;printf("Youscore:");scanf("%lf",&score);d=score/10;switch(d){case10:case9:printf("等级为At");printf("%4.1lf",score);break;case8:printf("等级为B");break;case7:printf("等级为C"