全国2009年1月自学考试C++程序设计试题(含答案) 下载本文

内容发布更新时间 : 2024/5/24 0:26:54星期一 下面是文章的全部内容请认真阅读。

全国2009年1月自学考试C++程序设计试题

void copy(Simple&s); void setxy(int i,int j){x=i;y=j;}

void print(){cout<<\};

void Simple::copy(Simple&s) {

x=s.x;y=s.y; }

void func(Simple s1,Simple&s2) {

s1.setxy(30,40); s2.setxy(70,80); }

void main() {

Simple obj1(1,2),obj2; obj2.copy(obj1); func(obj1,obj2); obj1.print(); obj2.print(); } x=1,y=2 x=70,y=80

53.给出下面程序的输出结果 #include\ int main() { int i=17; while(i>=10) if(--i%4==3)continue;

11/14全国2009年1月自学考试C++程序设计试题

全国2009年1月自学考试C++程序设计试题

else

cout<<\ } i=16 i=14 i=12 i=10

54.给出下面程序的输出结果 #include using namespace std; void main() {

int num=300; int &ref=num; cout<

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

55.定义堆栈类模板Stack(先进后出),栈的大小由使用者确定。要求该类模板对外提供 如下二种基本操作:

(1)push入栈(2)pop出栈,用数组来实现 #include using namespace std; template class Stack{ T x[size]; int current;

12/14全国2009年1月自学考试C++程序设计试题

全国2009年1月自学考试C++程序设计试题

public:

Stack(){current=0;} ....push(....); ....pop(....); };

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

#include using namespace std; template class Stack{

T x[size]; int current;

public: };

template

bool Stack::Push(T e){//插入新元素e为新的栈顶元素

if(current == size){//栈满 }

x[++current] = e; return true;

return false; Stack(){current=0;} bool Push(T e); bool Pop(T &e);

}//Push

template

bool Stack::Pop(T &e){//若栈不空,删除栈顶元素,并用e返回其值,并返回true,否则返回false

if( current == 0 ) return false; e=x[current--];

13/14全国2009年1月自学考试C++程序设计试题

全国2009年1月自学考试C++程序设计试题

return true;

}//Pop

//测试程序,不是答案的一部分 void main(){ }

Stack s; s.Push(1); //s.Push(2); int e; if(s.Pop(e)) else

cout<<\栈空\cout<

cout<<\再次出栈,试试看...\if(s.Pop(e)) else

cout<<\栈已空\cout<

14/14全国2009年1月自学考试C++程序设计试题