好文档 - 专业文书写作范文服务资料分享网站

历届自考C++ 程序设计试题及答案 - 图文

天下 分享 时间: 加入收藏 我要投稿 点赞

cout<

float cha(float x,float y) { float w; w=x-y; return w; }

五、程序分析题(本大题共4小题,每小题5分,共20分) 51.给出下面程序的输出结果。 #include template class Sample { T n; public: Sample(T i){n=i;}

int operator==(Sample &); };

template

int Sample::operator==(Sample&s) { if(n==s.n) return 1; else return 0; } void main( ) {

Samplesl(2),s2(3); .

cout<<″s1与s2的数据成员″<<(sl==s2 ?″相等″:″不相等″)<s3(2.5),s4(2.5);

cout<<″s3与s4的数据成员″<<(s3==s4 ? ″相等″:″不相等″)<

S3与S4数据成员相等 52.给出下面程序的输出结果。

#include using namespace std; template T max(T ml,T m2)

46

{return(m1>m2)?ml:m2:} void main( ) {

cout<

cout< using namespace std; class A { public: int x; A( ) { } A(int a){x=a;}

int get(int a){return x+a;} }; void main( ) { A a(8); int(A::*p)(int); p=A::get;

cout<<(a.*p)(5)<

cout<<(pi- >*P)(7)<

47

54.给出下面程序的输出结果。 include #include using namespace std; class Book { char*title; char*author; int numsold; public: Book( ){ }

Book(const char*strl,const char*str2,const int num) { int len=strlen(strl); title=new char[len+1]; strcpy(title,str1); len=strlen(str2); author=new char[len+1]; strcpy(author,str2); numsold=num; }

void setbook(const char*str1,const char*str2,const int num) { int len=strlen(str1); title=new char[len+1]; strcpy(title,str1); len=strlen(str2); author=new char[len+1]; strcpy(author,str2); numsold=num; }

~Book( ) { delete title; delete author; }

void print(ostream&output) { output<<″书名:″<

void main( ) {

Book obj1(″数据结构″,″严蔚敏″,200),obj2;

48

书名:数据结构 作者:严蔚敏 月销售量:200 书名:C++程序设计 作业:李春葆 月销售量:210 obj1.print(cout);

obj2.setbook(″C++语言程序设计″,″李春葆″,210); obj2.print(cout); }

六、程序设计题(本大题共1小题,共10分)

55.在三角形类TRI实现两个函数,功能是输入三个顶点坐标判断是否构成三角形 #include #include class point{

private:float x,y;

public:point(float a,float b){x=a;y=b;} point( ){x=0;y=0;} , void set(float a,float b){x=a;y=b;} float getx( ){return x;} float gety( ){return y;} }; class tri{ point x,y,z; float s1,s2,s3;

public;...settri(....);//用于输入三个顶点坐标 ....test(....);//用于判断是否构成三角形

}; 请写出两个函数的过程(如果需要形式参数,请给出形参类型和数量,以及返回值

类型)

判断三条边能构成三角形的条件:任意两边之和大于第三边或任意两边之差小于第3边。 void tri::settri(float x1,float y1,float x2,float y2,float x3,float y3) {x.set(x1,y1); y.set(x2,y2); z.set(x3,y3); }

void tri::test(){ s1=sqrt((x.getx()-y.getx())*(x.getx()-y.getx())+(x.gety()-y.gety())*(x.gety()-y.gety())); s2=sqrt((x.getx()-z.getx())*(x.getx()-z.getx())+(x.gety()-z.gety())*(x.gety()-z.gety())); s3=sqrt((y.getx()-z.getx())*(y.getx()-z.getx())+(y.gety()-z.gety())*(y.gety()-z.gety()));

49

3z2q69a9qf3qhtz4wkb2
领取福利

微信扫码领取福利

微信扫码分享