資源描述:
《C案例教程源程序.doc》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、第2章簡單C程序2.1.1顯示一行文字/*顯示一行文字“WelcometoCWorld!”的程序*/#includemain(){printf(”WelcometoCWorld!”);}2.1.2兩個整數(shù)求和/*計(jì)算并顯示兩整數(shù)之和的程序*/#includemain(){inta,b,sum;/*變量聲明*/printf(”Inputfirstinteger”);/*顯示提示信息*/scanf(”%d”,&a);/*讀取一個整數(shù)*/printf(”Inputsecondinteger”);/
2、*顯示提示信息*/scanf(”%d”,&b);/*讀取一個整數(shù)*/sum=a+b;/*兩數(shù)相加*/printf(”Sumis%d”,sum);/*顯示兩數(shù)之和*/return0;/*返回0表示程序成功地結(jié)束*/}2.2.1算術(shù)運(yùn)算#includemain(){printf(“L1:40%%7=%d”,40%7);printf(“L2:40%7=%d”,40%7);printf(“L3:3.0/-4.0+1.6*2.0/5.0=%f”,3.0/-4.0+1.6*2.0/5.0);printf(“L4:
3、2/3*1000=%d”,2/3*1000);printf(“L5:2/3*1000=%f”,2/3*1000);printf(“L6:2/3*1000=%e”,2/3*1000);printf(“L7:2.0/3*1000=%f”,2.0/3*1000);printf(“L8:2f/3*1000=%f”,2f/3*1000);printf(“L9:Sizeof3is%d”,sizeof(3));printf(“L10:Sizeof3.0is%d”,sizeof(3.0));}2.2.2賦值運(yùn)算#include
4、tdio.h>main(){intk,x,y;k=5;x=k++;/*后綴運(yùn)算,先把k的值賦給x,然后k的值加1*/printf("L1:k=%d,x=%d",k,x);k=5;y=++k;/*前綴運(yùn)算,先使k的值加1,然后將k的值賦給y*/printf("L2:k=%d,y=%d",k,y);x=y=5;printf("L3:--xis%d,y--is%d",--x,y--);k=x=y=5;k=++x-++y;printf("L4:%d,%d,%d",k,x,y);k=++x+y++;printf("L5:%d,%
5、d,%d",k,x,y);k=x--+--y;printf("L6:%d,%d,%d",k,x,y);k=-x+++y;printf("L7:%d,%d,%d",k,x,y);x=y=5;printf("L8:x+=x-=x*xis%d",x+=x-=x*x);printf("L9:y+=y-=y*=yis%d",y+=y-=y*=y);}2.2.3printf中輸出表列求值#includevoidmain(){inti=8;printf("%d,%d,%d,%d,%d",++i,--i,i--,i+
6、+,-i--);}2.2.4交換兩個變量的值#includemain(){inta,b,tem;printf(”Inputintegeraandb”);scanf(”%d,%d”,&a,&b);tem=a;a=b;b=tem;printf(”a=%d,b=%d”,a,b);}2.2.5四舍五入#include#includemain(){doublea,b;printf("Inputreala");scanf("%lf",&a);b=floor(a*100+0.5)/100;
7、printf("b=%.2f",b);}第4章分支程序設(shè)計(jì)4.1.1求絕對值#include”stdio.h”main(){intx,y;scanf(“%d”,&x);y=x;if(x<0)y=-x;printf(“x=%d,
8、x
9、=%d”,x,y);}4.1.2顯示三數(shù)中的奇數(shù)main(){intx,y,z;printf(“inputx,y,z:”);scanf(“%d,%d,%d”,&x,&y,&z);if(x%2)printf(“%disoddnumber!”,x);if(y%2)printf(“%disoddnumbe
10、r!”,y);if(z%2)printf(“%disoddnumber!”,z);}4.1.3三數(shù)中取極值main(){inta,b,c,max,min;printf(“inputa,