实验二
任务一
代码
1 #pragma once 2 3 #include <string> 4 5 // 类T: 声明 6 class T { 7 // 对象属性、方法 8 public: 9 T(int x = 0, int y = 0); // 普通构造函数 10 T(const T &t); // 复制构造函数 11 T(T &&t); // 移动构造函数 12 ~T(); // 析构函数 13 14 void adjust(int ratio); // 按系数成倍调整数据 15 void display() const; // 以(m1, m2)形式显示T类对象信息 16 17 private: 18 int m1, m2; 19 20 // 类属性、方法 21 public: 22 static int get_cnt(); // 显示当前T类对象总数 23 24 public: 25 static const std::string doc; // 类T的描述信息 26 static const int max_cnt; // 类T对象上限 27 28 private: 29 static int cnt; // 当前T类对象数目 30 31 // 类T友元函数声明 32 friend void func(); 33 }; 34 35 // 普通函数声明 36 void func(); 37 38 ------------------------------------------------------------------- 39 40 // 类T: 实现 41 // 普通函数实现 42 43 #include "t.h" 44 #include <iostream> 45 #include <string> 46 47 using std::cout; 48 using std::endl; 49 using std::string; 50 51 // static成员数据类外初始化 52 const std::string T::doc{"a simple class sample"}; 53 const int T::max_cnt = 999; 54 int T::cnt = 0; 55 56 57 // 对象方法 58 T::T(int x, int y): m1{x}, m2{y} { 59 ++cnt; 60 cout << "T constructor called.\n"; 61 } 62 63 T::T(const T &t): m1{t.m1}, m2{t.m2} { 64 ++cnt; 65 cout << "T copy constructor called.\n"; 66 } 67 68 T::T(T &&t): m1{t.m1}, m2{t.m2} { 69 ++cnt; 70 cout << "T move constructor called.\n"; 71 } 72 73 T::~T() { 74 --cnt; 75 cout << "T destructor called.\n"; 76 } 77 78 void T::adjust(int ratio) { 79 m1 *= ratio; 80 m2 *= ratio; 81 } 82 83 void T::display() const { 84 cout << "(" << m1 << ", " << m2 << ")" ; 85 } 86 87 // 类方法 88 int T::get_cnt() { 89 return cnt; 90 } 91 92 // 友元 93 void func() { 94 T t5(42); 95 t5.m2 = 2049; 96 cout << "t5 = "; t5.display(); cout << endl; 97 } 98 99 ------------------------------------------------------------ 100 101 #include "t.h" 102 #include <iostream> 103 104 using std::cout; 105 using std::endl; 106 107 void test(); 108 109 int main() { 110 test(); 111 cout << "\nmain: \n"; 112 cout << "T objects'current count: " << T::get_cnt() << endl; 113 } 114 115 void test() { 116 cout << "test class T: \n"; 117 cout << "T info: " << T::doc << endl; 118 cout << "T objects'max count: " << T::max_cnt << endl; 119 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 120 121 122 T t1; 123 cout << "t1 = "; t1.display(); cout << endl; 124 125 T t2(3, 4); 126 cout << "t2 = "; t2.display(); cout << endl; 127 128 T t3(t2); 129 t3.adjust(2); 130 cout << "t3 = "; t3.display(); cout << endl; 131 132 T t4(std::move(t2)); 133 cout << "t3 = "; t4.display(); cout << endl; 134 135 cout << "T objects'current count: " << T::get_cnt() << endl; 136 137 func(); 138 }
运行结果
问题一:
去掉line36不可以运行编译
问题二:
普通构造函数要在传递指定参数的时候,才会执行,析构函数是这个对象的作用域要结束的时候系统自己调用的。
问题三:
任务二
代码
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Complex{
public:
Complex(double x = 0,double y = 0);
Complex(const Complex &c);
~Complex();
private:
double real;
double imag;
public:
double get_real() const;
double get_imag() const;
void add(const Complex &c);
public:
static const string doc;
friend Complex add(const Complex &c1,const Complex &c2);
friend bool is_equal(const Complex &c1,const Complex &c2);
friend bool is_not_equal(const Complex &c1,const Complex &c2);
friend double abs(const Complex &c);
friend void output(const Complex &c);
};
1 #include "Complex.h" 2 #include <iostream> 3 #include <string> 4 #include<math.h> 5 #include<cmath> 6 7 using std::cout; 8 using std::endl; 9 using std::string; 10 11 const std::string Complex::doc{"a simplified Complex class"}; 12 13 double Complex::get_real() const{ 14 return real; 15 } 16 17 double Complex::get_imag() const{ 18 return imag; 19 } 20 21 Complex::Complex(double x,double y): real{x}, imag(y) { 22 } 23 24 Complex::Complex(const Complex &c1): real{c1.get_real()},imag{c1.get_imag()} { 25 } 26 27 Complex::~Complex(){ 28 } 29 30 void Complex::add(const Complex &c){ 31 real += c.get_real(); 32 imag += c.get_imag(); 33 } 34 35 Complex add(const Complex &c1,const Complex &c2){ 36 return Complex(c1.get_real() + c2.get_real(),c1.get_imag() + c2.get_imag()); 37 } 38 39 bool is_equal(const Complex &c1,const Complex &c2){ 40 if(c1.get_real() == c2.get_real() && c1.get_imag() == c2.get_imag()){ 41 return true; 42 } 43 return false; 44 } 45 46 bool is_not_equal(const Complex &c1,const Complex &c2){ 47 if(c1.get_real() == c2.get_real() && c1.get_imag() == c2.get_imag()){ 48 return false; 49 } 50 return true; 51 } 52 53 double abs(const Complex &c){ 54 return sqrt(c.get_real()*c.get_real() + c.get_imag()*c.get_imag()); 55 } 56 57 void output(const Complex &c){ 58 if(c.get_imag() >= 0){ 59 cout << c.get_real() << " + " << c.get_imag() << "i" << endl; 60 }else{ 61 cout << c.get_real() << " - " << std::abs(c.get_imag()) << "i" << endl; 62 } 63 64 }
1 #include "Complex.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 using std::boolalpha; 7 8 void test() { 9 cout << "类成员测试: " << endl; 10 cout << Complex::doc << endl; 11 12 cout << endl; 13 14 cout << "Complex对象测试: " << endl; 15 Complex c1; 16 Complex c2(3, -4); 17 const Complex c3(3.5); 18 Complex c4(c3); 19 20 cout << "c1 = "; output(c1); cout << endl; 21 cout << "c2 = "; output(c2); cout << endl; 22 cout << "c3 = "; output(c3); cout << endl; 23 cout << "c4 = "; output(c4); cout << endl; 24 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 25 26 cout << endl; 27 28 cout << "复数运算测试: " << endl; 29 cout << "abs(c2) = " << abs(c2) << endl; 30 c1.add(c2); 31 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 32 cout << boolalpha; 33 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 34 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 35 c4 = add(c2, c3); 36 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 37 } 38 39 int main() { 40 test(); 41 }
运行结果:
任务三
代码
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 }
结果
任务四
代码
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using namespace std; 6 7 class Fraction{ 8 public: 9 Fraction(int x,int y = 1); 10 Fraction(const Fraction &f); 11 ~Fraction(); 12 int get_up() const; 13 int get_down() const; 14 Fraction negative(); 15 16 private: 17 int up; 18 int down; 19 20 public: 21 static const string doc; 22 friend void output(const Fraction &f); 23 friend Fraction add(const Fraction &f1,const Fraction &f2); 24 friend Fraction sub(const Fraction &f1,const Fraction &f2); 25 friend Fraction mul(const Fraction &f1,const Fraction &f2); 26 friend Fraction div(const Fraction &f1,const Fraction &f2); 27 28 };
1 #include "Fraction.h" 2 #include <iostream> 3 #include <string> 4 #include<math.h> 5 #include<cmath> 6 7 using std::cout; 8 using std::endl; 9 using std::string; 10 11 const std::string Fraction::doc = "Fraction类基础功能测试"; 12 13 Fraction::Fraction(int x,int y){ 14 int a = abs(x),b = y; 15 while (b != 0) { 16 int temp = b; 17 b = a % b; 18 a = temp; 19 } 20 up = x / a; 21 down = y / a; 22 } 23 24 Fraction::Fraction(const Fraction &f): up{f.get_up()},down{f.get_down()}{ 25 } 26 27 Fraction::~Fraction(){ 28 } 29 30 int Fraction::get_up() const{ 31 return up; 32 } 33 34 int Fraction::get_down() const{ 35 return down; 36 } 37 38 Fraction Fraction::negative(){ 39 return Fraction((-1)*up,down); 40 } 41 42 void output(const Fraction &f){ 43 if(f.get_down() == 0){ 44 cout << "分母不能为零"; 45 return; 46 } 47 if(f.get_down() == 1){ 48 cout << f.get_up(); 49 }else{ 50 cout << f.get_up() << "/" << f.get_down(); 51 } 52 } 53 54 Fraction add(const Fraction &f1,const Fraction &f2){ 55 int gcd = f1.get_down(); 56 int temp_b = f2.get_down(); 57 58 while (temp_b != 0) { 59 int temp = temp_b; 60 temp_b = gcd % temp_b; 61 gcd = temp; 62 } 63 64 int minMax = std::abs(f1.get_down()*f2.get_down()) / gcd; 65 66 int i = minMax/f1.get_down(); 67 int j = minMax/f2.get_down(); 68 69 return Fraction(i*f1.get_up() + j*f2.get_up(),minMax); 70 } 71 72 73 74 75 Fraction sub(const Fraction &f1,const Fraction &f2){ 76 int gcd = f1.get_down(); 77 int temp_b = f2.get_down(); 78 79 while (temp_b != 0) { 80 int temp = temp_b; 81 temp_b = gcd % temp_b; 82 gcd = temp; 83 } 84 85 int minMax = std::abs(f1.get_down()*f2.get_down()) / gcd; 86 int i,j; 87 88 i = minMax/f1.get_down(); 89 j = minMax/f2.get_down(); 90 91 return Fraction(i*f1.get_up() - j*f2.get_up(),minMax); 92 } 93 94 95 96 97 Fraction mul(const Fraction &f1,const Fraction &f2){ 98 return Fraction(f1.get_up()*f2.get_up(),f1.get_down()*f2.get_down()); 99 } 100 101 Fraction div(const Fraction &f1,const Fraction &f2){ 102 return Fraction(f1.get_up()*f2.get_down(),f1.get_down()*f2.get_up()); 103 }
1 #include "Fraction.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 8 void test1() { 9 cout << "Fraction类测试: " << endl; 10 cout << Fraction::doc << endl << endl; 11 12 Fraction f1(5); 13 Fraction f2(3, -4), f3(-18, 12); 14 Fraction f4(f3); 15 cout << "f1 = "; output(f1); cout << endl; 16 cout << "f2 = "; output(f2); cout << endl; 17 cout << "f3 = "; output(f3); cout << endl; 18 cout << "f4 = "; output(f4); cout << endl; 19 20 Fraction f5(f4.negative()); 21 cout << "f5 = "; output(f5); cout << endl; 22 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl; 23 24 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 25 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 26 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 27 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 28 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 29 } 30 31 void test2() { 32 Fraction f6(42, 55), f7(0, 3); 33 cout << "f6 = "; output(f6); cout << endl; 34 cout << "f7 = "; output(f7); cout << endl; 35 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 36 } 37 38 int main() { 39 cout << "测试1: Fraction类基础功能测试\n"; 40 test1(); 41 42 cout << "\n测试2: 分母为0测试: \n"; 43 test2(); 44 }
运行结果
任务五
代码
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 4 class SavingAccount { 5 private: 6 int id; 7 double balance; 8 double rate; 9 int lastDate; 10 double accumulation; 11 12 static double total; 13 14 void record(int data, double amount); 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 int getId() const { return id; } 22 double getBalance() const { return balance; } 23 double getRate() const { return rate; } 24 25 static double getTotal() { return total; } 26 27 void deposit(int date, double amount); 28 void withdraw(int date, double amount); 29 30 void settle(int date); 31 32 void show() const; 33 };
1 #include "5.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double SavingAccount::total = 0; 7 8 SavingAccount::SavingAccount(int date, int id, double rate) 9 : id{ id }, balance{ 0 }, rate{ rate }, lastDate{ date }, accumulation{ 0 } { 10 cout << date << "\t#" << id << "is created" << endl; 11 } 12 13 void SavingAccount::record(int date, double amount) { 14 accumulation = accumulate(date); 15 lastDate = date; 16 amount = floor(amount * 100 + 0.5) / 100; 17 balance += amount; 18 total += amount; 19 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 20 } 21 22 void SavingAccount::deposit(int date, double amount) { 23 record(date, amount); 24 } 25 26 void SavingAccount::withdraw(int date, double amount) { 27 if (amount > getBalance()) 28 cout << "Error: not enough money" << endl; 29 else 30 record(date, -amount); 31 } 32 33 void SavingAccount::settle(int date) { 34 double interest = accumulate(date) * rate / 365; 35 if (interest != 0) 36 record(date, interest); 37 accumulation = 0; 38 } 39 40 void SavingAccount::show() const { 41 cout << "#" << id << "\tBalance: " << balance; 42 }
1 #include "5.h" 2 #include <iostream> 3 using namespace std; 4 5 int main() { 6 SavingAccount sa0(1, 21325302, 0.015); 7 SavingAccount sa1(1, 58320212, 0.015); 8 9 sa0.deposit(5, 5000); 10 sa1.deposit(25, 10000); 11 sa0.deposit(45, 5500); 12 sa1.deposit(60, 4000); 13 14 sa0.settle(90); 15 sa1.settle(90); 16 17 sa0.show(); cout << endl; 18 sa1.show(); cout << endl; 19 cout << "Total: " << SavingAccount::getTotal() << endl; 20 }
运行结果