版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一、基本输入输出1.#include <stdio.h>main()printf("a_bctdret_fn");printf("gh_tij_bb_k");注:“_”代表一个空格。运行结果为:e_ _ _ _ _ _ _ _fgh_ _ _ _ _ _ i_ _k3.#include <stdio.h> main() int z, x=6,y=5;char w=c;z=x+y+w;printf("%d,%d,%d",x,y,z);运行结果为:6,5,1102.#include <stdio.h>m
2、ain() char c1=a,c2=b,c3=c;printf("a%cb%cc%c ",c1,c2,c3);运行结果为:aabbcc4. #include <stdio.h>main() int i=128;float x=234.89;printf(“n”);printf("%5d,%8.2fn",i,x);printf("%2d,%4.1fn",i,x);运行结果为: 128, 234.89128,234.95.#include <stdio.h> void main(void) int x=15,y=
3、5; float f=1234.567f,b=12345; printf("%f %10f %10.2f %.2f%-10.2fn",f,f,f,f,f); printf("x+y=%dn",x+y); printf("b=%8f",b); 运行结果为:1234.567000 1234.567000 1234.57 x+y=20b=12345.0000006.#include <stdio.h>main( ) int i=8, j=9; int x, y, z , w; x=i- ; y=i ; z=+j; w=j; pr
4、intf(“%d,%d,%d,%d”,x,y,z,w); 运行结果为: 8,7,10,108. #include <stdio.h>main() int x=10;int y=79;printf("%5d,%5d,%5d",!x,x|y,x&&y); 注:“_”代表一个空格运行结果为:0, 1, 17. #include <stdio.h>main() int a=1,b=2;a=a+b; b=a-b; a=a-b;printf(“a=%d,b=%dn”,a,b);运行结果为:a=2,b=19. #include <stdio.
5、h> main() int z, x=2,y=4; char w=c;z=x+y;printf("%d,%d,%d,%c",x,y,z,w);运行结果为:2,4,6,c10.#include <stdio.h> main() char c='a'printf("%c,%dn",c,c);运行结果为:a,9711. #include <stdio.h>main ( ) char c1, c2;c1=getchar ( );printf (“%c, %dn”, c1,c1 );c2=c1+32;printf (“
6、%c,%dn”, c2,c2 );若敲进A,则运行结果为A,65a,9712#include <stdio.h>main() int z,x=7,y=4; char w=c;z=x>y&&y+3<7|w;printf("n%d,%d,%d",x,y,z);运行结果为:7,4,113.#include <stdio.h>main() int i=10,j=10;int x,y,z,w;x=i-;y=i;z=+j;w=j;printf("%d,%d,%d,%d",x,y,z,w);运行结果为:10,9,11
7、,1114. #include <stdio.h>main()int i=8,j=10,m=0,n=0;m+=i+;n- =-j; printf(“i=%d,j=%d,m=%d,n=%d”,i,j,m,n);运行结果为:i=9,j=9,m=8,n=-915. #include <stdio.h>main() int x, b0, b1, b2; printf("Please enter an integer x:"); scanf("%d", &x); b2 = x / 100; b1 = (x - b2 * 100) /
8、 10; b0 = x % 10; printf("bit0=%d, bit1=%d, bit2=%dn", b0, b1, b2);若输入352,运行结果为:bit0=2, bit1=5, bit2=3二、程序控制结构(顺序,分支,循环)1.#include <stdio.h>main() int a=1,b=3,c=5;if (c=a+b) printf("yesn");else printf("non");运行结果为:no2. #include <stdio.h>main() int a=12, b= -
9、34, c=56, min;min=a; if(min>b) min=b; if(min>c) min=c;printf("min=%d", min);运行结果为:min=-343.#include <stdio.h>main() int x=2,y= -1,z=5; if(x<y)if(y<0) z=0;else z=z+1; printf(“%dn”,z); 运行结果为: 54. #include <stdio.h>main()int a=10,b=50,c=30;if(a>b)a=b;b=c;c=a;printf(
10、"a=%d b=%d c=%dn",a,b,c);运行结果为:a=10 b=30 c=105. #include <stdio.h>main() float a,b,c,t;a=3;b=7;c=1;if(a>b) t=a;a=b;b=t;if(a>c) t=a;a=c;c=t;if(b>c) t=b;b=c;c=t;printf("%5.2f,%5.2f,%5.2f",a,b,c);运行结果为:1.00,3.00,7.006. #include <stdio.h>main()int a=2;float num=3
11、.12,x;if(num<4) x=2*num*a;printf("result is %fn",x);运行结果为:result is 12.4800007.#include<stdio.h>main( ) char c=A; if (0<=c ) &&(c<=9) printf(“YES”);elseprintf(“NO”);运行结果为:NO8#include <stdio.h>main ( ) float c=3.0 , d=4.0;if ( c>d ) c=5.0;else if ( c=d ) c=6.
12、0;else c=7.0; printf ( “%.1fn”,c ) ;运行结果为:7.09. #include <stdio.h>main() int a=0,b=1,c=0,d=20;if(a) d=d-10;else if(!b)if(!c) d=15;else d=25;printf("d=%dn",d);运行结果为:d=2010.#include <stdio.h>main() int a=2,b=3,c=1;if (a>b)if (a>c)printf (“%dn”,a);else printf (“%dn”,c);print
13、f (“over!n”); 运行结果为: over!11.#include <stdio.h>main()int m;scanf("%d", &m);if (m >= 0) if (m%2 = 0)printf("%d is a positive evenn", m);elseprintf("%d is a positive oddn", m); else if (m % 2 = 0) printf("%d is a negative evenn", m);else printf(&quo
14、t;%d is a negative oddn", m);若键入9,则运行结果为:-9 is a negative odd12.#include<stdio.h> main( ) char ch; ch=getchar( ); switch(ch) case A : printf(“%c”,A); case B : printf(“%c”,B); break; default: printf(“%sn”,”other”); 当从键盘输入字母A时,运行结果为:AB13. #include <stdio.h>main( ) int a=1,b=0; scanf(“
15、%d”,&a);switch(a) case 1: b=1;break;case 2: b=2;break;default : b=10;printf("%d", b);若键盘输入5,运行结果为:1014.#include <stdio.h>main ( ) int i=0,j=0,k=6; if(+i>0)|(+j>0) k+; printf("%d,%d,%dn",i,j,k);运行的结果为: 1,0,715.#include <stdio.h>main( ) int x , y , z; x=20, y=
16、40, z=60; while(x<y) x+=4, y-=4; z/=2; printf(“%d,%d,%d”,x,y,z);运行结果为:32,28,3016. #include <stdio.h>main() int num=0;while(num<=2) num+;printf("%dn",num);运行结果为:12317.#include <stdio.h>main() int n=9;while(n>6) n-;printf(“%d,”,n);运行结果为:8,7,6,18#include <stdio.h>ma
17、in( ) int sum=10,n=1;while(n<3)sum=sum-n; n+; printf(“%d,%d”,n,sum); 运行结果为:3,719.#include <stdio.h>main() int num,c; scanf("%d",&num); doc=num%10; printf("%d",c);while(num/=10)>0);printf("n"); 从键盘输入23,则运行结果为:3220#include <stdio.h>main() int s=0,a=5
18、,n;scanf("%d",&n);do s+=1; a=a-2; while(a!=n);printf("%d,%dn",s,a); 若输入的值1,运行结果为:2,121.#include <stdio.h>main() int n1,n2;scanf(“%d”,&n2);while(n2!=0) n1=n2%10;n2=n2/10;printf(“%d”,n1);若在运行时输入1298,运行结果为:892122.#include <stdio.h>main() int i;for (i=0;i<6;i+)
19、 printf (“%d”,+i); printf (“%d”,i+);运行结果为:135623#include "stdio.h" main() char c; c=getchar(); while(c!='?') putchar(c); c=getchar(); 如果从键盘输入abcde?fgh(回车)运行结果为:abcde24#include <stdio.h>main() char c; while(c=getchar()!=$) if(A<=c&&c<=Z) putchar(c); else if(a<
20、=c&&c<=z) putchar(c-32); 当输入为ab*AB%cd#CD$时,运行结果为:ABABCDCD25. #include <stdio.h>main()inti=0,s=0;do if(i%2) i+; continue; i+; s +=i;while(i<7);printf("%dn",s);运行结果为:1626.#include <stdio.h>main() int x=1, y =0;while(x<=10) y+=x*x; if (y>=10) break; x+;printf(“
21、%d %d”,y,x);运行结果为:327. #include <stdio.h>main() int x, y =0;for(x=1;x<=10;x+) if(y>=10) break; y=y+x; printf(“%d %d”,y,x);运行结果为:10 528.#include <stdio.h> main( ) int n=0; while(n<=3) switch(n) case 0 : ; case 1 : printf(“%d,”,n); case 2 : printf(“%d,”,n);n=n+3; break; default: p
22、rintf(“*”);n=n+1; 运行结果为:0,0,*29 #include <stdio.h>main() int x=1,y=0,a=0,b=0;switch(x) case 1: switch(y) case 0: a+;break; case 1: b+;break; case 2: a+;b+;break;printf(“a=%d, b=%d”,a,b);运行结果为:a=2,b=130. #include <stdio.h> main() char grade=C; switch(grade) case A: printf(“90-100n”); case
23、 B: printf(“80-90n”);case C: printf(“70-80n”);case D: printf(“60-70n”); break;case E: printf(“<60n”); default : printf(“error!n”); 运行结果为:70-8060-7031#include <stdio.h>main() int k=0;char c='A'do switch(c+) case 'A': k+;break;case 'B': k-;case 'C': k+=2;break;
24、case 'D': k=k%2;continue;case 'E': k=k+10;break;default: k=k/3;k+;while(c<'C') ;printf("k=%dn",k);运行结果为:k=432.#include <stdio.h>main() int i=10; switch ( i ) case 9: i+=1; case 10: i+=1; case 11: i+=1; default: i+=1; printf(“i=%dn”,i);运行结果为:i=1333#include &
25、lt;stdio.h>main( ) int sum=0;i=0; while(i<=100) sum=sum+i; printf(“i=%dn”,i);printf(“sum=%dn”,sum); 运行结果为:死循环,无结果34. #include <stdio.h>main() int i,sum=0;i=1;dosum=sum+i;i+;while(i<=10);printf(“%d”,sum);运行结果为: 5535.#include <stdio.h>main ( ) int i=0, sum=1 ;do sum+=i+ ; while (
26、i<6 );printf ( “%dn”, sum );运行结果为:1636.#include <stdio.h>main() int i;printf("n");for(i=0;i<6;i+) printf("%d",i);if (i%2=0)printf("n");运行结果为:01234537.#include <stdio.h> main( ) int i; for(i=0;i<8;i+) printf("%d,",+i); printf("%d,"
27、;, i+);printf("%d", i); 运行结果为:1,3,5,7,8,938.#include<stdio.h>main( ) int i=0, j=0;while( i<10) i+;while(j+<10) ;printf(“%d,%d”, i, j);运行结果为:10,1139.#include<stdio.h>main( )char i, j; for(i=0, j=9; i<j ; i+, j-) printf(“%c%c”, i, j);运行结果为:091827364540. #include <stdi
28、o.h>main() int i, n, sum = 0, counter = 0;printf("Input 4 Numbers:n");for (i = 0; i < 4; i+) scanf("%d", &n); if (n >= 0) sum += n; counter+; printf("sum=%d,counter=%dn", sum,counter);若键入3 -5 7 -9运行结果为:sum=10,counter=241. #include <stdio.h>main() int
29、i=5; do switch (i%2) case 4: i- -; break;case 6: i- -; continue; i- -; i- -; printf(“i=%dn”,i); while(i>0);运行结果为:i=3i=1i=-142.#include <stdio.h>main() int y=9; for(;y>0;y- -)if(y%3=0) printf(%d”,- -y); continue;运行结果为:852*43.#include <stdio.h>#define N 4main() int i; int x1=1,x2=2;f
30、or(i=1;i<=N;i+) printf("%4d%4d",x1,x2);if(i%2=0) printf("n");x1=x1+x2;x2=x2+x1;运行结果为:1 2 3 513 21 3444.#include <stdio.h>main() int i, j; for(i=0;i<5;i+) printf("n"); for (j=i+;j<6;j+)printf(“* “);运行结果为:* * * * * * * * * *45#include <stdio.h>main( )
31、 int x, y; for(x=30,y=0;x>=10,y<10; x-,y+) x/=2, y+=2; printf(“x=%d,y=%dn”,x,y);运行结果为:x=0,y=12*46. #include <stdio.h>#define N 4main( ) int i,j; for(i=1;i<=N;i+) for(j=1;j<i;j+) printf(" "); printf("*"); printf("n"); 运行结果为:* * * *三、函数1.#include <std
32、io.h>int Sub(int a, int b)return (a -b);main()int x, y, result = 0;scanf("%d,%d", &x,&y ); result = Sub(x,y ) ; printf("result=%dn",result);当从键盘输入:6,3运行结果为:result=32. #include <stdio.h>int min( int x, int y ) int m; if ( x> y ) m = x;else m = y; return(m);
33、60;main() int a=3,b=5,abmin ; abmin = min(a,b); printf(“min is %d”,abmin); 运行结果为:min is 53.#include<stdio.h> main( ) int x=10; int x=20; printf(“%d, ”, x); printf(“%d”, x); 运行结果为:20,104.#include<stdio.h>int fun(int m,int n)static s=1; s+;return(s*(m+n);main( ) int a=0,b=1;printf("%d
34、,",fun(a,b); printf("%d,",fun(a,b); 运行结果为:2,3,5.#include<stdio.h> func(int x) x=10; printf(“%d, ”,x); main( ) int x=20; func(x); printf(“%d”, x); 运行结果为:10,206.#include <stdio.h>int m=4;int func(int x,int y)int m=1;return(x*y-m);main()int a=2,b=3;printf("%dn",m);p
35、rintf("%dn",func(a,b)/m); 运行结果为:417.#include <stdio.h>int fun(int a, int b) if(a>b) return(a);else return(b);main() int x=15, y=8, r;r= fun(x,y);printf("r=%dn", r);运行结果为:r=158.#include <stdio.h>int fac(int n) int f=1,i;for(i=1;i<=n;i+) f=f * i;return(f);main() i
36、nt j,s;scanf(“%d”,&j);s=fac(j);printf("%d!=%dn",j,s);如果从键盘输入3, 运行结果为:3!=69. #include <stdio.h>int b=1;void fun() int c=2;static int a=0; a=a+b+c; printf(“%d ”,a);main() int cc;for(cc=1;cc<4;cc+) fun();运行结果为:3 6 910.#include <stdio.h>int f(int a) auto int b=0; static c=4;
37、 b=b+1; c=c+1; return(a+b+c); main() int a=3,i; for(i=0;i<3;i+)printf(“n%d”,f(a); 运行结果为:9101111. #include <stdio.h>unsigned fun6(unsigned num) unsigned k=1;do k*=num%10; num/=10;while(num); return k;main() unsigned n=26; printf(“%dn”,fun6(n); 运行结果为:1212. #include <stdio.h> float f1(fl
38、oat x,float y) float f2(float m,float n);float z; z=(x+y)/f2(x,y);return(z);float f2(float m,float n) float k; k=m-n; return(k);main() float a=2,b=3,c; c=f1(a,b);printf(“nc=%f”,c); 运行结果为:c=-513#include <stdio.h>int max(int x, int y);main() int a,b,c;a=7;b=8;c=max(a,b);printf("Max is %d&qu
39、ot;,c);max(int x, int y) int z;z=x>y? x : y;return(z) ;运行结果为:Max is 8*14.#include <stdio.h>int fac1(int n) int f; if (n= =1) f=1; else f=fac1(n-1)*n; printf(“f=%d,”,f); return(f); main() int y,n=4;y=fac1(n);printf(“y=%dn”,y);运行结果为:f=1,f=2,f=6,f=24,y=24四、数组1.#include <stdio.h>main() in
40、t i, a10;for(i=9;i>=0;i-) ai=10-i;printf(“%d%d%d”,a2,a5,a8);运行结果为:8522. #include <stdio.h>main() int i,a6; for (i=0; i<6; i+) ai=i; for (i=5; i>=0 ; i-) printf("%3d",ai); 运行结果为: 5 4 3 2 1 0 3. #include <stdio.h>main( ) int i,k,a10,p3;k=5;for(i=0;i<10;i+)ai=i;for(i=0
41、;i<3;i+)pi=ai*(i+1);for(i=0;i<3;i+)k+=pi*2;printf("%dn",k);运行结果为:214.#include <stdio.h>main( ) int n33, i, j;for(i=0;i<3;i+ )for(j=0;j<3;j+ )nij=i+j;printf(“%d ”, nij);printf(“n”);运行结果为:1 22 33 45.#include <stdio.h>int m33=1,2,3;int n33=1,2 ,3;main( ) printf(“%d,”,
42、m10+n00); printf(“%dn”,m01+n10);运行结果为:3,06.#include <stdio.h>main() int i;int x33=1,2,3,4,5,6,7,8,9; for (i=1; i<3; i+)printf("%d ",xi3-i); 运行结果为:6 87.#include <stdio.h>main() int j;char str10; scanf(“%s”, str); printf(“%sn”,str); 键盘输入:howare(空格) you(回车)运行结果为:howare8 #includ
43、e “stdio.h” func(int b ) int j; for(j=0;j<4;j+) bj=j; main( ) int a4, i; func(a); for(i=0; i<4; i+) printf(“%2d”,ai); 运行结果为: 0 1 2 39#include <stdio.h>main()char diamond5=_,_,*,_,*,_,*,*,_,_,_,*,_,*,_,*,_,_,*;int i,j;for(i=0;i<5;i+)for(j=0;j<5;j+)printf(“%c”,diamondij);printf(“n”);
44、注:“_”代表一个空格。运行结果为:_*_*_*_*_*_*_*10. #include <stdio.h>main( ) int i, f10; f0=f1=1; for(i=2;i<10;i+) fi=fi-2+fi-1; for(i=0;i<10;i+) if(i%4=0) printf(“n”); printf(“%d ”,fi); 运行结果为:1 1 2 35 8 13 2134 5511. #include <stdio.h>main() float fun(float x); float ave,a3=4.5,2,4; ave=fun(a);
45、printf(“ave=%7.2f”,ave); float fun(float x) int j;float aver=1; for (j=0;j<3;j+)aver=xj*aver; return(aver);运行结果为:ave= 36.0012#include <stdio.h>main()int a23=1,2,3,4,5,6;int b32,i,j;for(i=0;i<=1;i+)for(j=0;j<=2;j+) bji=aij;for(i=0;i<=2;i+) for(j=0;j<=1;j+) printf("%5d",
46、bij); 运行结果为: 1 4 2 5 3 613#include <stdio.h>f(int b,int n)int i,r; r=1; for (i=0;i<=n;i+) r=r*bi; return (r);main() int x,a=1,2,3,4,5,6,7,8,9; x=f(a,3); printf(“%dn”,x);运行结果为:24 14.#include"stdio.h"main()int j,k; static int x44,y44;for(j=0;j<4;j+) for(k=j;k<4;k+) xjk=j+k;for
47、(j=0;j<4;j+) for(k=j;k<4;k+) ykj=xjk;for(j=0;j<4;j+) for(k=0;k<4;k+) printf("%d,",yjk);运行结果为:0,0,0,0,1,2,0,0,2,3,4,0,3,4,5,6五、指针1.# include <stdio.h>main ( ) int x = 10, 20, 30, 40, 50 ;int *p ;p=x;printf ( “%d”, *(p+2 ) ); 运行结果为: 302.#include <stdio.h>main( ) char
48、s=”abcdefg”; char *p; p=s; printf(“ch=%cn”,*(p+5);运行结果为: ch=f3.#include<stdio.h>main ( ) int a=1, 2, 3, 4, 5 ;int x, y, *p;p=a;x=*(p+2);printf("%d:%d n", *p, x);运行结果为: 1:34.#include <stdio.h>main( ) int a =1, 2, 3, 4, 5, 6; int x, y, *p; p = &a0; x = *(p+2); y = *(p+4); printf(“*p=%d, x=%d, y=%dn”, *p, x, y);运行结果为: *p=1, x=3, y=55.#include<stdio.h>main() int a5=1,2,3,4,5; int i,*p; p=a; for(i=0;i<5;i+)printf("*%d",ai); for(i=0;i<5;i+) printf("#%d",*p+);运行结果为: *1*2*3*4*5#1#2#3#4#56.#includ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度食堂承包与营养搭配服务合同3篇
- 2025年生物科技企业部分股权增资扩股合同3篇
- 2025年鲜羊奶行业新型经销商合作模式合同范本3篇
- 二零二五年度原创动漫角色形象知识产权归属协议下载2篇
- 二零二五年空压机设备销售与安装验收合同2篇
- 2025年度高速公路服务区智能停车场车位租用合同范本
- 2025年度个人跨境电商担保代理合同4篇
- 2025年度科技创新型企业法人股权激励聘用合同范本
- 2025年度网络安全录像分析合同2篇
- 二零二五年度充电桩设备研发与创新基金投资合同4篇
- 青岛版二年级下册三位数加减三位数竖式计算题200道及答案
- GB/T 12723-2024单位产品能源消耗限额编制通则
- GB/T 16288-2024塑料制品的标志
- 麻风病防治知识课件
- 干部职级晋升积分制管理办法
- TSG ZF003-2011《爆破片装置安全技术监察规程》
- 2024年代理记账工作总结6篇
- 电气工程预算实例:清单与计价样本
- VOC废气治理工程中电化学氧化技术的研究与应用
- 煤矿机电设备培训课件
- 高考写作指导议论文标准语段写作课件32张
评论
0/150
提交评论