版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
譚浩強C程序設(shè)計例題代碼#include"stdio.h"voidmain()(printf(^ThisisaCprogram!\n");}^include"stdio.h"voidmain(){inta,b,sum;a=123;b=456;sum=a+b;printfC'sumis%d.\n”,sum);)ftinclude"stdio.h"voidmain(){intmax(intx,inty);inta,b,c;scanf("%d%d”,&a,&b);c=max(a,b);printf("max=%d\n〃,c);}intmax(intx,inty)if(x>y)z=x;elsez=y;return(z);}或者ttinclude*stdio.h"intmax(intx,inty)(intz;if(x>y)z=x;elsez=y;return(z);}voidmain(){//intmax(intx,inty)1-inta,b,c;scanf("%d%d”,&a,&b);c=max(a,b);printf("max二%d\n”,c);}ttinclude<stdio.h>voidmain()i=2;t=l;while(i<6)(t=t*i;i++;}printf("5!=%d\n”,t);}??include<stdio.h>voidmain()(intsignal;floatdeno=2.0,term,sum=l.0;while(deno<=100){sign二一sign;term=sign/deno;sum=sum+term;deno++;}printf("sum=%f\n”,sum);}#definePRICE30ftinclude<stdio.h>voidmain()!intnum,total;printfC'Pleaseinputnumber:");scanfnum);total=PRICE*num;printfC'totaled.\n\total);-2-}ftinclude<stdio.h>voidmainO(printfCabc\tde\rf\tg\n*);printf(z/h\ti\b\bjk\n");}(輸出結(jié)果為:fabcgdehjik顯示屏上的結(jié)果為:fgdehjk)ftinclude<stdio.h>voidmain(){charcl,c2;cl=97;c2=98;printf("%c,%c\n”,cl,c2);printf("%d,%d\n”,cl,c2);}ttinclude<stdio.h>voidmain()charcl,c2;cl=,a;c2='b';cl=cl-32;c2=c2-32;printf(z/%c,%c\n,z,cl,c2);)Sinclude<stdio.h>voidmain()(unsigneda;intb二T;a=b;printf("機\n",a);〃%u輸出的是無符號數(shù),刎輸出的是有符號數(shù),盡管a是無符號的數(shù),〃此要引起重視3-#include<stdio.h>voidmain()(chara,b,c;f'B';b='O'Y';putchar(a);putchar(b);putchar(c);putchar(*\n);ftinclude<stdio.h>voidmainO(charc;c=getchar();putchar(c);putchar(>\n);〃也可以用以下語句實現(xiàn)putchar(getchar());/printf(a%cM,getchar());}ttinclude<stdio.h>voidmain()(unsigneda=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);)^include<stdio.h>voidmain()(chara=,a';inti=97;printf("%d,%c\n”,a,a);printf("%d,%c\n”,i,i);}#include<stdio.h>voidmain()(printfC%3s\n%7,2s\n%.4s\n%-5.3s\n〃,“CHINA","CHINA","CHINA","CHINA");}ttinclude<stdio.h>voidmain(){-4-floatx,y;Ill;y=222222.222;printf(*%f\n*,x+y);}ttinclude<stdio.h>voidmain(){doublex,y;111111111;y=2222222222222.222222222;printfC%f\n\x+y);#include<stdio.h>voidmainO{floata=123.456;printf(*%f\n%10f\n%10.2f\n%.2f\n%~10.2f\n”,a,a,a,a,a);}ttinclude<stdio.h>voidmain()(floatf=123.456;printf(*%e\n%10e\n%10.2e\n%,2e\n%-10.2e\n”,f,f,f,f,f);}求三角形面積ttinclude<stdio.h>ttinclude<math.h>voidmain(){floata,b,c,s,area;scanf ”,&a,&b,&c);s=l.0/2*(a+b+c);area=sqrt(s*(s-a)*(s-b)*(s-c));printf(*a=%-7.2f,b=%-7.2f,c=%-7.2f,s=%-7.2f\n”,a,b,c,s);printf(*area=%-7.2f\n”,area);將大寫字母變成小寫字母#include<stdio.h>-5-voidmainO{charcl,c2;cl=getchar();printf("%c,%d\n”,cl,cl);c2=cl+32;printf("%c,%d\n”,c2,c2);}輸入兩個實數(shù),并按從大到小的順序輸出#includc<stdio.h>voidmain()(floata,b,t;scanf("%f%f”,&a,&b);if(a>b)(t=a;a=b;b=t;)printf("%f,a,b);輸入三個數(shù),并按由小到大答順序輸出^include<stdio.h>voidmainOfloata,b,c,t;scanf”,&a,&b,&c);if(a>b)(t=a;a=b;b=t;}if(a>c)(t=a;a=c;c=t;)if(b>c){-6-t=b;b=c;c=t;}printf("%f,%f,%f\nz,,a,b,c);}符號函數(shù)ttinclude<stdio.h>voidmainO{intx,y;scanf&x);if(x<0)y=-l;elseif(x==0)y=0;elsey=l;printf(〃x=%d,y=%d\n,z,x,y);}(上面第611行可以改為if(x>=0)if(x>0)y=l;elsey=0;elsey="l;)輸入一個字母,并把其轉(zhuǎn)換成小寫字母#include<stdio.h>voidmainO(charcl,c2;scanf&cl);c2=(cl>'A'&&cl<‘Z*)?(cl+32):cl;printfCz%c\n/z,c2);判斷閏年ttinclude<stdio.h>voidmain()(intyear,leap;scanf&year);if(year%4==0)if(year%100==0)iif(year%400==0)leap=l;elseleap=0;)elseleap=l;}elseleap=0;if(leap)printf(,z%dis”,year);elseprintf('%disnot”,year);printf(zzaleapyear.\n");}求一元二次方程的解#include<stdio.h>#include<math.h>voidmainO{floata,b,c,disc,xl,x2,realpart,imagpart;scanf ”,&a,&b,&c);printf(Z/Theequation");if(fabs(a)<le-6)printfC'isnotaquadratic.\nzz);elseidisc=b*b-4*a*c;if(fabs(disc)<le-6)printf("hastwoequalroots:%8.4f\n'z,-b/(2*a));elseif(disc>le-6)(xl=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);printf("hasdistinctrealroots:%8.4fand%8.4f\n〃,xl,x2);}else!realpart=-b/(2*a);imagpart=sqrt(-disc)/(2*a);printf("hascomplexroots:\n*);printfC'%8.4f+%8.4fi\n〃,realpart,imagpart);printf(zz%8.4f-%8.4fi\n”,realpart,imagpart);}-8-}}求運費ttinclude<stdio.h>voidmain()iintc,s;floatp,w,d,f;scanf("%f%f%dz/,&p,&w,&s);if(s>3000)c=12;elsec=s/250;switch(c)(case0:d=0;break;case1:d=2;break;case2:case3:d=5;break;case7:d=8;break;case11:d=10;break;case12:d=15;break;}f=p*w*s*(l-d/100.0);printf(*freight=%-15.4f.\n”,f);}求E100??include<stdio.h>voidmain()(inti,sum=0;i=l;loop:if(i<=100){sum=sum+i;i++;gotoloop;}-9-printf("%d\n",sum);)ftinclude<stdio.h>voidmain()inti,sum=O;i=l;while(i<=100)(sum=sum+i;i++;)printf("%d\n",sum);}#include<stdio.h>voidmain()Iinti,sum=0;i=l;do[sum=sum+i;i++;)while(i<=100):printf("%d\n",sum);)輸出100?200之間不能被3整除的數(shù)#include<stdio.h>voidmainOintn;for(n=100;n<-200;n++)if(n%3==0)continue;elseprintf("%-5d”,n);printf('\n");)求Pl??include<stdio.h>#include<math.h>voidmain()-10一(ints;floatn,t,pi;n=l.0;t=l.0;s=l;pi=0;while(fabs(t)>le-6)[pi=pi+t;s=-s;n=n+2;t=s/n;)pi=pi*4;printfCzpi=%10.6f\n",pi);)求Fibonacci數(shù)列前四十個數(shù)^include<stdio.h>voidmainO{longintfl,f2;inti;fl=l;f2=l;for(i=l;i<=20;i++)(printfC%121d%121d,z,fl,f2);fl=fl+f2;f2=fl+f2;if(i%2=0)printf("\n");}}求100~200間的全部素數(shù)^include<stdio.h>ftinclude<math.h>voidmain()(intm,k,i,n=0;for(m=101;m<=200;m=m+2){k=sqrt(m);for(i=2;i<=m;i++)if(m%i==0)break;-11-if(i>=k+l)printf(%-8d”,m);n=n+l;if(n%10==0)printf("\n");)printf("\n");)翻譯密碼ttinclude<stdio.h>voidmain()Icharc;while((c=getchar())!='\n')Iif((c>='a'&&c〈='z')||(c>='A'&&c〈='Z')){c=c+4;if(c>*z||(c>>Z*&&c<=,Z>+4))c=c-26;)printfc);)printf("\n");輸出Fibonaccittinclude<stdio.h>voidmainO{inti;intf[20]={l,1};for(i=2;i<=19;i++)f[i]=f[i-l]+f[i-2];for(i=0;i<20;i++)(if(說5==0)printf("\n");printf("%12d”,f[i]);)printf("\n");}-12-十個整數(shù)排序(由小到大)Sinclude<stdio.h>voidmain()!inta[10];inti,j,t;printf("Input10numbers:\n");for(i=0;i<10;i++)for(j=0;j<9;j++)for(i=0;i<9-j;i++)if(a[i]>a[i+l])(t=a[i];a[i]=a[i+l];a[i+l]=t;}printf("Thesortednumbersare:\n*);for(i=0;i<10;i++)printfa[i]);printf('\n");}將一個二維數(shù)組的行和列互換ftinclude<stdio.h>voidmain(){inta[2][3]={{l,2,3},{4,5,6}};intb[3][2],i,j;printf("arraya:\rT);for(i=0;i<2;i++)ifor(j=0;j<3;j++)printf("\n");)printf(""arrayb:\n");for(i=0;i<3;i++)(for(j=0;j<2;j++)-13-printfb[i][j]);printf("\n");))求一個陣列的最大元素,并輸出相應(yīng)的行和列ttinclude<stdio.h>voidmain()[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<3;i++)for(j=0;j<4;j++)max=a[i][j];row=i;colum=j;}printfC"max=%-5d,row=%-5d,colum=%-5d\rT,max,row,colum);}輸出Iamaboy.^include<stdio.h>voidmain()!1 「ICI-f'T'f9 9f9f? ? > > ? ,fi> > > > >1charaL10J={1, ,a,m, ,a, ,b,o,y/;inti;for(i=0;i<10;i++)printfa[i]);printf(*.\n");)輸出一個菱形圖ttinclude<stdio.h>voidmainO{chara[][5]={{' 'inti,j;for(i=0;i<5;i++)for(j=0;j<5;j++)printf("%c",a[i][j])14-printf("\n");))數(shù)字符串中的單詞個數(shù)ttinclude<stdio.h>voidmainO(charstring[81];inti,num=0,word=0;charc;gets(string);for(i=0;(c=string[i])!='\0';i++)if(c='')word=O;elseif(word==0){word=l;num=num+1;}puts(string);printf("Thereare%dwordsintheline.\n",num);}求三個字符串中最大者,并輸出ttinclude<stdio.h>ttinclude<string.h>voidmainO(charstring[20];charstr[3][20];inti;for(i=0;i<3;i++)gets(str[i]);if(strcmp(str[0],str[1])>0)strcpy(string,str[0]);elsestrcpy(string,str[l]);if(strcmp(string,str[2])<0)strcpy(string,str[2]);printf(z,\nThelargeststringis:\n%s\n”,string);}函數(shù)調(diào)用^include<stdio.h>-15-voidmain()iintmax(floatx,floaty);floata,b;intc;scanf”,&a,&b);c=max(a,b);printf("Maxis%d.\n",c);}intmax(floatx,floaty)(floatz;z=x>y?x:y;returnz;)用截弦法求一元三次方程的解ftinclude<stdio.h>#include<math.h>floatf(floatx);floatroot(floatxl,floatx2);voidmain()(floatxl,x2,fl,f2,x;doiprintf("inputxl,x2:\n");scanf&xl,&x2);f2=f(x2);while(fl*f2>=0);x=root(xl,x2);printf("Onerootoftheequationis%?2f?\n”,x);}floatf(floatx)ifloaty;y=((x-5.0)*x+16.0)*x-80.0;returny;)floatxpoint(floatxl,floatx2)(floaty;-16-y=(xl*f(x2)-x2*f(xl))/(f(x2)-f(xl));returny;}floatroot(floatxl,floatx2){floatx,y,yl;yl=f(xl);dox=xpoint(xl,x2);y=f(x);if(y*yl>0)(yl=y;xl=x;)elsex2二x;)while(fabs(y)>=le-4);returnx;}遞歸調(diào)用ftinclude<stdio.h>voidmainO(intage(intn);printf("刎.\n",age(5));}intage(intn){intc;if(n==l)c=10;elsec=age(n-l)+2;returnc;)求階乘ttinclude<stdio.h>floatfac(intn)(floati;-17-if(n<0)printf("dateerror");elseif(n==0||n==l)i=l;elsei=n*fac(n-l);returni;)voidmain()(intn;floaty;printf("inputn:\n");scanf&n);y=fac(n);printf("%d!=%.Of.\n〃,n,y);Hanoi塔問題(?9??9997???99979?999?7??9?9?9??9)8include<stdio.h>voidmain()(voidhanoi(intn,charone,chartwo,charthree);intm;printf("Inputthenumberofdiskes:\nzz);scanf&m);printf(*Thestepofmoveing%ddiskes:\n",m);hanoi(m,'A','B','C');)voidhanoi(intn,charone,chartwo,charthree){voidmove(charx,chary);if(n==l)move(one,three);elseihanoi(n-l,one,three,two);move(one,three);hanoi(n-l,two,one,three);voidmove(charx,chary)printf("%c―>%c\n”,x,y);}比較數(shù)組大小-18-ttinclude<stdio.h>voidmain()(intlarge(intx,inty);inta[10],b[10],i,n=0,m=0,k=0;printf(z,enterarraya:\n");for(i=0;i<10;i++)scanf("%d”,&a[i]);printf("enterarrayb:\n");for(i=0;i<10;i++)scanf&b[i]);printf("\n〃);for(i=0;i<10;i++)(if(large(a[i],b[i])==l)m=ni+l;elseif(large(a[i],b[i])==0)n=n+l;elsek=k+l;printf(*a[i]>b[i]%dtimes.\na[i]=b[i]%dtimes.\na[i]<b[i]%dtimes.\n",m,n,k);if(m〉k)printf("arrayaislargerthanarrayb.\n");elseif(m==k)printf("arrayaisequaltoarrayb.\n");elseprintf(/zarraybislargerthanarrayb.\n");}intlarge(intx,inty){intflag;if(x>y)flag=l;elseif(x==y)flag=0;elseflag=-l;returnflag;}(輸入注意,等號要用,不是“二”)數(shù)組的應(yīng)用#include<stdio.h>voidmain()(floataverage(floatarray[],intn);floatscore.l[5]={98.5,97,91.5,60,55):floatscore_2[10]={67.5,89.5,99,69.5,77,89.5,76.5,54,60,99.5};printf("theaverageofclassAis%6.2f.Xn*,average(score_l,5));printf("theaverageofclassBis%6.2f.\n/z,average(score_2,10));floataverage(floatarray[],intn)-19-inti;floataver,sum二array[0];for(i=l;i<n;i++)sum=sum+array[i];aver=sum/n;returnaver;)選擇法排序ftinclude<stdio.h>voidmain()(voidsort(intarray[],intn);inta[10],i;printf(z,enterthearray:\nz,);for(i=0;i<10;i++)scanf("%5d”,&a[i]);sort(a,10);printf("thesortedarrayis:\n〃);for(i=0;i<10;i++)voidsort(intarray[],intn)(inti,j,k,t;for(i=0;i<n-l;i++)ik二i;for(j=i+l;j<n;j++)if(array[j]<array[k])k=j;t=array[k];array[k]=array[i];array[i]=t;}}求一個3X4矩陣中的最大值ftinclude<stdio.h>voidmain()(intmax_value(intarray[][4]);inta[3][4]={{1,3,5,7},{2,4,6,8},{15,17,34,12}};printf(,zmaxvalueis%d.\n*,max_value(a));-20-}intmax_value(intarray[][4])inti,j,max;max=array[0][0];for(i=0;i<3;i++)for(j=0;j<4;j++)if(array[i][j]>max)max=array[i][j];return(max);}求平均值及最大最小值??include<stdio.h>floatmax=0,min=0;〃全局變量voidmain()(floataverage(floatarray[],intn);floatave,score[10];inti;for(i=0;i<10;i++)scanf&score[i]);ave=average(score,10);printfC'max=%-6.2f\nmin=%-6.2f\naverage=%-6.2f.\n〃,max,min,ave);}floataverage(floatarray[],intn)!inti;floataver,sum=array[0];max=min=array[0];for(i=l;i<n;i++){if(array[i]>max)max=array[i];if(array[i]<min)min=array[i];sum=sum+array[i];}aver=sum/n;return(aver);)ttinclude<stdio.h>inta=3,b=5;voidmain(){-21-intmax(inta,intb);inta=8;printf(*%d.\n",max(a,b));//結(jié)果為8)intmax(inta,intb)(intc;c=a>b?a:b;returnc;靜態(tài)局部變量的應(yīng)用ttinclude<stdio.h>voidmain()(intf(int);inta=2,i;for(i=0;i<3;i++)(printf(*%d",f(a));)printf("\n〃);}intf(inta)(autointb=0;staticintc=3;b=b+l;c=c+l;return(a+b+c);)求5!ttinclude<stdio.h>intfac(intn);inti;for(i=l;i<6;i++)printf(*%d!=%d\n/z,i,fac(i));)intfac(inti)(staticinta=l22-a=a*i;return(a);}求某個數(shù)的階乘(n!)ftinclude-stdio.h"voidmain()(longfac(long);longi,n;scanf&n);for(i=l;i<=n;i++)printf(*%ld!=%ld\n*,i,fac(i));longfac(longn)registerlongi,f=l;for(i=l;i<=n;i++)f=f*i;return(f);}求較大值(通過extern變量)ttinclude<stdio.h>voidmain()iintmax(int,int);externA,B;\\變量名可以寫,也可以不寫printfC'%d\n/Z,max(A,B));}intA=13,B=-8;intmax(intA,intB)(returnA>B?A:B);)Filel.cttinclude<stdio.h>intA;voidmain()intpower(int);intb=3,c,d,m;-23-printf(''interthenumberaanditspower scanf("%d%d”,&A,&m);c=A*b;printfC%d*%d=%d\n",A,b,c);d=power(m);printfC%d**%d=%d",A,m,d);}File2.cexternA;intpower(intn)(inti,y=l;for(i=l;i<=n;i++)y=y*A;return(y);}運用外部函數(shù)刪去字符串中某個字符Filel#include<stdio.h>voidmain()externvoidenter_string(charstr[]);externvoiddelate_string(charstr[],charch);externvoidprint_string(charstr[]);charc;charstr[80];enter_string(str);scanf&c);delate_string(str,c);printstring(str);}File2#include<stdio.h>voidenter_string(charstr[80])(gets(str);}File3#include<stdio.h>voiddelate_string(charstr[],charc)(inti,j;-24-for(i=0,j=0;str[i]!=,\0*;i++)if(str[i]!=c)str[j]=str[i];j++;str[j]-\0*;}File4ttinclude<stdio.h>voidprintstring(charstr[80])(puts(str);}宏定義的應(yīng)用ftinclude<stdio.h>#definePI3.1415926voidmain(){float1,s,r,v;printf(z,inputradius:");scanf(*%f*,&r);1=2.0*PI*r;s=PI*r*r;v=4.0/3*PI*r*r*r;printf(*l=%10.4f\ns=%10.4f\nv=%10,4f\n",1,s,v);}宏名的嵌套定義#include<stdio.h>ttdefineR3.0ttdefinePI3.1415926ftdefineL2*PI*R#defineSPI*R*Rvoidmain()iprintf("L二%f\ns二%f\n”,L,S);}帶參數(shù)的宏的應(yīng)用ttinclude<stdio.h>#definePI3.1415926ffdefineS(r)PI*r*r-25-voidmain(){floata,aera;a=3.6;aera=S(a);printf(*r=%f\naera=%f\n,z,a,aera);)用宏得到多個結(jié)果ttinclude<stdio.h>#definePI3.1415926#defineCIRCLE(R,L,S,V)L=2*PI*R;S=PI*R*R;V=4.0/3.0*PI*R*R*RvoidmainOfloatr,1,s,v;scanf&r);CIRCLE(r,l,s,v);printf("r二%6.2f\nl=%6.2f\ns=%6.2f\nv=%6.2f\n”,r,1,s,v);}用宏代表輸出格式ttinclude<stdio.h>ttdefinePRprintf#defineNL"\n"ftdefineD〃%d”#defineDIDNL#defineD2DDNLttdefineD3DDDNL#defineD4DDDDNL*defineS"%s〃NLvoidmain(){inta,b,c,d;charstring□=“CHINA”;a=l;b=2;c=3;d=4;PR(DI,a);PR(D2,a,b);PR(D3,a,b,c);PR(D4,a,b,c,d);PR(S,string);}條件編譯的應(yīng)用ttinclude<stdio.h>-26-ttdefineLETTER1voidmainO!charstr[20]=/,Clanguage”,c;inti=0;while((c=str[i])!='\0')(i++;#ifLETTERif(c>=,a'&&c<=,z')c=c-32;#elseif(c>=,A*&&c<=,Z')c=c+32;ttendifprintfc);printfC\n");指針的基本應(yīng)用ftinclude<stdio.h>voidmainO(inta,b;int*point_l,*point_2;a=100;b=10;point_l=&a;point_2=&b;printf("%d,%d\n”,a,b);printf(〃%d,%d\n",*point_l,*point_2);}運用指針求大值ttinclude<stdio.h>voidmain()(int*pl,*p2,*p,a,b;scanf("%d%d”,&a,&b);pl=&a;p2=&b;if(a<b)!p=pl;pl=p2;p2=p;printfC%d,%d\n”,*pl,*p2):指針變量作為函數(shù)參數(shù),比較數(shù)的大小ttinclude<stdio.h>voidmainOIvoidswap(int*pl,int*p2);inta,b;int*pointer_l,*pointer_2;scanf("%d%d”,&a,&b);pointer_l=&a;pointer_2=&b;if(a<b)swap(pointer_l,pointer_2);printf(〃%d,%d\n”,a,b);}voidswap(int*pl,int*p2){inttemp;temp=*pl;*pl=*p2;*p2=temp;)將數(shù)字自大到小排(但是不能實現(xiàn))^include<stdio.h>voidmain()voidswap(int*pl,int*p2);inta,b;int*pointer_l,*pointer_2;scanf("%d%d”,&a,&b);pointer_l=&a;pointer_2=&b;if(a<b)swap(pointer_l,pointer_2);printf("\n%d,%d\n',*pointer_l,*pointer_2);}voidswap(int*pl,int*p2)iint*p;p=pl;pl=p2;P2=p;}-28-〃自小至大排列輸入的3個數(shù)據(jù)#include<stdio.h>voidmain(){voidexchange(int*pl,int*p2,int*p3);inta,b,c,*pl,*p2,*p3;p1=&a;p2=&b;p3=&c;scanf(*%d%d%d*,&a,&b,&c);exchange(pl,p2,p3);printf("%d,%d,%d\n”,a,b,c);}voidexchange(int*pl,int*p2,int*p3){voidswap(int*pl,int*p2);if(*p1>*p2)swap(p1,p2);if(*pl>*p3)swap(pl,p3);if(*p2>*p3)swap(p2,p3);}voidswap(int*pl,int*p2)(inttemp;temp=*pl;*pl=*p2;*p2=temp;}〃輸出數(shù)組中的全部元素〃下標法ttinclude<stdio.h>voidmain()[inta[10];inti;for(i=0;i<10;i++)iscanf("%d”,&a[i]);printfC\n");for(i=0;i<10;i++)printf('%d",a[i]);printf("\n");}-29-〃通過數(shù)組名計算數(shù)組元素地址,找出?數(shù)組元素voidmain()(inta[10];inti;for(i=0;i<10;i++)iscanf("%d”,&a[i]);}printf("\n");for(i=0;i<10;i++)printf(*%d",*(a+i));printf('\n");}〃用指針變量指向數(shù)組元素voidmain()iinta[10];int*p,i;for(i=0;i<10;i++)scanf("%d”,&a[i]);printf(〃\n");for(p=a;p<a+10;p++)(printf(*%d",*p);)printf('\n");}注意指針變量的當前值#include<stdio.h>voidmain()(int*p,i,a[10];P=a;for(i=0;i<10;i++)scanf("%d”,p++);printf("\n〃);P=a;for(i=0;i<10;i++,p++)printf(*%d",*p);printf('\n");-30-將數(shù)組中n個整數(shù)按相反順序存放^include<stdio.h>voidmainOvoidinv(intx[],intn);inti,a[10]={3,7,9,11,0,6,7,5,4,2};printf(*Theoriginalarray:\n*);for(i=0;i<10;i++)!printf('%d",a[i]);}printfC\n*);inv(a,10);printf(z,Thearrayhasbeeninverted:\n,z);for(i=0;i<10;i++)printf('%d",a[i]);printf('\n");}voidinv(inta[],intn)(inttemp,i,j,m=(n-l)/2;for(i=0;i<=m;i++)(j=n-i-l;temp=a[i];a[i]=a[j];a[j]=temp;return;與上同#include<stdio.h>voidmain()ivoidinv(int*x,intn);inti,a[10]={3,7,9,11,0,6,7,5,4,2};printf(*Theoriginalarray:\n*);for(i=0;i<10;i++)(printf('%d",a[i]);}~31-printf("\n〃);inv(a,10);printfC'Thearrayhasbeeninverted:Xn");for(i=0;i<10;i++)printf("%d",a[i]);printf('\n");}voidinv(int*x,intn)(int*p,temp,*i,*j,m=(n-l)/2;i=x;j=x+n-l;p=x+m;for(;i<=p;i++,j—)temp=*i;*i二*j;*j=temp;)return;)作用同上??include<stdio.h>voidmain()(voidinv(int*x,intn);inti,arr[10],*p=arr;printf(*Theoriginalarray:\n^);for(i=0;i<10;i++,p++)scanfp);printf('\n");p=arr;inv(p,10);printf(/zThearrayhasbeeninverted:\n");for(;p<arr+10;p++)printf("%d",*p);printf('\n");int*p,temp,*i,*j,m=(n-l)/2;i=x;j=x+n-l;p=x+m;for(;i<=p;i++,j—)(temp=*i32-*i=*j;*j=temp;)return;}將十個數(shù)據(jù)由大到小排序(存在錯誤)^include<stdio.h>voidmain()(voidsort(intx[],intn);int*p,i,a[10];P=a;for(i=0;i<10;i++)scanf("%d”,p++);P二a;sort(p,10);for(p=a,i=0;i<10;i++)printfC%d”,*p);p++;printf("\n");)voidsort(intx[],intn)(inti,j,k,t;for(i=0;i<n-l;i++)(k=i;for(j=i+l;j<n;j++)if(x[j]>x[i])k=j;if(k!=i){t=x[i];x[i]=x[j];x[j]=t;}))〃輸出二維數(shù)組有關(guān)的值#include<stdio.h>^defineFORMAT"%d,%d\n"voidmain()-33-(inta[3][4]={1,3,5,7,9,11,13,15,17,19,21,23);printf(FORMAT,a,*a);printf(FORMAT,a[0],*(a+0));printf(FORMAT,&a[0],&a[0][0]);printf(FORMAT,a[l],a+1);printf(FORMAT,&a[l][0],*(a+l)+0);printf(FORMAT,a[2],*(a+2));printf(FORMAT,&a[2],a+2);printf(FORMAT,a[l][0],*(*(a+l)+0));)//用指針變量輸出二維數(shù)組元素的值ttinclude<stdio.h>voidmain()Iinta[3][4]={1,3,5,7,9,11,13,15,17,19,21,23);int*p:for(p=a[0];p<a[0]+12;p++)[if((p-a[0])%4=0)printf(*\n*);printf(*%4d*,*p);)printf("\n");)//輸出二維數(shù)組任一行任一列元素的值#include<stdio.h>inta[3][4]={1,3,5,7,9,11,13,15,17,19,21,23);int(*p)[4],i,j;P=a;scanf("%d%d”,&i,&j);printf(*a[%d][%d]=%d\n”,i,j,*(*(p+i)+j));}〃求平均分和輸出某個人的成績#include<stdio.h>voidmain()(voidaverage(float*p,intn);voidsearch(float(*p)[4],intn);floatscore[3][4]={{65,67,70,60},{80,87,90,81},{90,99,100,98}};-34-average(*score,12);search(score,2);}voidaverage(float*p,intn)(float*p_end;floatsum=0,aver;p_end=p+n-l;for(;p<=p_end;p++)sum=sum+(*p);aver=sum/n;}printf(*average=%5.2f\n”,aver);)voidsearch(float(*p)[4],intn)(inti;printf(z,thescoreofNO.%dare:n);for(i=0;i<4;i++)printf(*%5.2f",*(*(p+n)+i));printfC\n");}查找有不及格成績的學(xué)生的成績ttinclude<stdio.h>voidmain(){voidsearch(float(*p)[4],intn);floatscore[3][4]={{65,57,70,60},{58,87,90,81},{90,99,100,98}};search(score,3);voidsearch(float(*p)[4],intn)inti,j,flag;for(j=0;j<n;j++)(flag=O;for(i=0;i<4;i++)if(*(*(p+j)+i)<60)flag=l;if(flag==l)printf(*N0.%dfails,hisscoreare:\n*,j+1);for(i=0;i<4;i++)-35-printf(*%5.2f”,*(*(p+j)+i));printf("\n");輸出字符串ttinclude<stdio.h>voidmain(){charstring口二〃IloveChina!*;printf(*%s\n*,string);}〃以下也行#include<stdio.h>voidmainOchar*string=TloveChina!”;printf("%s\n",string);}復(fù)制字符串ttinclude<stdio.h>voidmainO!char amaboy.b[20];inti;for(i=0;*(a+i)!=,\0J;i++)*(b+i)=*(a+i);*(b+i)=\0';printf(''stringais:%s\n',a);printf(''stringbis:\n");for(i=0;b[i]!=\0*;i++)printf('%c",b[i]);printf("\n");)用指針處理ttinclude<stdio.h>chara[]=*Iamaboy!z,,b[20],*pl,*p2;inti;pl=a;p2=b;-36-for(;*pl!=\0J;pl++,p2++)*p2=*pl;*p2=>\0';printf("stringais:%s\n,z,a);printf(''stringbis:");for(i=0;b[i]!=,\Of;i++)printfb[i]);printf("\n〃);}用函數(shù)調(diào)用實現(xiàn)字符串的復(fù)制ttinclude<stdio.h>voidmain()(voidcopy_string(charform[],charto口);chara口=“iamateacher!;charb□=“Youareastudent!*;printf(''stringa=%s\nstringb二%s\n〃,a,b);printf("copystringatob:\n");copy_string(a,b);printf(^Xnstringa=%s\nstringb=%s\n,z,a,b);}voidcopy_string(charfrom[],charto[]){inti=0;while(from[i]!='\0')to[i]=from[i];i++;}\0';}找最大值??include<stdio.h>voidmain()(intmax(int,int);inta,b,c;scanf("%d%d”,&a,&b);c=max(a,b);printf(〃a=%d,b=%d,max=%d\n”,a,b,c);}-37-intmax(inta,intb){if(a<b)returnb;elsereturna;#include<stdio.h>voidmainO{intmax(int,int);int(*p)(int,int);inta,b,c;p=max;scanf("%d%d”,&a,&b);c=(*p)(a,b);printf("a=%d,b=%d,max=%d\n*,a,b,c);}intmax(inta,intb)(if(a<b)returnb;elsereturna;}指向函數(shù)的指針變量作參數(shù)#include<stdio.h>voidmain()iintmax(int,int);intmin(int,int);intadd(int,int);voidprocess(int,int,int(*fun)(int,int));inta,b;printf("enteraandb:");scanf("%d%d”,&a,&b);printf("max=");process(a,b,max);printf("min=");process(a,b,min);printf("add=〃);process(a,b,add);}intmax(inta,intb)-38一(intz;if(a>b)z=a;elsez=b;returnz;}intmin(inta,intb)(intz;if(a>b)z=b;elsez=a;returnz;intadd(inta,intb)intz;z=a+b;returnz;}voidprocess(inta,intb,int(*fun)(inta,intb))!intresult;result=(*fun)(a,b);printf(〃%d\n〃,result);}返回指針值的的函數(shù)的應(yīng)用ftinclude<stdio.h>voidmain()(floatscore口[4]={{60,70,80,90},{56,89,67,88},{34,78,90,66}};float*search(float(*pointer)[4],intn);float*p;inti,m;printf(*enterthenumberofstudent:");p=search(score,m);for(i=0;i<4;i++){printfC%5.2f\t”,*(p+i));}printf(*\n*);}-39-float*search(float(*pointer)[4],intm)!float*p;p=*(pointer+m);returnp;}〃用于輸出不及格的學(xué)生的成績ftinclude<stdio.h>voidmain()(floatscore[][4]={{60,70,80,90},{56,89,67,88},{34,78,90,66}};float*search(float("pointer)[4]);float*p;inti,j;for(i=0;i<3;i++)p=search(score+i);if(p==*(score+i)){printf(*N0.%dscoresi);for(j=0;j<4;j++)(printfC%5.2f\t〃,*(p+j));}printf('\n");float*search(float("pointer)[4])(inti;float*pt;pt=*(pointer+1);for(i=0;i<4;i++)(if(*(*pointer+i)<60)pt=*pointer;returnpt;指針數(shù)組的應(yīng)用ttinclude<stdio.h>ttinclude<string.h>voidmain()-40-(voidsort(char*name[],intn);voidprint(char*name[],intn);char*name[]={Z/Followme","BASIC","greatWall","FORTRAN","Computerdesign"};intn=5;sort(name,n);print(name,n);}voidsort(char*name[],intn)(char*temp;inti,j,k;for(i=0;i<nT;i++)(k=i;for(j=i+l;j<n;j++)if(strcmp(name[k],name[j])>0)k=j;temp=name[i];name[i]=name[k];name[k]=temp;voidprint(char*name[],intn)(inti;for(i=0;i<n;i++)printf("%s\n”,name[i]);}指向指針的指針的應(yīng)用舉例#include<stdio.h>voidmain()(char*name[]={*Followme","BASIC","GreatWall","FORTRAN","Computerdesign"};char**p;inti;for(i=0;i<5;i++)(p=name+i;printf("%s\n",*p);-41-ftinclude<stdio.h>voidmainO(inta[]={l,3,5,7,9};int*num[]={&a[0], &a[2],&a[3],&a[4]};int**p,i;p=num;for(i=0;i<5;i++)(printf**p);p++;}printf("\n");}結(jié)構(gòu)體的應(yīng)用#include<stdio.h>voidmain()istructstudentlongintnum;charname[20];charsex;charaddr[20];}a={10101,*LiLin",‘M',"123BeijingRoad*);printf("N0?:%ld\nName:%s\nSex:%c\nAddress:%s\n”,a.num,,a.sex,a.addr);}結(jié)構(gòu)體數(shù)組的應(yīng)用ttinclude<stdio.h>ttinclude<string.h>structpersonicharname[20];intcount;}leader[3]={*li*,0,"zhang”,0,"fun”,0};voidmain(){inti,j;charleader_name[20]42-for(i=l;i<=10;i++)(scanf('%s”,leader_name);for(j=0;j<3;j++)if(strcmp(leader_name,leader[j]?name)==0)leader[j].count++;}printfC\n");for(i=0;i<3;i++)printf(/z%5s:%d\nz/,leader[i].name,leader[i].count);}}結(jié)構(gòu)體數(shù)組的應(yīng)用ttinclude<stdio.h>ttinclude<string.h>structpersonicharname[20];intcount;}leader[3]={*li*,0,"zhang”,0,"fun”,0};voidmain(){inti,j;charlead_name[20];for(i=l;i<=10;i++)(scanf('%s”,lead_name);for(j=0;j<3;j++)if(strcmp(lead_name,leader[j].name)==0)leader[j].count++;}printfC\n");for(i=0;i<3;i++)printf("%5s:%d\n”,leader[i].name,leader[i].count);}結(jié)構(gòu)體指針變量的應(yīng)用#include<stdio.h>ttinclude<string.h>voidmain()(structstudent{-43-longnum;charname[20];charsex;floatscore;);structstudentstu_l;structstudent*p;p=&stu_l;stu_l.num=89101;strcpy(,“LiLin");stu_l.sex=,M';stu_l.score=89.5;printf(*No.:%ld\nname:%s\nsex:%c\nscore:%.2f\n〃,stu_l.num,stu_,stu_l.sex,stul.score);printf(*No.:%ld\nname:%s\nsex:%c\nscore:%.2f\n//,(*p).num,(*p).name,(*p).sex,(*p).score);}指針用于數(shù)組ffinclude<stdio.h>structstudent(intnum;charname[20];charsex;intage;);structstudentstu[3]={{10101,*LiLin",'M',18},{10102,"ZhangFun",'M',19},{10104,"WangMin",'F',20}};voidmainO{structstudent*p;printf(z,No.:Name:sex:age:\n");for(p=stu;p<stu+3;p++)printfC,r%-8d%-12s%-8c%-8d\n//,p->num,p->name,p->sex,p->age);}宏定義及結(jié)構(gòu)體變量的調(diào)用ttinclude<stdio.h>ttinclude<string.h>#defineFORMAT"%d\n%s\n%.2f\n%.2f\n%.2f\n〃structstudent(intnum;charname[20]44-floatscore[3];};voidmain()ivoidprint(structstudent);structstudentstu;stu.num=12345;strcpy(,"LiLi");stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.6;print(stu);}voidprint(structstudentstu)!printf(FORMAT,stu.num,,stu.score[0],stu.score[1],stu.score[21);printf("\n〃);鏈表的應(yīng)用ftinclude<stdio.h>ftdefineNULL0structstudent(longnum;floatscore;structstudent*next;};voidmain()(structstudenta,b,c,*head,*p;num=10101;a.score=89.5;num=10103;b.score=90;num=10107;c.score=85;head=&a;a.next二&b;b.next=&c;c.next=NULL;p=head;doprintf(*%ld%5.lf\n*,p->num,p->score);p=p->next45一}while(p!=NULL);}鏈表的綜合應(yīng)用^include<stdio.h>ttinclude<malloc.h>ttdefineNULL0#defineLENsizeof(structstudent)structstudent(longnum;floatscore;structstudent*next;);intn;structstudent*creat(void){structstudent*head;structstudent*pl,*p2;n=0;pl=p2=(structstudent*)malloc(LEN);scanf(*%ld,%f*,&pl->num,&pl->score);head=NULL;while(pl->num!=O)(n=n+l;if(n==l)head=pl;elsep2->next=pl;p2=pl;pl=(structstudent*)malloc(LEN);scanf%f”,&pl->num,&pl->score);}p2->next=NULL;returnhead;}voidprint(structstudent*head){structstudent*p;printfCAnNow,These%drecordsare:\nz
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年版廣告投放合同詳細條款
- 學(xué)期家委會工作計劃六篇
- 中國紅酒包裝設(shè)計行業(yè)發(fā)展監(jiān)測及發(fā)展戰(zhàn)略規(guī)劃報告
- 中國單雙面膠粘帶項目投資可行性研究報告
- 中國鹽酸貝那普利行業(yè)市場供需格局及投資規(guī)劃建議報告
- 消費者效用最大化探究問卷調(diào)查報告
- 大學(xué)生電工實習報告錦集十篇
- 網(wǎng)頁課程設(shè)計備忘錄
- 2022年醫(yī)院后勤個人工作計劃
- 筷子課程設(shè)計教案
- 2024年機動車檢測站質(zhì)量手冊程序文件記錄表格合集(根據(jù)補充要求編制)
- 公司未來發(fā)展規(guī)劃及目標制定
- 2024年01月11067知識產(chǎn)權(quán)法期末試題答案
- 2025版國家開放大學(xué)法律事務(wù)專科《民法學(xué)(2)》期末紙質(zhì)考試案例分析題庫
- 一年級家長會課件2024-2025學(xué)年
- 情侶防出軌合同模板
- 2024公安機關(guān)人民警察高級執(zhí)法資格考試題及答案
- 2023-2024學(xué)年云南省昆明市五華區(qū)八年級(上)期末物理試卷
- 陜西省渭南市2023-2024學(xué)年七年級上學(xué)期期末考試數(shù)學(xué)試題(含答案)2
- 廢棄催化劑中貴金屬的回收
- 期末 (試題) -2024-2025學(xué)年譯林版(三起)(2024)英語三年級上冊
評論
0/150
提交評論