您好,欢迎访问三七文档
改正错误(每题有几处错误,在错误处画线,并写出正确语句。)1.函数fun用以求某正整数的阶乘值。#includeiostream.hvoidfun(n){inti;doublef=0;if(n0){coutDataError!;return-1;}for(i=1;in;i++)f=f*i;returnf;}voidmain(){intn;coutInputn:;cinn;coutn!=fun(n)endl;}2.将一维数组按升序排列。#includeiostream.hvoidsort(intp,intn){inti,j,temp;for(i=1;in-1;i++)for(j=1;jn;j++)if(p[i]p[j]){temp=p[i];p[i]=p[j];p[j]=temp;}}voidmain(void){inta1[6]={1,3,2,5,4,6},*pi,i;pi=a1[0];sort(pi,6);for(i=0;i6;i++)couta1[i]'\t';coutendl;}3.#includeiostream.hclassone{inta1,a2;public:voidinitial(intx1,intx2){a1=x1;a2=x2;}};voidmain(){onedata(2,3);coutdata.a1endl;coutdata.a2endl;}4.#includeiostream.h#includeconio.hclassTpoint{doublex,y;Tpoint(doublea,doubleb){x=a;y=b;}frienddoubledistance(Tpointa,Tpointb);};doubleTpoint::distance(Tpointa,Tpointb){doubledist;dist=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));returndist;}voidmain(){Tpointp1(2,2),p2(5,5);cout上述两点之间的距离:p1.distance(p1,p2)endl;getch();}答案:1.函数fun用以求某正整数的阶乘值。#includeiostream.hdoublefun(intn){inti;doublef=1.0;if(n0){coutDataError!;return-1;}for(i=1;i=n;i++)f=f*i;returnf;}voidmain(){intn;coutInputn:;cinn;coutn!=fun(n)endl;}2.将一维数组按升序排列。#includeiostream.hvoidsort(intp[],intn){inti,j,temp;for(i=1;in-1;i++)for(j=i;jn;j++)if(p[i]p[j]){temp=p[i];p[i]=p[j];p[j]=temp;}}voidmain(void){inta1[6]={1,3,2,5,4,6},*pi,i;pi=a1;sort(pi,6);for(i=0;i6;i++)couta1[i]'\t';coutendl;}3.#includeiostream.hclassone{public:inta1,a2;one(intx1,intx2){a1=x1;a2=x2;}};voidmain(){onedata(2,3);coutdata.a1endl;coutdata.a2endl;}4.#includeiostream.h#includeconio.h#includemath.hclassTpoint{doublex,y;public:Tpoint(doublea,doubleb){x=a;y=b;}frienddoubledistance(Tpointa,Tpointb);};doubledistance(Tpointa,Tpointb){doubledist;dist=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));returndist;}voidmain(){Tpointp1(2,2),p2(5,5);cout上述两点之间的距离:distance(p1,p2)endl;getch();}1.下列程序求字符串长度。#includeiostream.hintfun(chars){inti=0;while(s[i]!='\n'){i++;}returni;}voidmain(){chars[80];cin.getline(s,80);//读一行字符coutlength:fun(s)endl;}2.下列程序功能是求x的y次方(缺省是x的2次方)#includeiostream.hdoublefun(doublex,doubley){inti;doubles=1;for(i=1;iy;i++)s=s*x;returns;}voidmain(){doublex=2.5,y=3;coutpow(2.5,2):fun(x)endl;coutpow(2.5,3):fun(x,y)endl;}3.#includeiostream.hclassPoint{private:intx,y;voidinit(inta,intb){x=a;y=b;}voidshow(){coutx=xy=yendl;}};voidmain(){Pointa(24,50);a.show();}4.#includeiostream.hclassAmplifier{floatinvol,outvol;public:Amplifier(floatvin,floatvout){invol=vin;outvol=vout;}floatgain();};Amplifier::floatgain(){returnoutvol/invol;}voidmain(){Amplifieramp(5.0,10.0);cout\n\nThegainis=gain()endl;}答案:1.下列程序求字符串长度。#includeiostream.hintfun(chars)//*sors[]{inti=0;while(s[i]!='\n')//\0{i++;}returni;}voidmain(){chars[80];cin.getline(s,80);//读一行字符coutlength:fun(s)endl;}2.下列程序功能是求x的y次方(缺省是x的2次方)#includeiostream.hdoublefun(doublex,doubley)//fun(doublex,doubley=2){inti;doubles=1;for(i=1;iy;i++)//(i=0;iy;i++)or(i=1;i=y;i++)s=s*x;returns;}voidmain(){doublex=2.5,y=3;coutpow(2.5,2):fun(x)endl;coutpow(2.5,3):fun(x,y)endl;}3.#includeiostream.hclassPoint{private:intx,y;public://插入voidinit(inta,intb)//Point{x=a;y=b;}voidshow(){coutx=xy=yendl;}};voidmain(){Pointa(24,50);a.show();}4.#includeiostream.hclassAmplifier{floatinvol,outvol;public:Amplifier(floatvin,floatvout){invol=vin;outvol=vout;}floatgain();};Amplifier::floatgain()//floatAmplifier::gain(){returnoutvol/invol;}voidmain(){Amplifieramp(5.0,10.0);cout\n\nThegainis=gain()endl;//amp.gain()}
本文标题:c++复习改错题
链接地址:https://www.777doc.com/doc-6945964 .html