资源描述
一、基本输入输出
1.
#include <stdio.h>
main()
{printf("a_bc\td\re\t_f\n");
printf("gh_\tij_\b\b__k");
}注:“_”代表一种空格。
运行成果为:
e_ _ _ _ _ _ _ _f
gh_ _ _ _ _ _ i_ _k
3.
#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,110
2.
#include <stdio.h>
main()
{
char c1=’a’,c2=’b’,c3=’c’;
printf("a%cb%cc%c ",c1,c2,c3);
}
运行成果为:
aabbcc
4.
#include <stdio.h>
main()
{ int i=128;
float x=234.89;
printf(“\n”);
printf("%5d,%8.2f\n",i,x);
printf("%2d,%4.1f\n",i,x);
}
运行成果为:
128, 234.89
128,234.9
5.
#include <stdio.h>
void main(void){
ﻩint x=15,y=5;
float f=1234.567f,b=12345;
printf("%f %10f %10.2f %.2f%-10.2f\n",f,f,f,f,f);
printf("x+y=%d\n",x+y);
printf("b=%8f",b);
}
运行成果为:
1234.567000 1234.567000 1234.57
x+y=20
b=12345.000000
6.
#include <stdio.h>
main( )
{ int i=8, j=9;
int x, y, z , w;
x=i-- ; y=i ;
z=++j; w=j;
printf(“%d,%d,%d,%d”,x,y,z,w);
}
运行成果为:
8,7,10,10
8.
#include <stdio.h>
main()
{ int x=10;
int y=79;
printf("%5d,%5d,%5d",!x,x||y,x&&y);
}注:“_”代表一种空格
运行成果为:
0, 1, 1
7.
#include <stdio.h>
main()
{ int a=1,b=2;
a=a+b;
b=a-b;
a=a-b;
printf(“a=%d,b=%d\n”,a,b);
}
运行成果为:
a=2,b=1
9.
#include <stdio.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,c
10.
#include <stdio.h>
main()
{char c='a';
printf("%c,%d\n",c,c);
}
运行成果为:
a,97
11.
#include <stdio.h>
main ( )
{ char c1, c2;
c1=getchar ( );
printf (“%c, %d\n”, c1,c1 );
c2=c1+32;
printf (“%c,%d\n”, c2,c2 );
}
若敲进A,则运行成果为
A,65
a,97
12.
#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,1
13.
#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,11
14.
#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=-9
15.
#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) / 10;
b0 = x % 10;
printf("bit0=%d, bit1=%d, bit2=%d\n", 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("yes\n");
else
printf("no\n");
}
运行成果为:
no
2.
#include <stdio.h>
main()
{ int a=12, b= -34, c=56, min;
min=a;
if(min>b) min=b;
if(min>c) min=c;
printf("min=%d", min);
}
运行成果为:
min=-34
3.
#include <stdio.h>
main()
{ int x=2,y= -1,z=5;
if(x<y)
if(y<0)
z=0;
else
z=z+1;
printf(“%d\n”,z);
}
运行成果为: 5
4.
#include <stdio.h>
main()
{int a=10,b=50,c=30;
if(a>b)
a=b;
b=c;
c=a;
printf("a=%d b=%d c=%d\n",a,b,c);
}
运行成果为:a=10 b=30 c=10
5.
#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.00
6.
#include <stdio.h>
main()
{int a=2;
float num=3.12,x;
if(num<4)
x=2*num*a;
printf("result is %f\n",x);
}
运行成果为:
result is 12.480000
7.
#include<stdio.h>
main( )
{ char c=’A’;
if ((‘0’<=c ) &&(c<=’9’))
printf(“YES”);
else
printf(“NO”);
}
运行成果为:
NO
8.
#include <stdio.h>
main ( )
{ float c=3.0 , d=4.0;
if ( c>d )
c=5.0;
else
if ( c==d )
c=6.0;
else
c=7.0;
printf ( “%.1f\n”,c ) ;
}
运行成果为:
7.0
9.
#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=%d\n",d);
}
运行成果为:
d=20
10.
#include <stdio.h>
main()
{ int a=2,b=3,c=1;
if (a>b)
if (a>c)
printf (“%d\n”,a);
else
printf (“%d\n”,c);
printf (“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 even\n", m);
ﻩelse ﻩ
printf("%d is a positive odd\n", m);ﻩ
}
ﻩelse
ﻩ {ﻩif (m % 2 == 0)
ﻩ printf("%d is a negative even\n", m);ﻩ
else
ﻩﻩ printf("%d is a negative odd\n", m);ﻩ
ﻩ }
}
若键入-9,则运行成果为:
-9 is a negative odd
12.
#include<stdio.h>
main( )
{ char ch;
ch=getchar( );
switch(ch)
{ case ‘A’ : printf(“%c”,’A’);
case ‘B’ : printf(“%c”,’B’);
break;
default: printf(“%s\n”,”other”);
}
}
当从键盘输入字母A时,运行成果为:
AB
13.
#include <stdio.h>
main( )
{ int a=1,b=0;
scanf(“%d”,&a);
switch(a)
{ case 1: b=1;break;
case 2: b=2;break;
default : b=10;}
printf("%d", b);
}
若键盘输入5,运行成果为:
10
14.
#include <stdio.h>
main ( )
{ int i=0,j=0,k=6;
if((++i>0)||(++j>0))
k++;
printf("%d,%d,%d\n",i,j,k);
}
运行旳成果为:
1,0,7
15.
#include <stdio.h>
main( )
{ int x , y , z;
x=20, y=40, z=60;
while(x<y)
x+=4, y-=4;
z/=2;
printf(“%d,%d,%d”,x,y,z);
}
运行成果为:
32,28,30
16.
#include <stdio.h>
main()
{ int num=0;
while(num<=2)
{ num++;
printf("%d\n",num);}
}
运行成果为:
1
2
3
17.
#include <stdio.h>
main()
{ int n=9;
while(n>6)
{n--;
printf(“%d,”,n);
}}
运行成果为:
8,7,6,
18.
#include <stdio.h>
main( )
{ int sum=10,n=1;
while(n<3)
{sum=sum-n;
n++;
}
printf(“%d,%d”,n,sum);
}
运行成果为:
3,7
19.
#include <stdio.h>
main()
{ int num,c;
scanf("%d",&num);
do
{c=num%10;
printf("%d",c);
}while((num/=10)>0);
printf("\n");
}
从键盘输入23,则运行成果为:
32
20
#include <stdio.h>
main()
{ int s=0,a=5,n;
scanf("%d",&n);
do
{ s+=1;
a=a-2;
}while(a!=n);
printf("%d,%d\n",s,a);
}
若输入旳值1,运行成果为:
2,1
21.
#include <stdio.h>
main()
{ int n1,n2;
scanf(“%d”,&n2);
while(n2!=0)
{ n1=n2%10;
n2=n2/10;
printf(“%d”,n1);
}
}
若在运行时输入1298,运行成果为:
8921
22.
#include <stdio.h>
main()
{ int i;
for (i=0;i<6;i++)
printf (“%d”,++i);
printf (“%d”,i++);
}
运行成果为:
1356
23.
#include "stdio.h"
main()
{char c;
c=getchar();
while(c!='?')
{putchar(c);
c=getchar();
} }
假如从键盘输入abcde?fgh(回车)运行成果为:abcde
24.
#include <stdio.h>
main()
{ char c;
while((c=getchar())!=’$’)
{ if(‘A’<=c&&c<=‘Z’)
putchar(c);
else if(‘a’<=c&&c<=‘z’)
putchar(c-32);
}}
当输入为ab*AB%cd#CD$时,
运行成果为:
ABABCDCD
25.
#include <stdio.h>
main()
{int i=0,s=0;
do
{ if(i%2)
{ i++; continue; }
i++; s +=i;
}while(i<7);
printf("%d\n",s);
}
运行成果为:
16
26.
#include <stdio.h>
main()
{int x=1, y =0;
while(x<=10)
{ y+=x*x;
if (y>=10)
break;
x++;
}
printf(“%d %d”,y,x);
}
运行成果为:
3
27.
#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 5
28.
#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: printf(“**”);
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=1
30.
#include <stdio.h>
main()
{ char grade=’C’;
switch(grade)
{ case ‘A’: printf(“90-100\n”);
case ‘B’: printf(“80-90\n”);
case ‘C’: printf(“70-80\n”);
case ‘D’: printf(“60-70\n”);
break;
case ‘E’: printf(“<60\n”);
default : printf(“error!\n”);
}
}
运行成果为:
70-80
60-70
31.
#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;
case 'D': k=k%2;continue;
case 'E': k=k+10;break;
default: k=k/3;
}
k++;
}while(c<'C') ;
printf("k=%d\n",k);
}
运行成果为:
k=4
32.
#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=%d\n”,i);
}
运行成果为:
i=13
33.
#include <stdio.h>
main( )
{ int sum=0;i=0;
while(i<=100)
sum=sum+i;
printf(“i=%d\n”,i);
printf(“sum=%d\n”,sum);
}
运行成果为:
死循环,无成果
34.
#include <stdio.h>
main()
{ int i,sum=0;
i=1;
do
{sum=sum+i;
i++;
}while(i<=10);
printf(“%d”,sum);ﻫ}
运行成果为:
55
35.
#include <stdio.h>
main ( )
{ int i=0, sum=1 ;
do
{sum+=i++ ;
}while ( i<6 );
printf ( “%d\n”, sum );
}
运行成果为:
16
36.
#include <stdio.h>
main()
{ int i;
printf("\n");
for(i=0;i<6;i++)
{ printf("%d",i);
if (i%2==0)
printf("\n");
}}
运行成果为:
0
12
34
5
37.
#include <stdio.h>
main( )
{ int i;
for(i=0;i<8;i++)
printf("%d,",++i);
printf("%d,", i++);
printf("%d", i);
}
运行成果为:
1,3,5,7,8,9
38.
#include<stdio.h>
main( )
{ int i=0, j=0;
while( i<10) i++;
while(j++<10) ;
printf(“%d,%d”, i, j);
}
运行成果为:10,11
39.
#include<stdio.h>
main( )
{char i, j;
for(i=’0’, j=’9’; i<j ; i++, j--)
printf(“%c%c”, i, j);
}
运行成果为:
40.
#include <stdio.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=%d\n", sum,counter);
}
若键入3 -5 7 -9
运行成果为:
sum=10,counter=2
41.
#include <stdio.h>
main()
{ int i=5;
do { switch (i%2)
{ case 4: i- -; break;
case 6: i- -; continue;
}
i- -;
i- -;
printf(“i=%d\n”,i);
} while(i>0);
}
运行成果为:
i=3
i=1
i=-1
42.
#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 4
main()
{ int i;
int x1=1,x2=2;
for(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 5
13 21 34
44.
#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( )
{int x, y;
for(x=30,y=0;x>=10,y<10; x--,y++)
x/=2, y+=2;
printf(“x=%d,y=%d\n”,x,y);
}
运行成果为:
x=0,y=12
*46.
#include <stdio.h>
#define N 4
main( )
{ int i,j;
for(i=1;i<=N;i++)
{ for(j=1;j<i;j++)
printf(" ");
printf("*");
printf("\n");
}
}
运行成果为:
*
*
*
*
ﻬ三、函数
1.
#include <stdio.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=%d\n",result);
}
当从键盘输入:6,3运行成果为:
result=3
2.
#include <stdio.h>
int min( int x, int y )
{ int m;
if ( x> y )
m = x;
else
m = y;
return(m);
}
main()
{ int a=3,b=5,abmin ;
abmin = min(a,b);
printf(“min is %d”,abmin);
}
运行成果为:
min is 5
3.
#include<stdio.h>
main( )
{ int x=10;
{ int x=20;
printf(“%d, ”, x);
}
printf(“%d”, x);
}
运行成果为:
20,10
4.
#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,",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,20
6.
#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("%d\n",m);
printf("%d\n",func(a,b)/m);
}
运行成果为:
4
1
7.
#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=%d\n", r);
}
运行成果为:
r=15
8.
#include <stdio.h>
int fac(int n)
{ int f=1,i;
for(i=1;i<=n;i++)
f=f * i;
return(f);
}
main()
{ int j,s;
scanf(“%d”,&j);
s=fac(j);
printf("%d!=%d\n",j,s);
}
假如从键盘输入3, 运行成果为:
3!=6
9.
#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 9
10.
#include <stdio.h>
int f(int a)
{ auto int b=0;
static c=4;
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));
}
运行成果为:
9
10
11
11.
#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(“%d\n”,fun6(n));
}
运行成果为:
12
12.
#include <stdio.h>
float f1(float 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=-5
13
#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",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=%d\n”,y);
}
运行成果为:
f=1,f=2,f=6,f=24,y=24
ﻬ四、数组
1.
#include <stdio.h>
main()
{ int i, a[10];
for(i=9;i>=0;i--)
a[i]=10-i;
printf(“%d%d%d”,a[2],a[5],a[8]);
}
运行成果为:
852
2.
#include <stdio.h>
main()
{ int i,a[6];
for (i=0; i<6; i++)
a[i]=i;
for (i=5; i>=0 ; i--)
printf("%3d",a[i]);
}
运行成果为:
5 4 3 2 1 0
3.
#include <stdio.h>
main( )
{ int i,k,a[10],p[3];
k=5;
for(i=0;i<10;i++)
a[i]=i;
for(i=0;i<3;i++)
p[i]=a[i*(i+1)];
for(i=0;i<3;i++)
k+=p[i]*2;
printf("%d\n",k);
}
运行成果为:
21
4.
#include <stdio.h>
main( )
{ int n[3][3], i, j;
for(i=0;i<3;i++ )
{for(j=0;j<3;j++ )
{n[i][j]=i+j;
printf(“%d ”, n[i][j]);
}
printf(“\n”);
}
}
运行成果为:
1 2
2 3
3 4
5.
#include <stdio.h>
int m[3][3]={{1},{2},{3}};
int n[3][3]={1,2 ,3};
main( )
{ printf(“%d,”, m[1][0]+n[0][0]);
printf(“%d\n”,m[0][1]+n[1][0]);
}
运行成果为:
3,0
6.
#include <stdio.h>
main()
{ int i;
int x[3][3]={1,2,3,4,5,6,7,8,9};
for (i=1; i<3; i++)
printf("%d
展开阅读全文