国家二级C++机试(操作题)模拟试卷659
基本操作题
1.请打开考生文件夹下的解决方案文件proj1,其中有点类Point和线段类Line和主函数main的定义,程序中位于每个“//ERROR *******found*******”之后的一行语句有错误,请加以改正。改正后程序的输出应为:
p1=(8,4) p2=(3,5)
注意:只修改两个“//ERROR *******found*******”下的那一行,不要改动程序中的其他内容。
#include<iostream>
#include<cmath>
using namespace std;
class Point{
double X,y;
public:
Point(double x=0.0,double y=0.0)
//ERROR *******found*******
{x=x,y=y;)
double getX()const{return x;)
double getY()const {return y;)
//ERROR *******found*******
void show()const{cout;<<’(’<<x<<’,’<<y<<’)’}
};
class Line{
Point;pl,p2;
public:
Line(Point pt1,Point pt2)
//ERROR *******found*******
{pt1=p1;pt2=p2;}
Point getp1()const{return p1 ;)
Point getP2()const{return p2;}
};
int main(){
Line line(Point:(8,4),Point(3,5));
cout<<”p1=”;
line.getp1().show();
cout<<”p2=”;
line.getp2().show();
tout<<endl;
return 0;
}
(1):x(x), y(y) { }或{this->x=x;this->y=y;}
(2)void show() const {cout<<’(<<x<<’,<< y<<’)’; }
(3): p1(pt1): p2(pt2) { }或{p1=pt1;p2=p2}
解析:(1)主要考查考生对构造函数的掌握,因为形参名和私有成员名称一样因此不能直接赋值,在这里使用成员列表初始化,也可以使用this指针赋值。
(2)主要考查考生对语句基本语法的掌握,根据语句:void show() const {cout<<’(’<<x<<’,<<y<<)’}。可看出函数体内并没有;作为cout语句的结束符,因此程序错误。
(3)主要考查考生对构造函数的掌握,形参是pt1和pt2,这里写反了,也可以使用成员列表初始化法,可以避免这种错误。
简单应用题
2.请打开考生文件夹下的解决方案文件proj2,其中有整数栈类IntList、顺序栈类SeqList和链接栈类LinkList的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
4 6 3 1 8
4 6 3 1 8
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动//“*******found*******”.
#include<iostEeain>
using namespace std;
class IntStack{//整数栈类
public:
virtual void push(int)=0;
//入栈
virtual int pop()=0;
//出栈并返回出栈元素
virtual int. topElement ()const=0;
//返回栈顶元素,但不出栈
virtual bool~sErapty()const=0;
//判断是否栈空
};
class SeqStack:public IntStack{
int data[100],//存放栈元素的数组
int:top; //栈顶元素的下标
public:
//*******found*******
SeqStack():____________{ }//把top初始化为-1表示栈空
void push(int n){data[++top]=n;)
//*******found*******
int pop()f return________;)
int topElement()const{return data[top];}
bool isEmpty()const{return top=-1;}
};
struct Node{
int data;
Node*next;
};
class LinkStack:public IntStack{
Node*top;
public:
//*******found*******
LinkStack():_____________{ }
//把top初始化为NULL表示栈空
void push(int n){
Node * P=new Node;
P一>data=n:
//*******found*******
________________;
top=P;
}
int pop(){
int d=top一>data;;
top=top一>next;
return d;
}
int topElement()const(return top一>data;}
bool isEmpty()const {return top==NULL,)
};
void pushData(IntStack&st){
st.push(8);
st.push(1);
st.push(3);
st.push(6);
st.push(4);
}
void popData(IntStack&st)f
while(!st.isEmpty())
cout<<st.pop()<<¨;
}
int main(){
SeqStack stl,pushData(stl);popData(stl);
cout<<endl;
LinkStack st2;pushData(st2);popData(st2)
本文档预览:3600字符,共6868字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载