C高级语言程序设计二_第1页
C高级语言程序设计二_第2页
C高级语言程序设计二_第3页
C高级语言程序设计二_第4页
C高级语言程序设计二_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第 1 页 ( 共 14 页 )上海大学20082009学年度春季学期试卷课程名: 高级语言程序设计(二) 课程号: 08305002 学分: 4 应试人声明: 我保证遵守上海大学学生手册中的上海大学考场规则,如有考试违纪、作弊行为,愿意接受上海大学学生考试违纪、作弊行为界定及处分规定的纪律处分。应试人 应试人学号 应试人所在院系 题号一(20)二(20)三(25)四(15)五(20)得分一、判断题(20分,每小题2分)1. 类的数据成员的访问属性不能为public,成员函数的访问属性不能为private。 (×)2. 类的成员函数对同类的所有对象的所有数据成员有无限制的访问能力。(

2、)3. 对象的空间通常由其基本空间和资源空间构成。()4. 任何类都一定有默认的构造函数。(×)5. 执行赋值运算时由系统自动调用复制构造函数。(×)6. 设计一个类,当其对象可能带有资源时,则应该为该类设计深复制构造函数、析构函数、重载深赋值运算符函数。()7. 类的友元函数是类的成员函数。(×)8. 类的静态成员函数只能通过对象来调用。(×)9. 重载运算符时不能改变运算符的运算优先级和结合方向。()10. 基类中访问属性为private的数据成员在派生类中是存在的,但是它们被隔离起来了,派生类新增的成员函数(即并非由基类继承而来的)不能直接访问它。

3、()成绩得分第 2 页 ( 共 14 页 )二、填空题(共20分,每空1分)1.根据如下程序及其运行结果,完成程序。#include <iostream>using namespace std;template <typename T>void swap(T &a, T &b) T temp;temp = a; a = b; b = temp;template <typename T> void swap_p(T *a, T *b)T temp ;temp = *a; *a = *b; *b = temp;int main()int m =

4、3, n = 5;double x = 3.3, y = 5.5;swap( m , n );cout << m << ", " << n << endl;swap_p( &x , &y );cout << x << " " << y << endl; return 0 ;得分运行结果5, 35.5; 3.3第 3 页 ( 共 14 页 )2.下面为复数类Complex的设计,请根据运行结果完成程序。#include <iostream&

5、gt;using namespace std;class Complexpublic:Complex(double real=0, double imag=0);Complex & Set(double real=0, double imag=0);double Real() const;double Imag() const;friend Complex operator+(const Complex &c1, const Complex &c2);Complex & operator+=(const Complex &c);friend ostrea

6、m & operator<<(ostream &out, const Complex &c);friend istream & operator>>(istream &in, Complex &c);private:double re, im;Complex:Complex(double real, double imag)re = real; im = imag;Complex & Complex: Set(double real, double imag)re = real; im = imag;return

7、*this;double Complex:Real() const return re ;double Complex:Imag() constreturn im ;第 4 页 ( 共 14 页 )Complex operator+(const Complex &c1, const Complex &c2)Complex temp;temp.Set(c1.re+c2.re, c1.im+c2.im);return temp ;Complex & Complex:operator+=(const Complex &c)re += c.re;im += c.im;r

8、eturn *this ;ostream & operator<<( ostream &out, const Complex &c)out << "(" << c.re << ", " << c.im << ")"return out ;istream & operator>>(istream &in, Complex &c)char str80;in.getline(str, 80, '(

9、9;);in.getline(str, 80, ',');c.re = atof(str);/ 标准函数double atof(char*);in.getline(str, 80, ')');/ 将字符串转换成对应的浮点型数值c.im = atof(str);return in;int main()Complex c1, c2(1.5, 2.5);cout << c1 << 't' << c2 << endl;c1.Set(10, 10);cout << c1 << endl

10、;cout << "输入一个复数: "cin >> c2;cout << (c1+=c2) << endl;cout << '(' << c1.Real() << ',' << c1.Imag() << ')' << endl;cout << c1+c2 << endl;return 0;运行结果(下划线部分为键盘输入)(0, 0) (1.5, 2.5)(10, 10)输入一个复数:(

11、200, 300)8(210, 310)(210,310)(410, 610)第 5 页 ( 共 14 页 )得分三、阅读程序写出运行结果(25分)1. (7分)#include <iostream>using namespace std;class Testpublic:Test(int a=0, int b=0): x(a), y(b) Test()if(x=y)cout << "数据成员的值相同,都等于 " << x << endl;elsecout << "数据成员的值不同,分别为 "

12、<< x << ", " << y << endl;friend ostream & operator<<(ostream &out, const Test &t)out << "(" << t.x << ", " << t.y << ")"return out;private:int x, y;int main()Test *p, t1;p = new Test(10);

13、Test t2(2, 3);cout << *p << 'n' << t1 << 'n' << t2 << endl;delete p;cout << "退出程序,返回操作系统" << endl;return 0;运行结果(10, 0) (0, 0) (2, 3) 数据成员的值不同,分别为 10, 0 退出程序,返回操作系统 数据成员的值不同,分别为 2, 3 数据成员的值相同,都等于 0 第 6 页 ( 共 14 页 )2. (6分)#inclu

14、de <iostream>using namespace std;class RMBpublic:RMB(double x=0)sgn = 1;if(x<0)sgn = -1; x = -x; int n = int(100*(x+0.005);yuan = n/100;jiao = n/10%10;fen = n%10;operator double()return sgn*(yuan + jiao/10.0 + fen/100.0);friend ostream & operator<<(ostream &out, const RMB &

15、;r)out << "¥" << (r.sgn=-1?"-":"") << r.yuan << "元" << r.jiao << "角" << r.fen << "分"return out;private:int yuan, jiao, fen, sgn;int main()RMB x(1.234), y(-1.235);cout << x << '

16、n' << y << endl;x = x + 1.0;y = 2*y;cout << x << 'n' << y << endl;x = x + y;y = -y;cout << x << 'n' << y << endl;return 0;运行结果¥1元2角3分 ¥-1元2角4分 ¥2元2角3分 ¥-2元4角8分 ¥-0元2角5分 ¥2元4角8分 第 7 页 ( 共 14 页 )3. (6分)#include <iostrea

17、m>#include <iomanip>#include <string>using namespace std;class Datepublic:Date(int year=2009, int month=1, int day=1) : y(year), m(month), d(day) int & Year() return y; int & Month() return m; int & Day() return d; friend ostream & operator<<(ostream &out, co

18、nst Date &d)out << setfill('0');out << d.y <<'/'<< setw(2) << d.m <<'/'<< setw(2) << d.d;out << setfill(' ');return out;private:int y, m, d;class Timepublic:Time(int hour=0, int minute=0, int second=0) : h(hour

19、), m(minute), s(second)int & Hour() return h;int & Minute()return m;int & Second()return s;friend ostream & operator<<(ostream &out, const Time &t)out << setfill('0');out << t.h <<':'<< setw(2) << t.m <<':'<

20、< setw(2) << t.s;out << setfill(' ');return out;private:int h, m, s;第 8 页 ( 共 14 页 )class TimeTablepublic:TimeTable(const string &event="nothing") : e(event) TimeTable(const string &event, const Date &date) : d(date), e(event) TimeTable(const string &

21、event,const Date &date,const Time &time): d(date), t(time), e(event) TimeTable(const string &event,int year, int month, int day,int hour, int minute, int second): d(year, month, day), t(hour, minute, second),e(event) Date & GetDate() return d; Time & GetTime() return t; string &a

22、mp; GetEvent() return e; friend ostream & operator<<(ostream &out, const TimeTable &tt)out << tt.d << ' ' << tt.t << ' ' << tt.e;return out;private:Date d;Time t;string e;第 9 页 ( 共 14 页 )int main()TimeTable a1, a2("贺新年");TimeT

23、able a3("六一儿童节庆祝会", 2009, 6, 1, 10, 30, 0);cout << a1 << endl;cout << a2 << endl;cout << a3 << endl;Date d(2009, 6, 10);Time t(8, 15, 0);string e("高级语言程序设计(二)考试");TimeTable a4(e, d, t);cout << a4 << endl;d.Day() = 15;e = "夏季学期开

24、始"TimeTable *p = new TimeTable(e, d);cout << *p << endl;p->GetDate().Month() = 7;p->GetDate().Day() = 12;p->GetEvent() = "学年结束,放假。"cout << *p << endl;delete p;return 0;运行结果 运行结果 2009/01/01 00:00:00 nothing 2009/01/01 00:00:00 贺新年 2009/06/01 10:30:00 六一

25、儿童节庆祝会 2009/06/10 08:15:00 高级语言程序设计(二)考试 2009/06/15 00:00:00 夏季学期开始 2009/07/12 00:00:00 学年结束,放假。 第 10 页 ( 共 14 页 )4. (6分)#include <iostream>#include <iomanip>using namespace std;class Timepublic:Time(int hour=0, int minute=0, int second=0)m = minute + second/60;s = second%60;h = hour + m

26、/60;m %= 60;friend ostream & operator<<(ostream &out, const Time &t)out << setfill('0') << setw(2) << t.h << ':' << setw(2) << t.m << ':' << setw(2) << t.s << setfill(' ');return out;Time &a

27、mp; operator+()s+;if(s > 60)m += s/60; s %= 60;if(m > 60)h += m/60; m %=60;return *this;Time operator+(int)Time temp = *this;+(*this);return temp;private:int h, m, s;int main()Time t(27, 200, 300), t1(10, 20, 30);cout << t << 'n' << t1 << endl;cout << t1+

28、<< endl;cout << t1 << endl;cout << +t1 << endl;cout << t1 << endl;return 0;运行结果 30:25:00 10:20:30 10:20:30 10:20:31 10:20:32 10:20:32 第 11 页 ( 共 14 页 )得分四、链表操作(15分,每小题5分)给定构成单向链表的结构体struct Nodedouble x;Node *next;1. 完成插入一个结点到链表,成为新链首结点的函数定义,函数原型为void Insert(

29、Node *&head, double x);其中形参x的作用为传递一个双精度浮点型数值给新创建的结点数据域成员。 void Insert(Node *&head, double x) Node *p = new Node; p->x = x; p->next = head; head = p; 2. 完成原型为double Sum(const Node *head);的函数定义,其功能是统计并返回单向链表中所有结点的数据成员x值的总和。 double Sum(const Node *head) double s = 0; for(Node*p=head; p!=N

30、ULL; p=p->next) s += p->x; return s; 3. 完成释放链表所有结点的函数void FreeList(Node *&head);的定义。假定链表中所有结点均为堆结点。 void FreeList(Node *&head) Node *p; while(head!=NULL) p = head; head = head->next; delete p; 第 12 页 ( 共 14 页 )得分五、(20分,每个函数2分)设计平面向量(即二维直角坐标系中的点)类,请完成尚未定义的10个函数。相关向量运算的数学定义:记,则,。/ Poi

31、nt.h头文件#ifndef POINT_H#define POINT_H#include <iostream>using namespace std;class Pointpublic:/ 成员函数Point(double x0=0, double y0=0);/ 构造函数virtual Point();/ 虚析构函数void Move(double x0, double y0);/ 修改点的坐标double GetX() const;/ 返回点的横坐标值double GetY() const;/ 返回点的纵坐标值double abs() const;/ 计算并返回向量的模/ 友

32、元函数friend Point operator+(const Point &p1, const Point &p2);/ 两个向量相加friend Point operator-(const Point &p1, const Point &p2);/ 两个向量相减friend Point operator*(double r, const Point &p);/ 标量与向量相乘friend Point operator*(const Point &p, double r);/ 向量与标量相乘friend double operator*(con

33、st Point &p1, const Point &p2);/ 两个向量点乘friend ostream & operator<<(ostream &out, const Point &p);/ 插入运算friend istream & operator>>(istream &in, Point &p);/ 抽取运算private:double x, y;/ 数据成员;#endif第 13 页 ( 共 14 页 )/ Point.cpp源程序文件#include "Point.h"#include <cmath>Point:Point(double

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论