#C++期末考试试题及答案 下载本文

内容发布更新时间 : 2024/5/18 10:03:25星期一 下面是文章的全部内容请认真阅读。

2002年春季软件学院C++期末考试试题及答案

考试时间:120分钟

1. (10>分

现有两种实体:“矩形”

int rectangleArea (int x, int y> {return (x>=0 && y>=0>?x*y:0;}a7xgiDiVL1 //计算矩形的面积

int flagpoleLength (int x, int y> {return (x>=0 && y>=0>?x+y:0;}a7xgiDiVL1 //计算旗杆的高度 void f(> {

struct Rectangle rect; struct Flagpole flgp;

rect.length=20; rect.width=5; flgp.height=20; flgp.depth=5;a7xgiDiVL1 int area=rectangleArea(rect.length, rect.width>;

int length=flagpoleLength(flgp.height, flgp.depth>;a7xgiDiVL1 }

试续写函数f,以各举一例说明,对函数rectangleArea和flagpoleLength的调用都可能出现语法上合法,但结果与函数的语义不一致的情况,并指出其原因。a7xgiDiVL1 答案:

续写的二句如下:

area=rectangleArea(flgp.height, flgp.depth>; length=flagpoleLength(rect.length, rect.width>;

原因:在函数调用时,只需实参和形参类型相容即可,而不管语义如何?如上例二句语法上是合法的,但结果与函数的语义不一致。若欲使该情况不发生,必须将数据及其上的操作绑定在一起,这必须用类来定义。a7xgiDiVL1 2. (15分>

阅读下面的程序,写出main的输出。 #include \

int f(int i, const int* j, int& k> { if(i%3= =0>

if(*j>=6> k*=i; k+=i; k-=i;

return *j+k; }

int main(> {

int i, j=1, k=2, m;

for(i=0; i<10; i++, j*=2> { m=f(i, &j, k>;

printf(\a7xgiDiVL1 }

return 0; } 答案:

i=0, j=1, k=2, m=3. i=1, j=2, k=2, m=4. i=2, j=4, k=2, m=6. i=3, j=8, k=6, m=14. i=4, j=16, k=6, m=22. i=5, j=32, k=6, m=38. i=6, j=64, k=36, m=100. i=7, j=128, k=36, m=164. i=8, j=256, k=36, m=292. i=9, j=512, k=324, m=836. 3.(15分>

根据main程序的输出结果,定义并实现max函数。 int {

cout<<“max=”<<<<<

cout<<“max=”<<<<<

<假定所有的赋值、比较运算符都已定义) 答案:

template T max(T x, T y> { return x>=y?x:y;} template T max(T x, T y, T z> { T temp;

temp=max(x,y>;

return temp>=z?temp:z; }

float max(int x, float y> { return x>=y?x:y; } 4.(20分>

定义堆栈类模板Stack,栈的大小由使用者确定。要求该类模板对外提供如下二种基本操作: <1)push

main(>

<2)pop

<假定赋值操作已定义) 答案:

#include

template class Stack{ T s[size]; int iCurrentElem; public: Stack(>; bool push(T x>; T pop(>; void print(>{for(int i=0; i cout<

template Stack::Stack(>{ for(int i=0; i s[i]=0; iCurrentElem=-1; }

template bool Stack::push(T x>{ if(iCurrentElem==(size-1>> {cout<<\a7xgiDiVL1 else {s[++iCurrentElem]=x; return true;} }

template T Stack::pop(>{ T temp; if(iCurrentElem==-1> {cout<<\a7xgiDiVL1 else {temp=s[iCurrentElem];s[iCurrentElem]=0; iCurrentElem--; return temp;}a7xgiDiVL1 }

void main(> { float t; Stack s1; s1.push(10>; s1.push(20>; s1.push(30>; s1.push(40>; s1.print(>; t=s1.pop(>; s1.print(>; cout<

5.(25分>

用C++语言定义MyString<包括成员函数的实现代码),使之能符合下面程序及在注释中描述的运行结果的要求:a7xgiDiVL1 main(> {

MyString s1 = \

s1.display(>; // 此时显示出: <0123456789>

s2.display(>; // 此时显示出<<>之间是五个空格): < >a7xgiDiVL1 s3.display(>; // 此时显示出: <>

s4.display(>; // 此时显示出: <0123456789> s3 = s1;

s3.display(>; // 此时显示出: <0123456789> s3 = 3+s3;

s3.display(>; // 此时显示出: <99> s2 = s1[2];

s2.display(>; // 此时显示出: <23456789> s1.display(>; // 此时显示出: <0123456789> s3 = s2++;

s2.display(>; // 此时显示出: <3456789> s3.display(>; // 此时显示出: <23456789> } 答案:

#include #include #include class MyString { char cpBody[81]; public:

MyString(const char* p = NULL>; MyString(int i>; MyString(MyString& s>;

MyString& operator=(const MyString& s>

{ strncpy(cpBody, s.cpBody, 80>; return *this; } MyString& operator[](int i>; MyString& operator++(int i> { static MyString s; s = *this;

*this = (cpBody[0] == '\\0'> ? *this : (*this>[1]; return s; }a7xgiDiVL1 void display(> { printf(\ friend MyString& operator+(int i, MyString& s>; };

MyString::MyString(const char* p> { if (p == NULL> cpBody[0] = '\\0'; else strncpy(cpBody, p, 80>; }

MyString::MyString(int i> { int j;

for (j = 0; j < i && j < 80; j++>

cpBody[j] = ' '; cpBody[j] = '\\0'; }

MyString::MyString(MyString& s> { *this=s;

/* 或者改用如下语句:int length;

length=strlen(s.cpBody>;

for(int i=0; i cpBody[i]=s.cpBody[i]; */a7xgiDiVL1 }

MyString& MyString::operator[](int i> { static MyString s; int j; s = *this;

for (j = i; cpBody[j] != '\\0'; j++> s.cpBody[j-i] = s.cpBody[j]; s.cpBody[j-i] = '\\0'; return s; }

MyString& operator+(int N, MyString& s> { static MyString st; int i,length; length=strlen(s.cpBody>; for(i=0;i s.cpBody[length+i]=s.cpBody[length-1];a7xgiDiVL1 s.cpBody[length+i]='\\0'; st=s; return st; }

6.(15分>

某公司有二类职员Employee和Manager,Manager亦属于Employee。每个Employee对象所具有的基本信息为:姓名、年令、工作年限、部门号,Manager对象除具有上述基本信息外,还有级别(level>信息。公司中的二类职员都具有二种基本操作:a7xgiDiVL1 1>. printOn(> // 输出个人信息

2>. retire(> // 判断是否到了退休年令,是,则从公司中除名。公司规定:

// Employee类的退休年令为55岁,Manager类的退休年令为60岁。

要求:

1>. 定义并实现类Employee和Manager;

2>. 分别输出公司中二类职员的人数<注意:Manager亦属于Employee)。 答案:

#include #include class Employee{ char name[21]; int workYear; int departmentNum; protected: