




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
《C++》編程題試卷
第三章
1.編寫一個求方程ax2+bx+c=0的根的程序,用3個函數(shù)分別求當b2-4ac大于
零、等于零、和小于零時的方程的根。要求從主函數(shù)輸入a,b,c的值并輸出結果。
Sinclude<iostream,h>
ttinclude<math,h>
voidequation_l(inta,intb,intc)
(
doublexl,x2,temp;
temp=b*b-4*a*c;
xl=(-b+sqrt(temp))/(2*a*1.0);
x2=(-b-sqrt(temp))/(2*a*1.0);
cout*〃兩個不相等的實根〃。endl;
cout<<z/xl=〃《<xl<<z,,x2=〃<<x2<<endl;
)..................................
voidequation_2(inta,intb,intc)
(
doublexl,x2,temp;
temp=b*b-4*a*c;
xl=("b+sqrt(temp))/(2*a*1.0);
x2-xl'
cout<<〃的個相等的實根〃<<endl;
cout<</zxl=〃<<xl<<,z,x2=z,<<x2<<endl;
voidequation_3(inta,intb,intc)
(
doubletemp,reall,real2,image1,image2;
temp=-(b*b-4*a*c);
reall二-b/(2*a*L0);
real2=reall;
imagel=sqrt(temp);
image2=一imagel;
cout<<〃兩個虛根〃Gendl;
cout<<zzxl=,z<<reall?,z+,z?imagel<</zj,z<<endl;
cout?,zx2=〃<<real2?/z+〃<<image2?,,j,,<<endl;
voidmainO
inta,b,c;
doubletemp;
cout<〈〃輸入a,b,c的值〃<<endl;
cin>>a>>b>>c;
cout<X”方程為:〃《a<<〃x*x+〃<<b?z,x+,z?c?z,0,z<<endl;
temp=b*b-4*a*c;
if(temp>0)
equation_l(a,b,c);
if(temp==0)
equation_2(a,b,c);
if(temp<0)
equation_3(a,b,c);
2.定義函數(shù)up(ch),如字符變量ch是小寫字母就轉換成大寫字母并通過up返回,否
則字符ch不改變。要求在短小而完全的程序中顯示這個程序是怎樣被調用的。
ttinclude<iostream>
usingnamespacestd;
charup(charc)
if(c>=97&&c<=122)
return(c-32);
else
returnc;
)
voidmainO
(
inti;
charc[15]=
{'A'v'e't'E'T4'Y'e'i'9',l'};
for(i=0;i<15;i++)
cout?up(c[i])<<,/,/z;
cout<<endl;
)
3.編寫主程序條用帶實數(shù)r和整數(shù)n兩個參數(shù)的函數(shù)并輸出r的n次第。
Sinclude<iostream,h>
ttinclude<math,h>
doublepower(doublea,intb)
(
inti;
doubleresult=1.0;
for(i=0;i<b;i++)
result=result*a;
returnresult;
}..
voidmainO
(
doubler;
intn;
cout<<z/r=〃;
cin>>r;
cout<<z/n=〃;
cin>>n;
cout<<r<<〃的〃?n<<〃次幕是:〃<<power(r,n)<<endl;}
4.編寫有字符型參數(shù)C和整形參數(shù)N的函數(shù),讓他們顯示出由字符C組成的三角形。
其方式為第1行有1個字符C,第2行有2個字符C,等等。
ttinclude<iostream>
usingnamespacestd;
voidprint_triangle(charc,intn)
inti,j;
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
{
cout?c;
)
cout?endl;
voidmainO
{一
print_triangle(?a',10);
}一
5.編寫一個ieqiu字符串長度的函數(shù),strlen(),再用strlen()函數(shù)編寫一個
函數(shù)revers(s)的倒序遞歸程序,使字符串s逆序。
ttinclude<iostream>
#include<string>
usingnamespacestd;
intstrlen(char*str)
(
intlen=0;
while(str[len]!='\0')
(
len++;
)
returnlen;
}.
voidrevers(char*b)
(
charc;
intj,len;
len=strlen(b);
j=len/2-l;
while(j>=0)
(
c=*(b+j);
*(b+j)=*(b+len-j-1);
*(b+len-j-l)=c;
j—;
}
b[len]八O';
)
voidmain()
(
charstr口二{"1234567890〃};
cout<<str?z,----的長度:“《strlen(str)?endl;
cout<<str<<endl;〃倒序前
revers(str);//
cout<<str<<endl;〃倒序后
)
6.用函數(shù)模板實現(xiàn)3個數(shù)值中按最小值到最大值排序的程序。
#include<iostream>
usingnamespacestd;
template
voidsort(Ta,Tb,Tc)
(
Tarray[3],temp;
inti,j;
array[0]=a;
array[1]=b;
array[2]=c;
for(i=0;i<3;i++)
(
for(j=0;j<2;j++)
if(array[j]>array[j+1])
(
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
)
)
cout<<array[0]<<array[1]<<array[2]<<endl;
voidmain()
(
sort(5,1,9);
)
7.利用函數(shù)模板設計一個求數(shù)組元素中和的函數(shù),并檢驗之。
ttinclude<iostream>
usingnamespacestd;
template
Tsum(Ta[],intn)
(
inti;
Ts=0;
for(i=0;i<n;i++)
s=s+a[i];
returns;
)
voidmain()
(
inta[5]={l,2,3,4,5};
ints=sum(a,5);
cout<<s?endl;
)
8.重載上題中的函數(shù)模板,使他能夠進行兩個數(shù)組的求和。
ttinclude<iostream>
usingnamespacestd;
template
Tsum(Ta[],intn)
(
inti;
Ts=0;
for(i=0;i<n;i++)
s=s+a[i];
returns;
template〃重載上面的模板
Tsum(Ta[],intn,Tb[],intm)
returnsum(a,n)+sum(b,m);
main()
inta[5]={l,2,3,4,5};
intb[10>{l,2,3,4,5,6,7,8,9,10};
intsi=sum(a,5);
ints2=sum(b,10);
ints3=sum(a,5,b,10);
cout<<sl<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
)
第四章
1.設計一個點類Point,再設計一個矩形類,矩形類使用Point類的兩個坐標點作為
矩形的對角頂點。并可以輸出4個坐標值和面積。使用測試程序驗證程序。
#include
usingnamespacestd;
classPoint〃點類
(
private:
intx,y;〃私有成員變量,坐標
public:
Point()//無參數(shù)的構造方法,對xy初始化
{
x=0;
y=0;
)
Point(inta,intb){
x=a;
y=b;
)
voidsetXY(inta,intb){
x=a;
y=b;
}
intgetX()〃得到x的方法
(
returnx;
)
intgetY()〃得到有的函數(shù)
returny;
);
classRectangle〃矩形類
private:
Pointpointl,point2,point3,point4;
public:
Rectangle();〃類Point的無參構造函數(shù)已經對每個對象
做初始化啦,這里不用對每個點多初始化了
Rectangle(Pointone,Pointtwo)
pointl=one;
point4=two;
init();
Rectangle(intxl,intyl,intx2,inty2)
pointl.setXY(xl,yl);
point4.setXY(x2,y2);
init();
voidinit()〃給另外兩個點做初始化的函數(shù)
point2.setXY(point4.getX(),
pointl.getY());
point3.setXY(pointl.getX(),
point4.getY());
voidprintPoint()〃打印四個點的函數(shù)
cout?z,A:(〃<Xpointl.getX()pointl.getY()<<〃)〃"endl;
cout?/zB:(,z?point2.getX()?,z,〃<<point2.getY()endl;
cout?z,C:(〃<<point3.getX()<<〃,〃<<point3.getY()?z,)zz?endl;
cout<<“D:(〃<<point4.getX()<<",〃<<point4.getY()<<〃)〃<<endl;
)
intgetArea()〃計算面積的函數(shù)
intheight,width,area;
height=pointl.getY()-point3.getY();
width=pointl.getX()-point2.getX();
area=height*width;
if(area>6)
returnarea;
else
return-area;
);
voidmain()
Pointpl(-15,56),p2(89,TO);〃定義兩個點
Rectanglerl(pl,p2);〃用兩個點做參數(shù),聲明一個矩形對象rl
Rectangler2(l,5,5,1);〃用兩隊左邊,聲明一個矩形對象r2
cout<<“矩形rl的4個定點坐標:〃<<endl;
rl.printPoint();
cout<<”矩形rl的面積:〃<<rl.getArea()?endl;
cout?"\n矩形r2的4個定點坐標:〃<<endl;
r2.printPoint();
cout<<〃矩形r2的面積:〃<<r2.getArea()?endl;
)
2.使用內聯(lián)函數(shù)設計一個類,用來表示直角坐標系中的任意一條直線并輸出它的屬
性。
ttinclude<iostream,h>
#include<math,h>
classLine
(
private:
intxl,yl,x2,y2;
public:
LineO;
Line(int=0,int=0,int=0,int=0);
voidprintPoint();
doublegetLength();
inlineLine::Line(inta,intb,intc,intd)
(
xl=a;
yl=b;
x2=c;
y2=d;
)
inlinevoidLine::printPoint()
(〃〃〃
cout<<"A:〃<<xl<<,z,〃<<yl<<endl;
cout<<,,B:〃<<x2<</?,〃<<y2<<endl;
).
inlinedoubleLine::getLength()
(
doublelength;
length=sqrt((x2-x1)*(x2-x1)+(y2-yl)*(y2-yl));
returnlength;
}..
voidmainO
(
Lineline(10,80,-10,12);
line.printPoint();
cout<<line.getLength()<<endl;
)
第五章
1.聲明復數(shù)的類,complex,使用友元函數(shù)add實現(xiàn)復數(shù)加法。
#include<iostream>
usingnamespacestd;
classComplex
private:
doublereal,image;
public:
Complex(){}
Complex(doublea,doubleb)
{
real=a;
image=b;
)
voidsetRI(doublea,doubleb)
(
real=a;
image=b;
)
doublegetReal()
{returnreal;
)
doublegetlmageO
{returnimage;
}
voidprint(){
if(image>0)
cout<<〃復數(shù):〃?real<<〃+image<X〃i〃<<endl;
if(image<0)
cout<〈"復數(shù):real<<z,-“<<image<</,i,,<<endl;
)
friendComplexadd(Complex,Complex);〃聲明友兀函數(shù)
);
Complexadd(Complexcl,Complexc2)〃定義友元函數(shù)
(
Complexc3;
c3.real=cl.real+c2.real;〃訪問Complex類中的私有成員
c3.image=cl.image+c2.image;
returnc3;
}..
voidmainO
(
Complexcl(19,0.864),c2,c3;
c2.setRI(90,125.012);
c3=add(cl,c2);
cout<<〃復數(shù)一:〃;cl.print();
cout<<〃復數(shù)二:〃;c2.print();
cout<<〃相力口后:〃;c3.print();
)
3,編寫一個程序,該程序建立一個動態(tài)數(shù)組,為動態(tài)數(shù)組的元素賦值,顯示動態(tài)數(shù)
組的值并刪除動態(tài)數(shù)組。
#include<iostream>
usingnamespacestd;
voidmainO
inti,n,temp=0;
cout<<"輸入數(shù)組大?。骸保?/p>
cin>>n;
double*array=newdouble[n];〃用指針,動態(tài)申請數(shù)組大小
coutC〈”給每個數(shù)組元素賦值:"<<endl;
for(i=0;i<n;i++)
(
cout?//arrayE//?i<<"]=
cin?temp;
*(array+i)=temp;〃給數(shù)組元素賦值
)
cout?”動態(tài)數(shù)組個元素的值如下:endl;
for(i=0;i<n;i++)
{,
cout?//array[,/?i<<"]="<<array[i]<<endl;〃打印數(shù)組
元素
)
delete[]array;〃釋放內存
)
4.定義一個Dog類,它用靜態(tài)數(shù)據成員Dogs記錄Dog的個體數(shù)目,靜態(tài)成員函數(shù)
GetDogs用來存取Dogs。段訐并謝成這個類。
#include<iostream>
usingnamespacestd;
classDog
(
private:
staticintdogs;〃靜態(tài)數(shù)據成員,記錄Dog的個體數(shù)目
public:
Dog(){}
voidsetDogs(inta)
{dogs=a;
}
staticintgetDogs(){
returndogs;
)
);
intDog::dogs=25;〃初始化靜態(tài)數(shù)據成員
voidmain0
{..........................
cout?"未定義Dog類對象之前:x=〃<<Dog::getDogs()?endl;;//x
在產生對象之前即存在,輸出25
Doga,b;
cout?"a中x:/z?a.getDogs()<<endl;
cout?"b申x:/z?b.getDogs()<<endl;
kcoqupttD?o〃ers晝(R對6象0)卦,的x設置值后:〃<<endl;
cout?〃a中x:,z?a.getDogs()<<endl;
cout<X〃b申x:〃<<b.getDogs()<<endl;
)
第六章
1.設計一個基類,從基類派生圓柱,設計成員函數(shù)輸出它們的面積和體積;
#include<iostream>
usingnamespacestd;
classBasic〃基類
(
protected:
doubler;
public:
Basic(){r=0;}
Basic(doublea):r(a){}
);
classCircular:publicBasic〃從基類派生圓類
(
protected:
doublearea;
public:
Circular(doublea)
{
r=a;
area=area=3.1415926*r*r;
)
doublegetArea()〃返回圓面積
{
returnarea;
)
);
classColumn:publicCircular〃從圓類派生圓柱類
(
protected:
doubleh;
doublecubage;
public:
Column(doublea,doubleb):Circular(a){
h=b;
cubage=getArea()*h;
.}
doublegetCubage()〃返向圓柱體積函數(shù){
returncubage;
)
};,
voidmainO
(
Circularcircular(45);
Columncolumn(12,10);
cout<<〃圓的面積:〃<<circular.getAreaO<<endl;
cout<<〃圓柱的體積column.getCubage()<<endl;
)
3.定義一個線段類作為矩形的基類,基類有起點和終點坐標,有輸出左邊和長度以
及線段和x軸的夾角的成員函數(shù)。矩線段對象的兩個坐標作為自己一條邊的位置,
它具有另外一條邊,能輸出矩形的4個頂點坐標。給出類的定義并用程序驗證它們
的功能。
ttinclude<iostream>
ttinclude<cmath>
usingnamespacestd;
classPoint〃點類
protected:
doublex,y;
public:
Point(){)
Point(doublea,doubleb)
{
x=a;y=b;
)
doublegetX()
{returnx;}
doublegetY()
{returny;}
);
classLine
(
DKOtPCtpd'
Pointpl,p2;“Point對象做成員
doublelength,angle;
public:
Line(doublea,doubleb,doublec,doubled):pl(a,b),p2(c,d)〃用兩
對坐標初始化線段
(
init();
)
Line(Pointa,Pointb)〃用兩個點的對象初始化線段
(
pl=a;p2=b;
init();
)
voidinit()〃計算線段長度,以及和x軸的夾角的度數(shù)
(
doublexl=pl.getX(),yl=pl.getY();
doublex2=p2.getX(),y2=p2.getY();
length=sqrt((xl-x2)*(xl-x2)+(yl-y2)*(yl-y2));
angle=atan((y2-yl)/(x2-xl));
angle=angle*180/3.141592653;
)
voidprintXY()
{,—
cout<<//(/z<<pl.getX()pl.getY()<<");("〈〈p2.getX()
p2.getYO<<")"<〈endl;
)
voidprintLength()
(
cout<〈”線段長度:"<Xlength?endl;
)
voidprintAngle()
{,
cout?”與x軸的夾角:"<<angle?///z?endl;
)
);
classRectangle:publicLine
protected:
Line*line;
public:
Rectangle(doublea,doubleb,doublec,doubled,doublee,double
f,doubleg,doubleh):Line(a,b,c,d)
{.
line=newLine(e,f,g,h);
)
Rectangle(Pointa,Pointb,Pointc,Pointcl):Line(a,b)〃4個點
對象,初始化
{.
line=newLine(c,d);
)
voidprintPoint()
{cout<<〃矩形4個頂點:\n〃;
printXY();
line->printXY();
)
);
voidmain()
(
Pointpl(0,0),p2(4,3),p3(12,89),p4(10,-50);
Line11(0,0,4,3);
11.printXY();
11.printLengthO;
11.printAngle();
Line12(pl,p2);
12.printXY();
12.printLengthO;
12.printAngle();
Rectanglerl(12,45,89,10,10,23,56,1);
rl.printPoint();
Rectangler2(pl,p2,p3,p4);
r2.printPoint();
4.基類是使用極坐標的點類,從它派生一個圓類,圓類用點類的左邊作圓心,圓周
通過極坐標原點,圓類有輸出圓心直、圓半徑和面積的成員函數(shù)。完成類的設計并
驗證之。
ttinclude<iostream>
#include<cmath>
usingnamespacestd;
classPoint〃點類
(
protected:intx,y;
public:Point(){}
);
classCircular:publicPoint〃圓類,繼承點類
protected:
doubler,area;
public:
Circular(inta,intb)
(
x=a;
y=b;
r=sqrt(x*x+y*y);
area=3.1415926*r*r;
)
voidprintPoint()
(
cout<<〃圓形直角坐標:C?x<<〃,〃<<y<<〃)〃<<endl;
}
voidprintRadius()
{cout<〈〃圓的半徑:〃<<r?endl;
}
voidprintAreaO{
cout<<"圓的面積:〃<<area<<endl;
)
};..
voidmainO
(
Circularc(10,25);
c.printPoint();
c.printRadius();
c.printAreaO;
5.設計一個線段基類,當創(chuàng)建五參數(shù)對象時,才要求用戶輸入長度。同樣,其派生
的直角三角形類也是在產生對象時要求輸入兩個直角邊的長度。直角三角形在派生
矩形類,矩形類的參數(shù)也由鍵盤輸入。設計這些類并測試他們的功能。
ttinclude<iostream>
ttinclude<cmath>
usingnamespacestd;
classLine/7線段基類
(
protected:
doublesizeA;
public:
Line()
{
cout<<〃輸入線段的長度:〃<<endl;
cin>>sizeA;
)
Line(doublea)
{.
sizeA=a;
)
doublegetLength()
(
returnsizeA;
);
classTriangle:publicLine//三角形類
{
protected:doublesizeB,sizeC;
public:TriangleO
(
cout<<〃輸入線段長度:〃<<endl;
cin>>sizeB;
sizeC=sqrt(sizeB*sizeB+sizeA*sizeA);
)
voidprintSize()
〃.{cout*〃直角三角形,三條邊分別為:
cout<X〃A:〃<<sizeA<<〃,b:〃<<sizeB<<〃,C:〃<<sizeC<<endl;
)
);
classRectangle:publicTriangle//矩形類
(
protected:
doublesizeD;
public:
Rectangle()
(
sizeC=sizeA;
sizeD=sizeB;
)
voidprintSize()
(
cout<<〃矩形,四條邊分別為:〃;
cout<<,zA:z,<<sizeA<<〃,b:zz<<sizeB?",C:"<<sizeC<<",D:〃<<sizeD
?endl;
);
voidmain()
(
/*Line*1=newLineO;
cout<<〃線段長度為:〃Gl->getLength()<<endl;
*/
ATriangle*t=newTriangle();
t->printSize();*/
Rectangle=newRectangle();
r->printSize();
)
第七章
1.使用類模板演示復制兼容性規(guī)則。
#include<iostream>
usingnamespacestd;
template
classPoint
(
protected:
Tx,y;
public:
Point(Ta,Tb)
(
x=a;
y=b;
voidshow()
\zXxz\z〃y-〃exl
cout<<〃x=},<<nd
);
template
classRectangle:publicPoint
(
private:
Th,w;
public:
Rectangle(Ta,Tb,Tc,Td):Point(a,b)
{h=c;
w=d;
}
voidshow(){
cout<<z/x=z,<<x<<",y=〃<<y?,z;h=,z<<h<<",w=,z?w<<endl;
)
);
voidmain()
(
Pointa(3,4);
Rectangleb(5.1,6.2,7.3,8.4);
a.show();
b.show();
Point&ra=b;〃子類對象初始化父類的引用
ra.show();
Point*p=&b;〃子類對象的地址,賦給指向父類的指針
p->show();
Rectangle*pb=&b;〃子類指針pb
pb->show();
a=b;〃派生類對象的屬性值,更新父類對象
的屬性值
a.show();
2.設計一個點的類模板,分別使用繼承、包含的方法設計線段類模板,要求演示構
造函數(shù)和復制構造函數(shù)的設計方法,并用主程序驗證之。
ftinclude<iostream>
usingnamespacestd;
templateclassPoint
(
public:
Tx,y;
Point(Ta=0,Tb=0)
(
x=a;
y=b;
}
voidshow()
{.....................
cout<<z/x="<<x<<z,,y="<Xy?endl;
)
);
templateclassLine1:publicPoint//繼承Point類模板,的線段類模板
(-
protected:
Txl,yl;
public:
Line_l(Ta,Tb,Tc,Td):Point(a,b)
(
xl=c;
yl=d;
)...........
Line1(constLine」&);〃復制構造函數(shù)
voidshow()
(
cout?z/r?x<〈","<<y?");c?xl?",
〃<<yl<<")"<<endl;
}
);
templateLine」::Line1(constLine1&t):Point(t.x,t.y)
{"
xl=t.xl;
yl=t.yl;
templateclassLine2〃包含point類模板,的線段類
{一
protected:
Pointpl,p2;
public:
Line_2(Ta,Tb,Tc,Td)
(
pl.x=a:
pl.y=b;
p2.x=c;
p2.y=d;
Line2(constLine2&);〃復制構造函數(shù)
voidshow()
{..
cout<<〃(〃<<pl.x<<,z,,z<<pl.y<<〃);(z,<<
p2.x<<,z,〃<<p2.y<<〃)〃<<endl;
)
);
templateLine_2::Line_2(constLine_2&t)
(
pl=t.pl;
p2=t.p2;
}..
voidmainO
(
Line」LI(1,2,3,4);
cout<<,,Ll:〃;L1.show();
Line」L2(LI);〃用現(xiàn)有的對象,初始化新對象
cout<<z,L2:〃;L2.show();
Line_2JI(5,6,7,8);
cout?,zJl:〃;JLshow。;
Line_2J2(JI);
cout<<z/J2:〃;J2.show();
)
3.已知有一個整型數(shù)組a,其內容先對數(shù)組進行升序排列,
再使用它產生向量b,然后再在向量的尾部追加1L并按降序排列輸出向量的內容
和capacity()的內容。
ttinclude<iostream>
ttinclude<vector>
ttinclude<algorithm>
usingnamespacestd;
voidmainO
(
inta[]={1,3,5,7,9,2,4,6,8,10};
sort(a,a+10);〃免對數(shù)組進行升序排序〃〃
copy(a,a+10,ostream_iterator(cout,z/〃));
cout<<endl;
vectorpa(a,a+10);〃再聲明向量
pa.push_back(H);〃向量尾部追加11
reverse_copy(pa.begin(),pa.end(),ostreamiterator(cout,zzz,));//
按降序輸出向量的內容
cout<<z/\ncapacity:z,?pa.capacity()<<endl;〃輸出capacity()的
內容
)
第九章
1.利用流格式控制,進行成績和名字的輸出,要求名字左對齊,分數(shù)右對齊。
#include<iostream>
Sinclude<string>
usingnamespacestd;
classStudent
{
private:
stringname;
floatscore;
public:
Student(){}
Student(stringn,floats)
(
name=n;
score=s;
).
stringgetName()
(
returnname;
}
floatgetScore()
{
returnscore;
)
};..
voidmainO
{,,〃
Studentsi("liming”,98);
Students2(〃sdfh〃,90);
Students3(〃vn.fy〃,80);
Students4(〃cnbtrt〃,70);
Students5(〃ryuety〃,48);
cout.width(15);cout?left<<"姓名"<<right<X"分數(shù)"<<endl;
cout.width(15);cout<<left?si.getName()<<right<<si.getScore()
?endl;
cout.width(15);cout<<left?s2.getName()<<right<<s2.getScore()
?endl;
cout.width(15);cout<<left?s3.getName()<<right<<s3.getScore()
?endl;
cout.width(15);cout<<left?s4.getName()<<right<<s4.getScore()
?endl;
cout.width(15);cout<<left<<s5.getName()<<right<<s5.getScore()
<<endl;
2.編寫一個產生文本文件的程序。
Sinclude<iostream>
#include<fstrearn>
usingnamespacestd;
voidmain()
char*p=『C++程序設計〃};
ofstreammyFile(,zWorl9_5_2.txt〃);
myFile<<p;
)
3.編寫一個程序,要求輸入三角形的3條邊,然后判斷是否合理,如果不合理,給
出信息并要求重新輸入;如果合理,計算其面積并將結果存入文件中。
#include<iostream>
#include<fstream>
ttinclude<cmath>
#include<vector>
#include<iomanip>
#include<string>
usingnamespacestd;
classTriangle
(
doublesizeA,sizeB,sizeC,area;
public:
Triangle(){}
voidsetAreaO
{
doublep=(sizeA+sizeB+sizeC)*0.5;
area=sqrt(p*(p-sizeA)*(p-sizeB)*(p-sizeC));
)
voidsetSizeA(doublea)
(
sizeA=a;
)
voidsetSizeB(doubleb)
(
sizeB=b;
)
voidsetSizeC(doublec)
(
sizeC=c;
)
voidset(vector&);
);
//*成員函數(shù):set
〃*參數(shù):向量對象的引用
//*返回值:無
//*功能:為向量賦值并將向量存入文件
/51c51c55csic51csic51c51c5|c5|c^|c5jc5|c5§c5{c^|c5|c^|c^cz|c^c5JC5JCJJC
voidTriangle::set(vector&v)
(
Trianglet;
doublea,b,c;
while(l)
(
cout義〃三角形,邊A:〃;
cin?a;
if(a=-1)〃結束符為-1
(
ofstreamwriteFile;
charfileName[20];
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 武術拓展活動方案
- 汽車租車活動方案
- 河道綠化活動方案
- 植物調研活動方案
- 四下數(shù)學期末必考簡便運算專項訓練
- 愛國旗主題班會課件
- 智能紡織品在產品設計中的應用與創(chuàng)新
- 農田水利領域高效節(jié)水灌溉技術的實踐與應用探索
- 供應鏈綠色轉型:脫碳作用機制與實現(xiàn)路徑
- 文化遺產數(shù)字化與智慧圖書館建設的研究與探討
- 大學課件-機電傳動控制(完整)
- 廠石墨深加工項目可行性研究報告
- 鋼結構起重機行車軌道安裝工程檢驗批質量驗收記錄表
- Translating China智慧樹知到答案章節(jié)測試2023年湖南工業(yè)大學
- 耳尖放血課件完整版
- 輸尿管結石診療指南
- 基坑開挖專項施工方案
- 2023年安順市公共資源交易服務中心事業(yè)單位工作人員招聘筆試題庫及答案解析
- GB/T 9074.18-2017自攻螺釘和平墊圈組合件
- 變壓器培訓資料
- 斷絕子女關系協(xié)議書模板(5篇)
評論
0/150
提交評論