钱能--c++程序设计教程(第二版)第五章

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

可以在VS2010上运行,供正在学习C++的同学参考。我也是新手,可能在编程风格和思路上有待提高,也有一些题目不知道该如何下手,路过的朋友知道的可以告诉我,我的邮箱是guosheng-14@163.com,可以督促我继续写以后的答案。vc++6.0可能在头文件上有所区别。第五章第一部分//例5-2----寻找向量#includeiostream#includefstream#includesstream#includestring#includevectorusingnamespacestd;typedefvectorintmatrix1;typedefvectormatrix1matrix2;voidinput(matrix2&v2);voidfind(matrix2&v2);voidoutput(matrix1&v1);intmain(){matrix2v2;input(v2);find(v2);}voidinput(matrix2&v2){ifstreamin(aa.txt);intn,t;inn;v2.resize(n);for(strings;getline(in,s)&&(n0);n--)for(istringstreamsin(s);sint;v2[n].push_back(t));}voidoutput(matrix1&v1){for(inti=0;iv1.size();i++)coutv1[i];coutendl;}voidfind(matrix2&v2){for(inti=0;iv2.size();i++){for(intj=0;jv2[i].size();j++)if(v2[i][j]==-1)output(v2[i]);}}//5.2.2—函数副作用#includeiostream#includevectorusingnamespacestd;vectorintadd(vectorint&a,vectorint&b);voidprint(vectorint&a);intmain(){intaa[]={2,3,1,2,3,2,1},bb[]={5,3,1,1,6,2,2};vectorinta(aa,aa+7);vectorintb(bb,bb+7);vectorintc;c=add(a,b);print(a);print(b);print(c);}voidprint(vectorint&a){for(inti=0;ia.size();i++)couta[i];coutendl;}vectorintadd(vectorint&a,vectorint&b){for(inti=0;ia.size();i++)a[i]+=b[i];returna;}//5.4.2—函数指针传递#includeiostream#includevector#includealgorithmusingnamespacestd;intbitsum(inta);boolbitsumcomp(inta,intb){returnbitsum(a)bitsum(b);}intmain(){inta[]={33,41,12,19,14,71,78,59};vectorintv(a,a+8);sort(v.begin(),v.end(),bitsumcomp);for(inti=0;iv.size();i++)coutv[i];coutendl;}intbitsum(inta){intx=0;x=a%10+a/10;returnx;}//5.4.3—函数指针数组#includeiostreamusingnamespacestd;typedefvoid(*Menufunction)();voidf1(){coutgood;}voidf2(){coutbetter;}voidf3(){coutbest;}intmain(){Menufunctionm[]={f1,f2,f3};cout1---playgoodendl2---playbetterendl3---playbestendl0---quiteendl;coutyourchoise?endl;for(intchoise=1;choise;){cinchoise;switch(choise){case1:m[0]();break;case2:m[1]();break;case3:m[2]();break;}}}//5.7.1—函数重载示例#includeiostreamusingnamespacestd;intmyabs(inta)///写成myabs,否则写成abs编译不通过{return(a0)?a:-a;}doublemyabs(doublea){return(a0)?a:-a;}intmain(){coutmyabs(-10)endl;coutmyabs(-12.23)endl;}//5.7.4—默认参数#includeiostreamusingnamespacestd;voiddelay(inta=2);intmain(){coutdelay2sendl;delay();coutdelay5sendl;delay(5);}voiddelay(inta){intsum=0;for(inti=1;i=a;i++)for(intj=1;j3500;++j)for(intk=1;k100000;++k)sum++;}//5.7.7#includeiostream#includevectorusingnamespacestd;vectorintb(10,0);boolprocess(vectorint&a);voidprint(vectorint&a=b);intmain(){vectorinta(10,5);if(process(a))print(a);elseprint();}boolprocess(vectorint&a){intsum=0;for(inti=0;ia.size();i++)sum+=a[i];if(sum100)return1;elsereturn0;}voidprint(vectorint&a){for(inti=0;ia.size();i++)couta[i];coutendl;}第二部分//1—递归#includeiostreamusingnamespacestd;doublepoly(int,double);intmain(){coutpleaseinputadouble:endl;doublex;intn;cinx;coutpleaseinputaint:endl;cinn;coutpoly(n,x)endl;}doublepoly(intn,doublex){if(n==0)return1;elseif(n==1)returnx;elseif(n1)return(((2*n-1)*x*poly(n-1,x)-(n-1)*poly(n-2,x))/n);}//2—改写#includeiostreamusingnamespacestd;voidinput(inta[][5]);inttotal(inta[][5]);doubleever(inta[][5],int);intmain(){inta[4][5];input(a);couttotal:total(a)endl;coutever:ever(a,20)endl;}voidinput(inta[][5]){coutpleaseinput20numbers:endl;for(inti=0;i4;i++)for(intj=0;j5;j++)cina[i][j];}inttotal(inta[][5]){intsum=0;for(inti=0;i4;i++)for(intj=0;j5;j++)sum+=a[i][j];returnsum;}doubleever(inta[][5],intx){return(total(a)/double(x));}//3—文本最大公约数#includeiostream#includefstream#includevector#includesstream#includestring#includealgorithmusingnamespacestd;intgongyue(inta,intb);voidprint(vectorintv);intmain(){ifstreamin(abc.txt);vectorintv;for(strings;getline(in,s);){inta,b;istringstreamsin(s);sinab;v.push_back(gongyue(a,b));}print(v);sort(v.begin(),v.end());print(v);}intgongyue(inta,intb){intitem=((ab)?b:a);for(inti=item;i=1;i--){if(a%i==0&&b%i==0)returni;}}voidprint(vectorintv){for(inti=0;iv.size();i++)coutv[i];coutendl;}4.又是这个题。。。真心不会//5—编程实现#includeiostream#includevector#includefstream#includesstream#includealgorithmusingnamespacestd;voidprint(vectorint&a);intsqresum(inta);boolspecthan(inta,intb){returnsqresum(a)sqresum(b);}intmain(){ifstreamin(abc.txt);strings;intx;getline(in,s);vectorintv;for(istringstreamsin(s);sinx;)v.push_back(x);for(inti=0;iv.size();i++)coutv[i];coutendl;sort(v.begin(),v.end(),specthan);print(v);}intsqresum(inta){intth,hr,te,si;th=a/1000;hr=(a-th*1000)/100;te=(a-th*1000-hr*100)/10;si=a%10;returnth*th+hr*hr+te*te+si*si;}voidprint(vectorint&a){for(inti=0;ia.size();i++)couta[i];coutendl;}//6—字符串排序#includeiostream#includefstream#includealgorithm#includevector#includesstreamusingnamespacestd;intmain(){ifstreamin(abc.txt);for(strings;getline(in,s);){vectorcharch;strings1;for(istringstreamsin(s);sins1;){for(inti=0;is1.size();i++)ch.push_back(s1[i]);}coutbeforeorder:

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功