实验2
1、实验任务1:
(1)代码部分
1 //t.h 2 #pragma once 3 4 #include <string> 5 6 // 类T: 声明 7 class T { 8 // 对象属性、方法 9 public: 10 T(int x = 0, int y = 0); // 普通构造函数 11 T(const T &t); // 复制构造函数 12 T(T &&t); // 移动构造函数 13 ~T(); // 析构函数 14 15 void adjust(int ratio); // 按系数成倍调整数据 16 void display() const; // 以(m1, m2)形式显示T类对象信息 17 18 private: 19 int m1, m2; 20 21 // 类属性、方法 22 public: 23 static int get_cnt(); // 显示当前T类对象总数 24 25 public: 26 static const std::string doc; // 类T的描述信息 27 static const int max_cnt; // 类T对象上限 28 29 private: 30 static int cnt; // 当前T类对象数目 31 32 // 类T友元函数声明 33 friend void func(); 34 }; 35 36 // 普通函数声明 37 void func(); 38 39 40 41 //t.cpp 42 // 类T: 实现 43 // 普通函数实现 44 45 #include "t.h" 46 #include <iostream> 47 #include <string> 48 49 using std::cout; 50 using std::endl; 51 using std::string; 52 53 // static成员数据类外初始化 54 const std::string T::doc{"a simple class sample"}; 55 const int T::max_cnt = 999; 56 int T::cnt = 0; 57 58 59 // 对象方法 60 T::T(int x, int y): m1{x}, m2{y} { 61 ++cnt; 62 cout << "T constructor called.\n"; 63 } 64 65 T::T(const T &t): m1{t.m1}, m2{t.m2} { 66 ++cnt; 67 cout << "T copy constructor called.\n"; 68 } 69 70 T::T(T &&t): m1{t.m1}, m2{t.m2} { 71 ++cnt; 72 cout << "T move constructor called.\n"; 73 } 74 75 T::~T() { 76 --cnt; 77 cout << "T destructor called.\n"; 78 } 79 80 void T::adjust(int ratio) { 81 m1 *= ratio; 82 m2 *= ratio; 83 } 84 85 void T::display() const { 86 cout << "(" << m1 << ", " << m2 << ")" ; 87 } 88 89 // 类方法 90 int T::get_cnt() { 91 return cnt; 92 } 93 94 // 友元 95 void func() { 96 T t5(42); 97 t5.m2 = 2049; 98 cout << "t5 = "; t5.display(); cout << endl; 99 } 100 101 102 //task1 103 #include "t.h" 104 #include <iostream> 105 106 using std::cout; 107 using std::endl; 108 109 void test(); 110 111 int main() { 112 test(); 113 cout << "\nmain: \n"; 114 cout << "T objects'current count: " << T::get_cnt() << endl; 115 } 116 117 void test() { 118 cout << "test class T: \n"; 119 cout << "T info: " << T::doc << endl; 120 cout << "T objects'max count: " << T::max_cnt << endl; 121 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 122 123 124 T t1; 125 cout << "t1 = "; t1.display(); cout << endl; 126 127 T t2(3, 4); 128 cout << "t2 = "; t2.display(); cout << endl; 129 130 T t3(t2); 131 t3.adjust(2); 132 cout << "t3 = "; t3.display(); cout << endl; 133 134 T t4(std::move(t2)); 135 cout << "t3 = "; t4.display(); cout << endl; 136 137 cout << "T objects'current count: " << T::get_cnt() << endl; 138 139 func(); 140 }
(2)运行结果
(3)问题1:
不能运行
func函数在类内仅声明了友元关系,并没有对函数进行声明
问题2:
普通构造函数:根据传入参数为m1,m2赋值,若没有参数则按默认值赋值;
复制构造函数:根据传入的类,将类的成员值复制给m1,m2;
移动构造函数:将另一个类的成员值移动到当前类中;
析构顺序:t5,t4,t3,t2,t1;
问题3:
编译错误
2、实验任务2:
(1)代码部分:
1 //complex.h 2 #ifndef X_H 3 #define X_H 4 #include<string> 5 using std::string; 6 7 class Complex{ 8 public: 9 Complex(double x=0.0,double y=0.0); 10 Complex(const Complex &c); 11 double get_real(); 12 double get_imag(); 13 void add(const Complex &c); 14 friend Complex add(const Complex &c1,const Complex &c2); 15 friend bool is_equal(const Complex &c1,const Complex &c2); 16 friend bool is_not_equal(const Complex &c1,const Complex &c2); 17 friend double abs(const Complex &c); 18 friend void output(const Complex &c); 19 private: 20 double num1; 21 double num2; 22 public: 23 static const string doc; 24 }; 25 26 #endif 27 28 //complex.cpp 29 #include<iostream> 30 #include<math.h> 31 #include "Complex.h" 32 using std::cout; 33 using std::endl; 34 using std::string; 35 36 const string Complex::doc{"a simplified Complex class"}; 37 Complex::Complex(double x,double y):num1{x},num2{y}{}; 38 Complex::Complex(const Complex &c):num1{c.num1},num2{c.num2}{}; 39 double Complex::get_real(){ 40 return num1; 41 } 42 double Complex::get_imag(){ 43 return num2; 44 } 45 void Complex::add(const Complex &c){ 46 num1+=c.num1; 47 num2+=c.num2; 48 } 49 Complex add(const Complex &c1,const Complex &c2){ 50 Complex c3(c1.num1+c2.num1,c1.num2+c2.num2); 51 return c3; 52 } 53 bool is_equal(const Complex &c1,const Complex &c2){ 54 if(c1.num1==c2.num1&&c1.num2==c2.num2) 55 return true; 56 return false; 57 } 58 bool is_not_equal(const Complex &c1,const Complex &c2){ 59 if(c1.num1!=c2.num1||c1.num2!=c2.num2) 60 return true; 61 return false; 62 } 63 double abs(const Complex &c){ 64 return sqrt(c.num1*c.num1+c.num2*c.num2); 65 } 66 void output(const Complex &c){ 67 if(c.num2>=0) 68 cout<<c.num1<<"+"<<c.num2<<'i'<<endl; 69 cout<<c.num1<<c.num2<<'i'<<endl; 70 } 71 72 //task2 73 // 待补足(多文件组织代码时,需要包含的头文件) 74 #include <iostream> 75 #include<math.h> 76 #include<string> 77 #include "Complex.h" 78 79 using std::cout; 80 using std::endl; 81 using std::boolalpha; 82 using std::string; 83 84 void test() { 85 cout << "类成员测试: " << endl; 86 cout << Complex::doc << endl; 87 88 cout << endl; 89 90 cout << "Complex对象测试: " << endl; 91 Complex c1; 92 Complex c2(3, -4); 93 const Complex c3(3.5); 94 Complex c4(c3); 95 96 cout << "c1 = "; output(c1); cout << endl; 97 cout << "c2 = "; output(c2); cout << endl; 98 cout << "c3 = "; output(c3); cout << endl; 99 cout << "c4 = "; output(c4); cout << endl; 100 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 101 102 cout << endl; 103 104 cout << "复数运算测试: " << endl; 105 cout << "abs(c2) = " << abs(c2) << endl; 106 c1.add(c2); 107 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 108 cout << boolalpha; 109 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 110 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 111 c4 = add(c2, c3); 112 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 113 } 114 115 int main() { 116 test(); 117 }
(2)运行结果:
3、实验任务3:
(1)代码部分:
1 #include <iostream> 2 #include <complex> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 using std::complex; 8 9 void test() { 10 cout << "标准库模板类comple测试: " << endl; 11 complex<double> c1; 12 complex<double> c2(3, -4); 13 const complex<double> c3(3.5); 14 complex<double> c4(c3); 15 16 cout << "c1 = " << c1 << endl; 17 cout << "c2 = " << c2 << endl; 18 cout << "c3 = " << c3 << endl; 19 cout << "c4 = " << c4 << endl; 20 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl; 21 cout << endl; 22 23 cout << "复数运算测试: " << endl; 24 cout << "abs(c2) = " << abs(c2) << endl; 25 c1 += c2; 26 cout << "c1 += c2, c1 = " << c1 << endl; 27 cout << boolalpha; 28 cout << "c1 == c2 : " << (c1 == c2) << endl; 29 cout << "c1 != c3 : " << (c1 != c3) << endl; 30 c4 = c2 + c3; 31 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 32 } 33 34 int main() { 35 test(); 36 }
(2)运行结果:
(3)对比任务:
问题1:进行复数运算时直接使用运算符号不用通过调用函数add;输出时直接使用cout不用调用output函数
问题2:通过与标准库的交互可以使类的操作更加简洁
4、实验任务4:
(1)代码部分:
1 //Fraction.h 2 #pragma once 3 #include<string> 4 5 using std::string; 6 7 class Fraction 8 { 9 public: 10 Fraction(int x=1,int y=1); 11 Fraction(const Fraction &f); 12 int get_up(); 13 int get_down(); 14 Fraction negative(); 15 friend void output(const Fraction &f); 16 friend Fraction add(const Fraction &f1,const Fraction &f2); 17 friend Fraction sub(const Fraction &f1,const Fraction &f2); 18 friend Fraction mul(const Fraction &f1,const Fraction &f2); 19 friend Fraction div(const Fraction &f1,const Fraction &f2); 20 private: 21 int up,down; 22 public: 23 static const string doc; 24 }; 25 26 //Fraction.cpp 27 #include<bits/stdc++.h> 28 #include"Fraction.h" 29 30 using namespace std; 31 32 const string Fraction::doc{"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.\n" }; 33 Fraction::Fraction(int x,int y):up{x},down{y}{}; 34 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}{}; 35 int Fraction::get_up() { 36 return up; 37 } 38 int Fraction::get_down(){ 39 return down; 40 } 41 Fraction Fraction::negative(){ 42 Fraction f1(-1*up,down); 43 return f1; 44 } 45 void output(const Fraction &f){ 46 if(f.up==0){ 47 cout<<0<<endl; 48 return; 49 } 50 if(f.down==0){ 51 cout<<"分母不能为零"<<endl; 52 return; 53 } 54 int num1=1,num2=1,mod=1,bigger; 55 if(f.up<0){ 56 num1=-1; 57 } 58 if(f.down<0){ 59 num2=-1; 60 } 61 if(num1*f.up==num2*f.down) 62 cout<<num1*num2<<endl; 63 if(num1*f.up>num2*f.down) 64 bigger=num1*f.up; 65 else 66 bigger=num2*f.down; 67 for(int i=2;i<=bigger/2;i++){ 68 if(f.up%i==0&&f.down%i==0) 69 mod=i; 70 } 71 if(f.down/mod==1||f.down/mod==-1){ 72 73 cout<<f.up/mod<<endl; 74 return; 75 } 76 if(num1<0&&num2<0){ 77 cout<<f.up/mod*num1<<'/'<<f.down/mod*num2<<endl; 78 return; 79 } 80 cout<<f.up/mod<<'/'<<f.down/mod<<endl; 81 82 } 83 Fraction add(const Fraction &f1,const Fraction &f2){ 84 Fraction f3(1); 85 f3.up=f1.up*f2.down+f2.up*f1.down; 86 f3.down=f1.down*f2.down; 87 return f3; 88 } 89 Fraction sub(const Fraction &f1,const Fraction &f2){ 90 Fraction f3(1); 91 f3.up=f1.up*f2.down-f2.up*f1.down; 92 f3.down=f1.down*f2.down; 93 return f3; 94 } 95 Fraction mul(const Fraction &f1,const Fraction &f2){ 96 Fraction f3(1); 97 f3.up=f1.up*f2.up; 98 f3.down=f1.down*f2.down; 99 return f3; 100 } 101 Fraction div(const Fraction &f1,const Fraction &f2){ 102 Fraction f3(1); 103 f3.up=f1.up*f2.down; 104 f3.down=f1.down*f2.up; 105 return f3; 106 } 107 108 //main.cpp 109 #include "Fraction.h" 110 #include <iostream> 111 112 using std::cout; 113 using std::endl; 114 115 116 void test1() { 117 cout << "Fraction类测试: " << endl; 118 cout << Fraction::doc << endl << endl; 119 120 Fraction f1(5); 121 Fraction f2(3, -4), f3(-18, 12); 122 Fraction f4(f3); 123 cout << "f1 = "; output(f1); cout << endl; 124 cout << "f2 = "; output(f2); cout << endl; 125 cout << "f3 = "; output(f3); cout << endl; 126 cout << "f4 = "; output(f4); cout << endl; 127 128 Fraction f5(f4.negative()); 129 cout << "f5 = "; output(f5); cout << endl; 130 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 131 132 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 133 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 134 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 135 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 136 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 137 } 138 139 void test2() { 140 Fraction f6(42, 55), f7(0, 3); 141 cout << "f6 = "; output(f6); cout << endl; 142 cout << "f7 = "; output(f7); cout << endl; 143 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 144 } 145 146 int main() { 147 cout << "测试1: Fraction类基础功能测试\n"; 148 test1(); 149 150 cout << "\n测试2: 分母为0测试: \n"; 151 test2(); 152 }
(2)运行结果:
5、实验任务5
(1)代码部分:
1 #pragma once 2 3 class SavingAccount { 4 private: 5 int id; 6 double balance; 7 double rate; 8 int lastDate; 9 double accumulation; 10 11 static double total; 12 13 void record(int date, double amount); 14 15 double accumulate(int date) const { 16 return accumulation + balance * (date - lastDate); 17 } 18 19 public: 20 SavingAccount(int date, int id, double rate); 21 22 int getId() const { return id; } 23 double getBalance() const { return balance; } 24 double getRate() const { return rate; } 25 26 static double getTotal() { return total; } 27 28 void deposit(int date, double amount); 29 void withdraw(int date, double amount); 30 void settle(int date); 31 void show() const; 32 33 }; 34 #include"account.h" 35 #include<iostream> 36 #include<cmath> 37 38 using namespace std; 39 40 double SavingAccount::total = 0; 41 42 SavingAccount::SavingAccount(int date, int id, double rate) 43 : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 44 cout << date << "\t#" << id << "is created" << endl; 45 } 46 47 void SavingAccount::record(int date, double amount) { 48 accumulation = accumulate(date); 49 lastDate = date; 50 amount = floor(amount * 100 + 0.5) / 100; 51 balance += amount; 52 total += amount; 53 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 54 } 55 56 void SavingAccount::deposit(int date, double amount) { 57 record(date, amount); 58 } 59 60 void SavingAccount::withdraw(int date, double amount) { 61 if (amount > getBalance()) 62 cout << "Error: not enough money" << endl; 63 else 64 record(date, -amount); 65 } 66 67 void SavingAccount::settle(int date) { 68 double interest = accumulate(date) * rate / 365; 69 if (interest != 0) 70 record(date, interest); 71 accumulation = 0; 72 } 73 74 void SavingAccount::show() const { 75 cout << "#" << id << "\tBalance: " << balance; 76 } 77 #include "account.h" 78 #include <iostream> 79 80 using namespace std; 81 82 int main() { 83 SavingAccount sa0(1, 21325302, 0.015); 84 SavingAccount sa1(1, 58320212, 0.015); 85 86 sa0.deposit(5, 5000); 87 sa1.deposit(25, 10000); 88 sa0.deposit(45, 5500); 89 sa1.withdraw(60, 4000); 90 91 sa0.settle(90); 92 sa1.settle(90); 93 94 sa0.show(); cout << endl; 95 sa1.show(); cout << endl; 96 cout << "Total: " << SavingAccount::getTotal() << endl; 97 98 }
(2)运行结果: