您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > C++程序设计第三版(谭浩强)第六章习题答案
6.1Ìâ#includeiostream#includestringusingnamespacestd;constintn=10;stringname[n];intnum[n],score[n];intmain(){inti;voidinput_data();input_data();coutendlThelistoffailed:endl;for(i=0;in;i++)if(score[i]60)coutname[i]num[i]score[i]endl;return0;}voidinput_data(){inti;for(i=0;in;i++){coutinputname,numberandscoreofstudenti+1:;cinname[i]num[i]score[i];}}6.2Ìâ#includeiostream#includecstringusingnamespacestd;intmain(){voidswap(char*,char*);charstr1[20],str2[20],str3[20];coutinputthreeline:endl;gets(str1);gets(str2);gets(str3);if(strcmp(str1,str2)0)swap(str1,str2);if(strcmp(str1,str3)0)swap(str1,str3);if(strcmp(str2,str3)0)swap(str2,str3);coutendlNow,theorderis:endl;coutstr1endlstr2endlstr3endl;return0;}voidswap(char*p1,char*p2)/*½»»»Á½¸ö×Ö·û´®*/{charp[20];strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);}6.2ÌâÁíÒ»½â#includeiostream#includestringusingnamespacestd;intmain(){voidchange(string&,string&);stringstr1=,str2=,str3=;char*p1=&str1[0],*p2=&str2[0],*p3=&str3[0];coutinputthreeline:endl;gets(p1);gets(p2);gets(p3);if(str1str2)change(str1,str2);if(str1str3)change(str1,str3);if(str2str3)change(str2,str3);coutendlNow,theorderis:endl;coutstr1endlstr2endlstr3endl;return0;}voidchange(string&st1,string&st2)/*½»»»Á½¸ö×Ö·û´®*/{stringst;st=st1;st1=st2;st2=st;}6.3Ìâ#includeiostreamusingnamespacestd;intmain(){voidinput(int*number);voidmax_min_value(int*number);voidoutput(int*number);intnumber[10];input(number);//µ÷ÓÃÊäÈë10¸öÊýµÄº¯Êýmax_min_value(number);//µ÷Óý»»»º¯Êýoutput(number);//µ÷ÓÃÊä³öº¯Êýreturn0;}voidinput(int*number)//ÊäÈë10¸öÊýµÄº¯Êý{inti;coutinput10numbers:;for(i=0;i10;i++)cinnumber[i];}voidmax_min_value(int*number)//½»»»º¯Êý{int*max,*min,*p,temp;max=min=number;for(p=number+1;pnumber+10;p++)if(*p*max)max=p;//½«´óÊýµØÖ·¸³¸ømaxelseif(*p*min)min=p;//½«Ð¡ÊýµØÖ·¸³¸ømintemp=number[0];number[0]=*min;*min=temp;//½«×îСÊýÓëµÚÒ»Êý½»»»temp=number[9];number[9]=*max;*max=temp;//½«×îСÊýÓëµÚÒ»Êý½»»»}voidoutput(int*number)//Êä³öº¯Êý{int*p;coutnow,theyare:;for(p=number;pnumber+10;p++)cout*p;coutendl;return;}6.4Ìâ#includeiostreamusingnamespacestd;intmain(){voidmove(int*array,intn,intm);intnumber[20],n,m,i;couthowmanynumbers?;//ѯÎʹ²ÓжàÉÙ¸öÊýcinn;coutinputnnumbers:endl;//ÒªÇóÊäÈën¸öÊýfor(i=0;in;i++)cinnumber[i];couthowmanyplacesdoyouwantmove?;//ѯÎʺóÒƶàÉÙ¸öλÖÃcinm;move(number,n,m);//µ÷ÓÃmoveº¯ÊýcoutNow,theyare:endl;for(i=0;in;i++)coutnumber[i];coutendl;return0;}voidmove(int*array,intn,intm)//ʹѻ·ºóÒÆÒ»´ÎµÄº¯Êý{int*p,array_end;array_end=*(array+n-1);for(p=array+n-1;parray;p--)*p=*(p-1);*array=array_end;m--;if(m0)move(array,n,m);//µÝ¹éµ÷ÓÃ,µ±Ñ»·´ÎÊým¼õÖÁΪ0ʱ,Í£Ö¹µ÷ÓÃ}6.5Ìâ#includeiostreamusingnamespacestd;intmain(){inti,k,m,n,num[50],*p;coutinputnumberofperson:n=;cinn;p=num;for(i=0;in;i++)*(p+i)=i+1;//ÒÔ1ÖÁnΪÐò¸øÿ¸öÈ˱àºÅi=0;//iΪÿ´ÎÑ»·Ê±¼ÆÊý±äÁ¿k=0;//kΪ°´1,2,3±¨ÊýʱµÄ¼ÆÊý±äÁ¿m=0;//mΪÍ˳öÈËÊýwhile(mn-1)//µ±Í˳öÈËÊý±Èn-1ÉÙʱ(¼´Î´Í˳öÈËÊý´óÓÚ1ʱ)Ö´ÐÐÑ»·Ìå{if(*(p+i)!=0)k++;if(k==3)//½«Í˳öµÄÈ˵ıàºÅÖÃΪ0{*(p+i)=0;k=0;m++;}i++;if(i==n)i=0;//±¨Êýµ½Î²ºó£¬i»Ö¸´Îª0}while(*p==0)p++;coutThelastoneisNO.*pendl;return0;}6.6Ìâ#includeiostreamusingnamespacestd;intmain(){intlength(char*p);intlen;charstr[20];coutinputstring:;cinstr;len=length(str);coutThelengthofstringislenendl;return0;}intlength(char*p)//Çó×Ö·û´®³¤¶ÈµÄº¯Êý{intn;n=0;while(*p!='\0'){n++;p++;}return(n);}6.7Ìâ#includeiostreamusingnamespacestd;intmain(){voidcopystr(char*,char*,int);intm;charstr1[20],str2[20];coutinputstring:;gets(str1);coutwhichcharacterdoyouwantbegintocopy?;cinm;if(strlen(str1)m)coutinputerror!endl;else{copystr(str1,str2,m);coutresult:str2endl;}return0;}voidcopystr(char*p1,char*p2,intm)//×Ö·û´®²¿·Ö¸´Öƺ¯Êý*/{intn;n=0;while(nm-1){n++;p1++;}while(*p1!='\0'){*p2=*p1;p1++;p2++;}*p2='\0';}6.8Ìâ#includeiostreamusingnamespacestd;intmain(){intupper=0,lower=0,digit=0,space=0,other=0,i=0;char*p,s[20];coutinputstring:;while((s[i]=getchar())!='\n')i++;p=&s[0];while(*p!='\n'){if(('A'=*p)&&(*p='Z'))++upper;elseif(('a'=*p)&&(*p='z'))++lower;elseif(*p=='')++space;elseif((*p='9')&&(*p='0'))++digit;else++other;p++;}coutuppercase:upperendllowercase:lowerendl;coutspace:spaceendldigit:digitendlother:otherendl;return0;}6.9Ìâ#includeiostreamusingnamespacestd;intmain(){voidmove(int*);inta[3][3],*p,i;coutinputmatrix:endl;for(i=0;i3;i++)cina[i][0]a[i][1]a[i][2];p=&a[0][0];move(p);coutNow,matrix:endl;for(i=0;i3;i++)couta[i][0]a[i][1]a[i][2]endl;coutendl;return0;}voidmove(int*pointer){inti,j,t;for(i=0;i3;i++)for(j=i;j3;j++){t=*(pointer+3*i+j);*(pointer+3*i+j)=*(pointer+3*j+i);*(pointer+3*j+i)=t;}}6.10Ìâ#includeiostreamusingnamespacestd;intmain(){voidchange(int*p);inta[5][5],*p,i,j;coutinputmatrix:endl;for(i=0;i5;i++)//ÊäÈë¾ØÕófor(j=0;j5;j++)cina[i][j];p=&a[0][0];//ʹpÖ¸Ïò0ÐÐ0ÁÐÔªËØchange(p);//µ÷Óú¯Êý£¬ÊµÏÖ½»»»coutNow,
本文标题:C++程序设计第三版(谭浩强)第六章习题答案
链接地址:https://www.777doc.com/doc-4495890 .html