1.5請參照本章例題,編寫一個C程序,輸出以下信息:
************ Very Goodj! ************ 解: main() { printf(" ************ \n"); printf("\n"); printf(" Very Good! \n"); printf("\n"); printf(" ************\n"); } 1.6編寫一個程序,輸入a b c三個值,輸出其中最大者。 解:main() {int a,b,c,max; printf("請輸入三個數(shù)a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c);
max=a; if(max<b) max=b; if(max<c) max=c; printf("最大數(shù)為:%d",max); } 第三章 3.3 請將下面各數(shù)用八進制數(shù)和十六進制數(shù)表示: (1)10 (2)32 (3)75 (4)-617 (5)-111 (6)2483 (7)-28654 (8)21003 解:十 八 十六 (10)=(12)=(a) (32)=(40)=20 (75)=(113)=4b (-617)=(176627)=fd97 -111=177621=ff91 2483=4663=963 -28654=110022=9012 21003=51013=520b 3.5字符常量與字符串常量有什么區(qū)別? 解:字符常量是一個字符,用單引號括起來。字符串常量是由0個或若干個字符 而成,用雙引號把它們括起來,存儲時自動在字符串最后加一個結(jié)束符號'\0'. 3.6寫出以下程序的運行結(jié)果: #include<stdio.h> void main() { char c1='a',c2='b',c3='c',c4='\101',c5='\116'; printf("a%c b%c\tc%c\tabc\n",c1,c2,c3); printf("\t\b%c %c\n",c4,c5); 解:程序的運行結(jié)果為: aabb cc abc A N 3.7將"China"譯成密碼.密碼規(guī)律:用原來的字母后面第4個字母代替原來的字母, 例如,字母"A"后面第4個字母是"E",用"E"代替"A".因此,"China"應(yīng)譯為"Glmre". 請編一程序,用賦初值的議程使c1,c2,c3,c4,c5分別變成'G','1','m','r','e',并 輸出. main() {char c1="C",c2="h",c3="i",c4='n',c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; printf("密碼是%c%c%c%c%c\n",c1,c2,c3,c4,c5); } 3.8例3.6能否改成如下: #include<stdio.h> void main() { int c1,c2;(原為 char c1,c2) c1=97; c2=98; printf("%c%c\n",c1,c2); printf("%d%d\n",c1,c2); } 解:可以.因為在可輸出的字符范圍內(nèi),用整型和字符型作用相同. 3.9求下面算術(shù)表達式的值. (1)x+a%3*(int)(x+y)%2/4=2.5(x=2.5,a=7,y=4.7) (2)(float)(a+b)/2+(int)x%(int)y=3.5(設(shè)a=2,b=3,x=3.5,y=2.5) 3.10寫出下面程序的運行結(jié)果: #include<stdio.h> void main() { int i,j,m,n; i=8; j=10; m=++i; n=j++; printf("%d,%d,%d,%d\n",i,j,m,n); } 解:結(jié)果: 9,11,9,10 第4章 4.4.a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1='a',c2='b'.想得 到以下的輸出格式和結(jié)果,請寫出程序要求輸出的結(jié)果如下: a= 3 b= 4 c= 5 x=1.200000,y=2.400000,z=-3.600000 x+y= 3.60 y+z=-1.20 z+x=-2.40 u= 51274 n= 128765 c1='a' or 97(ASCII) c2='B' or 98(ASCII) 解: main() { int a,b,c; long int u,n; float x,y,z; char c1,c2; a=3;b=4;c=5; x=1.2;y=2.4;z=-3.6; u=51274;n=128765; c1='a';c2='b'; printf("\n"); printf("a=%2d b=%2d c=%2d\n",a,b,c); printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z); printf("x+y=%5.2f y=z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x); printf("u=%6ld n=%9ld\n",u,n); printf("c1='%c' or %d(ASCII)\n",c1,c2); printf("c2='%c' or %d(ASCII)\n",c2,c2); } 4.5請寫出下面程序的輸出結(jié)果. 結(jié)果: 57 5 7 67.856400,-789.123962 67.856400 ,-789.123962 67.86,-789.12,67.856400,-789.123962,67.856400,-789.123962 6.785640e+001,-7.89e+002 A,65,101,41 1234567,4553207,d687 65535,17777,ffff,-1 COMPUTER, COM 4.6用下面的scanf函數(shù)輸入數(shù)據(jù),使a=3,b=7,x=8.5,y=71.82,c1='A',c2='a', 問在鍵盤上如何輸入? main() { int a,b; float x,y; char c1,c2; scanf("a=%d b=%d,&a,&b); scanf(" x=%f y=%e",&x,&y); scanf(" c1=%c c2=%c",&c1,&c2); } 解:可按如下方式在鍵盤上輸入: a=3 b=7 x=8.5 y=71.82 c1=A c2=a 說明:在邊疆使用一個或多個scnaf函數(shù)時,第一個輸入行末尾輸入的"回車"被第二 個scanf函數(shù)吸收,因此在第二\三個scanf函數(shù)的雙引號后設(shè)一個空格以抵消上行 入的"回車".如果沒有這個空格,按上面輸入數(shù)據(jù)會出錯,讀者目前對此只留有一 初步概念即可,以后再進一步深入理解. 4.7用下面的scanf函數(shù)輸入數(shù)據(jù)使a=10,b=20,c1='A',c2='a',x=1.5,y=-3.75,z=57.8,請問 在鍵盤上如何輸入數(shù)據(jù)? scanf("%5d%5d%c%c%f%f%*f %f",&a,&b,&c1,&c2,&y,&z); 解: main() { int a,b; float x,y,z; char c1,c2; scanf("%5d%5d%c%c%f%f",&a,&b,&c1,&c2,&x,&y,&z); } 運行時輸入: 10 20Aa1.5 -3.75 +1.5,67.8 注解:按%5d格式的要求輸入a與b時,要先鍵入三個空格,而后再打入10與20。%*f是用來禁止賦值的。在輸入時,對應(yīng)于%*f的地方,隨意打入了一個數(shù)1.5,該值不會賦給任何變量。 3.8設(shè)圓半徑r=1.5,圓柱高h=3,求圓周長,圓面積,圓球表面積,圓球體積,圓柱體積,用scanf輸入數(shù)據(jù),輸出計算結(jié)果,輸出時要求有文字說明,取小數(shù)點后兩位數(shù)字.請編程. 解:main() { float pi,h,r,l,s,sq,vq,vz; pi=3.1415926; printf("請輸入圓半徑r圓柱高h:\n"); scanf("%f,%f",&r,&h); l=2*pi*r; s=r*r*pi; sq=4*pi*r*r; vq=4.0/3.0*pi*r*r*r; vz=pi*r*r*h; printf("圓周長為: =%6.2f\n",l); printf("圓面積為: =%6.2f\n",s); printf("圓球表面積為: =%6.2f\n",sq); printf("圓球體積為: =%6.2f\n",vz); } 4.9輸入一個華氏溫度,要求輸出攝氏溫度,公式為C=5/9(F-32),輸出要有文字說明,取兩位小數(shù). 解: main() { float c,f; printf("請輸入一個華氏溫度:\n"); scanf("%f",&f); c=(5.0/9.0)*(f-32); printf("攝氏溫度為:%5.2f\n",c); } 第五章 邏輯運算和判斷選取結(jié)構(gòu) 5.4有三個整數(shù)a,b,c,由鍵盤輸入,輸出其中最大的數(shù). main() { int a,b,c; printf("請輸入三個數(shù):"); scanf("%d,%d,%d",&a,&b,&c); if(a<b) if(b<c) printf("max=%d\n",c); else printf("max=%d\n",b); else if(a<c) printf("max=%d\n",c); else printf("max-%d\n",a); } 方法2:使用條件表達式. main() {int a,b,c,termp,max; printf(" 請輸入 A,B,C: "); scanf("%d,%d,%d",&a,&b,&c); printf("A=%d,B=%d,C=%d\n",a,b,c); temp=(a>b)?a:b; max=(temp>c)? temp:c; printf(" A,B,C中最大數(shù)是%d,",max); } 5.5 main() {int x,y; printf("輸入x:"); scanf("%d",&x); if(x<1) {y=x; printf("X-%d,Y=X=%d \n",x,y); } else if(x<10) {y=2*x-1; printf(" X=%d, Y=2*X-1=%d\n",x,y); } else {y=3*x-11; printf("X=5d, Y=3*x-11=%d \n",x,y); } } 5.7給一個不多于5位的正整數(shù),要求:1.求它是幾位數(shù)2.分別打印出每一位數(shù)字3.按逆序打印出各位數(shù)字.例如原數(shù)為321,應(yīng)輸出123. main() { long int num; int indiv,ten,hundred,housand,tenthousand,place; printf("請輸入一個整數(shù)(0-99999):"); scanf("%ld",&num); if(num>9999) place=5; else if(num>999) place=4; else if(num>99) place=3; else if(num>9) place=2; else place=1; printf("place=%d\n",place); printf("每位數(shù)字為:"); ten_thousand=num/10000; thousand=(num-tenthousand*10000)/1000; hundred=(num-tenthousand*10000-thousand*1000)/100; ten=(num-tenthousand*10000-thousand*1000-hundred*100)/10; indiv=num-tenthousand*10000-thousand*1000-hundred*100-ten*10; switch(place) {case 5:printf("%d,%d,%d,%d,%d",tenthousand,thousand,hundred,ten,indiv); printf("\n反序數(shù)字為:"); printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,tenthousand); break; case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv); printf("\n反序數(shù)字為:"); printf("%d%d%d%d\n",indiv,ten,hundred,thousand); break; case 3:printf("%d,%d,%d\n",hundred,ten,indiv); printf("\n反序數(shù)字為:"); printf("%d%d%d\n",indiv,ten,hundred); case 2:printf("%d,%d\n",ten,indiv); printf("\n反序數(shù)字為:"); printf("%d%d\n",indiv,ten); case 1:printf("%d\n",indiv); printf("\n反序數(shù)字為:"); printf("%d\n",indiv); } } 5.8 1.if語句 main() {long i; float bonus,bon1,bon2,bon4,bon6,bon10; /*初始化變量*/ bon1=100000*0.1; bon2=100000*0.075+bon1; bon4=200000*0.05+bon2; bon6=200000*0.03+bon4; bon10=400000*0.015+bon6; printf("請輸入利潤"); scanf("%ld",&i); /*計算*/ if(i<=le5) bonus=i*0.1; else if(i<2e5) bonus=bon1+(1-100000)*0.075; else if(i<=4e5) bonus=bon2+(i-200000)*0.05; else if(i<=6e5) bonus=bon4+(i-400000)*0.03; else if(i<=le6) bonus=bon6+(i-600000)*0.015; else bonus=bon10+(i-1000000)*0.01; printf("獎金是 %10.2f",bonus); } 用switch語句編程序 main() {long i; float bonus,bon1,bon2,bon4,bon6,bon10; int branch; /*初始化變量*/ bon1=100000*0.1; bon2=bon1+100000*0.075 bon4=bon2+200000*0.05; bon6=bon4+200000*0.03; bon10=bon6+400000*0.015; printf("請輸入利潤:"); scanf("%ld",&i); branch=i/100000; if(branch>10) branch=10; /*計算*/ switch(branch) {case 0:bonus=i*0.1;break; case 1:bonus=bon1+(i-100000)*0.075;break; case 2: case 3:bonus=bon2+(i-200000)*0.05;break; case 4: case 5:bonus=bon4+(i-400000)*0.03;break; case 6: case 7: case 8: case 9:bonus=bon6+(i-600000)*0.015;break; case 10:bonus=bon10+(i-1000000)*0.01; } printf(" 獎金是 %10.2f",bonus); } 5.9 輸入四個整數(shù),按大小順序輸出. main() {int t,a,b,c,d; printf("請輸入四個數(shù):"); scanf("%d,%d,%d,%d",&a,&b,&c,&d); printf("\n\n a=%d,b=%d,c=%d,d=%d \n",a,b,c,d); if(a>b) {t=a;a=b;b=t;} if(a>c) {t=a;a=c;c=t;} if(a>d) {t=a;a=d;d=t;} if(b>c) {t=b;b=c;c=t;} if(b>d) {t=b;b=d;d=t;} if(c>d) {t=c;c=d;d=t;} printf("\n 排序結(jié)果如下: \n"); printf(" %d %d %d %d \n",a,b,c,d); } 5.10塔 main() { int h=10; float x,y,x0=2,y0=2,d1,d2,d3,d4; printf("請輸入一個點(x,y):"); scanf("%f,%f",&x,&y); d1=(x-x0)*(x-x0)+(y-y0)(y-y0); d2=(x-x0)*(x-x0)+(y+y0)(y+y0); d3=(x+x0)*(x+x0)+(y-y0)*(y-y0); d4=(x+x0)*(x+x0)+(y+y0)*(y+y0); if(d1>1 && d2>1 && d3>1 && d4>1) h=0; printf("該點高度為%d",h); } 第六章 循環(huán)語句 6.1輸入兩個正數(shù),求最大公約數(shù)最小公倍數(shù). main() { int a,b,num1,num2,temp; printf("請輸入兩個正整數(shù):\n"); scanf("%d,%d",&num1,&num2); if(num1<num2) { temp=num1; num1=num2; num2=temp; } a=num1,b=num2; while(b!=0) { temp=a%b; a=b; b=temp; } printf("它們的最大公約數(shù)為:%d\n",a); printf("它們的最小公倍數(shù)為:%d\n",num1*num2/2); } 6.2輸入一行字符,分別統(tǒng)計出其中英文字母,空格,數(shù)字和其它字符的個數(shù). 解: #include <stdio.h> main() { char c; int letters=0,space=0,degit=0,other=0; printf("請輸入一行字符:\n"); while((c=getchar())!='\n') { if(c>='a'&&c<='z'||c>'A'&&c<='Z') letters++; else if(c==' ') space++; else if(c>='0'&&c<='9') digit++; else other++; } printf("其中:字母數(shù)=%d 空格數(shù)=%d 數(shù)字?jǐn)?shù)=%d 其它字符數(shù)=%d\n",letters,space, digit,other); } 6.3求s(n)=a+aa+aaa+…+aa…a之值,其中工是一個數(shù)字. 解: main() { int a,n,count=1,sn=0,tn=0; printf("請輸入a和n的值:\n"); scanf("%d,%d",&a,&n); printf("a=%d n=%d \n",a,n); while(count<=n) { tn=tn+a; sn=sn+tn; a=a*10; ++count; } printf("a+aa+aaa+…=%d\n",sn); } 6.4 求1+2!+3!+4!+…+20!. main() { float n,s=0,t=1; for(n=1;n<=20;n++) { t=t*n; s=s+t; } printf("1!+2!+…+20!=%e\n",s); } 6.5 main() { int N1=100,N2=50,N3=10; float k; float s1=0,s2=0,s3=0; for(k=1;k<=N1;k++)/*計算1到100的和*/ { s1=s1+k; } for(k=1;k<=N2;k++)/*計算1到50各數(shù)平方和*/ { s2=s2+k*k; } for(k=1;k<=N3;k++) { s3=s3+1/k; } printf("總和=%8.2f\n",s1+s2+s3); } 6.6水仙開花 main() { int i,j,k,n; printf(" '水仙花'數(shù)是:"); for(n=100;n<1000;n++) { i=n/100; j=n/10-i*10; k=n%10; if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) { printf("%d",n); } } printf("\n"); } 6.7完數(shù) main() #include M 1000/*定義尋找范圍*/ main() { int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9; int i,j,n,s; for(j=2;j<=M;j++) { n=0; s=j; for(i=1;i<j;i++) { if((j%i)==0) { if((j%i)==0) { n++; s=s-i; switch(n)/*將每個因子賦給k0,k1…k9*/ { case 1: k0=i; break; case 2: k1=i; break; case 3: k2=i; break; case 4: k3=i; break; case 5: k4=i; break; case 6: k5=i; break; case 7: k6=i; break; case 8: k7=i; break; case 9: k8=i; break; case 10: k9=i; break; } } } if(s==0) { printf("%d是一個‘完數(shù)’,它的因子是",j); if(n>1) printf("%d,%d",k0,k1); if(n>2) printf(",%d",k2); if(n>3) printf(",%d",k3); if(n>4) printf(",%d",k4); if(n>5) printf(",%d",k5); if(n>6) printf(",%d",k6); if(n>7) printf(",%d",k7); if(n>8) printf(",%d",k8); if(n>9) printf(",%d",k9); printf("\n"); } } 方法二:此題用數(shù)組方法更為簡單. main() { static int k[10]; int i,j,n,s; for(j=2;j<=1000;j++) { n=-1; s=j; for(i=1;i<j;i++) { if((j%i)==0) { n++; s=s-i; k[n]=i;/*將每個因子賦給k0,k1...k9*/ } } if(s==0) { printf("%d是一個完數(shù),它的因子是:",j); for(i=0;i<n;i++) printf("%d,",k[i]); printf("%d\n",k[n]); } } 5.8 有一個分?jǐn)?shù)序列:2/1,3/2,5/3,8/5……求出這個數(shù)列的前20項之和. 解: main() { int n,t,number=20; float a=2,b=1,s=0; for(n=1;n<=number;n++) { s=s+a/b; t=a,a=a+b,b=t; } printf("總和=%9.6f\n",s); } 6.9球反彈問題 main() { float sn=100.0,hn=sn/2; int n; for(n=2;n<=10;n++) { sn=sn+2*hn;/*第n次落地時共經(jīng)過的米數(shù)*/ hn=hn/2;/*第n次反跳高度*/ } printf("第10次落地時共經(jīng)過%f米 \n",sn); printf("第10次反彈%f米.\n",hn); } 6.10猴子吃桃 main() { int day,x1,x2; day=9; x2=1; while(day>0) { x1=(x2+1)*2; x2=x1; day--; } printf("桃子總數(shù)=%d\n",x1); } 6.14打印圖案 main() { int i,j,k; for(i=0;i<=3;i++) { for(j=0;j<=2-1;j++) printf(" "); for(k=0;k<=2*i;k++) printf("*"); printf("\n"); } for(i=0;i<=2;i++) { for(j=0;j<=i;j++) printf(" "); for(k=0;k<=4-2*i;k++) printf("*"); printf("\n"); } 6.15乒乓比賽 main() { char i,j,k;/*i是a是對手;j是b是對手;k是c的對手*/ for(i='x';i<='z';i++) for(j='x';j<='z';j++) { if(i!=j) for(k='x';k<='z';k++) { if(i!=k&&j!=k) {if(i!='x' && k!='x' && k! ='z') printf("順序為:\na-%c\tb--%c\tc--%c\n",i,j,k); } } } } 7.1用篩選法求100之內(nèi)的素數(shù). /*用篩選法求100之內(nèi)的素數(shù)*/ #include<math.h> #define N 101 main() {int i,j,line,a[N]; for(i=2;i<N;i++) a[i]=i; for(i=2;i<sqrl(N);i++) for(j=i+1;j<N;j++) {if(a[i]!=0 && a[j]!=0) if(a[j]%a[i]==0) a[j]=0; printf("\n"); for(i=2,line=0;i<N;i++) { if(a[i]!=0) {printf("%5d",a[i]); line++; if(line==10) {printf("\n"); line=0;} } } 7.2用選擇法對10個數(shù)排序. /*選擇法排序.*/ #define N 10 main() { int i,j,min,temp,a[N]; /*輸入數(shù)據(jù)*/ printf("請輸入十個數(shù):\n"); for (i=0;i<N;i++) { printf("a[%d]=",i); scanf("%d",&a[i]); } printf("\n"); for(i=0;i<N;i++) printf("%5d",a[i]); printf("\n"); /*排序*/ for (i=0;i<N-1;i++) { min=i; for(j=i+1;j<N;j++) if(a[min]>a[j]) min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; } /*輸出*/ printf("\n排序結(jié)果如下:\n"); for(i=0;i<N;i++) printf("%5d",a[i]); } 7.3對角線和: /*計算矩陣對角線元素之和*/ main() { float a[3][3],sum=0; int i,j; printf("請輸入矩陣元素:\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%f",&a[i][j]); for(i=0;i<3;i++) sum=sum+a[i][i]; printf("對角元素之和=6.2f",sum); } 7.4插入數(shù)據(jù)到數(shù)組 /*插入數(shù)據(jù)到數(shù)組*/ main() {int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number,end,i,j; printf("初始數(shù)組如下:"); for (i=0;i<10;i++) printf("%5d",a[i]); printf("\n"); printf("輸入插入數(shù)據(jù):"); scanf("%d",&number); end=a[9]; if(number>end) a[10]=number; else {for(i=0;i<10;i++) { if(a[i]>number) {temp1=a[i]; a[i]=number; for(j=i+1;j<11;j++) {temp2=a[j]; a[j]=temp1; temp1=temp2; } break; } } } for(i=0;j<11;i++) printf("a%6d",a[i]); } 7.5將一個數(shù)組逆序存放。 /*數(shù)組逆序存放*/ #define N 5 main() { int a[N]={8,6,5,4,1},i,temp; printf("\n 初始數(shù)組:\n"); for(i=0;i<N;i++) printf("%4d",a[i]); for(i=0;i<N/2;i++) { temp=a[i]; a[i]=a[N-i-1]; a[N-i-1]=temp; } printf("\n 交換后的數(shù)組:\n"); for(i=0;i<N;i++) printf("%4d",a[i]); } 7.6楊輝三角 /*打印楊輝三角形*/ #define N 11 main() { int i,j,a[N][N]; for(i=1;i<N;i++) {a[i][i]=1; a[i][1]=1; } for(i=3;i<N;i++) for(j=2;j<=i-1;j++) a[i][j]=a[i01][j-1]+a[i-1][j]; for(i=1;i<N;i++) { for(j=1;j<=i;j++) printf("%6d",a[i][j]; printf("\n"); } printf("\n"); } 7.8鞍點 /*查找鞍點*/ #define N 10 #define M 10 main() { int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj; printf("\n輸入行數(shù)n:"); scanf("%d",&n); printf("\n輸入列數(shù)m:"); scanf("%d",&m); for(i=0;i<n;i++) { printf("第%d行?\n",i); for(j=0;j<m,j++); scanf("%d",&a[i][j]; } for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%5d",a[i][j]); pritf("\n"); } flag2=0; for(i=0;i<n;i++) { max=a[i][0]; for(j=0;j<m;j++) if(a[i][j]>max) { max=a[i][j]; maxj=j; } for (k=0,flag1=1;k<n && flag1;k++) if(max>a[k][max]) flag1=0; if(flag1) { printf("\n第%d行,第%d列的%d是鞍點\n",i,maxj,max); flag2=1; } } if(!flag2) printf("\n 矩陣中無鞍點! \n"); } 7.9變量說明:top,bott:查找區(qū)間兩端點的下標(biāo);loca:查找成功與否的開關(guān)變量. /*折半查找*/ #include<stdio.h> #define N 15 main() { int i,j,number,top,bott,min,loca,a[N],flag; char c; printf("輸入15個數(shù)(a[i]>[i-1])\n); scanf("%d",&a[0]); i=1; while(i<N) { scanf("%d",&a[i]); if(a[i]>=a[i-1]) i++; esle {printf("請重輸入a[i]"); printf("必須大于%d\n",a[i-1]); } } printf("\n"); for(i=0;i<N;i++) printf("%4d",a[i]); printf("\n"); flag=1; while(flag) { printf("請輸入查找數(shù)據(jù):"); scanf("%d",&number); loca=0; top=0; bott=N-1; if((number<a[0])||(number>a[N-1])) loca=-1; while((loca==0)&&(top<=bott)) { min=(bott+top)/2; if(number==a[min]) { loca=min; printf("%d位于表中第%d個數(shù)\n",number,loca+1); } else if(number<a[min]) bott=min-1; else top=min+1; } if(loca==0||loca==-1) printf("%d不在表中\(zhòng)n",number); printf("是否繼續(xù)查找?Y/N!\n"); c=getchar(); if(c=='N'||c=='n') flag=0; } } 7.10/*統(tǒng)計字符數(shù)*/ main() { int i,j,uppn,lown,dign,span,othn; char text[3][80]; uppn=lown=dign=span=othn=0; for(i=0;i<3;i++) { printf("\n請輸入第%d行:\n",i); gets(text[i]); for(j=0;j<80 && text[i][j]!='\0';j++) {if(text[i][j]>='A' && text[i][j]<='Z') uppn+=1; else if(text[i][j]>='a' && text[i][j]<='z') lown+=1; else if(text[i][j]>='1' && text[i][j]<='9') dign+=1; else if(text[i][j]=' ') span+=1; else othn+=1; } } for(i=0;i<3;i++) printf("%s=n",text[i]); printf("大寫字母數(shù):%d\n",uppn); printf("小寫字母數(shù):%d\n",lown); printf("數(shù)字個數(shù):%d\n",dign); printf("空格個數(shù):%d\n",span); printf("其它字符:%d\n",othn); } 7.11/*打印圖案*/ main() {static char a[5]={'*','*','*','*','*'}; int i,j,k; char space=' '; for(i=0;i<=5;i++) {printf("\n"); for(j=1;j<=3*i;j++) printf("%lc",space); for(k=0;k<=5;k++) printf("%3c",a[k]; } } 7.12/*譯電文*/ #include<stdio.h> main() {int i,n; char ch[80],tran[80]; printf("請輸入字符:"); gets(ch); printf("\n密碼是%c",ch); i=0; while(ch[i]!='\0') {if((ch[i]>='A')&&(ch[i]<='Z')) tran[i]=26+64-ch[i]+1+64; else if((ch[i]>='a')&&(ch[i]<='z')) tran[i]=26+96-ch[i]+1+96; else tran[i]=ch[i]; i++; } n=i; printf("\n原文是:"); for(i=0;i<n;i++) putchar(tran[i]); } 7.13/*連接兩個字符串(不用'stract')*/ main() { char s1[80],s2[40]; int i=0,j=0; printf("\n請輸入字符串1:"); scanf("%s",s1); printf("\n請輸入字符串2:"); scanf("%s",s2); while(s1[i]!='\0') i++; while(s2[j]!='\0') s1[i++]=s2[j++]; s1[i]='\0'; printf("\n連接后字符串為:%s",s1); } 7.14/*字符串比較*/ #include<stdio.h> main() {int i,resu; char s1[100],s2[100]; printf("請輸入字符串1:\n"); gets(s1); printf("\n 請輸入字符串2:\n"); gets(s2); i=0; while((s1[i]==s2[i]) && (s1[i]!='\0'))i++; if(s1[i]=='\0' && s2[i]=='\0')resu=0; else resu=s1[i]-s2[i]; printf(" %s與%s比較結(jié)果是%d",s1,s2,resu); } 7.15/*字符串復(fù)制*/ #include<stdio.h> main() { char from[80],to[80]; int i; printf("請輸入字符串"); scanf("%s",from); for(i=0;i<=strlen(from);i++) to[i]=from[i]; printf("復(fù)制字符串為:%s\n",to); } 第八章 函數(shù) 8.1(最小公倍數(shù)=u*v/最大公約數(shù).) hcf(u,v) int u,v; (int a,b,t,r; if(u>v) {t=u;u=v;v=t;} a=u;b=v; while((r=b%a)!=0) {b=a;a=r;} return(a); } lcd(u,v,h)
int u,v,h; {int u,v,h,l; scanf("%d,%d",&u,&v); h=hcf(u,v); printf("H.C.F=%d\n",h); l=lcd(u,v,h); printf("L.C.d=%d\n",l); } {return(u*v/h);}
main() {int u,v,h,l; scanf("%d,%d",&u,&v); h=hcf(u,v); printf("H.C.F=%d\n",h); l=lcd(u,v,h); printf("L.C.D=%d\n",l); } 8.2求方程根 #include<math.h> float x1,x2,disc,p,q; greater_than_zero(a,b)
float a,b; { x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); } equal_to_zero(a,b)
float a,b; {x1=x2=(-b)/(2*a);} smaller_than_zero(a,b)
float a,b; {p=-b/(2*a); q=sqrt(disc)/(2*a); } main() { float a,b,c; printf("\n輸入方程的系數(shù)a,b,c:\n"); scanf("%f,%f,%f",&a,&b,&c); printf("\n 方程是:%5.2f*x*x+%5.2f*x+%5.2f=0\n",a,b,c); disc=b*b-4*a*c; printf("方程的解是:\n"); if(disc>0) {great_than_zero(a,b); printf("X1=%5.2f\tX2=%5.2f\n\n",x1,x2); } else if(disc==0) { zero(a,b); printf("X1=%5.2f\tX2=%5.2f\n\n",x1,x2); } else { small_than_zero(a,b,c); printf("X1=%5.2f+%5.2fi\tX2=%5.2f-%2.2fi\n",p,q,p,q); } } 8.3素數(shù) /*判斷整數(shù)是否為素數(shù)*/ main() { int number; printf("請輸入一個正整數(shù):\n"); scanf("%d",&number); if(prime(number)) printf("\n %d 是素數(shù).",number); else printf("\n %d 不是素數(shù)",number); } int prime(number)/*此函數(shù)用于差別素數(shù)*/
int number; { int flag=1,n; for(n=2;n<number/2 && flag==1;n++) if(number%n==0) flag=0; return(flag); } 8.4 /*矩陣的轉(zhuǎn)置*/ #define N 3 int array[N][N]; convert(array) int array[3][3]; { int i,j,t; for(i=0;i<N;i++) for(j=i+1;j<N;j++) { t=array[i][j]; array[i][j]=array[j][i]; array[j][i]=t; } } main() { int i,j; printf("輸入數(shù)組元素:\n"); for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d",&array[i][j]; printf("\n數(shù)組是:\n"); for(i=0;i<N;i++) { for(j=0;j<N;j++) printf("%5d",array[i][j]); printf("\n"); } convert(array);
printf("轉(zhuǎn)置數(shù)組是:\n"); for(i=0;i<N;i++) { for(j=0;j<N;j++) printf("%5d",array[i][j]); printf("\n"); } } 8.5/*字符串反序存放*/ main() { char str[100]; printf("輸入字符串:\n"); scanf("%s",str); inverse(str); printf("轉(zhuǎn)換后的字符串是: %s\n",str); } inverse(str)
char str[]; { char t; int i,j; for(i=0,j=strlen(str);i<strlen(str)/2;i++,j--) { t=str[i]; str[i]=str[i-1]; str[i-1]=t; } } 8.6/*字符串聯(lián)接*/ char concatenate(string1,string2,string); char string1[],string2[],string[]; { int i,j; for(i=0;string1[i]!='\0';i++) string[i]=string1[i]; for(j=0;string2[j]!='\0';j++) string[i+j]=string2[j]; string[i+j]='\0'; } main() { char s1[100],s2[100],s[100]; printf("\n輸入字符串1:\n"); scanf("%s",s1); printf("輸入字符串2:\n"); scanf("%s",s2); concatenate(s1,s2,s); printf("連接后的字符串:%s\n",s); } 8.8/*在四個數(shù)字符中加空格*/ main() { char str[80]; printf("請輸入含有四個數(shù)字的字符串:\n"); scanf("%s",str); insert(str); } insert(str)
char str[]; { int i; for(i=strlen(str);i>0;i--) { str[2*i]=str[i]; str[2*i-1]=' '; } printf("\n 結(jié)果是:\n %s",str); } 8.9/*統(tǒng)計字母數(shù)字空格和其它字符數(shù)*/ int alph,digit,space,others; main() { char text[80]; printf("\n輸入字符串:\n"); gets(text); alph=0; digit=0; space=0; others=0; count(text); printf("\n %d 字母,%d 數(shù)字,%d 空格,%d 其它字符\n"); } count(str)
char str[]; { int i; for(i=0;str[i]!='\0';i++) if((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z') alph++; else if(str[i]>'0' && str[i]<='9') digit++; else if(strcmp(str[i],' ')==0) space++; else others++; } 8.10/*輸出文本行的最長單詞*/ int alphabetic(c); char c; { if((c>='a' && c<='z'||(c>='A' && c<='Z')) return(1); else return(0); } /*尋找最長的詞*/
int longest (string) char string[]; { int len=0,i,length=0,flag=1,place,point; for(i=0;i<=strlen(string);i++) if(alphabctic(string[i])) if(flag) { point=i; flag=0; } else len++; else { flag=1; if len>length) {length=len; place=point; len=0; } } return(place); } main() { int i; char line[100]; printf("輸入一行文本\n");
gets(line); printf("\n最長的單詞是:"); for(i=longest(line);alphabctic(line[i]);i++) printf("%c",line[i]; printf("\n"); } 8.11/*起泡法排序*/ #define N 10 char str[N];
main() { int i,flag; for(flag=1;flag==1;) { printf("\n輸入字符串,長度為10:\n"); scanf("%s",&str); if(strlen(str)>N) printf("超過長度,請重輸!"); else flag=0; } sort(str); printf("\n 排序結(jié)果:"); for(i=0;i<N;i++) printf("%c",str[i]); } sort(str)
char str[N]; { int i,j; char t; for(j=1;j<N;j++) for(i=0;(i<N-j)&&(str[i]!='\0');i++) if(str[i]>str[i+1]) { t=str[i]; str[i]=str[i+1]; str[i+1]=t; } } 8.16/*十六進制轉(zhuǎn)換為十進制*/ #include<stdio.h> #define MAX 1000 main() { int c,i,flag,flag1; char t[MAX]; i=0;
flag=0; flag1=1; printf("\n輸入十六進制數(shù):"); while((c=getchar())!='\0'&&i<MAX&&flag1) { if c>='0' && c<='9'||c>='a'&&c<='f'||c>='A'&&c<='F') {flag=1; t[i++]=c; } else if(flag) { t[i]='\0'; printf("\n 十進制數(shù)%d\n",htoi(t)); printf("繼續(xù)嗎?"); c=getchar(); if(c=='N'||c=='n') flag1=0; else {flag=0; i=0; printf("\n 輸入十六進制數(shù):"); } } } } htoi(s) char s[]; { int i,n; n=0; for(i=0;s[i]!='\0';i++) {if(s[i]>='0'&&s[i]<='9') n=n*16+s[i]-'0'; if(s[i]>='a'&&s[i]<='f') n=n*16+s[i]-'a'+10; if(s[i]>='A'&&s[i]<='F') n=n*16+s[i]-'A'+10; } return(n); } 8.17/*遞歸法將整數(shù)轉(zhuǎn)換成字符*/ #include<stdio.h> void counvert(n)
int n; { int i; if((i=n/10)!=0) convert(i); putchar(n%10+'0'); } main()
{ int number; printf("\n 輸入整數(shù):"); scanf("%d",&number); printf("\n 輸出是: "); if(number<0) { putchar('-'); number=-number; } convert(number); } 8.18/*計算某晶為該年的第幾天*/ main() { int year,month,day; int days; printf("\n 請輸入日期(年,月,日)\n"); scanf("%d,%d,%d",&year,&month,&day); printf("\n %d年%d月%d日",year,month,day); days=sum_day(month,day); if(leap(year)&&month>=3) days=days+1; printf("是該年的%d天.\n",days); } static int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}
int(sum_day(month,day)
int month,day; { int i; for(i=1;i<month;i++) day+=day_tab[i]; return(day); } int leap(year)
int year; { int leap; leap=year%4==0&&year%100!=0||year%400==0; return(leap); } 第九章 編譯預(yù)處理 9.1/*交換兩變量值*/ #define SWAP(a,b) t=b;b=a;a=t main() { int a,b,t; printf("請輸入兩個整數(shù) a,b:"); scanf("%d,%d",&a,&b); SWAP(a,b); printf("交換結(jié)果為:a=%d,b=%d\n",a,b); } 9.2/*求兩個整數(shù)相除的余數(shù)*/ #define SURPLUS(a,b) ((a)%(b)) main() { int a,b; printf(" 請輸入兩個整數(shù) a,b:"); scanf("%d,%d",&a,&b); printf("a,b相除的余數(shù)為:%d\n",SURPLUS(a,b)); } 9.3/*輸入三角形的三條邊,求其面積*/ #include<math.h> #defin S(a,b,c) ((a+b+c)/2) #define AREA(a,b,c) (sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(s(a,b,c)-c))) main() { float a,b,c; printf("請輸入三角形的三條邊:"); scanf("%f,%f,%f",&a,&b,&c); if(a+b>c && a+c>b && b+c>a) printf("其面積為:%8.2f.\n",AREA(a,b,c)); else printf("不能構(gòu)成三角形!"); } 9.4/*判斷輸入年份是否為閏年*/ #define LEAP_YEAR(y) (y%4==0) && (y%100!=0)||(y%400==0) main() { int year; printf("\n請輸入某一年:"); scanf("%d",&year); if(LEAP_YEAR(year)) printf("%d 是閏年.\n",year); else printf("%d 不是閏年.\n",year); } 9.5解:展開后: printf("value=%format\t",x); printf("value=%format\t",x);putchar('\n'); printf("value=%format\t");printf("value=%format\t",x2);putchar('\n'); 輸出結(jié)果: value=5.000000ormat value=5.000000ormat value=3.000000ormat value=8.000000ormat 9.8/*用函數(shù)方法,從三個數(shù)中找出最大值*/ main() { int a,b,c; printf("請輸入三個整數(shù):"); scanf("%d,%d,%d",&a,&b,&c); printf("三個之中最大值為:%d\n",max(a,b,c)); } max(x,y,z)
int x,y,z; { int t; t=(x>y? x:y); return(t>z? t:z); } 9.10/*翻譯電碼*/ #include <stdio.h> #define MAX 80 #define CHANGE 1 main() { char str[MAX]; int i; printf("請輸入文本行:\n"); scanf("%s",str); #if(CHANGE) { for (i=0;i<MAX;i++) { if(str[i]!='\0' if(str[i]>='a' && str[i]<'z' || str[i]>='A'&&str[i]<'Z') str[i]+=1; else if(str[i]=='z' || str[i]=='Z') str[i]-=25; } } #endif printf("輸出電碼為:\n%s",str); } 20:16 2010-4-23 20:35 2010-4-23
12:38 2008-9-1
20:21 2010-10-23
|
|