C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1
1 CRECT 类 1 CRect 类 设计矩形类, 包含 长度和宽度信息 基本构造函数 基础属性的访问接口 ( 读 / 写, Read/Write, Get/Set) 计算周长和面积 ( 注 : 基本构造函数, 一个无参数的默认构造函数, 以及一个初始化数据成员的构造函数如果数据成员的初始化有多种形式, 就提供多个构造函数 基本属性的读写接口, 应用程序根据提供的写接口给数据成员赋值 读接口是为了返回具体的数据 计算周长和面积, 指的是写一个带返回值的函数, 计算结果由函数返回, 不是写一个在屏幕上显示计算结果的函数 显示结果一般在测试时 cout 调用函数接口的返回值! ) 2
1 CRECT 类 rect.h 1 #pragma once 2 3 c l a s s CRect 4 { 5 public : 6 CRect ( ) ; 7 CRect ( double w, double h) ; 8 ~CRect ( ) ; 9 10 double Perimeter ( ) const ; 11 double Area ( ) const ; 12 p r i v a t e : 13 double w, h ; 14 } ; rect.cpp 1 #include "rect.h" 2 #include <iostream> 3 using namespace std ; 4 5 CRect : : CRect ( ) 6 { 7 w = 0 ; 8 h = 0 ; 9 } 10 11 CRect : : ~ CRect ( ) 12 { 13 } 14 15 CRect : : CRect ( double w, double h) 16 { 17 this >w = w; 18 this >h = h ; 19 } 20 21 double CRect : : Perimeter ( ) const 22 { 23 return 2*(w+h) ; 24 } 25 26 double CRect : : Area ( ) const 27 { 28 return w*h ; 29 } main.cpp 3
1 CRECT 类 1 #include "rect.h" 2 #include <iostream> 3 using namespace std ; 4 5 i n t main ( ) 6 { 7 CRect rc ( 3. 0, 4. 0 ) ; 8 double c = rc. Perimeter ( ) ; 9 double s = rc. Area ( ) ; 10 11 cout << " Perimeter is " << c << endl ; 12 cout << " Area is " << s << endl ; 13 return 0 ; 14 } 4
2 课程类 2 课程类 设计课程类 包含基本属性 : 课程名称, 课时, 学分数, 上课地点, 上课时间 任课老师 基本接口 : 访问属性的基本接口 ( 即读写 /Get Set) 为了理解接口的作用, 使用设计好的课程类, 从键盘输入一门课程的信息, 然后将这门课的信息存入一个指定的文件 ( 注 : 从键盘输入一门课程的信息, 指在应用程序中创建一个课程对象 或者通过构造函数机制创建 或者用相关写 /Set 接口对该对象的数据成员赋值 将这门课的信息存入一个指定的文件, 有两种做法 给课程类增加一个输出到文件的接口, 通过类的成员函数实现 利用获取属性的接口读取对象的信息, 在应用程序里创建文件和输出信息 ) 5
2 课程类 course.h 1 #pragma once 2 #include <s t r i n g > 3 4 c l a s s Course 5 { 6 public : 7 Course ( ) ; 8 ~Course ( ) ; 9 Course ( const std : : s t r i n g & n, i n t h, i n t s, const std : : s t r i n g &p, std : : s t r i n g t i, const std : : s t r i n g &te ) ; 10 11 void SetName( std : : s t r i n g name) { this >name = name ; } 12 void SetHour ( i n t hour ) { this >hour = hour ; } 13 void SetCredit ( i n t s ) { this >s c o r e = s ; } 14 void SetPlace ( std : : s t r i n g place ) { this >place = place ; } 15 void SetTime ( std : : s t r i n g time ) { this >time = time ; } 16 void SetTeacher ( std : : s t r i n g teacher ) { this >teacher = teacher ; } 17 18 std : : s t r i n g GetName() const { return name ; } 19 i n t GetHour ( ) const { return hour ; } 20 i n t GetCredit ( ) const { return s c o r e ; } 21 std : : s t r i n g GetPlace ( ) const { return place ; } 22 std : : s t r i n g GetTime ( ) const { return time ; } 23 std : : s t r i n g GetTeacher ( ) const { return teacher ; } 24 25 f r i e n d std : : istream& operator>> ( std : : istream& i s, Course& course ) ; 26 f r i e n d std : : ostream& operator<< ( std : : ostream& os, const Course& course ) ; 27 p r i v a t e : 28 std : : s t r i n g name ; 29 i n t hour ; 30 i n t s c o r e ; 31 std : : s t r i n g place ; 32 std : : s t r i n g time ; 33 std : : s t r i n g teacher ; 34 } ; course.cpp 1 #include <iostream> 2 #include <fstream> 3 #include "course.h" 4 5 using namespace std ; 6 7 Course : : Course ( ) 8 { 9 name = "no name" ; 10 hour = 0 ; 6
2 课程类 11 s c o r e = 0 ; 12 place = "no place" ; 13 time = "" ; 14 teacher = "no teacher" ; 15 } 16 17 Course : : ~ Course ( ) 18 { 19 } 20 21 Course : : Course ( const std : : s t r i n g & n, i n t h, i n t s, const std : : s t r i n g &p, std : : s t r i n g t i, const std : : s t r i n g &te ) : name(n), hour (h), s c o r e ( s ), place (p), time ( t i ), teacher ( te ) 22 { 23 } 24 25 std : : istream& operator >> ( std : : istream& i s, Course& course ) 26 { 27 i s >> course. name >> course. hour >> course. s c o r e >> course. place >> course. time >> course. teacher ; 28 return i s ; 29 } 30 31 std : : ostream& operator<< ( std : : ostream& os, const Course& course ) 32 { 33 os << course. name << '\t' << course. hour << '\t' << course. s c o r e << '\t' << course. place << '\t' << course. time << '\t' << course. teacher ; 34 return os ; 35 } main.cpp 1 #include "course.h" 2 #include <iostream> 3 #include <fstream> 4 using namespace std ; 5 6 i n t main ( ) 7 { 8 Course c ; 9 10 cin >> c ; 11 cout << c << endl ; 12 ofstream o f s ( "course.txt" ) ; 13 o f s << c << endl ; 14 15 return 0 ; 16 } 7
3 运算符重载和友元 3 运算符重载和友元 三维向量类 重载运算符实现 二元运算符 :+,-, 点乘, 向量 * 小数 一元运算 :+,-( 正, 负 ) 友元函数 : 叉乘 CVector3 CrossProduct(const CVector3 &v1, const CVector3 &v2); «输出,» 输入 ( 注 : 可以同时复习课本 p423 第 7 题 ) 8
3 运算符重载和友元 vector3.h 1 #pragma once 2 #include <iostream> 3 4 c l a s s CVector3 5 { 6 f r i e n d CVector3 CrossProduct ( const CVector3 &v1, const CVector3 &v2 ) ; 7 f r i e n d std : : ostream & operator <<(std : : ostream &os, const CVector3 & v ) 8 { 9 os << v. x << "," << v. y <<","<< v. z << std : : endl ; 10 return os ; 11 } 12 13 f r i e n d std : : istream & operator >> ( std : : istream &i s, CVector3 & v ) 14 { 15 i s >> v. x >> v. y >> v. z ; 16 return i s ; 17 } 18 19 public : 20 CVector3 ( ) ; 21 ~CVector3 ( ) ; 22 CVector3 ( double x, double y, double z ) ; 23 24 CVector3 operator+(const CVector3 & c ) const ; 25 CVector3 operator ( const CVector3 & c ) const ; 26 CVector3 operator *( double n) const ; 27 28 CVector3 operator +() const ; 29 CVector3 operator () const ; 30 double operator *( const CVector3 &v ) ; 31 p r i v a t e : 32 double x ; 33 double y ; 34 double z ; 35 36 } ; vector3.cpp 1 #include "vector3.h" 2 #include <iostream> 3 using namespace std ; 4 5 CVector3 : : CVector3 ( ) 6 { 7 x = 0 ; 8 y = 0 ; 9 z = 0 ; 9
3 运算符重载和友元 10 } 11 12 CVector3 : : ~ CVector3 ( ) 13 { 14 15 } 16 17 CVector3 : : CVector3 ( double x, double y, double z ) 18 { 19 this >x = x ; 20 this >y = y ; 21 this >z = z ; 22 } 23 24 CVector3 CVector3 : : operator+(const CVector3 & c ) const 25 { 26 return CVector3 ( x+c. x, y+c. y, z+c. z ) ; 27 } 28 29 CVector3 CVector3 : : operator ( const CVector3 & c ) const 30 { 31 return CVector3 ( x c. x, y c. y, z c. z ) ; 32 } 33 34 double CVector3 : : operator *( const CVector3 &c ) 35 { 36 return x * c. x, y * c. y, z * c. z ; 37 } 38 39 CVector3 CVector3 : : operator +() const 40 { 41 return CVector3 (x, y, z ) ; 42 } 43 44 CVector3 CVector3 : : operator () const 45 { 46 return CVector3( x, y, z ) ; 47 } 48 49 CVector3 CVector3 : : operator *( double n) const 50 { 51 return CVector3 (n*x, n*y, n*z ) ; 52 } 53 54 CVector3 CrossProduct ( const CVector3 &v1, const CVector3 &v2 ) 55 { 56 return CVector3 ( v1. y * v2. z v1. z * v2. y, v1. z * v2. x v1. x * v2. z, v1. x * v2. y v1. y * v2. x ) ; 57 } 10
3 运算符重载和友元 main.cpp 1 #include "vector3.h" 2 #include <iostream> 3 using namespace std ; 4 5 i n t main ( ) 6 { 7 CVector3 v1 ; 8 cout << " Enter v1 :" << endl ; 9 cin >> v1 ; 10 11 CVector3 v2 ; 12 cout << " Enter v2 :" << endl ; 13 cin >> v2 ; 14 15 CVector3 v3 ; 16 v3 = v1 + v2 ; 17 cout <<"v1 + v2 is "<< v3<<endl ; 18 19 CVector3 v4 ; 20 v4 = v1 v2 ; 21 cout << "v1 - v2 is " << v4<<endl ; 22 23 CVector3 v5 ; 24 v5 = CrossProduct ( v1, v2 ) ; 25 cout << "v1 x v2 is " << v5<<endl ; 26 27 double dot_ r esult ; 28 dot_ result = v1 * v2 ; 29 cout << "v1 dot v2 is " << d ot_ re sult << endl ; 30 31 return 0 ; 32 } 11
4 C++ 简单动画 - 选作 4 C++ 简单动画 - 选作 示例程序是一个简单动画演示 CShape 是简单动画类 CShape 加载了表示一组连续动画的所有数据 CImage *image,cimage 是 VS 提供的图像处理类, 可以读入图像, 和在指定位置显示图像 示例程序在 (0,0) 位置显示的是动画的第一帧, 请你修改这个类, 使得能够在指定的任意位置循环播放这些连续动画 并试图用键盘控制动画的位置 ( 注 : 需要加上动画的 (x,y) 位置 (width,height) 大小属性, 当前播放帧属性 ) 12
5 无符号大整数 5 无符号大整数 众所周知,C/C++ 的基本数据类型中整型都是有数据范围的, 如 int 的数据范围是 - 2147483648 到 2147483647 如果要计算 1234567890000000000000987654321 + 8888888888888888888888888 的结果,int 甚至是 long long 都不行 现在要求实现一个无符号的并且没有长度限制的大整数类, 对于下面的程序 #include <iostream> #include "UnsignedBigInt.h" using namespace std ; i n t main ( ) { i n t x ; UnsignedBigInt a, b ; cin >> a >> b >> x ; } i f ( a < b) { cout << a << " is smaller." << endl ; } e l s e i f ( a > b) { cout << b << " is smaller." << endl ; } e l s e { cout << " equal." << endl ; } cout << a + b << endl ; cout << a + x << endl ; 输入 :1234567890000000000000987654321 8888888888888888888888888 123321 能给出正确的输出 附加要求 : 类中所有的成员变量都必须不是 public 的你的程序对于下面输出能得到正确的结果吗? 1. 100000000000000000000000000000 100000000000000000000000000000 10 2. 10000000000000000000000000000000 10 10 3. 0 10000000000000000000000000000000 10 4. 0 0 0 以下为示例的.h 文件 1 #pragma once 2 #include <iostream> 3 #include<s t r i n g > 4 #inc lude <vector> 5 6 c l a s s UnsignedBigInt 7 { 13
5 无符号大整数 8 f r i e n d std : : istream& operator >>(std : : istream &i s, UnsignedBigInt &b i g i n t ) ; 9 f r i e n d std : : ostream& operator <<(std : : ostream &os, const UnsignedBigInt &b i g i n t ) ; 10 public : 11 UnsignedBigInt ( void ) ; 12 UnsignedBigInt ( const std : : s t r i n g s t r ) ; 13 14 UnsignedBigInt o p e r a t o r +( const UnsignedBigInt &other ) const ; 15 16 bool operator <( const UnsignedBigInt &other ) const ; 17 bool operator >( const UnsignedBigInt &other ) const ; 18 p r i v a t e : 19 std : : vector<int> b i t s ; 20 } ; 14
5 无符号大整数 BigInt.h 1 #pragma once 2 #include <iostream> 3 #include<s t r i n g > 4 #inc lude <vector> 5 6 c l a s s UnsignedBigInt 7 { 8 f r i e n d std : : istream& operator >>(std : : istream &i s, UnsignedBigInt &b i g i n t ) ; 9 f r i e n d std : : ostream& operator <<(std : : ostream &os, const UnsignedBigInt &b i g i n t ) ; 10 public : 11 UnsignedBigInt ( void ) ; 12 UnsignedBigInt ( const std : : s t r i n g s t r ) ; 13 14 UnsignedBigInt o p e r a t o r +( const UnsignedBigInt &other ) const ; 15 16 bool operator <( const UnsignedBigInt &other ) const ; 17 bool operator >( const UnsignedBigInt &other ) const ; 18 p r i v a t e : 19 std : : vector<int> b i t s ; 20 } ; BigInt.cpp 1 #include "BigInt.h" 2 3 UnsignedBigInt : : UnsignedBigInt ( void ) 4 { 5 b i t s. push_back (0) ; 6 } 7 8 UnsignedBigInt : : UnsignedBigInt ( const std : : s t r i n g s t r ) 9 { 10 f o r ( i n t i = s t r. s i z e ( ) 1 ; i >= 0 ; i ) 11 { 12 i f ( s t r [ i ] > '9' s t r [ i ] < '0' ) { b i t s. c l e a r ( ) ; return ; } 13 b i t s. push_back ( s t r [ i ] '0' ) ; 14 } 15 i f ( s t r. s i z e ( ) == 0) b i t s. push_back (0) ; 16 } 17 18 UnsignedBigInt UnsignedBigInt : : o p e r a t o r +( const UnsignedBigInt & other ) const 19 { 20 unsigned i n t n = b i t s. s i z e ( )>other. b i t s. s i z e ( )? b i t s. s i z e ( ) : other. b i t s. s i z e ( ) ; 21 22 UnsignedBigInt r e s u l t ; 23 r e s u l t. b i t s. c l e a r ( ) ; 24 i n t c = 0 ; 25 f o r ( unsigned i n t i = 0 ; i < n ; ++i ) 15
5 无符号大整数 26 { 27 i n t sum = c ; 28 i f ( i< b i t s. s i z e ( ) ) sum += b i t s [ i ] ; 29 i f ( i < other. b i t s. s i z e ( ) ) sum += other. b i t s [ i ] ; 30 31 c = sum / 10; 32 r e s u l t. b i t s. push_back (sum % 10) ; 33 } 34 i f ( c ) 35 r e s u l t. b i t s. push_back ( c ) ; 36 return r e s u l t ; 37 } 38 39 bool UnsignedBigInt : : operator <( const UnsignedBigInt & other ) const 40 { 41 i f ( b i t s. s i z e ( )!= other. b i t s. s i z e ( ) ) 42 return b i t s. s i z e ( ) < other. b i t s. s i z e ( ) ; 43 44 f o r ( unsigned i n t i = b i t s. s i z e ( ) 1; i >= 0 ; i ) 45 { 46 i f ( b i t s [ i ]!= other. b i t s [ i ] ) 47 return b i t s [ i ] < other. b i t s [ i ] ; 48 } 49 return f a l s e ; 50 } 51 52 bool UnsignedBigInt : : operator >( const UnsignedBigInt & other ) const 53 { 54 return other < * t h i s ; 55 } 56 57 std : : istream& operator >>(std : : istream &i s, UnsignedBigInt &b i g i n t ) 58 { 59 std : : s t r i n g b i g i n t _ s t r ; 60 i s >> b i g i n t _ s t r ; 61 b i g i n t = UnsignedBigInt ( b i g i n t _ s t r ) ; 62 return i s ; 63 } 64 65 std : : ostream& operator <<(std : : ostream &os, const UnsignedBigInt &b i g i n t ) 66 { 67 f o r ( i n t i = b i g i n t. b i t s. s i z e ( ) 1; i >=0; i ) 68 os << b i g i n t. b i t s [ i ] ; 69 return os ; 70 } main.cpp 1 #inc lude <iostream> 2 #include "BigInt.h" 16
5 无符号大整数 3 using namespace std ; 4 5 i n t main ( ) 6 { 7 UnsignedBigInt a ( "123456" ), b( "123457" ), x ( "923457" ) ; 8 // cin >> a >> b >> x ; 9 10 i f ( a < b) { 11 cout << a << " is smaller." << endl ; 12 } 13 e l s e i f ( a > b) { 14 cout << b << " is smaller." << endl ; 15 } 16 e l s e { 17 cout << " equal." << endl ; 18 } 19 20 UnsignedBigInt sum = a + b ; 21 22 cout << sum << endl ; 23 cout << a << " + " << x << " = " ; 24 cout << a + x << endl ; 25 26 cin. get ( ) ; 27 return 0 ; 28 } 17