C語言習題參考答案_第1頁
C語言習題參考答案_第2頁
C語言習題參考答案_第3頁
C語言習題參考答案_第4頁
已閱讀5頁,還剩126頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

《程序設計技術》(第三版)習題參考答案習題1ー、單項選擇題DADDDBDCCD二、填空題①主mainx=x*(x+b)④普通、格式、分隔和轉義⑤指定輸出數(shù)據(jù)格式voidmain()⑦“howareyou!"三、閱讀程序題9,11,9,102,-1,-2aabbccabc2,2575767.5864000,-789.12402367.5864000,-7.89e+002-789.124023,67,86, -789.12,67.5864000,-789.124023,67.5864000,-789.1240236.758640e+001,-7.89e+002A,65,101,411234567,4553207,12d68765535,177777,ffff,65535COMPUTER,COM.a+c=102a+c=ff+m=17.950000a+m=17.650000c+f=102.300000double=1746.150019四、程序設計題//xt010401.cppWinclude<stdio.h>WdefinePI3.1415926voidmain(){doubler,h;printf("請輸入半徑r和高h:");scanfぐ’%If,%lf",&r,&h);printf("圓半徑:%lf\n",2*PI*r);printf(“圓面積:%lf\n",PI*r*r);printf(“圓球表面積:%lf\n/,,4*PI*r*r);printf("圓球體積:4.0/3*PI*r*r*r);printf("圓柱體積:%lf\n/z,PI*r*r*h);)//xt010402.cpp#include<stdio.h>voidmain(){intc;printf(z,Inputacharacter:z,);c=getchar();printf("%c\t%d\n”,c,c);}//xt010403.cppftinclude<stdio.h>voidmain(){intx,y,h=30,f=90;x=(4*h-f)/2;y=(f-2*h)/2;printf("雞:%d,兔:%d\n”,x,y);//xt010404.cpp#include<stdio.h>ftinclude<math.h>voidmain(){doublea,b,c,s,area;printf(〃請輸入三角形的三條邊長(注意三條邊長應能夠構成三角形):〃);scanf(〃%lf,%lf,%lf",&a,&b,&c);s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));printf("三角形的面積是:%lf\n",area);}//xt010405.cpp#include<stdio.h>voidmain(){doublef,c;printf(〃請輸入華氏溫度:〃);scanf(〃%lf〃,&f);c=5.0/9.0*(f-32);printf(〃與華氏溫度%.21f對應的攝氏溫度是:%.21f\n〃,f,c);//xt010406.cpp#include<stdio.h>voidmain(){charcl,c2,c3,c4,c5;printf(〃請輸入需加密的字符串(5個字符):〃);cl=getchar();c2=getchar();c3=getchar();c4=getchar();c5=getchar();printf(〃加密后的字符串是:〃);putchar(cl+5);putchar(c2+5);putchar(c3+5);putchar(c4+5);putchar(c5+5);printf(〃、n〃);)習題2一、單項選擇題CBADCCCAAC二、填空題①a+b==O&&a*b!=0②邏輯與③邏輯或continue(c=getchar())!='\n'⑥%c三、閱讀程序題x=ll,y=20zl=l,z2=lTOC\o"1-5"\h\zsign(x) : -1 x<00 x=01 x>0a=2,b=l97531ABBBCCCCCDDDDDDDEEEEEDDD6.2*1*0$4*3*2$四、程序設計題/*xt020401.cpp*/#include<stdio.h>voidmain(){intnum;printf("****Inputnum:****\n");scanfI%d”,&num);if(num%3==0&&num%5==0&&num%7==0)printf("**YES!**\n");elseprintf("**NO!**\n");)/*xt020402.cpp*/#include<stdio.h>voidmain(){inth=0;floatx,y,x0=2,y0=2,dl,d2,d3,d4;printf(***input:x,y***\n");scanfぐ’%f,%f",&x,&y);dl=(x-xO)*(x-xO)+(y-yO)*(y-yO);/?點(x,y)到各中心點的距離?/d2=(x-xO)*(x-xO)+(y+yO)*(y+yO);d3=(x+xO)*(x+xO)+(y-yO)*(y-yO);d4=(x+xO)*(x+xO)+(y+yO)*(y+yO);if(dl<=lIId2<=lIId3<=l||d4<=l)h=15;printf("x=%f,y=%f\n",x,y);printf("h=%d\n",h);}/*xt020403.cpp*/#include<stdio.h>voidmain(){inti,j,k,n;for(n=100;nく=999;n++){i=n/100;j=n/10-i*10;k=n%10;if(n==i*i*i+j*j*j+k*k*k)printf("%d\n",n);/*xt020404.cpp*/ftinclude<stdio.h>#include<math.h>#defineEPS1.Oe-6voidmain()|floate=l,t;longintj=l,n=l;do{j=j*n;t=l.0/j;e=e+t;n++;}while(fabs(t)>=EPS);printf("e二%f\n”,e);)/*xt020405.cpp*/ftinclude<stdio.h>voidmain(){intnum=l;do{if((num+3)%5==0&&(num-3)%6==0)break;num+=l;}while(1);printf("num=%d\n”,num);}/*xt020406.cpp*/ftinclude<stdio.h>voidmain(){intnum;for(num=l;numく=99;num++)if(num*num%10==num||num*num%100==num)

printf("%4d,%5d”,num,num*num);printf("\n");)/*xt020407.cpp*/#include<stdio.h>voidmain(){intn,k=l;floats=0;for(n=l;n<=10000;n++){s=s+l,〇/⑵O*n-1)*k;k=-k;}printf("PI二%f\n”,4*s);)/*xt020408.cpp*/#include<stdio.h>voidmain(){intnlO,n5,n2,nl;for(nlO=l;nl0<=9;nlO++)for(n5=l;n5<=17;n5++)for(n2=l;n2<=37;n2++){ nl=40-nl0-n5-n2;if(nl>=l&&nl0*10+n5*5+n2*2+nl==100)printf("nlO=%d,n5=%d,n2二%d,nl二%d\n”,nlO,n5,n2,nl);})/*xt020409.cpp*/#include<stdio.h>voidmain(){floatsn=100,hn=sn/2;intn=2;while(n<=10){sn+=2*hn;hn=hn/2;n++;}printf(z,%f,\n”,sn,hn);)/*xt020410.cpp*/#include<stdio.h>voidmain(){intday,xl,x2=l;for(day=9;day>0;day--){xl=(x2+l)*2;x2=xl;}printf("Total=%d\n”,xl);)習題3單項選擇題BBCACDDABB填空題①9②〇③10④x⑤一致⑥n/2sum+asum/n三、閱讀程序題246s=302422421lanugeTOC\o"1-5"\h\z8 1 65 79 26.max=10,row=2,colum=l四、程序設計題//xt030401.cppftinclude<stdio.h>#include<stdlib.h>#include<time.h>voidmain(void){intnum[101,a;;srand((unsigned)time(NULL));printf(〃請輸入ー個2位數(shù),試試您是否能中獎:〃);scanfぐ'%d",&a);for(inti=0;i<10;i++){num[i]=(rand()+10)%100;if(a==num[i])printf("猜中一個數(shù)%d下標是%d”,a,i);)}printfi \n");for(i=0;i<10;i++)printf("%d\t",num[i]);)//xt030402.cppftinclude<stdio.h>#include<stdlib.h>ftinclude<time.h>voidmain(void){inta[5]={23,45,60,67,88},x,i,j,yes=0;for(i=0;i<5;i++)printfぐ’%d,",a[i]);printf("\nwhichnumberdoyouwanttodelete?\n");scanf("%d",&x);for(i=0;i<5;i++)if(a[i]==x){yes=l;for(j=i;j<5;j++)a[j]=a[j+l];)if(yes==l)for(i=0;i<4;i++)printf("%d,",a[i]);elseprintf("Thenumberyouwantdoesn'texist");)//xt030403.cppWinclude<stdio.h>ftinclude<stdlib.h>#include<time.h>WdefineSIZE1000voidmain()intx,a[SIZE],size,sum=0;srand((unsigned)time(NULL));while((size=rand()%1000)>200);printf("\nRandomizlistsize(<1000):%d”,size);printf("\nAutomakea%dsizeoflist:\n",size);for(x=0;x<size;x++){a[x]=rand()%1000;printf("%d\t",a[x]);sum+=arr[x];)printf("\nThesumofarrayelementsis%d”,sum);}//xt030404.cpp#include<stdio.h>#include<stdlib.h>ftinclude<time.h>WdefineSIZE100voidmain()|intx,a[SIZE],max=0,min=1000,bl,b2;srand((unsigned)time(NULL));printf("\nAutomakea%dsizeoflist:\n",size);for(x=0;x<SIZE;x++){a[x]=rand()%1000;printf("%d\t",a[x]);)for(x=0;x<SIZE;x++){if(a[x]>max) max=a[x],bl=x;if(a[x]<min) min=aEx],b2=x;)printf("\nThearrayelementsmax=%din%d\nmin二%din%d\n”,max,bl,min,b2);}//xt030405.cpp#include<stdio.h>ftdefineSIZE100WdefineKIND5voidmain()(intx,y,len,total[KIND+1];inta[]={l,1,1,1,2,2,2,3,5,5,5,5,1,1,3,3,3,3,3,2,2,2,5,5,5};len=sizeof(a)/sizeof(a[0]);for(x=0;x<KIND+l;x++){total[x]=0;)for(y=l;y<KIND+l;y++){for(x=0;x<len;x++)(if(a[x]==y)total[y]++;})printf("\nThelistkinded:\n/,);for(x=0;xくlen;x++)printf("%d",a[x]);printf("\nThetotalresult:");for(x=l;x<KIND+l;x++) printf(" %d-%d,",x,total[x]);)//xt030406.cpp#defineN26ftinclude<stdio.h>intmain(void){staticinta[N+l],b[N+l];inti,j,s,m,p;scanfぐ‘%d%d”,&s,&m);for(i=l;i<=N;i++)a[i]=i;i=s-l;j=O;p=O;while(p<N){i++;bh:if(i>N)i-=N;if(a[i]==0){i++;gotobh;}j++;if(j%m==0){p++;b[p>a[i];a[i>0;j=0;}}for(i=l;i<=N;i++)printf("%3d",b[i]);printf('\n");)//xt030407.cppWinclude<stdio.h>voidmain()|inti,j,s=0,ave,v[3];staticinta[5][3]={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}};for(i=0;i<3;i++){for(j=0;j<5;j++){s=s+a[j][i];v[i]=s/5;s=0;)ave=(v[0]+v[l]+v[2])/3;printf("math:%d\nclanguag:%d\ndbase:%d\n",v[0],v[l],v[2]);printf("totalave:%d\n",avel);)//xt030408.cppWinclude<stdio.h>voidmain(){inti,j,a[10][10],b[]={l,2,3,4,5,6,7,8,9,10};for(i=0;i<10;i++)a[0][i]=b[i];for(i=l;i<10;i++)for(j=0;j<10;j++)if(i>j)a[i][j]=a[j][i];elsea[i][j]=a[i-l][j-1];for(i=0;i<10;i++){for(j=0;j<10;j++)printf(,,%d\t,z,a[i][j]);printf("\n");))//xt030409.cpp#include<stdio.h>#include<stdlib.h>#include<time.h>ftdefineSIZE5000#defineN7voidmain(){intx,y,a[N]={0,0,0,0,0,0,0},s;srand((unsigned)time(NULL));x=0;while(x<SIZE){s=rand()%6+l;if(s>0&&s<7){a[s]++;x++;printf(^XnRandomiz1-6list\n");for(x=l;x<N;x++)(s=a[x]*0.07;for(y=0;yくs;y++)printf,x);printf("\t%d\t%2.lf%%\n”,a[x],(float)a[x]*100/SIZE);))//xt030410.cppWinclude<stdio.h>#defineN20voidmain()|intx,s,tag;chara[N];printf("Pleaseinputnumbersandit'slengthlessthen%d:",N-1);s=l;while(s<N){scanf("%c",&a[s]);if(a[s]=10)break;s++;printf("\nThenumbersis:\n");for(x=l;x<s;x++){printf("%c",a[x]);)tag=0;for(x=l;x<s/2;x++){if(a[x]!=a[s-x]){tag=l;break;)}if(tag==0)printf("\nThenumberispalindrome!");elseprintf("\nThenumberisn'tpalindrome!");習題4ー、單項選擇題ABACABCBDD二、填空題①主②main函數(shù)③main函數(shù)④個數(shù)⑤類型⑥順序⑦函數(shù)類型float(floatx,intn)f(x,n)x*f(x,n-1)三、閱讀程序題x=5,y=25y=153.0sl=3.14,s2=5.1420055,33.00,1.00,0.25四、程序設計題//xt040401.cppttinclude<stdio.h>voidmain(){voiddis(intn);intn;scanf("%d",&n);dis(n);)voiddis(intn){intkO,k,sum,i;kO=l;do{k=kO;sum=0;for(i=l;i<=n;i++){sum=sum+k;k=k+2;)if(sum==n*n*n)break;elsek0=k0+2;}while(1);for(i=l;i<=n;i++){printf("%5d”,kO);k0=k0+2;))//xt040402.cpp#include<stdio.h>voidmain(){floath(intn,floatx);intn;floatx;scanfぐ’%d,%f",&n,&x);printf("h(%d,%f)=%f",n,x,h(n,x));)floath(intn,floatx){if(n==0)return1;elseif(n==l)return(2*x);elsereturn(2*x*h(n-l,x)-2*(n-1)*h(n-2,x));//xt040403.cppftinclude<stdio.h>voidmain(){intf(intn);inti;for(i=2;i<=1000;i++)if(f(i))printf("%6d”,i);)intf(intn){if(n==n*n%10I|n==n*n%100||n==n*n%1000)return1;elsereturn0;)//xt040404.cpp#include<stdio.h>voidmain(){longfun(inta,intn);intk,n;scanf("%d,%d",&k,&n);

printf("%ld\n”,fun(k,n));longfun(inta,intn){intj;longs=0,t=0;for(j=l;j<=n;j++){t=t*10+a;s=s+t;)returns;}//xt040405.cpp#include<math.h>#include<stdio.h>voidmain(){intnum,middlel,middle2;intdetect(intd);evenprintf("Pleaseinputapositivenumber(>=6):");evenscanf("%d",&num);for(middlel=2;middlel<=num/2;middlel+=l)if(detect(middlel)){middle2=num-middlel;if(detect(middle2)){printf("%d=%d+%d\n”,num,middlel,middle2);break;})}intdetect(intd){inti;for(i=2;i<=sqrt(d*l.0);i++)if(d%i==0)return(0);return1;)//xt040406.cppWinclude<stdio.h>voidmain(){floatf(floatx,intn);floatx,an;intn;scanfぐ’%f,%d",&x,&n);an=f(x,n);printf("an=%f\n”,an);floatf(floatx,intn){if(n==0)return1;elsereturnx/n*f(x,n-1);)//xt040407.cpp#include<stdio.h>voidmain(){floatf(floatx,intn);floatx,fn;intn;scanf("%f,%d",&x,&n);fn=f(x,n);printf("an二%f\n”,fn);)floatf(floatx,intn){if(n==0)return1;elseif(n>0)returnx*f(x,n-1);elsereturnf(x,n+l)/x;)//xt040408.cpp#include<stdio.h>voidmain(){voidf(intn);intn;scanf("%d",&n);while(n<=0)scanf&n);f(n);)voidf(intn){if(n/10==0)putchar(n%10+‘〇');else{putchar(n%10+'0');f(n/10);//xt040409.cpp#include<stdio.h>voidmain(){voidf(intn);intn;scanfぐ‘%d",&n);while(n<=0)scanf("%d",&n);f(n);)voidf(intn){if(n/10)f(n/10);putchar(n%10+,0));)//xt040410.cppWinclude<stdio.h>voidmain(){intf(intn);intn;for(n=2;n<=1000;n++)if(f(n))printf("%disa完數(shù)、n”,n);intf(intn){inti,s=0;for(i=l;i<n;i++)if(n%i==0)s=s+i;if(s==n)return1;elsereturn0;)習題5ー、單項選擇題AACCADDACB二、填空題①“char*funcl()”定義ー個返回字符指針值的函數(shù),“char(*func2)()”定義ー個指向函數(shù)的指針②ー個返回值為單精度類型指針的函數(shù)名;③(*p)(a,b,c)④findmin⑤(*f)(x,y,z);三、閱讀程序題6!=7205,4,341511249111131315//xt050401.cppSinclude"stdio.h"voidmain(){voidf(int,int,int);inta,b,c;void(*p)(int,int,int);scanf("%d%d%d",&a,&b,&c);P=f;(*p)(a,b,c);voidf(intx,inty,intz){intt;if(x<y)t=x,x=y,y=t;if(y<z)t=z,z=y,y=t;if(x<y)t=x,x=y,y=t;printf("%5d%5d%5d\n”,x,y,z);)//xt050402.cpp《include<stdio.h>intmax(inta,intb){if(a>b)returna;elsereturnb;)voidmain(){intmax(inta,intb);int(*pmax)(int,int);intx,y,z;pmax=max;printf(/zinputtwonumbers:\n/z);scanf(zz%d%dzz,&x,&y);z=(*pmax)(x,y);printf(zzmaxmum=%dzz,z);)//xt050403.cppWinclude“stdio.h"voidgcd(intx,inty){inttemp;while(y!=0){temp=x%y;x=y;y=temp;)printf(zzThegreatestcommondivisoris:%d\n”,x);}voidlcm(intx,inty){inttemp,m,n;m=x;n=y;while(x!=0){temp=y%x;y二x;x=temp;}multipleprintf("Thelowestcommonis:%d\n",m*n/y);multiple)voidmainO{inta,b;void(*p)(int,int);printf("Pleaseinputaandb:");scanf("%d%d",&a,&b);if(a>b)P=gcd;elsep=lcm;(*p)(a,b);)//xt050404.cpp#include"stdio.h"voidmainO{intx,y;intsum(int,int),diff(int,int),product(int,int);voidmma(intx,inty,ints(int,int));printf("Pleaseinputx,y:\n");scanf("%d%d",&x,&y);printf("Thesumofxandyis:");mma(x,y,sum);printf("Thedifferenceofxandyis:");mma(x,y,diff);printf("Theproductofxandyis:");mma(x,y,product);}intsum(intx,inty)(returnx+y;)intdiff(intx,inty){returnx-y;)intproduct(intx,inty)returnx*y;voidmma(inta,intb,ints(int,int)){intw;w=s(a,b);printf w);)//xt050405.cpp#include<stdio.h>doublefund(intx),func2(inty);voidmain(){double(*fptr)(int);/*定義指向函數(shù)的指針變量fptr*/intn;printf(inputanumber:");scanf("%d",&n);if(n>l){if(n%2==0)fptr=funcl;/*n為偶數(shù),指針變量ptr指向函數(shù)fund()*/elsefptr=func2;/*n為奇數(shù),指針變量ptr指向函數(shù)func2()*/printf("value=%9.4f\n",(*fptr)(n));elseprintf("error!\n");)doublefund(intx){intk;doublevalue;value=l.0;for(k=2;k<=x;k=k+2)value=value+l/(double)k;returnvalue;)doublefunc2(inty){intk;doublevalue;value=l.0;for(k=3;k<=y;k=k+2)value=value+l/(double)k;returnvalue;)//xt050406.cppftinclude"stdio.h"#include"math,h"voidmain(){intn,yinzi(int),(*f)(int);f=yinzi;printf("inputanumbern:");scanf("%d",&n);if((*f)(n)==2)printf("\n%disaprimenumber.",n);elseprintf("\n%disnotaprimenumber.",n);}intyinzi(intx){inti,k=0,q=(int)sqrt(x);for(i=l;i<=q;i++)if(x%i==0){k+=2;printf("%5d%5d",i,x/i);)returnk;)//xt050407.cppftinclude<stdio.h>#include<math.h>doublefl(doublex){returnx*x*log(x);)doublef2(doublex)|returnx*sin(x);)doublef3(doublex){returnx/exp(x);}doublecollect(double(*p)(doublex),doublea,doubleb,doublen){inti;doubleh,area;h=(b-a)/n;area=((*p)(a)+(*p)(b))/2.0;for(i=l;i<n;i++)area+=(*p)(a+i*h);return(area*h);voidmain(){doubleyl,y2,y3;yl=collect(fl,1.0,2.0,1000);y2=collect(f2,0.0,3.0,500);y3=collect(f3,0.0,1.0,10000);printf("yl=%6.2f y2=%6.2fy3=%6.2f\n”,yl,y2,y3);)//xt050408.cpp#include"stdio.h"ftinclude"math,h"introot(double*x,intm,doubleeps,double(*f)(double)){doublexO;do{xO二?x;*x=(*f)(xO);m=m-l;}while((m!ニ〇)&&(fabs(*x-x0)>=eps));if(mニニ〇)return(0);return(1);doublefl(doublex)return(1.0+atan(x));)doublef2(doublex)|return(0.5*cos(x));)doublef3(doublex){return((6.0+3*x-x*x)/4);/*x=(6+3*x-x*x)/4*/}voidmain(){doublex,(*p)(double);x=l.0;P=fl;if(root(&x,50,0.00001,p))printf("xl二%f\n”,x);P=f2;if(root(&x,50,0.00001,p))printf(/,xl=%f\n,,>x);P=f3;if(root(&x,50,0.00001,p))printf("xl=%f\n”,x);)//xt050409.cpp#include"stdio.h"int*f(intp[],intq[],intm){inti;for(i=0;i<m;i++)q[i]=p[i]*p[i];returnq;}voidmain(){inta[6]={l,2,3,4,5,6},i,*k;intb[6];k=f(a,b,6);for(i=0;i<6;i++)printfぐ'%5d”,k[i]);)//xt050410.cppftinclude<stdio.h>#include<stdlib.h>voidmain(){intwanshu(intx);int*ptr;ptr=(int*)malloc(sizeof(int));if(ptr==NULL){printf("Failedtocreateanewobject.");exit(0);)scanf("%d",ptr);if(wanshu(*ptr))printf("%disa完數(shù):",*ptr);elseprintf("%disnota完數(shù):",*ptr);free(ptr);)intwanshu(intx){inti=l,sum=0;while(i<x){if(x%i==0)sum+=i;i++;if(sum==x)return1;elsereturn0;)習題6一、單項選擇題BDACDCACAD二、填空題①是其值可以是二級地址(指針)②是其值可以是多級地址址(指針)③沿著數(shù)組所占據(jù)的存儲區(qū)域向前和向后移動多個或者是ー個數(shù)組元素④可以在程序的運行過程中根據(jù)需要創(chuàng)建的數(shù)組⑤int*v,int*keya,&keyadd-a⑧v+n三、閱讀程序題1.112689TOC\o"1-5"\h\z3 5 7 9111 4 7 0 813120003 3 3 3 36 8 11 3 38 9 3 3 33 3 3 3 36.111111413111111114691167111四、程序設計題//xt060401.cpp#include<stdio.h>voidmain(){intyhEll],row,col,i;*(yh+l)=l;for(i=0;i<40-2;i++)printf(z,");printfぐ’%4d\n”,*(yh+l));for(row=2;row<=10;row++){*(yh+row)=l; /?每行的最后ー個元素值為1*/for(col=row-l;col>=2;co!一) /*生成一行*/*(yh+col)=*(yh+col)+*(yh+col-1);for(i=0;i<40-2*row;i++)/?輸出合適的空格?/printf("");for(col=l;col<=row;col++)/?輸出一行?/printf("%4d",*(yh+col));printf("\n");)}//xt060402.cppftinclude<stdio.h>ftinclude<stdlib.h>Winclude<time.h>voidmain(){inta[10][5],b[10],i,j;srand(time(NULL));for(i=0;i<10;i++)for(j=0;j<5;j++)*(a[i]+j)=rand()%100;for(i=0;i<10;i++){*(b+i)=a[i][0];for(j=0;j<5;j++)if(*(b+i)<*(*(a+i)+j))*(b+i)=*(*(a+i)+j);)for(i=0;i<10;i++){for(j=0;j<5;j++)printf(〃%4d”,a[i][j]);printf(*\t%4d\n*,b[i]);)}//xt060403.cppftinclude<stdio.h>ftinclude<stdlib.h>Winclude<time.h>intdelmem(int*v,intn,intdel);voidmain(){int*a,i,n,del;srand(time(NULL));printf(〃請輸入處理的數(shù)組長度:〃);scanf&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i<n;i++)*(a+i)=rand()%100;printf(〃刪除操作前的數(shù)據(jù)如下所示:\n〃);for(i=0;i<n;i++)printf("%4d”,*(a+i));printf(〃、n請輸入欲刪除的元素值:〃);scanf("%d”,&del);n=delmem(a,n,del);printf(〃刪除操作后的數(shù)據(jù)如下所示:\n〃);for(i=0;i<n;i++)printfぐ'%4d”,*(a+i));printf(〃、n〃);)intdelmem(int*v,intn,intdel){inti,j;for(i=0;i<n;i++)if(*(v+i)=del){for(j=i;j<n-2;j++)*(v+j)=*(v+j+l);returnn;//xt060404.cpp#include<stdio.h>#include<stdlib.h>#include<time.h>ftdefineN15voidmain(){intscore[N][4]={0},i,j,x;srand(time(NULL));for(i=0;i<N;i++)for(j=0;j<3;j++){x=rand()%100;if(x<50)J—;elsescore[i][j]=x;)for(i=0;i<N;i++)for(j=0;j<3;j++)*(*(score+i)+3)+=*(*(score+i)+j);printf(z,scorel\tsocre2\tsocre3\tsum\nz,);for(i=0;i<N;i++){for(j=0;j<4;j++)printf *(*(score+i)+j));printf(〃、n");)}//xt060405.cppftinclude<stdio.h>#include<stdlib.h>#include<time.h>ftdefineN15voidmain(){intscore[N][4]={0},i,j,x;intt[4];srand(time(NULL));for(i=0;i<N;i++) 〃生成模擬成績數(shù)據(jù)for(j=0;j<3;j++){x=rand()%100;if(x<50)J—;elsescore[i][j]=x;for(i=0;i<N;i++) 〃統(tǒng)計每個學生的總分for(j=0;j<3;j++)*(*(score+i)+3)+=*(*(score+i)+j);for(i=0;i<N-l;i++){x=i;for(j=i+l;j<N;j++)if(score[j][3]>score[x][3])x=j;if(x!=i)for(j=0;j<4;j++){*(t+j)=*(*(score+x)+j);*(*(score+x)+j)二*(*(score+i)+j);*(*(score+i)+j)=*(t+j);})printf("scorel\tsocre2\tsocre3\tsum\n");for(i=0;i<N;i++){for(j=0;j<4;j++)printf("%d\t”,*(*(score+i)+j));printfI\n");//xt060406.cpp#include<stdio.h>^include<stdlib.h>voidmain(){int*ArrayCat(int*s,intslen,int*t,inttlen);inta[]={l,3,5,7,9},b[]={2,4,6,8,10},*p=NULL,i;p=ArrayCat(a,sizeof(a)/sizeof(int),b,sizeof(b)/sizeof(int));for(i=0;i<(sizeof(a)+sizeof(b))/sizeof(int);i++)printfぐ'%4d”,p[i]);printf("\n");)int*ArrayCat(int*s,intslen,int*t,inttlen){int*st,i;st=(int*)malloc(sizeof(int)*(slen+tlen));for(i=0;i<slen;i++)st[i]=*(s+i);for(;i<slen+tlen;i++)st[i]=*(t+i-slen);returnst;//xt060407.cppftinclude<stdio.h>#include<stdlib.h>#include<time.h>voidmain(){voidreverse(int*v,intn);int*a,n,i;srand(time(NULL));printf(〃請輸入數(shù)組的長度:〃);scanfぐ'%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i<n;i++)a[i]=rand()%100;for(i=0;i<n;i++)printf("%4d",a[i]);printf("\n");reverse(a,n);for(i=0;i<n;i++)printfぐ'%4d”,a[i]);printf("\n");voidreverse(int*v,intn){inti,j,t;for(i=0,j=n-l;i<j;i++,j—)t=*(v+i),*(v+i)=*(v+j),*(v+j)=t;)//xt060408.cpp#include<stdio.h>#include<stdlib.h>ftinclude<time.h>int*ArrayCopy(int*source,intn);voidmain(){int*a,*b=NULL,n,i;srand(time(NULL));printf(〃請輸入被拷貝的數(shù)組長度:〃);scanfぐ‘%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i<n;i++)*(a+i)=rand()%100;b=ArrayCopy(a,n);for(i=0;i<n;i++)printf("%4d",b[i]);printf('\n");int*ArrayCopy(int*source,intn){inti,*target;target=(int*)malloc(sizeof(int)*n);for(i=0;i<n;i++)target[i]=source[i];returntarget;)//xt060409.cpp#include<stdio.h>#include<stdlib.h>#include<time.h>voidsort(intv[],intn);voidreverse(intv[],intn);voidmain(){int*data,i,n;srand(time(NULL));printf(〃請輸入需要處理的數(shù)據(jù)個數(shù):〃);scanf("%d",&n);data=(int*)malloc(sizeof(int)*n);for(i=0;i<n;i++)*(data+i)=rand()%100;printf(〃排序前的數(shù)據(jù)如下所示:'n〃);for(i=0;i<n;i++)printf("%4d”,data[i]);sort(data,n);reverse(data,n);printfC\n排序后的數(shù)據(jù)如下所示:\n〃);for(i=0;i<n;i++)printf("%4d”,data[i]);printf(〃、n〃);)voidsort(intv[],intn){inti,j,k,temp;for(i=0;i<n;i++){k=i;for(j=i+l:j<n;j++)if(v[j]<v[k])k=j;if(k!=i)temp=v[i],v[i]=v[k],v[k]=temp;voidreverse(intv[],intn){inti,j;for(i=0,j=n-l;i<j;i++,j—)v[i]=v[i]+v[j],v[j]=v[i]-v[j],v[i]=v[i]-v[j];)//xt060410.cpp#include<stdio.h>#include<stdlib.h>#include<time.h>voidsort(intv[],intn);voidreverse(intv[],intn);voidmain(){intm,n,i,j,**row;srand(time(NULL));printf(〃請輸入二維數(shù)組的行數(shù):〃);scanf("%d",&m);printf(〃請輸入二維數(shù)組的列數(shù):〃);scanf(〃%d〃,&n);row=(int**)malloc(sizeof(int*)*m);for(i=0;i<m;i++)row[i]=(int*)malloc(sizeof(int)*n);for(i=0;i<m;i++)for(j=0;j<n;j++)row[i][j]=rand()%100;for(i=0;i<m;i++)if(i%2=0)sort(rowEi1,n);else{sort(row[i],n);reverse(row[i],n);}for(i=0;i<m;i++){for(j=0;j<n;j++)printf("%4d”,row[i][j]);printf("\n");))voidsort(intv[],intn){inti,j,k,temp;for(i=0;i<n;i++){k=i;for(j=i+l;j<n;j++)if(v[j]<v[k])k=j;if(k!=i)temp=v[i],v[i]=v[k],v[k]=temp;voidreverse(intv[],intn){inti,j;for(i=0,j=n-l;i<j;i++,j—)v[i]=v[i]+v[j],v[j]=v[i]-v[j],v[i]=v[i]-v[j];習題7ー、單項選擇題AABCBCABDD二、填空題①、〇'②字符串③字符④11⑤12⑥字符型⑦連續(xù)⑧j=i⑨continue⑩i++三、閱讀程序題HelloWexloWexloexloWorld123abedxyabcdxyzyzegnnegnahcDBCDegc123456SWI*CH#*Wa四、程序設計題//xt070401.eppftinclude<stdio.h>intfind_char(char*str,intch){intk,num=0;for(k=0;str[k]!='、〇’;k++)if(str[k]==ch)num++;returnnum;voidmain(){chars[100];intnum,c;printf(,z\nlnputaString:");gets(s);printf("\nlnputacharacter:");c=getchar();num=find_char(s,c);printf("\nnum=%d",num);)//xt070402.cpp《include<stdio.h>#include<string.h>voidmain(){charsi[50],s2[50];intk,t;puts("\nInputsstringsi:");gets(si);puts("\nInputsstrings2:");gets(s2);k=0;while(sl[k]==s2[k]&&sl[k]!=0)k++;t=sl[k]-s2[k];printf("\nt=%d",t);)//xt070403.cpp#include<stdio.h>char*Myitoa(intn,char*p){intk,len=-l,t;t=n;do{n=n/10;len++;}while(n>0);*(p+len+l)=0;do{k=t%10;t=t/10;*(p+len)=k+48;}while(--len>=0);returnp;voidmain(){charstring[20];intk=12345;Myitoa(k,string);printf("版“,string);)//xt070404.cpp#include<stdio.h>#include<string.h>intIsAp1idrome(char*word){char*head,*end;intlen,flag=l;len=strlen(word);head二word;end=head+len-1;while(*head==,')head++;if(head>end)return0;while(*end==,')end—;for(;head<end;){if(*head!=*end){flag=O;break;)head++;end一;)returnflag;)voidmain(){charword[80];printf(,zInputaword:");gets(word);if(IsAplidrome(word))printf("\n%sisapalindrome.",word);elseprintf("\n%sisnotapalindrome.",word);)//xt070405.cppftinclude<stdio.h>intIsdigit(charc){if(c>=0*&&c<=91)return1;return0;voidmain(){charp[80],tmp[80],*s=p,flag=0;intnum=0;gets(s);while(*s){if(Isdigit(*s)){tmp[num]=*s;num++;flag=l;)if(flag&&Isdigit(*(s+l))=0){tmp[num]=,、〇';printf("\n%s,tmp);flag=0;num=0;)s++;//xt070406.cpp#include<stdio.h>#include<string.h>voidinvertp(char*p){staticintlen,k=0;inttmp;len=strlen(p);if(k>len/2-l)return;tmp=*(p+k);(p+k)=*(p+len-k-1);(p+len-k-1)=tmp;k++;invertp(p);)voidmain(){charstring[80]="1234567”;invertp(string);printf("\nThenewstringis:%s",string);//xt070407.cpp#include<stdio.h>voidMyput(char*s){while(*s!=‘、〇')printf("%c”,*s++);printf("\n");)voidmain(){charword[80]=/,HowareYou!;Myput(word);)//xt070408.cpp#include<stdio.h>#include<string.h>voidmain(){chars[100];intk;printfi\nlnputStrings:");gets(s);for(k=0;s[k]!=0;k++){if(s[k]>=,A'&&s[k]<='U'){sEk]+=5;continue;if(s[k]>='V'&&s[k]<=,Z')s[k]=s[k]+5-26;)printf("\nThenewstringis:%s”,s);)//xt070409.cpp#include<stdio.h>#include<string.h>ftinclude<stdlib.h>voidto_lowcase(char*s){while(*s){if(*s>=,A'&&*s<=,Z')*s+=32;s++;))intMy_Find(char*pl,char*p2){char*sl,*s2,*p,*tl,*t2;intlen2,num=0;sl=(char*)malloc(strlen(pl)+l);s2=(char*)malloc(strlen(p2)+l);tl=sl;t2=s2;strcpy(si,pl);strcpy(s2,p2);to_lowcase(si);to_lowcase(s2);Ien2=strlen(p2);while((p=strstr(si,s2))&&len2!=0){num++;sl=p+strlen(s2);)free(tl);free(t2);returnnum;)voidmain(){charsi[100],s2[50];intlen2;printf("\nlnputStringsi:");gets(si);printf("\nlnputstrings2:");gets(s2);len2=My_Find(sl,s2);printf("\nnum=%d”,len2);//xt070410.cpp#include<stdio.h>#include<string.h>voidmain(){charword[80];intmax=0,len,k=0,ptmp;puts(,zInputastring:");gets(word);len=strlen(word);while(word[k]){while(word[k]==,')k++;len=0;while(word[k]!=,'&&word[k]){len++;k++;)if(max<len){max=len;ptmp=k-max;printf("\nThewordis:");for(k=ptmp;k<ptmp+max;k++)printf("%c",word[k]);習題8ー、單項選擇題DCCDADDDDB二、填空題①若干個數(shù)據(jù)項②結構體成員③定義結構體數(shù)據(jù)類型的變量④同時存在⑤同時⑥需要存儲區(qū)域最大的ー個分配存儲空間long*char*double*三、閱讀程序題110,zascalbasicb5,3516021liming19abcdefghijk四、程序設計題//xt080401.cpp#include<stdio.h>structcomp{doublere;doubleim;);typedefstructcompCOMP;voidmain(){COMPsub(COMPx,COMPy);COMPa,b,res;printf(,zInputcomplexnuma:");scanfぐ'%If,%f",&a.re,&a.im);printf(z,Inputcomplexnumb:");scanfぐ’%If,%f”,&b.re,&b.im);res=sub(a,b);printf("res.re二%g,res.im=%g\n,res.re,res.im);)COMPsub(COMPx,COMPy){COMPt;t.re二x.re-y.re;t.im二x.im-y.im;returnt;)//xt080402.cpp#include<stdio.h>#include<malloc.h>voidmain(){structnode{intx;structnode*next;);intn;structnode*h二NULL,*p,*q;printfCInputanumber:");scanf("%d",&n);while(n){p=(structnode*)malloc(sizeof(structnode));p->x=n;p->next=h;if(!h)h=p;elseif(n<=h->x){p->next=h;h=p;)else{q=h;while(q->next&&n>qー〉next->x)q=qー〉next;pー〉next=qー〉next;q->next=p;printfiInputanumber: ;scanf("%d",&n);P=h;n=0;while(p){printf("%8d”,p->x);if(++n%6=0)printf(〃、!!〃);p=p->next;)printf("\n");)//xt080403.cpp《include<stdio.h>#include<stdlib.h>ftdefineN3structstud{charid[5];charname[20];intscore[4];doubleave;);voidinputscore(structstud*rec);voidprintscore(structstud*s);voidsumave(structstud*s);voidmain(){structstudst[N];inti;for(i=0;i<N;i++)inputscore(&stEi]);sumave(st);printscore(st);)voidinputscore(structstud*rec){inti;charin_bufE10];gets(rec->id);gets(rec->name);for(i=0;i<4;i++){gets(in_buf);rec->scoreEi]=atoi(in_buf);))voidsum_ave(structstud*s){intk,i,sum;for(k=0;k<N;k++){sum=O;for(i=0;i<4;i++)sum+=s[k].score[i];s[k].ave=sum/4;))voidprintscore(structstud*s){inti,k;for(k=O;kくN;k++){printf("%s\t%s:",s[k].id,s[k].name);for(i=0;i<4;i++)printf("%5a,s[k].score[i]);printf("\t%f\n”,s[k].ave);))//xt080404.cppWinclude<stdio.h>#include<malloc.h>WdefineN10voidmain(){voidreverse(intv[],intn);intarr[N],i;printf(z,Inputtheelementofarray:\n");for(i=0;i<N;i++)scanf,&arr[i]);reverse(arr,N);for(i=0;i<N;i++)printfぐ'%5d”,arr[i]);printf("\n");)voidreverse(intvロ,intn){structnode{intx;structnode*next;};structnode*h=NULL,*p;inti;for(i=0;i<n;i++){p=(structnode*)malloc(sizeof(structnode));p->x=v[i];p->next=h;h=p;P=h;for(i=0;p;i++,p=p->next)v[i]=p->x;)//xt080405.cpp#include<stdio.h>ftdefineN3structstu{longid;doubleshu,yu,ave;);voidmain(){structstus[N];voidsort(structstuv[],intn);structstu*p;for(p=s;p<s+N;p++){scanf("%ld,%lf,%lf,z,&(p->id),&(p->shu),&(p->yu))fp->ave=(p->shu+p->yu)/2;)sort(s,N);for(p=s;p<s+N;p++)%5.2fprintfC%ld—%5.2f%5.2f\n”,p->id,p->shu,p->yu,p->ave);%5.2f)voidsort(structstuv[],intn){inti,j,k;structstut;for(i=0;i<n-l;i++){k=i;for(j=i+l;j<n;j++)if((*(v+j)).ave>(*(v+k)).ave)k=j;if(k!=i)t=*(v+i),*(v+i)=*(v+k),*(v+k)=t;))//xt080406.cppWinclude<stdio.h>#include<stdlib.h>structlist{intd;str

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論