C程序设计(第三版)潭浩强著课本例题程序源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1.1输出一行信息#includestdio.hvoidmain(){printf(ThisisaCprogram.\n);}1.2求两数之和#includestdio.hvoidmain(){inta,b,sum;a=123;b=456;sum=a+b;printf(sumis%d\n,sum);}1.3求2个数中较大者#includestdio.hvoidmain(){intmax(intx,inty);inta,b,c;scanf(%d,%d,&a,&b);c=max(a,b);printf(max=%d,c);}intmax(intx,inty){intz;if(xy)z=x;elsez=y;return(z);}3.1符号常量的使用#definePRICE30#includestdio.hvoidmain(){intnum,total;num=10;total=num*PRICE;printf(total=%d\n,total);}3.5转义字符的使用#includestdio.hvoidmain(){printf(abc\tde\rf\tg\n);printf(h\ti\b\bjk\n);}显示结果:f_-_-_-_gdeh_-_-_-j_k3.7大小写字母的转换#includestdio.hvoidmain(){charc1,c2;c1='a';c2='b';c1=c1-32;c2=c2-32;printf(%c%c,c1,c2);}4.11#includestdio.hvoidmain(){charc1,c2;c1=getchar();printf(%c,%d\n,c1,c1);c2=c1+32;printf(%c,%d\n,c2,c2);}5.4输入一个字符,判断它是否大写字母,如果是,将它转换成小写字母;如果不是,不转换,然后输出最后得到的字符#includestdio.hvoidmain(){charch;scanf(%c,&ch);ch=(ch='A'&&ch='Z')?(ch+32):ch;printf(%c\n,ch);}3.8强制类型转换#includestdio.hvoidmain(){floatx;inti;x=3.6;i=(int)x;printf(x=%f,i=%d\n,x,i);}运行结果:x=3.600000,i=34.1输出单个字符#includestdio.hvoidmain(){chara,b,c;a='B';b='O';c='Y';putchar(a);putchar(b);putchar(c);putchar('\n');}结果:BOY4.3无符号数据的输出#includestdio.hvoidmain(){unsignedinta=65535;intb=-2;printf(a=%d,%o,%x,%u\n,a,a,a,a);printf(b=%d,%o,%x,%u\n,b,b,b,b);}结果:a=-1,177777,ffff,65535b=-2,177776,fffe,655344.5字符串的输出#includestdio.hvoidmain(){printf(%3s,%7.2s,%.4s,%-5.3s\n,CHINA,CHINA,CHINA,CHINA);}输出入下:CHINA,_-_-_CH,CHIN,CHI_-4.6输出实数时的有效位数#includestdio.hvoidmain(){floatx,y;x=111111.111;y=222222.222;printf(%f,x+y);}结果:333333.3281254.7输出双精度数时的有效位数#includestdio.hvoidmain(){doublex,y;x=1111111111111.111111111;y=2222222222222.222222222;printf(%f,x+y);}结果:3333333333333.3330004.8输出实数时指定小数位数#includestdio.hvoidmain(){floatf=123.456;printf(%f%10f%10.2f%.2f%-10.2f\n,f,f,f,f,f);}结果:123.456001_-123.456001_-_-_-123.46_-123.46_-123.46_-_-5.1由小到大输出输入的两个实数#includestdio.hvoidmain(){floata,b,t;scanf(%f,%f,&a,&b);if(ab){t=a;a=b;b=t;}printf(%5.2f,%5.2f\n,a,b);}5.2输入3个数,按由小到大输出#includestdio.hvoidmain(){floata,b,c,t;scanf(%f,%f,%f,&a,&b,&c);if(ab){t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}{t=b;b=c;c=t;}printf(%5.2f,%5.2f,%5.2f\n,a,b,c);}5.3有一函数-1(x0)Y=0(x=0)1(x0)输入一个x值,输出一个y值#includestdio.hvoidmain(){intx,y;scanf(%d,&x);if(x0)y=-1;elseif(x==0)y=0;elsey=1;printf(x=%d,y=%d\n,x,y);}5.3A#includestdio.hvoidmain(){intx,y;scanf(%d,&x);if(x=0)if(x0)y=1;elsey=0;elsey=-1;printf(x=%d,y=%d\n,x,y);}6.1用if和goto语句构成循环,求#includestdio.hvoidmain(){inti,sum=0;i=1;loop:if(i=100){sum=sum+i;i++;gotoloop;}printf(%d\n,sum);}6.2while语句实现#includestdio.hvoidmain(){inti,sum=0;i=1;while(i=100){sum=sum+i;i++;}printf(%d\n,sum);}6.3用do……while语句求。。#includestdio.hvoidmain(){inti,sum=0;i=1;do{sum=sum+i;i++;}while(i=100);printf(%d\n,sum);}6.4while和do...while循环的比较#includestdio.hvoidmain(){intsum=0,i;scanf(%d,&i);while(i=10){sum=sum+i;i++;}printf(sum=%d\n,sum);}运行情况:1sum=5511sum=0#includestdio.hvoidmain(){intsum=0,i;scanf(%d,&i);do{sum=sum+i;i++;}while(i=10);printf(sum=%d\n,sum);}运行情况:1sum=5511sum=116.5把100—200之间不能被3整除的数输出#includestdio.hvoidmain(){intn;for(n=100;n=200;n++){if(n%3==0)continue;printf(%d,n);}}求100内能被3整除的所有数的和。#includestdio.hmain(){intx,sum;sum=0;for(x=1;x=100;x++)if(x%3==0)sum=sum+xprintf(“%d\n”,sum);}6.6用/4=1-1/3+1/5-1/7+……公式求的近似值,直到某一项的绝对值小于10为止#includestdio.h#includemath.hvoidmain(){ints;floatn,t,pi;t=1;pi=0;n=1.0;s=1;while(fabs(t)1e-6){pi=pi+t;n=n+2;s=-s;t=s/n;}pi=pi*4;printf(pi=%10.6f\n,pi);}6.8判断m是否素数#includestdio.h#includemath.hvoidmain(){intm,i,k;scanf(%d,&m);k=sqrt(m);for(i=2;i=k;i++)if(m%i==0)break;if(ik)printf(%disaprimenumber\n,m);elseprintf(%disnotaprimenumber\n,m);}6.9求100-200间的全部素数#includestdio.h#includemath.hvoidmain(){intm,k,i,n=0;for(m=101;m=200;m=m+2){k=sqrt(m);for(i=2;i=k;i++)if(m%i==0)break;if(i=k+1){printf(%d,m);n=n+1;}if(n%10==0)printf(\n);}printf(\n);}7.1数组元素的引用使a[0]-a[9]的值为0-9,然后按逆序输出#includestdio.hvoidmain(){inti,a[10];for(i=0;i=9;i++)a[i]=i;for(i=9;i=0;i--)printf(%d,a[i]);printf(\n);}结果:98765432107.3用起泡法对10个数排序(由小到大)#includestdio.hvoidmain(){inta[10];inti,j,t;printf(input10numbers:\n);for(i=0;i10;i++)scanf(%d,&a[i]);printf(\n);for(j=0;j9;j++)for(i=0;i9-j;i++)if(a[i]a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;}printf(thesortednumbers:\n);for(i=0;i10;i++)printf(%d,a[i]);printf(\n);}7.4将一个二维数组行和列的元素互换,存到另一个二维数组中12314a=b=2545636#includestdio.hvoidmain(){inta[2][3]={{1,2,3},{4,5,6}};intb[3][2],i,j;printf(arraya:\n);for(i=0;i=1;i++){for(j=0;j=2;j++){printf(%5d,a[i][j]);b[j][i]=a[i][j];}printf(\n);}printf(arrayb:\n);for(i=0;i=2;i++){for(j=0;j=1;j++)printf(%5d,b[i][j]);printf(\n);}}7.5#includestdio.hvoidmain(){inti,j,row=0,colum=0,max;inta[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}};max=a[0][0];for(i=0;i=2;i++)for(j=0;j=3;j++)if(a[i][j]max){max=a[i][j];row=i;colum=j;}printf(max=%d,row=%d,colum=%d\n,max,row,colum);}8.14有一个3*4的矩阵,求所有元素中的最大值#includestdio.hmax_value(intarray[][4]){inti,j,max;max=array[0][0];for(i=0;i3;i++)for(j=0;j4;j++)if(array[i][j]max)max=array[i][j];return(max);}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功