C教材中的源程序代码
更新时间:2024-03-30 17:51:01 阅读量: 综合文库 文档下载
教材中的源程序代码
例1.1
# include
printf(“How are you!\\n”); printf(“Welcome you!”); return 0; } 例1.2
#include
int main ( ) /*主函数*/ {
int a ,b,sum; /*定义变量*/ a=111;b=222; /*为变量赋值*/ sum=a+b; /*求两数之和*/ printf (“sum is: %d”,sum); /*输出sum的值*/ return 0; }
例1.3
#include
int sumab (int x, int y); /*函数声明*/ int main ( ) /*主函数*/ {
int a,b,sum; /*定义变量*/ printf(“input a and b:”); /*提示字符串*/
scanf (“%d %d”, &a,&b); /*输入变量a和b的值*/ sum=sumab(a,b); /*调用sumab函数*/ printf(“sum=%d”, sum); /*输出sum的值*/ return 0; }
int sumab (int x, int y) /*定义sumab函数*/ {
int z; z=x+y; return z; }
例2.1
#include
{ int a=200,b=100,c; c=a+b+15;
printf(\ printf(\
printf(\ getch(); return 0; } 例2.2
#include
{ int a,b,c; unsigned d; long e;
scanf(\ printf(\ return 0; } 例2.3
#include
{ float a,b; double x,y;
scanf(\printf(\ return 0; }
例2.4
#include
{ printf(\
printf(\?A\\?\\53\\\ return 0; } 例2.5
#include
{ char c1,c2,c3=‘P’; scanf(\ c2=‘D’;
printf(\
printf(\,%c,%c,%c \\n \ return 0; } 例2.6
#include
printf(\请输入2个两个大写英文字符:\ scanf(\printf(\ \
c1=c1+32; c2=c2+32; printf(\ return 0; }
例2.7
#include
j=++i; /*表达式++i的值是7 */ k=i++; /*表达式i++的值是7 */ printf(\,%d,%d\\n \i=-6;
j=--i; /*表达式--i的值是-7 */ k=i--; /*表达式i--的值是-7 */ printf(\,%d,%d\\n \ return 0; }
例2.8
#include
{ int m,n,i,j,k=5; i=(j=6,j++,k+j);
printf(\,%d \\n\
printf(\,%d,%d,%d \\n \ return 0; }
例2.9
#include
{ int a;float b; double c; char d;
printf(\
printf(\printf(\ return 0; }
例3.1
#include
{ float x, y,c,area;
printf(“输入长和宽:”); scanf(“%f, %f”, &x,&y); c=2*(x+y); area=x*y;
printf(“周长是:%f\\n”, c); printf(“面积是:%f\\n”, area); return 0; } 例3.2
#include
{ int a=65,b=97;
printf(“%d %d\\n”,a,b); printf(“%d,%d\\n”,a,b); printf(“%c,%c\\n”,a,b); printf(“a=%d,b=%d”,a,b); return 0; }
例3.3
#include
{int a,b; char c; float s1,s2,sum; a=65; b=-3;
s1=123.4; s2=56.75; sum=s1+s2; c=?A?;
printf(“%d %c%d %o %f %c %d”,a,a,b,b,sum,c,c); return 0; }
例3.4
#include
printf(“\\“what do you like?\\””); return 0; } 例3.5
#include
{int i = 56, j=13; float f=12.3456; printf(“i=%-4d”, i); printf(“j=M\\n”,j); printf(“f=%6.2f\\n”,f); return 0; } 例3.6
#include
float x=123.4567, y=-567.123; char c=?A?;
long d=1234567;
unsigned long e=65535; printf(“==\\n”,a,b); printf(“%-12f,%-12f\\n”,x,y); printf(“%7.2f,%7.2f\\n”,x,y); printf(“%e,.2e\\n”,x,y);
printf(“%c,%d,%o,%x\\n”,c,c,c,c); printf(“%ld,%lo,%lx\\n”,d,d,d); printf(“%u,%o,%x\\n”,e,e,e);
printf(“%s,%5.3s\\n”,“computer”, “computer”); return 0; }
例3.7
# include
scanf(“%d %d”, &a,&b); /*由键盘输入两个数并赋给变量a、b*/ printf(“a+b=%d\\n”,a+b); /*计算总和并输出内容*/ printf(“(a+b)/2=%f \\n”, (float) (a+b)/2.0); /*输出平均值*/ return 0; }
例3.8
#include
char ch;
printf(“Input a character:”);
ch=getchar( ); /*输入一个字符,并赋给变量ch*/ printf(“\\nThe character you input is:”); putchar(ch); return 0; } 例3.9
#include
printf(“Input a character:”);
ch=getch(); /*输入一个字符,并赋给变量ch*/ printf(“\\nThe character you input is:“);
putchar(ch); return 0; } 例3.10
#include
printf(“输入用分表示的时间数:”); scanf(“%d”, &k); m=k/60; n=k`;
printf(“\\n%d分等于%d小时%d分。\\n”, k, m, n); return 0; }
例3.11
#include
{char ch; float s1,s2;
printf(“Input a character:”); ch=getchar(); printf(“\\n%c,%d,%o,%x\\n”,ch,ch,ch,ch); s1=ch*ch; s2=3.14*ch*ch; printf(“%f,%f\\n”,s1,s2); return 0; }
例4.1
# include
printf(\ scanf(\
if (x>0) printf(\ return 0; }
例4.2
# include
printf(\输入一个整数:\ scanf(\if (a%2= =0)
printf(\是偶数\\n\ else
printf(\是奇数\\n\ return 0;
}
例4.3
# include
printf(\ scanf(\ if(x<1) y=x+1;
else if (1<=x && x<2) y=x+2; /*或写成else if (x<2) y=x+2;*/ else y=x+3;
printf(\ return 0; }
例4.4
# include
scanf(\
k=(m/10==3)?(m=m*10) : (m=m*6); printf(\ return 0; }
例4.5
# include
printf(\ scanf(\ switch(ch) {case 'y' :
case 'Y' : printf(\ case 'n' :
case 'N' : printf(\ default : printf(\ }
return 0; }
例4.6
# include
printf(\ scanf(\ if(x<10)
if(x<0) y=5*x-6; else y=4*x; else y=2*x+3;
printf(\ return 0; }
例4.7
# include
printf(\ scanf(\switch(x>=0)
{case 0 : y=5*x-6; break; case 1: switch (x>=10)
{case 0 : y=4*x; break; case 1 : y=2*x+3; break; } }
printf(\ return 0; }
例4.8
# include
scanf(\ if(a>b)
{t=a; a=b; b=t;} /*交换a、b,使a中存储小的数*/ if(a>c)
{t=a; a=c; c=t;} /*交换a、c,使a中存储小的数*/ if(b>c)
{t=b; b=c; c=t;} /*交换b、c,使c中存储大的数*/ printf(\ return 0; }
例4.9
# include
{ float score; int grade; scanf(“%f”, &score); grade =(int)(score/10); switch(grade)
{case 0 : case 1 :case 2 :case 3 :case 4:case 5:printf(“E \\n”);break;
case 6 : printf(“D\\n”);break; case 7 : printf(“C\\n”);break; case 8 : printf(“B\\n”);break; case 9 : case 10 : printf(“A\\n”); } return 0; } 例4.10
#include
{ float a,b,c,d,x1,x2,p,q;
printf(\输入方程系数 a,b,c:\ scanf(\ d=b*b-4*a*c;
if (fabs(d)<=1e-6) /*即d==0*/ printf(\有两个相等的实根:%8.4f\\n\
else if (d>1e-6) /*即d>0*/ { x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a);
printf(\有两个不相等的实根:%8.4f 和 %8.4f\\n\ } else
{ p=-b/(2*a);
q=sqrt(-d)/(2*a);
printf(\有两个共轭复根\\n\ printf(\ printf(\ } return 0; }
例4.11
# include
float bonus,bon1,bon2,bon4,bon6,bon10; bon1=10*0.1;
bon2=bon1+10*0.075; bon4=bon2+20*0.05; bon6=bon4+20*0.03; bon10=bon6+40*0.015; printf(\利润值:\ scanf(\ if (i<=10)
bonus=i*0.1; else if (i<=20)
bonus=bon1+(i-10)*0.075; else if (i<=40)
bonus=bon2+(i-20)*0.05; else if (i<=60)
bonus=bon4+(i-40)*0.03; else if (i<=100)
bonus=bon6+(i-60)*0.015; else
bonus=bon10+(i-100)*0.01; printf(\奖金是%f\\n\ return 0; }
# include
{long i; int c;
float bonus,bon1,bon2,bon4,bon6,bon10; bon1=10*0.1;
bon2=bon1+10*0.075; bon4=bon2+20*0.05; bon6=bon4+20*0.03; bon10=bon6+40*0.015;
printf(\利润值:\ scanf(\ c=i/10;
if (c>10) c=10; switch(c)
{ case 0 : bonus=i*0.1;break;
case 1 : bonus=bon1+(i-10)*0.075;break; case 2 :
case 3 : bonus=bon2+(i-20)*0.05;break; case 4:
case 5 : bonus=bon4+(i-40)*0.03;break; case 6 : case 7 : case 8 :
case 9 : bonus=bon6+(i-60)*0.015;break; case 10 : bonus=bon10+(i-100)*0.01; } 例5.1
# include
{ int i=1, sum=0;
while(i<=99) {sum+=i; i+=2; }
printf(\ return 0; }
例5.2
# include
{ scanf(\ sum+=n;
}while(sum<=1000); printf(\ return 0; }
例5.3
# include
{ int num,digit;
printf(\ scanf(\ do
{digit=num; printf(\ num/=10;
}while(num!=0); printf(\ return 0; } 例5.4
# include
printf(\ scanf(\ for(i=1;i<=n;i++) s = s*i;
printf(\ return 0; }
例5.5
#include
printf(\ for(;getchar()!='\\n';n++) ; printf(\ return 0; }
例5.6
# include
for(i=1;i<10;i++) /*外层循环控制输出的行数*/
{ for(j=1;j<=i;j++) /*内层循环控制每行输出的列数,以及输出的内容*/ printf(\ printf(\ } return 0; }
例5.7
# include
{ int i,j,k,n=5; char c1= ? ?, c2=?*?; for(i=0;i {for(j=0;j for(k=0;k<=2*i;k++)/*输出每行中后面部分的若干个*号*/ printf(\ printf(\ } return 0; } 例5.8 #include printf(\ scanf(\ k=sqrt(m); for(i=2;i<=k;i++) if (m%i==0) break; if (i<=k) printf(\ else printf(\ return 0; } 例5.9 # include {int i=0,n; long sum=0; while (i<100) {scanf(\ i++; if (n<=0) continue; sum+=n; } printf(\return 0; } 例5.10 # include { int i,n,sum=0; i=1; scanf(“%d”,&n); loop : if (i<=n) { sum+=i; i++; goto loop; } printf(\ return 0; } 例5.11 # include { float i=1.0; int k=1; double t=1.0, pi=0; do{ pi= pi+t; i+=2; k=-k; t=k/i; }while(fabs(t)>=1e-6); pi= pi*4; printf(\ return 0; } 例5.12 # include {int men,women,child; for(men=0;men<=45;men++) for(women=0;women<=45;women++) for(child=0;child<=45;child++) if(men+women+child==45&&men*3+women*2+child*0.5==45) printf(\ women=%d child=%d\\n\ return 0; } 例5.13 main( ) {int m, n, p, q, k, t ; scanf(“%d, %d”, &m,&n); if (n if (n%k==0 && m%k==0) t=k; if (t==1) printf(“%d和%d互质\\n”, m, n); else printf(“%d和%d的最大公约数是:%d\\n”, m, n, t); for(k=m*n; k>=p; k--) if (k%n==0 && k%m==0) t=k; printf(“%d和%d的最小公倍数是:%d\\n”, m, n, t); } 例6.1 #include {int n, sum=0, a[10]; float aver; for ( n=0; n<10; n++) {scanf(“%d”, &a[n]); sum=sum+ a[n]; } aver =sum/10.0: for(n=0;n<=9;n++) printf(“%d,”, a[n]); printf(“%d,%f\\n”, sum,aver); return 0; } 例6.2 # include { int n,m,sign=0; int num[N]={16,35,48,29,56,43,93,64,90,48}; printf(“Please input the number:”) ; scanf(“%d”, &n); for ( m=0; m if (n==num[m]) {printf(“%d,%d \\n”, m,num[m]); sign=1;} if(sign!=1)printf(“Have no this number。”) ; return 0; } 例6.3 # define N 10 # include {int k, max, loca=0, a[N]; srand(time(NULL) ); /*设置随机数种子为当前时间*/ for (k=0;k for(k=1;k if (a[k]>max) {max=a[k]; loca=k;} printf(\ return 0; } 例6.4 # define N 10 # include { int i, j, m, temp, a[N]; for (i=0; i for (i=0; i<=N-2; i++) {m=i; for (j=i+1; j<=N-1; j++) if (a[j]>a[m]) m=j; temp=a[i]; a[i]=a[m]; a[m]=temp; } for (i=0; i<=N-1; i++) printf (\printf(\return 0; } 例6.5 # define N 11 # include int x[N]={98,96,87,78,72,64,56,51,43,36}; for (k=0;k<=N-2;k++) printf(“%d ,”,x[k]); printf(“\\n input the number inserted\\n”); scanf(“%d”,&y); if (y<=x[N-2]) x[N-1]=y; /*若y小于或等于x[N-2],则将y插在最后*/ else { i=0; while (i {if (y>x[i]) /*若y大于x[i],则将y插在下标为i的位置*/ {for (k=N-2;k>=i;k--) /*下标大于或等于i的元素依次向后移动一个位置*/ x[k+1]=x[k]; x[i]=y; break; } i++; } } for (k=0;k<=N-1;k++) printf(“%d ,”,x[k]); printf(“\\n”); return 0; } 例6.6 # define N 15 # include { int top, bott, mid, loca, a, b[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; scanf(\ top=0; bott=N-1; loca=-1; while (top<=bott) {mid=(top+bott)/2; if (a==b[mid]) {printf(\ loca=mid; break; } else if (a if (loca==-1) printf(\ return 0; } 例6.7 # include { int a[2][3],b[2][3],c[2][3],d[2][3] ,i,j,k=1; for (i=0;i<2;i++) for (j=0;j<3;j++) {a[i][j]=k++; b[i][j]=a[i][j]+6; } for (i=0;i<2;i++) for (j=0;j<3;j++) {c[i][j]=a[i][j]+2*b[i][j]; d[i][j]=8*a[i][j]-b[i][j]; } for (i=0;i<2;i++) {for (j=0;j<3;j++) printf(\”,c[i][j]); printf(\ } for (i=0;i<2;i++) {for (j=0;j<3;j++) printf(\”,d[i][j]); printf(\ } return 0; } 例6.8 # include { int a[2][2]={1,2,4,5}; int b[2][2]={7,8,10,11}; int i,j,a1[2][2],b1[2][2],c[2][2]; for (i=0;i<2;i++) for (j=0;j<2;j++) {a1[j][i]=a[i][j]; /* a1是a的转置矩阵*/ b1[j][i]=b[i][j]; /* b1是b的转置矩阵*/ } for (i=0;i<2;i++) for (j=0;j<2;j++) c[i][j]=5*a1[i][j]-2*b1[i][j]; for (i=0;i<2;i++) {for (j=0;j<2;j++) printf(\”,c[i][j]); printf(\ } return 0; } 例6.9 # define N 7 # include int main() {int i, j, a[N][N]; for (i=1; i for (i=3; i a[i][j]=a[i-1][j-1]+a[i-1][j]; /*除第1列和对角线及对角线以上的元素外*/ /*其它元素a[i][j]满足a[i][j]=a[i-1][j-1]+a[i-1][j]*/ for (i=1; i {for (j=1; j<=i; j++) printf(“]”, a[i][j]); printf(“\\n”); } return 0; } 例6.10 # define M 100 # define N 5 int main() {int i, j; float score[M][N], aver[M], sum; for (i=0; i scanf(“%f”, &score[i][j]); for (i=0; i for (j=0; j for (i=0; i # include # define N 100 /*假设字符数小于100*/ int main() { char a[N]; int i, k=0; for (i=0; i {scanf (\ if (a[i]== '#')break; k++; } for (i=0; i if ('a'<=a[i]&&a[i]<='z'||'A'<=a[i]&&a[i]<='Z') printf (\ printf(\ return 0; } 例6.12 # include { char a[30],b[30]={'\\0'}; int i,j=0; scanf(\ printf(\ for (i=0;i<30;i++) if (a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U') {printf(\ b[j++]=a[i]; } printf(\ printf(\ return 0; } 例6.13 int main() { int i; char s1[12]={\ char s2[10]=\ strcpy(s1,s2); puts(s1); puts(s2); for (i=0;i<12;i++) printf(\ printf(\ strcpy(s1,\ puts(s1); for (i=0;i<12;i++) printf(\ return 0; } 例6.14 # include char ch, s1[30], s2[30] ; printf(\:\ gets(s1); printf(\:\ scanf(\ for (i=0;s1[i]!='\\0';i++) if (s1[i]!=ch) s2[j++]=s1[i]; s2[j]='\\0'; puts(s2); return 0; } 例6.15 # define K 10 # include { int m, n; char ch, a[K+1]; printf(“input character :\\n”); for ( n=1; n<=K; n++) /*没有使用下标为0的数组元素*/ scanf(“%c”,&a[n]) ; for ( n=1; n<=K-1; n++) for ( m=1; m<=K-n; m++) if (a[m]>a[m+1]) {ch= a[m]; a[m]= a[m+1]; a[m+1]=ch;} for ( n=1; n<=K; n++) printf(“<”, a[n]) ; return 0; } 例6.16 # include { int i,k=0,h; char big[20], str[N][20]; /*设每个字符串的长度都小于20*/ for (i=0;i {if (strcmp(str[i],big)>0) strcpy(big,str[i]); if (strlen(str[i])>h) { h=strlen(str[i]); k=i; } } printf(\ printf(\ return 0; } 例6.17 # include { char str1[80],str2[80]; int i,j=0,k=0,num; gets(str1); while (str1[k]==' ') k++; /*循环结束时,k值是英文句子前面的空格数*/ for (i=k;str1[i]!='\\0';i++) str2[j++]=str1[i]; /*将str1的前端空格去除后复制到str2中*/ if (strlen(str2)==0) printf(\ /*若输入的是空句子,包含0个单词*/ else {num=1; for (i=1;str2[i]!='\\0';i++) if (str2[i]==' '&&str2[i+1]!=' ') num++; /*若一个空格后连接一个非空格,则表示出现了一个单词*/ printf(\ } return 0; } 例6.18 # include {char number[N][10], name[N][10], phnoe[N][15], address[N][20], find[10]; int k, p,mark=0; for(k=0; k {gets(number[k]); gets(name[k]); gets(phnoe[k]); gets(address ); } printf(“输入需要查找的学生的学号:\\n” ); gets(find); for(k=0; k if (strcmp(find, number[k])==0) {p=k; mark=1; break;} if (mark==1) printf(“%s,%s,%s,%s\\n”, number[p], name[p], phnoe[p]),address[p]); else printf(“没有发现!”); return 0; } 例7.1 # include int score[N]={97,90,88,82,79,78,73,68,66,65}; void function1( ) {int i,s=0; for(i=1;i printf(“平均分为:%f\\n”,(float)s/N); return ; } int function2(int n) {int i,j,k=-1; for(i=1;i if (n==score[i]) k=i; return k; } main() { int a, t; function1(); printf(“输入一个分数”); scanf(“%d”,&a); t=function2(a); if (t>=0) printf(“该分数在%d个分数中排名第%。”,N,t+1); else printf(“不存在此分数!”); return 0; } 例7.2 int max(int a, int b) { int x; if(a>b) x=a; else x=b; return x; } 例7.3 #include void fun(int b) /*函数定义,b为形参*/ { int st[20]={2,6,3,5,7,1,4,3,4,2,2,6,6,1,7,5,5,2,1,7}; int k ,n=0; for (k=0; k<20; k++) if (st[k]==b) n++; printf(\} int main() { int a,yn=0; while(yn==0) { printf(\—9): \ scanf(\调用函数,a为实参*/ printf(\:\ scanf(\ } return 0; } 例7.4 #include float fadd(float a , float b) /*函数定义*/ {float s; s=a+b; return(s); /*返回计算结果:两个实数的和*/ } int main() { float x,y,sum,aver; scanf(“%f,%f”,&x,&y); /*输入两个实数*/ sum=fadd(x,y); /*函数调用*/ aver=fadd(x*x,y*y)/2; printf(“\\n%f,%f”,sum,aver); return 0; } 例7.5 # include int prime(int i) /*函数定义*/ { int j,k,flag=1; k=sqrt(i); for (j=2;j<=k;j++) if (i%j==0) {flag=0; break; } return flag; } int main( ) { int i; for (i=3; i<100; i++) if (prime(i)==1) /*函数调用*/ printf (\ printf(\return 0; } 例7.6 #include int imax(int x,int y) /*函数定义*/ {return (x>y?x:y); } int main() { int n1,n2,n3,n4,d; scanf(“%d,%d,%d,%d”,&n1,&n2,&n3,&n4); d=imax(imax(n1,n2),imax(n3,n4)); /*函数调用*/ printf(“The max=%d”,d); return 0; } 例7.7 #include double sum(int m); /*函数声明*/ long fact(int p); /*函数声明*/ int main() { int n; float total; printf(“\\nplease input n:”); scanf(“%d”,&n); total=sum(n); /*函数调用*/ printf(“\\ne=%f”, total); return 0; } double sum(int m) /*函数定义*/ { double s=1; int i; for(i=1;i<=m; i++) s+=1.0/fact(i); /*函数调用*/ return(s); } long fact(int k) /*函数定义*/ { long f=1; int i; for(i=1;i<=k; i++) f*=i; return(f); } 例7.8 #include long fact(int n); /*函数声明*/ int main() { int n; printf(“\\nplease enter n:”); scanf(“%d”,&n); printf(“\\n n!=%ld”,fact(n)); /*函数调用*/ return 0; } long fact(int n) /*函数定义*/ {if (n==1||n==0) return(1); /*若是终结条件,返回终结条件下的值*/ else return(n*fact(n-1)); /*若非终结条件,递归调用函数自身*/ } 例7.9 #include long fib(int n); /*函数声明*/ int main ( ) {int i ; for (i=1; i<=20; i++) {printf (“ld”, fib(i)) ; /*函数调用*/ if ( i%5==0 ) printf(“\\n”); } return 0; } long fib(int n) /*函数定义*/ {if ((n==1)||(n==2)) return 1; else return (fib(n-1)+fib(n-2)); } 例7.10 #include void move(char x,char y) {printf(“%c-->%c\\n”,x,y); } void hanoi(int n,char one,char two,char three) { if (n==1) move(one,three); else {hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } main() {int m; scanf(“%d”,&m); hanoi(m,‘A’,‘B’,‘C’); } 例7.11 #include void add(int b[],int n); /*函数声明*/ int main() {int i,a[ ]={0,1,2,3,4,5,6,7,8,9}; for(i=0;i<10;i++) printf(“=”,a[i]); add(a,10); /*函数调用*/ for(i=0;i<10;i++) printf(“=”,a[i]); return 0; } void add(int b[],int n) /*函数定义*/ { int i; for(i=0;i #include float average(float a[],int n); /*函数声明*/ int main() {float score[N],aver; int i,num=0; printf(“input scores:\\n”); for(i=0;i aver=average(score,N); /*函数调用*/ printf(“average=%f\\n”,aver); for(i=0;i {if (score[i] printf(“less than average: %d”,num); return 0; } float average(float a[],int n) /*函数定义*/ {int i; float aver ,sum=a[0]; for(i=1;i 例7.13 # include int a=0; float b; /*定义全局变量a,b*/ float func(int s[ ],int n); /*函数声明*/ int main() { int k; int x[10]; /*定义局部变量*/ for (k=0;k<10;k++) scanf(“%d”,&x[k]); b=func(x,10); for (k=0;k<10;k++) if (x[k]>b) a++; printf(“\\n %d”,a); /*输出全局变量a的值*/ return 0; } float func(int s[ ],int n) {int k,a=0; float b; /*定义局部变量k,a,b*/ for(k=0;k printf(“\\n%d”,a); /*输出局部变量a的值*/ b=(float)a/n; return(b); } 例7.14 #include int sum(int x); /*函数声明*/ int main( ) {int i,k; for(i=1;i<=10;i++) k=sum(i); printf(“\\n1+2+ ? +- = -”,i-1,k); return 0; } int sum(int x) { static int s=0; /*定义静态存储类型内部变量*/ s=s+x; return s; } 例7.15 /*file1.c*/ # include int x=0; int main() { func( ); printf(“\\nx=%d”,x); return 0; } /*file2.c*/ extern int x; /*外部变量声明*/ int func( ) { x+=3; } 例7.16 /*file1.c*/ #include {extern char get_ch(); /*外部函数声明*/ printf(“%c”,get_ch()); return 0; } /*file2.c*/ char get_ch() {char ch; ch=getchar(); if (‘a’<=ch && ch<=‘z’) ch=ch-32; return(ch); } 例7.17 # include int numb() /*随机产生正整数函数(两位数)*/ {int n; mark1: n=rand();/*rand()值是0到32767之间的随机整数*/ if (n>=100 || n<10) goto mark1; return n; } void add() /*练习加法函数*/ {int a,b,c,x=1; while(x==1) {a=numb(); b=numb(); printf(\ scanf(\ if (a+b==c) printf(\回答正确!\\n\ else printf(\回答错误!\\n\ printf(\若想停止练习加法请输入0, 否则请输入1。\\n\ scanf(\ } return; } void sub() /*练习减法函数*/ {int a,b,c,x=1; while(x==1) {mark2:a=numb(); b=numb(); if (a if (a-b==c) printf(\回答正确!\\n\ else printf(\回答错误!\\n\ printf(\若想停止练习减法请输入0, 否则请输入1。\\n\ scanf(\ } return; } main() {int d; srand(time(NULL)); /*设置随机数种子为当前时间*/ while (1) {printf(\小学生两位数加、减法练习软件-----------\\n\ printf(\、练习两位数加法--------------\\n\ printf(\、练习两位数减法--------------\\n\ printf(\、结束练习------------- -----\\n\ printf(\请输入1或2或3---:\ scanf(\ if (d==1) add(); else if (d==2) sub(); else if (d==3) {printf(\本次练习结束了,再见!------- \\n\ break; } else printf(\输入错误!请重新输入!----- \\n\ } } 例7.18 #include #define SIZE 100 /*不妨设班级学生数少于100*/ void inputdata( ); /*函数声明*/ void calculdata( ); /*函数声明*/ void statisdata( ); /*函数声明*/ void querydata ( ); /*函数声明*/ float score[SIZE][4]; /* score存储每个学生平时、期中、期末和总评成绩*/ char name[SIZE][15]; /* name存储每个学生姓名*/ int sum; /* sum存储实际学生人数*/ int main() { int choo; printf(“请输入实际学生人数:”); scanf(“%d”, &sum); while(1) { printf(“请选择下面的某一项任务:”); printf(“\\n(1)输入每个学生的姓名、平时成绩、期中成绩、期末成绩。”); printf(“\\n(2)计算并输出每个学生的总评成绩。”); printf(“\\n(3)根据总评成绩对学生分类统计,输出各分数段的学生数。”); printf(“\\n(4)根据输入的学生姓名,查找并打印该学生的各项成绩。”); printf(“\\n(5)结束程序运行。”); printf(“\\n请输入你的选择(1或2或3或4或5):”); scanf(“%d”, &choo); if (choo==5)break; switch(choo) {case 1 : inputdata( ); break; /*调用输入学生姓名成绩函数*/ case 2 : calculdata( ); break; /*调用计算总评成绩函数*/ case 3 : statisdata( ); break; /*调用统计各分数段人数函数*/ case 4 : querydata( ); break; /*调用根据姓名查找函数*/ } } printf(“\\n程序运行结束,再见。”); return 0; } void inputdata( ) /*输入学生姓名成绩函数*/ { int i; for(i=0;i {printf(“\\n请输入学生的姓名:”); gets(name[i]); printf(“请顺序输入学生平时、期中、期末成绩(例如98,85,96):”); scanf(“%f,%f,%f”,&score[i][0], &score[i][1],&score[i][2]); } return; } void calculdata( ) /*计算总评成绩函数*/ { int i; for(i=0;i score[i][3]= 0.1*score[i][0]+ 0.3*score[i][1]+ 0.6*score[i][2] printf(“\\n每个学生总评成绩如下:\\n”); for(i=0;i printf(“%s:%f\\n”,name[i],score[i][3]); return; } void statisdata( ) /*统计各分数段人数函数*/ { int i,d, grade[5]={0}; /* grade存储分类统计结果(各分数段人数)*/ for (i=0;i { d=(int)(score[i][3]/10); switch(d) { case 10 : case 9 : grade[4]=grade[4]+1;break; case 8 : grade[3]=grade[3]+1;break; case 7 : grade[2]=grade[2]+1;break; case 6 : grade[1]=grade[1]+1;break; default : grade[0]=grade[0]+1; } } printf(“\\n总评成绩≥90的人数是:%d”, grade[4] ); printf(“\\n总评成绩≥80且<90的人数是:%d”, grade[3] ); printf(“\\n总评成绩≥70且<80的人数是:%d”, grade[2] ); printf(“\\n总评成绩≥60且<70的人数是:%d”, grade[1] ); printf(“\\n总评成绩<60的人数是:%d”, grade[0] ); return; } void querydata( ) /*根据姓名查找函数*/ { char lookname[20]; int i; printf(“\\n请输入姓名:”); gets(lookname); for(i=0;i if (strcmp(name[i],lookname)==0) { puts(name[i]); printf(“平时成绩%f, 期中成绩%f, 期末成绩%f,总评成绩%f\\n”, score[i][0],score[i][1], score[i][2],score[i][3] ); } if (i==sum) printf(“没有找到!\\n”); return; } 例8.1 # include # define SIZE 100 int main() {int i,sum=0, data[SIZE]; for(i=0;i {scanf(“%d”, &data[i]); sum=sum+data[i]; } printf(“sum=%d\\n”,sum); return 0; } 例8.2 # include #define S(a, b, h) (a+b)*h/2 int main() {int c1=6, c2=8, c3=10; printf(“S=%d\\n”, S(c1, c2, c3)); return 0; } 例8.3 #define START { #define OK } #define MAX(x,y) x>y?x:y # include #include “bj.h” int main() START double x=567.89, y=123.45; int a=25, b=37; printf(“double MAX=%lf\\n”, MAX(x,y)); printf(“int MAX=%d\\n”, MAX(a,b)); return 0; OK 例8.4 /*file.c*/ #include myfile.txt void fun() {char c; if((c=getchar())!=?\\n?) {putchar(c); fun();} } 例8.5 # include {int i, a[10]; float s=0, t=1 ; for (i=0; i<10; i++) scanf(“%d”, &a[i]); # if TERM for (i=0; i<10; i++) t=t*a[i]; printf (\ # else for (i=0; i<10; i++) s= s+a[i]; printf (\ # endif return 0; } 例9.1 # include p=&i; /*p指向i */ i=10; printf(“%d,%d\\n”,i,*p); *p=100; printf(“%d,%d\\n”,i,*p); p=&j; *p=200; printf(“%d,%d\\n”,j,*p); return 0; } 例9.2 # include int main() {int num1=12, *p1; /*定义一个指向int型数据的指针变量p1 */ float num2=3.14, *p2; /*定义一个指向float型数据的指针变量p2 */ char ch=?p?, *p3; /*定义一个指向char型数据的指针变量p3 */ p1=&num1; /*取变量num1的地址,赋值给p1 */ p2=&num2; /*取变量num2的地址,赋值给p2 */ p3=&ch; /*取变量ch的地址,赋值给p3 */ printf(“num1=%d, *p1=%d\\n”, num1, *p1); printf(“num2=%4.2f, *p2=%4.2f\\n”, num2, *p2); printf(“ch=%c, *p3=%c\\n”, ch, *p3); return 0; } 例9.3 # include int main() { int num1,num2; int *p1=&num1, *p2=&num2, *p; printf(“Input the first number: ”); scanf(“%d”,p1); printf(“Input the second number: ”); scanf(“%d”,p2); printf(“num1=%d, num2=%d\\n”, num1, num2); if( *p1 > *p2 ) /*如果num1>num2,则交换指针的值*/ {p= p1;p1= p2;p2=p;} printf(“min=%d, max=%d\\n”, *p1, *p2); return 0; } 例9.4 # include int main() { int a[10], *p=a, i,sum=0; printf(“Input 10 numbers: ”); for(i=0; i<10; i++) scanf(“%d”, p+i); /*使用指针变量来输入数组元素的值*/ printf(“array a: ”); for(i=0; i<10; i++) {printf(“%d ”, *(p+i)) ; /*使用指向数组的指针变量输出数组元素的值*/ sum=sum+*(p+i) ; } printf(“\\n sum=%d \\n”, sum); return 0; } 例9.5 # include int main() { char string[ ] = “she is our teacher “; printf(\ return 0; } 例9.6 # include int main() { char *string =“she is our teacher”; printf(“%s\\n”,string); return 0; } 例9.7 # include void copystr(char str1[], char str2[]) { int i=0; while(str1[i] != ?\\0?) { str2[i] = str1[i]; i++; } str2[i] = ?\\0?; } int main() { char a[20 ] = “I love china!”; char b[20 ] = “good!”; printf(“a =%s\\n b =%s\\n”, a,b); copystr(a,b); printf(“a =%s\\n b =%s\\n”, a,b); return 0; } # include void copystr(char str1[], char str2[]) { int i=0; while(str1[i] != ?\\0?) { str2[i] = str1[i]; i++; } str2[i] = ?\\0?; } int main() {int i; char *a = “I love china!”; char *b = “good!”; printf(“a =%s\\n b =%s\\n”, a,b); copystr(a,b); for (i=0;*(a+i)!=?\\0?; i++) putchar(*(a+i)); printf(“\\n”); for (i=0;*(b+i)!=?\\0?; i++) putchar(*(b+i)); return 0; } # include void copystr (char *str1, char *str2) { for (; *str1 != ?\\0?; str1++, str2++) *str2 = *str1; *str2 = ?\\0?; } int main() {char *a = “I love china!”; char *b = “good!”; for (i=0;*(a+i)!=?\\0?; i++) putchar(*(a+i)); printf(“\\n”); for (i=0;*(b+i)!=?\\0?; i++) putchar(*(b+i)); printf(“\\n”); copystr(a,b); puts(a); puts(b); return 0; } # include void copystr (char *str1, char *str2) {int i=0; for (; (*(str2+i)=*(str1+i))!= ?\\0?; i++) ; /*循环体为空语句*/ } int main() {char a[20]= “I love china!”; char b[20]= “good!”; printf(“a =%s\\n b =%s\\n”, a,b); copy_string (a, b); puts(a); puts(b); return 0; } 例9.8 # include int main() {int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; int (*p)[4], row, col; /*定义行指针变量p*/ p=a; printf(“Input row = ”); scanf(“%d”, &row); printf(“Input col = ”); scanf(“%d”, &col); printf(“a[%d][%d] = %d\\n”, row, col, *(*(p+row)+col)); return 0; } 例9.9 # include { void total_average(float *p,int n); void student_average(float (*p)[3],int n); int i,j,k; float x, score[5][3]; printf(“input score:\\n”); for(i=0;i<5;i++) for(j=0;j<3;j++) { scanf(“%f”,&x); score[i][j]=x; } total_average(score[0],15); printf(“Input No. of student:\\n”); scanf(“%d”, &k); student_average(score,k); return 0; } void total_average(float *p,int n) { float *p_end, sum=0, aver ; p_end=p+n-1; for (; p<=p_end; p++) sum=sum+(*p); aver=sum/n; printf(“average=%5.2f\\n”,aver); } void student_average(float (*p)[3],int n) {int i; float sum1=0.0, aver; for (i=0; i<3; i++) sum1=sum1+*(*(p+n)+i); aver=sum1/3; printf(“The average score of No.%d is :%5.2f”,n,aver); } 例9.10 #include void sort(char *addr[], int n); /*函数声明*/ void print(char *addr[], int n); /*函数声明*/ int main() {char *addr[] ={“Shanghai”, “Beijing” , “Guangzhou” , “Hangzhou”, “Nanjing”}; int n = 5; sort(addr, n); /*排序函数的调用*/ print(addr, n); /*输出函数的调用*/ return 0; } void sort(char *addr[], int n) /*选择法排序*/ {char *temp; int i, j, k; for(i=0; i for(j=i+1; j if (strcmp(addr[k], addr[j])> 0) /*比较addr[k]与addr[j]所对应字符串的大小*/ k = j; if (k != i) /*交换addr[i]与addr[k]的指向 */ {temp = addr[i]; addr[i] = addr[k]; addr[k] = temp; } } } void print(char *addr[], int n) { int i; for (i=0; i printf(\ } 例9.11 #include {char *addr[] ={“Shanghai”, “Beijing” , “Guangzhou” , “Hangzhou”, “Nanjing”}; char **p;int n; p=addr; for(n=0; n<5; n++,p++) printf(\ addr[n], *p); } 例9.12 #include void swap(int *p1, int *p2); /*函数声明*/ int main() {int a=3, b=5, *pa, *pb; printf(“Before swap:”); printf(“a=%d,b=%d\\n”,a,b); pa=&a; pb=&b; swap(pa,pb); printf(“After swap:”); printf(“a=%d,b=%d\\n”,a,b); return 0; } void swap(int *p1, int *p2) /*函数定义*/ {int temp; temp=*p1; *p1=*p2; *p2=temp; } 例9.13 int max(int x, int y); int main () { int (*p)(int, int); int a,b,c; p = max; scanf(\c = (*p)(a,b); printf(\ return 0; } int max(int x, int y) {int z; if (x>y) z = x; else z = y; return z; } 例9.14 #include float f1(float x); /*函数声明*/ float f2(float x); /*函数声明*/ float f3(float x); /*函数声明*/ float f4(float x); /*函数声明*/ int main( ) { void test(float (*f)(float)); /*函数声明*/ int n; printf(“input n:”); scanf(“%d”,&n); switch(n) {case 1: test(f1);break; /*把函数f1的入口地址传给函数test的形参*/ case 2: test(f2);break; /*把函数f2的入口地址传给函数test的形参*/ case 3: test(f3);break; /*把函数f3的入口地址传给函数test的形参*/ case 4: test(f4);break; /*把函数f4的入口地址传给函数test的形参*/ } return 0; } void test(float (*f) (float)) /*函数定义*/ {float x, result; printf(“input x:”); scanf(“%f”,&x); result=(*f)(x); printf(“result=%f”,result); } float f1(float x) /*函数定义*/ {return x;} float f2(float x) /*函数定义*/ {return 2*x*x-1;} float f3(float x) /*函数定义*/ {return 4*x*x*x-3*x;} float f4(float x) /*函数定义*/ {return 8*x*x*x*x-8*x*x+1; } 例9.15 # include char *seek(char *q,char ch) ; /*函数声明*/ int main() {char str[80],ch,*p; printf(“请输入一串字符:” ); gets(str); printf(“请输入要查找的一个字符:” ); scanf(“%c”, &ch); p=seek(str, ch); if (p==NULL) printf(“字符%c在本字符串中不存在。\\n”, ch ); else {printf(“在字符串中,字符%c第一次出现的地址是:%d。\\n”, ch, p ); printf(“从地址%d开始,直到字符串尾的所有字符是:%s。\\n”, p,p); } return 0; } int *seek(char *q, char ch ) /*定义返回指针值的函数*/ {while (*q!=?\\0?) if (*q!=ch) q++; else return q; /*返回要查找的字符的地址*/ return NULL; /*没有找到, 返回空指针*/ } 例9.16 #include int main (int argc, char *argv[]) { int i; for (i = 0; i < argc; ++i) printf(\ return 0; } 例9.17 # include char *spbh[N]={\ float sse[N][M]= {{45,47,50,52},{38,40,41,43},{23,20,24,23},{34,37,33,30},{41,43,40,39}}; /*sse第k行的4个数是spbh[k]所对应的商品编号的4个销售额(k=0,1,2,3,4)*/ void f1( ) {char num[10]; int i,j; float sum=0; printf(\请输入商品编号:\ scanf(\ for (i=0; i if ( strcmp(num,spbh[i])==0) j=i; if (0<=j&&j /*若为真,则j中存储的是与给定商品编号对应的数组sse的行下标*/ {for (i=0; i printf(\商品编号为%s的四年销售总额为%f。\\n\ } else printf(\不存在此编号!\\n\ return; } void f2( ) {char num[10]; int year,i,j,k; ; printf(\请输入商品编号:\ scanf(\ for (i=0; i if ( strcmp(num,*(spbh+i))==0) j=i; printf(\请输入年份(2006或2007或2008或2009):\ scanf(\ k=year-2006; /*k值是与年份对应的数组sse的列下标*/ if ((0<=j&&j printf(\编号为%s的商品在%d年销售额为%f。\\n\ else printf(\不存在此编号或年份!\\n\ return; } void f3( ) {int year,i,k,*p; float sum=0; p=&year; printf(\请输入年份(2006或2007或2008或2009):\ scanf(\ k=*p-2006; if (0<=k&&k printf(\在%d年所有商品销售额为%f。\\n\ } else printf(\年份输入错误!\\n\ return; } void f4( ) {int year,i,k,maxnum=0,minnum=0; float max,min; printf(\请输入年份(2006或2007或2008或2009):\ loop2: scanf(\ if (year<2006||year>2009) {printf(\年份输入错误!\\n\ goto loop2; } k=year-2006; max=*(sse[0]+k); min=*(sse[0]+k); for (i=1; i if (*(sse[i]+k) printf(\在%d年所有商品销售额中,\ printf(\商品编号%s的销售额%f最大,\ printf(\商品编号%s的销售额%f最小。\ return; } int main() {int xz; while(1) {printf(\关于商品销售额的计算和查找+++++++++++\\n\ printf(\根据给定的商品编号,计算该商品四年的销售总额。\\n\ printf(\根据给定的商品编号和年份,找出与之对应的销售额。\\n\ printf(\根据给定的年份,计算该年所有商品的销售总额。\\n\ printf(\根据给定的年份,分别找出该年销售额最大、最小的商品。\\n\ printf(\结束程序运行。\\n\ printf(\请选择(1,2,3,4,5):\ loop1:scanf(\ if (xz<1||xz>5) {printf(\错误输入!请重新输入:\ if (xz==5) break; switch (xz) {case 1: f1(); break; case 2: f2(); break; case 3: f3(); break; case 4: f4(); break; } } printf(\程序运行结束, 再见!\ return 0; } 例10.1 #include char name[10] float score_math; float score_english; float score_computer }; int main() { struct student st; float aver; printf(“Input number:”); scanf(“%d”,&st.num); printf(“Input name:”); scanf(“%s”,st.name); printf(“Input score of mathmatics:”); scanf(“%f”,&st.score_math); printf(“Input score of english:”); scanf(“%f”,&st.score_english); printf(“Input score of computer:”); scanf(“%f”,&st.score_computer); aver=(st.score_math+ st.score_english+ st.score_computer)/3; printf(“Number=%d\\n”, st.num); printf(“Name=%s\\n”, st.name); printf(“average=%f\\n”, aver); return 0; } 例10.2 # include int main() { int i; float x, average=0.0, averscore=0.0; struct student st[N]; for(i=0;i {printf(“Input number:”); scanf(“%d”,&st[i].num); printf(“Input name:”); scanf(“%s”,st[i].name); printf(“Input age:”); scanf(“%d”,&st[i].age); printf(“Input score:”); scanf(“%f”,&x); st[i].score=x; /*用一实型变量间接给score赋值*/ average=average+st[i].age; /*计算年龄的总和*/ averscore=averscore+st[i].score; /*计算成绩的总和*/ } average=average/N; /*计算平均值*/ averscore=averscore/N; for(i=0;i { printf(“Number=%d, ”,st[i].num); printf(“Name=%s, ”,st[i].name); printf(“Age=%d, ”,st[i].age); printf(“Score=%f\\n”,st[i].score); } printf(“Average Age =%f\\n”, average); printf(“Average Score=%f\\n”, averscore); return 0; } 例10.3 # include int main() { int i; float x, average=0.0, averscore=0.0; struct student st[N],*p; p=st; for(i=0; i {printf(“Input number:”); scanf(“%d”,&p->num); printf(“Input name:”); scanf(“%s”, p->name); printf(“Input age:”); scanf(“%d”,& p->age); printf(“Input score:”); scanf(“%f”,&x); p->score=x; /*用一实型变量间接给score赋值*/ average=average+ p->age; /*计算年龄的总和*/ averscore=averscore+ p->score; /*计算成绩的总和*/ } average=average/N; /*计算平均值*/ averscore=averscore/N; for(p=st, i=0; i { printf(“Number=%d, ”, p->num); printf(“Name=%s, ”, p->name); printf(“Age=%d, ”, p->age); printf(“Score=%f\\n”, p->score); } printf(“Average age =%f\\n”, average); printf(“Average score=%f\\n”, averscore); return 0; } 例10.4 1)管理人员的结构为: struct manager_type {char position[11]; /*职务*/ char rank[21]; /*级别*/ }; 2)技术人员的结构为: struct technician_type { char tech_post[11]; /*职称*/ char degree[11]; /*学位*/ char major[11]; /*专业*/ }; 3)工人的结构为: struct worker_type { char speciality[11]; /*工种*/ int rank; /*级别*/ char education[11]; /*文化程度*/ }; 4)定义一分类挡案数据结构为共用体: union sort_type { struct manager_type m; struct technician_type t; struct worker_type w; }; 5) 定义出生日期的结构: struct date { int year; int month; int day; } 6)利用上述定义,建立一职工档案数据结构: struct employee_type { char name[8]; int sex; struct date birsday; int type; union sort_type d; }; 例10.5 # include int main() {enum week{sun,mon,tue,wed,thu,fri,sat}; enum week wk; char *name[ ]={“Sunday”,“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” }; for(wk=sun; wk<=sat; wk++) printf(“\\n-: %s”, wk, name[wk]); return 0; } 例10.6 # include # define N 100 /*假设产品的种类数小于100种*/ struct product { char name[10]; char place[10]; int price; long count; }a[N]; int num; /*num存储库存产品的实际种类数*/ void fun1() {int i, k=0; num=0; for (i=0;i {printf(\输入产品名\\n\ printf(\输入产地:\\n\ printf(\输入进货单价(元/吨)\\n\ printf(\输入库存数量(吨)\\n\ num=num+1; printf(\若结束输入请按1,若继续输入请按0。\\n\ if (k==1) break; } return; } void fun2() {int i; long sum=0; for (i=0;i sum=sum+a[i].count; printf(\库存产品数量总和=%ld\\n\ return; } void fun31() {int i; char pron[10]; printf(\请输入产品名:\ for (i=0;i if (strcmp(pron,a[i].name)==0) printf(\ return; } void fun32() {int i; char pron[10]; printf(\请输入产地:\ for (i=0;i if (strcmp(pron,a[i].place)==0) printf(\ return; } void fun3() {int se; while(1) {printf(\按产品名或产地查找并输出产品信息------\\n\ printf(\按产品名查找并输出产品信息------\\n\ printf(\按产地查找并输出产品信息------\\n\ printf(\结束查找,返回上一级操作。------\\n\ printf(\请选择(1,2,3):\ if (se==1) fun31(); else if (se==2) fun32(); else break; } return; } int main() { int se ; while (1) {printf(\库存产品信息管理------\\n:\ printf(\输入库存产品信息------\\n\ printf(\计算并输出库存产品数量总和------\\n\ printf(\查找并输出产品信息------\\n\ printf(\结束操作------\\n\ printf(\请选择(1,2,3,4):\ scanf(\ if (se==1) fun1(); else if (se==2) fun2(); else if (se==3) fun3(); else if (se==4) break; else {printf(\选择有误!请重新选择!\\n\ } printf(\谢谢使用!再见!\\n\ return 0; } 例11.1 # include int main() {unsigned char a=18,b; b=~a; printf(“~a=%d”,b); return 0; } # include int main() {char a=18,b; b=~a; printf(“~a=%d”,b); return 0; } 例11.2 # include int main() {unsigned char a=18,b; b=a<<3; printf(“a<<3=%d”,b); } # include int main() {char a=18,b; b=a<<3; printf(“a<<3=%d”,b); return 0; 例11.3 # include int main() {unsigned char a=18,b; b=a>>3; printf(“a>>3=%d”,b); return 0; } 例11.4 # include int main() {unsigned char a=173,b=203,c; c=a&b; printf(“a&b=%d”,c); return 0; } 例11.5 # include int main() {char a=-83,b=-53,c; c=a&b; printf(“a&b=%d”,c); return 0; } 例11.6 # include int main() {unsigned char a=173,b=203,c; c=a|b; printf(“a|b=%d”,c); return 0; } 例11.7 # include int main() {char a=-83,b=-53,c; c=a|b; printf(“a|b=%d”,c); return 0; } 例11.8 # include int main() {unsigned char a=173,b=203,c; c=a^b; printf(“a^b=%d”,c); return 0; } 例11.10 # include {unsigned int i,n,b,mask; mask=0x8000; printf(\ scanf(\ printf(\ %0x is:\ for(i=0;i<16;i++) {b=(mask&n)?1:0; printf(\ if(i==7) printf(\ mask=mask>>1; } return 0; } 例11.11 # include int main() {unsigned int n,a,b,c,d; printf(\ scanf(\ printf(\ scanf(\ b=a<<16-n; c=a>>n; d=b|c; printf(\ return 0; } 例12.1 #include {FILE *fp; int i; fp=fopen(“cj.dat”, “rb”); if(fp==NULL) printf(“File open failed!\\n”); else printf(“File open successful!\\n”); i=fclose(fp); if (i==0) printf(“File close successful!\\n”); else printf(“File close failed!\\n”); return 0; } 例12.2 #include { FILE *fp ; char ch; if ((fp=fopen(“d:\\\\yw.txt”,“w”))==NULL) {printf(“cannot open file!\\n”); exit(0); } ch=getchar( ); while(ch !=?#?) { if (ch>=‘a? && ch<=‘z?) fputc(ch , fp) ; else putchar(ch) ; ch = getchar( ); } fclose(fp); return 0; } 例12.3 # include { FILE *fp1 , *fp2 ; char ch , file1[30] , file2[30] ; /* file1和file2分别用来存储文件名*/ scanf(“%s”, file1); /* file1中存储的文件名必须是已经存在的文件*/ scanf(“%s”, file2); if ((fp1=fopen(file1 , “r”))==NULL) {printf(“cannot open infile\\n”); exit(0); } if ((fp2=fopen(file2 , “w”))==NULL) {printf(“cannot open outfile\\n”); exit(0); } while (!feof(fp1)) /*在屏幕上显示file1文件信息*/ {ch=fgetc(fp1); putchar(ch); } rewind(fp1); /*让文件内部的位置指针重新移动到文件首*/ while ( !feof(fp1) ) /*复制file1文件中的英文字母到file2*/ { ch=fgetc(fp1) ; if ((ch>=‘a? && ch<=‘z?) || (ch>=‘A? && ch<=‘Z?)) fputc( ch , fp2 ); } fclose(fp1); fclose(fp2); return 0; } 例12.4 #include char name[8]; int age; }stu[SIZE]; int main() { FILE *fp; int i; if ((fp=fopen(\ {printf(\ exit(0); } for (i=0; i printf(\ getchar(); /* 用getchar()来抵消前面的回车符*/ gets(stu[i].name); printf(\ scanf(\ } for (i=0;i
正在阅读:
C教材中的源程序代码03-30
当代社会马克思主义对大学生的作用12-28
第3讲 - 自由落体与竖直上抛运动05-13
华南理工营销学管理作业1 答案04-28
管理会计作业04-29
2018-2019年石家庄市灵寿县南燕川乡营里小学一年级上册语文复习03-24
泰山玻璃纤维有限公司上市可行性分析106-04
在全市乡村振兴暨脱贫攻坚会议上的讲话稿02-25
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 源程序
- 教材
- 代码
- 小学教学常规检查汇报材料
- 20170100-建筑工程EPC项目技术标文件管理要点管控要素(共3个模
- 小学语文沪教版三年级下册第二单元第6课《新年的礼物》优质课公
- 在全区酒业发展会议上的讲话
- 经济效益及合同等-XX隧道 - 图文
- 一个人调查自己的家庭历史
- 2018届九年级语文下册第一单元综合测试卷(新版)新人教版
- 液相微萃取在分析化学中的应用
- 嘉兴电信优化案例--高铁测试TAC更新失败优化案例 - 图文
- 深圳市人才住房和保障性住房配建监管协议书
- 2010年中考数学真题分类汇编(150套)专题四十七 开放探究型问题
- 国务院常务会议决定扩大中小学教师职称制度改革试点
- 主题班会-我感恩,我雅行
- 2014年9月未来教育计算机四级网络工程师题库及参考答案(共9套)
- 植物学考试模拟试卷A卷
- 浅谈死刑的存废问题
- 2018年中药配方颗粒发展现状及市场前景趋势分析(目录)
- 焦炉设计计算要点
- 九年级物理光的传播和色散光的反射练习题 - 图文
- 1.1正弦定理和余弦定理知识点