• 295.50 KB
  • 2022-04-22 11:46:02 发布

《程序设计技术》习题参考答案(修改).doc

  • 65页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'《程序设计技术》(第三版)习题参考答案习题1一、单项选择题DADDDBDCCD二、填空题①主②main③x=x*(x+b)④普通、格式、分隔和转义⑤指定输出数据格式⑥voidmain()⑦"howareyou!"三、阅读程序题1.9,11,9,102.2,-1,-23.aabbccabcAN4.2,25.575767.5864000,-789.12402367.5864000,-7.89e+00267,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.6.a+c=102a+c=ff+m=17.950000a+m=17.650000c+f=102.300000double=1746.150019四、程序设计题//xt010401.cpp#include#definePI3.1415926voidmain(){doubler,h;printf("请输入半径r和高h:");scanf("%lf,%lf",&r,&h);printf("圆半径:%lfn",2*PI*r); printf("圆面积:%lfn",PI*r*r);printf("圆球表面积:%lfn",4*PI*r*r);printf("圆球体积:%lfn",4.0/3*PI*r*r);printf("圆柱体积:%lfn",PI*r*r*h);}//xt010402.cpp#includevoidmain(){intc;printf("Inputacharacter:");c=getchar();printf("%ct%dn",c,c);}//xt010403.cpp#includevoidmain(){intx,y,h=30,f=90;x=(4*h-f)/2;y=(f-2*h)/2;printf("鸡:%d,兔:%dn",x,y);}//xt010404.cpp#include#includevoidmain(){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("三角形的面积是:%lfn",area);}//xt010405.cpp#includevoidmain(){doublef,c;printf("请输入华氏温度:");scanf("%lf",&f);c=5.0/9.0*(f-32);printf("与华氏温度%.2lf对应的摄氏温度是:%.2lfn",f,c);}//xt010406.cpp#includevoidmain() {charc1,c2,c3,c4,c5;printf("请输入需加密的字符串(5个字符):");c1=getchar();c2=getchar();c3=getchar();c4=getchar();c5=getchar();printf("加密后的字符串是:");putchar(c1+5);putchar(c2+5);putchar(c3+5);putchar(c4+5);putchar(c5+5);printf("n");}习题2一、单项选择题CBADCCCAAC二、填空题①a+b==0&&a*b!=0②逻辑与③逻辑或④continue⑤(c=getchar())!="n"⑥%c三、阅读程序题答案:答案:该程序所完成功能的函数关系为:答案:答案:答案: 答案:四、程序设计题/*xt020401.cpp*/#includevoidmain(){intnum;printf("****Inputnum:****n");scanf("%d",&num);if(num%3==0&&num%5==0&&num%7==0)printf("**YES!**n");elseprintf("**NO!**n");}/*xt020402.cpp*/#includevoidmain(){inth=0;floatx,y,x0=2,y0=2,d1,d2,d3,d4;printf("***input:x,y***n");scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);/*点(x,y)到各中心点的距离*/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=15;printf("x=%f,y=%fn",x,y);printf("h=%dn",h);}/*xt020403.cpp*/#includevoidmain(){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("%dn",n);}}/*xt020404.cpp*/#include#include#defineEPS1.0e-6voidmain(){floate=1,t;longintj=1,n=1;do{j=j*n;t=1.0/j;e=e+t;n++;}while(fabs(t)>=EPS);printf("e=%fn",e);}/*xt020405.cpp*/#includevoidmain(){intnum=1;do{if((num+3)%5==0&&(num-3)%6==0)break;num+=1;}while(1);printf("num=%dn",num);}/*xt020406.cpp*/#includevoidmain(){intnum;for(num=1;num<=99;num++)if(num*num%10==num||num*num%100==num)printf("%4d,%5d",num,num*num);printf("n");}/*xt020407.cpp*/ #includevoidmain(){intn,k=1;floats=0;for(n=1;n<=10000;n++){s=s+1.0/(2.0*n-1)*k;k=-k;}printf("PI=%fn",4*s);}/*xt020408.cpp*/#includevoidmain(){intn10,n5,n2,n1;for(n10=1;n10<=9;n10++)for(n5=1;n5<=17;n5++)for(n2=1;n2<=37;n2++){n1=40-n10-n5-n2;if(n1>=1&&n10*10+n5*5+n2*2+n1==100)printf("n10=%d,n5=%d,n2=%d,n1=%dn",n10,n5,n2,n1);}}/*xt020409.cpp*/#includevoidmain(){floatsn=100,hn=sn/2;intn=2;while(n<=10){sn+=2*hn;hn=hn/2;n++;}printf("%f,%fn",sn,hn);}/*xt020410.cpp*/#includevoidmain(){intday,x1,x2=1;for(day=9;day>0;day--){x1=(x2+1)*2;x2=x1;}printf("Total=%dn",x1);} 习题3单项选择题BBCACBDABB填空题①9②0③10④x⑤一致⑥n/2⑦sum+a⑧sum/n三、阅读程序题1.2462.s=303.24224214.lanuge5.8163574926.max=10,row=2,colum=1四、程序设计题//xt030401.cpp#include#include#includevoidmain(void){  intnum[10],a;;  srand((unsigned)time(NULL));  printf("请输入一个2位数,试试您是否能中奖:");scanf("%d",&a);  for(inti=0;i<10;i++)     {num[i]=(rand()+10)%100;    if(a==num[i])     printf("猜中一个数%d下标是%d",a,i);     }  }printf("-------------------------n");  for(i=0;i<10;i++)printf("%dt",num[i]);} //xt030402.cpp#include#include#includevoidmain(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=1;for(j=i;j<5;j++)a[j]=a[j+1];}if(yes==1)for(i=0;i<4;i++)printf("%d,",a[i]);elseprintf("Thenumberyouwantdoesn"texist");}//xt030403.cpp#include#include#include#defineSIZE1000voidmain(){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#include#include#defineSIZE100voidmain(){ intx,a[SIZE],max=0,min=1000,b1,b2;  srand((unsigned)time(NULL));printf("nAutomakea%dsizeoflist:n",size);for(x=0;xmax)max=a[x],b1=x;if(a[x]#defineSIZE100#defineKIND5voidmain(){intx,y,len,total[KIND+1];inta[]={1,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;xintmain(void){staticinta[N+1],b[N+1];inti,j,s,m,p; scanf("%d%d",&s,&m);for(i=1;i<=N;i++)a[i]=i;i=s-1;j=0;p=0;while(pN)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=1;i<=N;i++)printf("%3d",b[i]);printf("n");}//xt030407.cpp#includevoid main(){int i,j,s=0,ave,v[3];static int a[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[1]+v[2])/3;printf("math:%dnc languag:%dndbase:%dn",v[0],v[1],v[2]);printf("totalave:%dn",avel);}//xt030408.cpp#includevoidmain(){inti,j,a[10][10],b[]={1,2,3,4,5,6,7,8,9,10};for(i=0;i<10;i++)a[0][i]=b[i];for(i=1;i<10;i++)for(j=0;j<10;j++)if(i>j)a[i][j]=a[j][i];elsea[i][j]=a[i-1][j-1];for(i=0;i<10;i++){for(j=0;j<10;j++)printf("%dt",a[i][j]); printf("n");}}//xt030409.cpp#include#include#include#defineSIZE5000#defineN7voidmain(){intx,y,a[N]={0,0,0,0,0,0,0},s;  srand((unsigned)time(NULL));x=0;while(x0&&s<7){a[s]++;x++;}}printf("nRandomiz1-6listn");for(x=1;x#defineN20intmain(){intx,s,tag;chara[N];printf("Pleaseinputnumbersandit"slengthlessthen%d:",N-1);s=1;while(svoidmain() {voiddis(intn);intn;scanf("%d",&n);dis(n);}voiddis(intn){intk0,k,sum,i;k0=1;do{k=k0;sum=0;for(i=1;i<=n;i++){sum=sum+k;k=k+2;}if(sum==n*n*n)break;elsek0=k0+2;}while(1);for(i=1;i<=n;i++){printf("%5d",k0);k0=k0+2;}}//xt040402.cpp#includevoidmain(){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==1)return(2*x);elsereturn(2*x*h(n-1,x)-2*(n-1)*h(n-2,x));}//xt040403.cpp#include voidmain(){intf(intn);inti;for(i=2;i<=1000;i++)if(f(i))printf("%6d",i);}intf(intn){if(n==n*n%10||n==n*n%100||n==n*n%1000)return1;elsereturn0;}//xt040404.cpp#includevoidmain(){longfun(inta,intn);intk,n;scanf("%d,%d",&k,&n);printf("%ldn",fun(k,n));}longfun(inta,intn){intj;longs=0,t=0;for(j=1;j<=n;j++){t=t*10+a;s=s+t;}returns;}//xt040405.cpp#include#includevoidmain(){intnum,middle1,middle2;intdetect(intd);printf("Pleaseinputapositiveevennumber(>=6):");scanf("%d",&num);for(middle1=2;middle1<=num/2;middle1+=1)if(detect(middle1)){middle2=num-middle1;if(detect(middle2)){printf("%d=%d+%dn",num,middle1,middle2);break; }}}intdetect(intd){inti;for(i=2;i<=sqrt(d*1.0);i++)if(d%i==0)return(0);return1;}//xt040406.cpp#includevoidmain(){floatf(floatx,intn);floatx,an;intn;scanf("%f,%d",&x,&n);an=f(x,n);printf("an=%fn",an);}floatf(floatx,intn){if(n==0)return1;elsereturnx/n*f(x,n-1);}//xt040407.cpp#includevoidmain(){floatf(floatx,intn);floatx,fn;intn;scanf("%f,%d",&x,&n);fn=f(x,n);printf("an=%fn",fn);}floatf(floatx,intn){if(n==0)return1;elseif(n>0)returnx*f(x,n-1);elsereturnf(x,n+1)/x;} //xt040408.cpp#includevoidmain(){voidf(intn);intn;scanf("%d",&n);while(n<=0)scanf("%d",&n);f(n);}voidf(intn){if(n/10==0)putchar(n%10+"0");else{putchar(n%10+"0");f(n/10);}}//xt040409.cpp#includevoidmain(){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.cpp#includevoidmain(){intf(intn);intn;for(n=2;n<=1000;n++)if(f(n))printf("%disa完数n",n);}intf(intn){inti,s=0; for(i=1;iintmax(inta,intb){if(a>b)returna;elsereturnb;}voidmain(){intmax(inta,intb);int(*pmax)(int,int);intx,y,z;pmax=max;printf("inputtwonumbers:n");scanf("%d%d",&x,&y);z=(*pmax)(x,y);printf("maxmum=%d",z);}//xt050403.cpp#include"stdio.h"voidgcd(intx,inty){inttemp;while(y!=0){temp=x%y;x=y;y=temp;}printf("Thegreatestcommondivisoris:%dn",x);}voidlcm(intx,inty){inttemp,m,n;m=x;n=y;while(x!=0){temp=y%x;y=x;x=temp;} printf("Thelowestcommonmultipleis:%dn",m*n/y);}voidmain(){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"voidmain(){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("%dn",w);} //xt050405.cpp#includedoublefunc1(intx),func2(inty);voidmain(){double(*fptr)(int);/*定义指向函数的指针变量fptr*/intn;printf("inputanumber:");scanf("%d",&n);if(n>1){if(n%2==0)fptr=func1;/*n为偶数,指针变量ptr指向函数func1()*/elsefptr=func2;/*n为奇数,指针变量ptr指向函数func2()*/printf("value=%9.4fn",(*fptr)(n));}elseprintf("error!n");}doublefunc1(intx){intk;doublevalue;value=1.0;for(k=2;k<=x;k=k+2)value=value+1/(double)k;returnvalue;}doublefunc2(inty){intk;doublevalue;value=1.0;for(k=3;k<=y;k=k+2)value=value+1/(double)k;returnvalue;}//xt050406.cpp#include"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=1;i<=q;i++)if(x%i==0){k+=2;printf("%5d%5d",i,x/i);}returnk;}//xt050407.cpp#include#includedoublef1(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=1;i=eps));if(m==0)return(0);return(1);}doublef1(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=1.0;p=f1;if(root(&x,50,0.00001,p))printf("x1=%fn",x);p=f2;if(root(&x,50,0.00001,p))printf("x1=%fn",x);p=f3;if(root(&x,50,0.00001,p))printf("x1=%fn",x);}//xt050409.cpp#include"stdio.h"int*f(intp[],intq[],intm){inti;for(i=0;i#includevoidmain(){intwanshu(intx);int*ptr;ptr=(int*)malloc(sizeof(int));if(ptr==NULL){printf("Failedtocreateanewobject.");exit(0);}scanf("%d",ptr);if(wanshu(*ptr))printf("%disa完数:",*ptr);elseprintf("%disnota完数:",*ptr);free(ptr);}intwanshu(intx){inti=1,sum=0;while(ivoidmain(){intyh[11],row,col,i;*(yh+1)=1;for(i=0;i<40-2;i++)printf("");printf("%4dn",*(yh+1));for(row=2;row<=10;row++){*(yh+row)=1;/*每行的最后一个元素值为1*/for(col=row-1;col>=2;col--)/*生成一行*/*(yh+col)=*(yh+col)+*(yh+col-1);for(i=0;i<40-2*row;i++)/*输出合适的空格*/printf("");for(col=1;col<=row;col++)/*输出一行*/printf("%4d",*(yh+col));printf("n");} }//xt060402.cpp#include#include#includevoidmain(){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%4dn",b[i]);}}//xt060403.cpp#include#include#includeintdelmem(int*v,intn,intdel);voidmain(){int*a,i,n,del;srand(time(NULL));printf("请输入处理的数组长度:");scanf("%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i#include#include#defineN15voidmain(){intscore[N][4]={0},i,j,x;srand(time(NULL));for(i=0;i#include#include#defineN15 voidmain(){intscore[N][4]={0},i,j,x;intt[4];srand(time(NULL));for(i=0;iscore[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("score1tsocre2tsocre3tsumn");for(i=0;i#includevoidmain(){int*ArrayCat(int*s,intslen,int*t,inttlen);inta[]={1,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#include#includevoidmain(){voidreverse(int*v,intn);int*a,n,i;srand(time(NULL));printf("请输入数组的长度:");scanf("%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i#include#includeint*ArrayCopy(int*source,intn);voidmain(){int*a,*b=NULL,n,i;srand(time(NULL)); printf("请输入被拷贝的数组长度:");scanf("%d",&n);a=(int*)malloc(sizeof(int)*n);for(i=0;i#include#includevoidsort(intv[],intn);voidreverse(intv[],intn);voidmain(){int*data,i,n;srand(time(NULL));printf("请输入需要处理的数据个数:");scanf("%d",&n);data=(int*)malloc(sizeof(int)*n);for(i=0;i#include#includevoidsort(intv[],intn);voidreverse(intv[],intn);voidmain(){intm,n,i,j,**row;srand(time(NULL));printf("请输入二维数组的行数:");scanf("%d",&m);printf("请输入二维数组的列数:");scanf("%d",&n);row=(int**)malloc(sizeof(int*)*m);for(i=0;iintfind_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("nInputaString:");gets(s);printf("nInputacharacter:");c=getchar();num=find_char(s,c);printf("nnum=%d",num);}//xt070402.cpp#include#includevoidmain(){chars1[50],s2[50];intk,t;puts("nInputsstrings1:");gets(s1);puts("nInputsstrings2:");gets(s2);k=0;while(s1[k]==s2[k]&&s1[k]!=0)k++;t=s1[k]-s2[k];printf("nt=%d",t);}//xt070403.cpp#includechar*Myitoa(intn,char*p){intk,len=-1,t;t=n;do{n=n/10;len++;}while(n>0); *(p+len+1)=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("%s",string);}//xt070404.cpp#include#includeintIsAplidrome(char*word){char*head,*end;intlen,flag=1;len=strlen(word);head=word;end=head+len-1;while(*head=="")head++;if(head>end)return0;while(*end=="")end--;for(;headintIsdigit(charc){if(c>="0"&&c<="9")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=1;}if(flag&&Isdigit(*(s+1))==0){tmp[num]="";printf("n%s",tmp);flag=0;num=0;}s++;}}//xt070406.cpp#include#includevoidinvertp(char*p){staticintlen,k=0;inttmp;len=strlen(p);if(k>len/2-1)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#includevoidMyput(char*s){while(*s!="")printf("%c",*s++);printf("n");}voidmain(){charword[80]="HowareYou!";Myput(word);}//xt070408.cpp#include#includevoidmain(){chars[100];intk;printf("nInputStrings:");gets(s);for(k=0;s[k]!=0;k++){if(s[k]>="A"&&s[k]<="U"){s[k]+=5;continue;}if(s[k]>="V"&&s[k]<="Z")s[k]=s[k]+5-26;}printf("nThenewstringis:%s",s);}//xt070409.cpp#include#include#includevoidto_lowcase(char*s){while(*s){if(*s>="A"&&*s<="Z")*s+=32; s++;}}intMy_Find(char*p1,char*p2){char*s1,*s2,*p,*t1,*t2;intlen2,num=0;s1=(char*)malloc(strlen(p1)+1);s2=(char*)malloc(strlen(p2)+1);t1=s1;t2=s2;strcpy(s1,p1);strcpy(s2,p2);to_lowcase(s1);to_lowcase(s2);len2=strlen(p2);while((p=strstr(s1,s2))&&len2!=0){num++;s1=p+strlen(s2);}free(t1);free(t2);returnnum;}voidmain(){chars1[100],s2[50];intlen2;printf("nInputStrings1:");gets(s1);printf("nInputstrings2:");gets(s2);len2=My_Find(s1,s2);printf("nnum=%d",len2);}//xt070410.cpp#include#includevoidmain(){charword[80];intmax=0,len,k=0,ptmp;puts("Inputastring:");gets(word);len=strlen(word);while(word[k]){while(word[k]=="")k++; len=0;while(word[k]!=""&&word[k]){len++;k++;}if(max structcomp{doublere;doubleim;};typedefstructcompCOMP;voidmain(){COMPsub(COMPx,COMPy);COMPa,b,res;printf("Inputcomplexnuma:");scanf("%lf,%f",&a.re,&a.im);printf("Inputcomplexnumb:");scanf("%lf,%f",&b.re,&b.im);res=sub(a,b);printf("res.re=%g,res.im=%gn",res.re,res.im);}COMPsub(COMPx,COMPy){COMPt;t.re=x.re-y.re;t.im=x.im-y.im;returnt;}//xt080402.cpp#include#includevoidmain(){structnode{intx;structnode*next;};intn;structnode*h=NULL,*p,*q;printf("Inputanumber:");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;}printf("Inputanumber:");scanf("%d",&n);}p=h;n=0;while(p){printf("%8d",p->x);if(++n%6==0)printf("n");p=p->next;}printf("n");}//xt080403.cpp#include#include#defineN3structstud{charid[5];charname[20];intscore[4];doubleave;};voidinputscore(structstud*rec);voidprintscore(structstud*s);voidsum_ave(structstud*s);voidmain(){structstudst[N];inti;for(i=0;iid); gets(rec->name);for(i=0;i<4;i++){gets(in_buf);rec->score[i]=atoi(in_buf);}}voidsum_ave(structstud*s){intk,i,sum;for(k=0;k#include#defineN10voidmain(){voidreverse(intv[],intn);intarr[N],i;printf("Inputtheelementofarray:n");for(i=0;ix=v[i];p->next=h;h=p;}p=h;for(i=0;p;i++,p=p->next)v[i]=p->x;}//xt080405.cpp#include#defineN3structstu{longid;doubleshu,yu,ave;};voidmain(){structstus[N];voidsort(structstuv[],intn);structstu*p;for(p=s;pid),&(p->shu),&(p->yu));p->ave=(p->shu+p->yu)/2;}sort(s,N);for(p=s;pid,p->shu,p->yu,p->ave);}voidsort(structstuv[],intn){inti,j,k;structstut;for(i=0;i(*(v+k)).ave)k=j;if(k!=i)t=*(v+i),*(v+i)=*(v+k),*(v+k)=t;}}//xt080406.cpp #include#includestructlist{intd;structlist*link;};typedefstructlistL;voidmain(){L*createlist();voidprintlist(L*h);L*head;head=createlist();printlist(head);}L*createlist(){L*p,*q,*ph;inta;ph=(L*)malloc(sizeof(L));p=q=ph;printf("Inputtheelementoflist,-1forend.n");scanf("%d",&a);while(a!=-1){p=(L*)malloc(sizeof(L));p->d=a;q->link=p;q=p;scanf("%d",&a);}p->link=NULL;returnph;}voidprintlist(L*h){L*p=h->link;while(p!=NULL){printf("%5d",p->d);p=p->link;}printf("n");}//xt080407.cpp#include#includestructlist{intd; structlist*link;};typedefstructlistL;voidmain(){L*createlist();voidprintlist(L*h);voidinsert(L*h,inta,intkey);inta,k;L*head;head=createlist();printf("InputvalueforInsert.n");scanf("%d,%d",&a,&k);insert(head,a,k);printlist(head);}L*createlist(){L*p,*q,*ph;inta;ph=(L*)malloc(sizeof(L));p=q=ph;printf("Inputtheelementoflist,-1forend.n");scanf("%d",&a);while(a!=-1){p=(L*)malloc(sizeof(L));p->d=a;q->link=p;q=p;scanf("%d",&a);}p->link=NULL;returnph;}voidprintlist(L*h){L*p=h->link;while(p!=NULL){printf("%5d",p->d);p=p->link;}printf("n");}voidinsert(L*h,inta,intkey){L*s,*p,*q;s=(L*)malloc(sizeof(L));s->d=key; q=h;p=h->link;if(p==NULL){s->link=p;q->link=s;return;}while(p->d!=a&&p->link!=NULL){q=p;p=p->link;}if(p->d==a){s->link=p;q->link=s;}else{s->link=NULL;p->link=s;}}//xt080408.cpp#include#includestructlist{intd;structlist*link;};typedefstructlistL;voidmain(){L*createlist();voidprintlist(L*h);voidinsert(L*h,inta,intkey);voidreverselist(L*h);inta,k;L*head;head=createlist();printf("InputvalueforInsert.n");scanf("%d,%d",&a,&k);insert(head,a,k);printlist(head);reverselist(head);printlist(head);}L*createlist() {L*p,*q,*ph;inta;ph=(L*)malloc(sizeof(L));p=q=ph;printf("Inputtheelementoflist,-1forend.n");scanf("%d",&a);while(a!=-1){p=(L*)malloc(sizeof(L));p->d=a;q->link=p;q=p;scanf("%d",&a);}p->link=NULL;returnph;}voidprintlist(L*h){L*p=h->link;while(p!=NULL){printf("%5d",p->d);p=p->link;}printf("n");}voidinsert(L*h,inta,intkey){L*s,*p,*q;s=(L*)malloc(sizeof(L));s->d=key;q=h;p=h->link;if(p==NULL){s->link=p;q->link=s;return;}while(p->d!=a&&p->link!=NULL){q=p;p=p->link;}if(p->d==a){s->link=p;q->link=s;}else {s->link=NULL;p->link=s;}}voidreverselist(L*h){L*p,*q;p=h->link;if(p!=NULL){h->link=NULL;do{q=p->link;p->link=h->link;h->link=p;p=q;}while(p!=NULL);}}//xt080409.cpp#include#includetypedefstructnode{intno;structnode*next;}NODE;voidmain(){intn,start,end,i,count;NODE*h,*p,*tail,*newh;printf("Inputthenumberofpeople:");scanf("%d",&n);if(n<=0){printf("nmustbe>=0!n");return;}printf("Inputthestartingpointandendingpoint:");scanf("%d,%d",&start,&end);if(start<1||end<1){printf("Dataerror!n");return;}newh=NULL;tail=(NODE*)malloc(sizeof(NODE));h=tail;h->no=n;for(i=n-1;i>=1;i--) {p=(NODE*)malloc(sizeof(NODE));p->no=i;p->next=h;h=p;}tail->next=h;h=tail;start=(start-1)%n;for(i=0;inext;for(count=n;count>1;count--){start=(end-1)%count;for(i=0;inext;if(newh){tail->next=h->next;tail=h->next;}else{newh=h->next;tail=newh;}h->next=h->next->next;}tail->next=h;tail=h;tail->next=NULL;count=0;if(n==1)newh=tail;while(newh){printf("%5d",newh->no);newh=newh->next;if(++count%6==0)printf("n");}printf("n");}//xt080410.cpp#include#includetypedefstructnode{floatcoef;intexp; structnode*next;}NODE;voidmain(){NODE*pa,*pb,*pc,*tail,*p,*q,*r;floatcoef;intexp,count;pa=(NODE*)malloc(sizeof(NODE));pa->coef=0.0;pa->exp=-1;/*建立多项式A的头节点*/tail=pa;do{printf("输入多项式A中每一项的系数和指数(指数小于0时结束):");scanf("%f,%d",&coef,&exp);if(exp<0)break;p=(NODE*)malloc(sizeof(NODE));p->coef=coef;p->exp=exp;tail->next=p;tail=p;}while(1);tail->next=NULL;pb=(NODE*)malloc(sizeof(NODE));pb->coef=0.0;pb->exp=-1;/*建立多项式B的头节点*/tail=pb;do{printf("输入多项式B中每一项的系数和指数(指数小于0时结束):");scanf("%f,%d",&coef,&exp);if(exp<0)break;p=(NODE*)malloc(sizeof(NODE));p->coef=coef;p->exp=exp;tail->next=p;tail=p;}while(1);tail->next=NULL;if(pa->next==NULL)pc=pb;elseif(pb->next==NULL)pc=pa; else{pc=(NODE*)malloc(sizeof(NODE));pc->coef=0.0;pc->exp=-1;/*建立多项式C的头节点*/pc->next=NULL;tail=pc;pa=pa->next;while(pa){p=pb->next;while(p){coef=pa->coef*p->coef;exp=pa->exp+p->exp;q=pc;while(q->next){if(exp==q->next->exp){q->next->coef+=coef;gotonext;}elseif(q->next->exp>exp)break;elseq=q->next;}r=(NODE*)malloc(sizeof(NODE));r->coef=coef;r->exp=exp;r->next=q->next;q->next=r;next:p=p->next;}pa=pa->next;}}p=pc;while(p->next){if(p->next->coef==0.0){q=p->next;p->next=q->next;free(q);}p=p->next;}printf("多项式A和多项式B的乘积为:n"); p=pc;count=0;while(p->next){printf("(%5.1f,%2d)",p->next->coef,p->next->exp);if(++count%6==0)printf("n");p=p->next;}printf("n");}习题9一、单项选择题ADBBBCDCDC二、填空题①显示器②w③指定文件内部记录指针移动的起始位置④sizeof(student)三、阅读程序题1.younk2.文件拷贝3.135794.Name:LileiPhone:123456Score:1005.AAA6.18四、程序设计题//xt090401.cpp#include#includevoidmain(){structperson{charname[20];charsex;intage;floatheight;}person1;FILE*fp;charfname[20];inti;printf("pleaseinputafilename:n");scanf("%s",fname);if((fp=fopen(fname,"wb"))==NULL) {printf("cannotopenthisfile!n");exit(1);}printf("pleaseinputthreepersons"name,sex,ageandheight:n");for(i=0;i<3;i++){scanf("%s%c%d%f",person1.name,&person1.sex,&person1.age,&person1.height);if(fwrite(&person1,sizeof(person1),1,fp)!=1){printf("writeerror!n");exit(2);}}fclose(fp);if((fp=fopen(fname,"rb"))==NULL){printf("cannotopenthisfile!n");exit(3);}printf("nthecontentofthefileis:nNamettSextAgetHeightn");while(!feof(fp)){if(fread(&person1,sizeof(person1),1,fp)==1)printf("%-16s%-8c%-8d%-10.2fn",person1.name,person1.sex,person1.age,person1.height);}fclose(fp);exit(0);}//xt090402.cpp#include#include#includevoidmain(){inti;FILE*fp;if((fp=fopen("result.txt","w"))==NULL){puts("OPenfilefailed!");exit(1);}doubleresult=0.0;for(i=0;i<=32;i++){result=cos((double)(i*2)/32);printf("%lfn",result);fprintf(fp,"%9.6f",result);}fclose(fp); }//xt090403.cpp#include#includevoidmain(){FILE*fp;inti,j,n,ni;charc[160],t,ch;if((fp=fopen("file1","r"))==NULL){printf("file1cannotbeopenedn");exit(1);}printf("nAcontentsare:n");for(i=0;(ch=fgetc(fp))!=EOF;i++){c[i]=ch;putchar(c[i]);}fclose(fp);ni=i;if((fp=fopen("file2","r"))==NULL){printf("file2cannotbeopenedn");exit(2);}printf("nBcontentsare:n");for(;(ch=fgetc(fp))!=EOF;i++){c[i]=ch;putchar(c[i]);}fclose(fp);n=i;for(i=0;ic[j]){t=c[i];c[i]=c[j];c[j]=t;}if((fp=fopen("file3","w"))==NULL){printf("file3cannotbeopenedn");exit(3);}for(i=0;i#include#includevoidmain(){FILE*fp;charstr[100];inti=0;if((fp=fopen("test","w"))==NULL){printf("cannotopenthefilen");exit(1);}printf("pleaseinputastring:n");gets(str);while(str[i]!=""){if(str[i]>="a"&&str[i]<="z")str[i]=str[i]-32;fputc(str[i],fp);i++;}fclose(fp);fp=fopen("test","r");fgets(str,strlen(str)+1,fp);printf("%sn",str);fclose(fp);}//xt090405.cpp#includeintmain(intargc,char*argv[]){charfilename[20];FILE*fpt;printf("Inputfilename:n");gets(filename);if((fpt=fopen(filename,"w"))==NULL){printf("CannotOpenFile!");return1;}charch;ch=getchar();while(ch!="#"){fputc(ch,fpt);ch=getchar(); }return0;}//xt090406.cpp#includecharLine[2000];voidclearLine(){Line[0]="";}intgetMaxLineNum(charfilename[20]){intLineMax=0,Count=0,LineNum=0,LenMaxLine=0;charch;FILE*fpt;if((fpt=fopen(filename,"r"))==NULL){printf("CannotOpenFile!");return1;}ch=fgetc(fpt);while(ch!=EOF){putchar(ch);if(ch==10){if(LineMaxstructStudent{charname[20];charphone[20];intscore;};structStudentinputStudent(){structStudentStu;printf("Name:n");gets(Stu.name);printf("Phone:n");gets(Stu.phone);printf("Score:n");scanf("%d",&Stu.score);getchar(); returnStu;}intwriteStus(structStudentStus[],intnum){charfilename[]="class.txt";FILE*fpt;if((fpt=fopen(filename,"w"))==NULL){printf("CannotOpenFile!");return1;}fwrite(Stus,sizeof(structStudent),num,fpt);fclose(fpt);return0;}voidwrite(){structStudentStus[5];for(inti=0;i<5;i++){printf("InputInfomationofthe%dthStudent:n",i);Stus[i]=inputStudent();}writeStus(Stus,5);}voidmain(intargc,char*argv[]){write();}//xt090408.cpp#includestructStudent{charname[20];charphone[20];intscore;};voidoutputStudent(structStudentStu){printf("Name:%sn",Stu.name);printf("Phone:%sn",Stu.phone);printf("Score:%5dn",Stu.score);}intreadStudent(structStudent*stu,intnum){charfilename[]="class.txt";FILE*fpt;if((fpt=fopen(filename,"r"))==NULL){printf("CannotOpenFile!");return1;} fseek(fpt,sizeof(structStudent)*num,SEEK_SET);fread(stu,sizeof(structStudent),1,fpt);fclose(fpt);return0;}voidread(){structStudentStu;readStudent(&Stu,2);outputStudent(Stu);}voidmain(intargc,char*argv[]){read();}//xt090409.cpp#include#includestructStudent{charname[20];charphone[20];intscore;};intreadStudentArr(structStudentStus[],intnum){charfilename[]="class.txt";FILE*fpt;if((fpt=fopen(filename,"r"))==NULL){printf("CannotOpenFile!");return1;}fread(Stus,sizeof(structStudent),num,fpt);fclose(fpt);return0;}intwriteStudentArr(structStudentStus[],intnum){charfilename[]="class.txt";FILE*fpt;if((fpt=fopen(filename,"w"))==NULL){printf("CannotOpenFile!");return1;}fwrite(Stus,sizeof(structStudent),num,fpt);fclose(fpt);return0;} intSwapSudent(structStudent*StuOne,structStudent*StuTwo){structStudentStuTemp;StuTemp=*StuOne;*StuOne=*StuTwo;*StuTwo=StuTemp;return0;}voidbubble(structStudentStus[],intnum){for(inti=0;i0)SwapSudent(&Stus[j],&Stus[j+1]);}voidshowdata(structStudentStus[],intnum){printf("NametPhonetScoretn");for(inti=0;i#includestructStudent{charname[20];charphone[20];intscore;};intreadStudentArr(structStudentStus[],intnum){charfilename[]="class.txt";FILE*fpt;if((fpt=fopen(filename,"r"))==NULL){printf("CannotOpenFile!");return1;} fread(Stus,sizeof(structStudent),num,fpt);fclose(fpt);return0;}intwriteStudentArr(structStudentStus[],intnum){charfilename[]="class.txt";FILE*fpt;if((fpt=fopen(filename,"w"))==NULL){printf("CannotOpenFile!");return1;}fwrite(Stus,sizeof(structStudent),num,fpt);fclose(fpt);return0;}intSwapSudent(structStudent*StuOne,structStudent*StuTwo){structStudentStuTemp;StuTemp=*StuOne;*StuOne=*StuTwo;*StuTwo=StuTemp;return0;}voidbubble(structStudentStus[],intnum){for(inti=0;i0)SwapSudent(&Stus[j],&Stus[j+1]);}structStudentinputStudent(){structStudenttempstu;puts("Name:");gets(tempstu.name);puts("Phone:");gets(tempstu.phone);puts("Score:");scanf("%d",&tempstu.score);returntempstu;}voidshowdata(structStudentStus[],intnum){printf("NametPhonetScoretn");for(inti=0;ivoidmain(){voiddisplayBits(unsignednum);unsigneda;printf("pleaseanumberofHEX:");scanf("%x",&a);displayBits(a);}voiddisplayBits(unsignednum){intmask,i;mask=1<<15;for(i=1;i<=16;i++){putchar(num&mask?"1":"0");num<<=1; if(i%4==0)putchar(",");}printf("Bn");}//xt100402.cpp#includevoidmain(){voiddisplayBits(unsignednum);unsigneda;printf("pleaseanumberofHEX:");scanf("%x",&a);displayBits(a);a>>=4;displayBits(a);}voiddisplayBits(unsignednum){intmask,i;mask=1<<15;for(i=1;i<=16;i++){putchar(num&mask?"1":"0");num<<=1;if(i%4==0)putchar(",");}printf("Bn");}//xt100403.cpp#includevoidmain(){voiddisplayBits(unsignednum);unsignedpower2(intnumber,intpow);unsigneda,pow;printf("pleaseanumberofDEC:");scanf("%d",&a);printf("inputleftmovebits:<16:");scanf("%d",&pow);displayBits(a);displayBits(power2(a,pow));printf("n%d",power2(a,pow));}unsignedpower2(intnumber,intpow){returnnumber<voidmain(){voiddisplayBits(unsignednum);unsignedpackCharacters(charc1,charc2);unsigneda;charc1,c2;printf("pleasetwocharacters:");scanf("%c%c",&c1,&c2);a=packCharacters(c1,c2);printf("nresult:%c%c%xn",c1,c2,a);displayBits(a);}unsignedpackCharacters(charc1,charc2){unsigneda=c1;a<<=8;a|=c2;returna;}voiddisplayBits(unsignednum){intmask,i;mask=1<<15;for(i=1;i<=16;i++){putchar(num&mask?"1":"0");num<<=1;if(i%4==0)putchar(",");}printf("Bn");}//xt100405.cpp#include voiddisplayBits(unsignednum);voidmain(){voidunpackCharacters(unsignednum);unsigneda;scanf("%d",&a);unpackCharacters(a);}voidunpackCharacters(unsignednum){unsigneda;charc1,c2;a=num&0xFF00;c1=a>>8;c2=num&0x00FF;displayBits(num);printf("nc1=%x,c2=%x",c1,c2);printf("nc1=%c,c2=%c",c1,c2);}voiddisplayBits(unsignednum){intmask,i;mask=1<<15;for(i=1;i<=16;i++){putchar(num&mask?"1":"0");num<<=1;if(i%4==0)putchar(",");}printf("Bn");}//xt100406.cpp#includechar*encrypt(char*word,charcode);voidmain(){char*p,s[]="lostcontroloftheskiddingcar.";p=encrypt(s,"G");printf("nencryptstring=%s",p);p=encrypt(s,"G");printf("nunencryptstring=%s",p);}char*encrypt(char*word,charcode){char*p=word;while(*word){*word^=code;word++;}returnp; }//xt100407.cpp#includevoidmain(){voiddisplayBit(unsignednum);unsignedgetbits(unsignedvalue,intn1,intn2);unsigneda,n1,n2;printf("pleaseanumberofHEX:");scanf("%x",&a);printf("nandinputn1,n2(startis0)?");scanf("%d%d",&n1,&n2);printf("(a)fromn1ton2=%xn",getbits(a,n1,n2));displayBit(a);displayBit(getbits(a,n1,n2));}unsignedgetbits(unsignedvalue,intn1,intn2){unsignedi;i=~0;i=(i>>n1)&(i<<(15-n2));i=value&i;i=i>>(15-n2);returni;}voiddisplayBit(unsignednum){intmask,i;mask=1<<15;for(i=1;i<=16;i++){putchar(num&mask?"1":"0");num<<=1;if(i%4==0)putchar(",");}printf("Bn");}//xt100408.cpp#includevoidmain(){unsignedextracteOddBits(unsignedx);intn=123;n=extracteOddBits(n);printf("%dn",n);}unsignedextracteOddBits(unsignedx){inti,j; unsignedz,a,q;z=0;for(i=1;i<=15;i+=2){q=1;for(j=1;j<=(16-i-2)/2;j++)q=q*2;a=x>>(16-i);a=a<<15;a=a>>15;z=z+a*q;}returnz;}//xt100409.cpp#includevoidmain(){unsignedunmutexCode(unsignedvalue);unsigneda;printf("pleaseanumberofHEX:");scanf("%x",&a);printf("Bcode(a)=%x",unmutexCode(a));}unsignedunmutexCode(unsignedvalue){unsignedi;i=value&0x8000;if(i==0x8000)i=~value+1;elsei=value;returni;}'