实验2 C++
任务1:
t.h
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 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 };
t.cpp
// 类T: 实现 // 普通函数实现 #include "t.h" #include <iostream> #include <string> using std::cout; using std::endl; using std::string; // static成员数据类外初始化 const std::string T::doc{"a simple class sample"}; const int T::max_cnt = 999; int T::cnt = 0; // 对象方法 T::T(int x, int y): m1{x}, m2{y} { ++cnt; cout << "T constructor called.\n"; } T::T(const T &t): m1{t.m1}, m2{t.m2} { ++cnt; cout << "T copy constructor called.\n"; } T::T(T &&t): m1{t.m1}, m2{t.m2} { ++cnt; cout << "T move constructor called.\n"; } T::~T() { --cnt; cout << "T destructor called.\n"; } void T::adjust(int ratio) { m1 *= ratio; m2 *= ratio; } void T::display() const { cout << "(" << m1 << ", " << m2 << ")" ; } // 类方法 int T::get_cnt() { return cnt; } // 友元 void func() { T t5(42); t5.m2 = 2049; cout << "t5 = "; t5.display(); cout << endl; }
task1.cpp
1 #include "t.h" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test(); 8 9 int main() { 10 test(); 11 cout << "\nmain: \n"; 12 cout << "T objects'current count: " << T::get_cnt() << endl; 13 } 14 15 void test() { 16 cout << "test class T: \n"; 17 cout << "T info: " << T::doc << endl; 18 cout << "T objects'max count: " << T::max_cnt << endl; 19 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 20 21 22 T t1; 23 cout << "t1 = "; t1.display(); cout << endl; 24 25 T t2(3, 4); 26 cout << "t2 = "; t2.display(); cout << endl; 27 28 T t3(t2); 29 t3.adjust(2); 30 cout << "t3 = "; t3.display(); cout << endl; 31 32 T t4(std::move(t2)); 33 cout << "t3 = "; t4.display(); cout << endl; 34 35 cout << "T objects'current count: " << T::get_cnt() << endl; 36 37 func(); 38 }
运行结果:
问题1:能正常运行,因为func()已经在类内部声明为友元函数,无需在类外再次声明。
问题2:
普通构造函数:在对象被创建时利用特定的值构造对象,将对象初始化为一个特定的状态;主函数中声明T t2(3,4) 时调用。
复制构造函数:使用一个已经存在的对象(由复制构造函数的参数指定),去初始化同类的一个新对象;主函数中声明T t3(t2) 时调用。
移动构造函数:通过移动右值引用对象来安全地构造新对象,并且避免冗余复制对象的代价;主函数中T t4(std::move(t2)) 时调用。
析构函数:用来完成对象被删除前的一些清理工作;在对象的生存周期即将结束的时刻被自动调用。
问题3:不能正确运行。
任务2:
1 #include <stdlib.h> 2 #include <string> 3 #include <math.h> 4 5 using namespace std; 6 7 class Complex{ 8 public: 9 Complex(double r,double i); 10 Complex(double r); 11 Complex(); 12 Complex(const Complex&c); 13 private: 14 double real; 15 double imag; 16 17 public: 18 static const string doc; 19 public: 20 double get_real() const; 21 double get_imag() const; 22 void add(const Complex &c); 23 friend Complex add(const Complex &c1,const Complex &c2); 24 friend bool is_equal(const Complex &c1,const Complex &c2); 25 friend bool is_not_equal(const Complex &c1,const Complex &c2); 26 friend void output(Complex c); 27 friend double abs(Complex c); 28 };
1 #include "y.h" 2 3 using namespace std; 4 5 const string Complex::doc{"a simplified Complex calss"}; 6 7 Complex::Complex(double r,double i){ 8 real=r; 9 imag=i; 10 } 11 12 Complex::Complex(double r) {real=r;imag=0;} 13 14 Complex::Complex() {real=0;imag=0;} 15 16 Complex::Complex(const Complex&c){ 17 real=c.real; 18 imag=c.imag; 19 } 20 21 double Complex::get_real() const { 22 return real; 23 } 24 25 double Complex::get_imag() const { 26 return imag; 27 } 28 29 void Complex::add(const Complex &c){ 30 real+=c.real; 31 imag+=c.imag; 32 } 33 34 Complex add(const Complex &c1,const Complex &c2){ 35 Complex sum; 36 sum.real+=c1.real+c2.real; 37 sum.imag+=c1.imag+c2.imag; 38 return sum; 39 } 40 41 bool is_equal(const Complex &c1,const Complex &c2){ 42 bool flag=false; 43 if(c1.real==c2.real && c1.imag==c2.imag) 44 flag=true; 45 return flag; 46 } 47 48 bool is_not_equal(const Complex &c1,const Complex &c2){ 49 bool flag=true; 50 if(c1.real==c2.real && c1.imag==c2.imag) 51 flag=false; 52 return flag; 53 } 54 55 void output(Complex c){ 56 cout<<c.real; 57 if(c.imag<0) 58 cout<<" - "<<-c.imag<<'i'; 59 else 60 cout<<" + "<<c.imag<<'i'; 61 } 62 63 double abs(Complex c){ 64 double ans=0; 65 double r1,i1; 66 r1=c.real*c.real; 67 i1=c.imag*c.imag; 68 ans=sqrt(r1+i1); 69 return ans; 70 }
1 #include "y.h" 2 3 using std::cout; 4 using std::endl; 5 using std::boolalpha; 6 7 void test() { 8 cout << "类成员测试: " << endl; 9 cout << Complex::doc << endl; 10 11 cout << endl; 12 13 cout << "Complex对象测试: " << endl; 14 Complex c1; 15 Complex c2(3, -4); 16 const Complex c3(3.5); 17 Complex c4(c3); 18 19 cout << "c1 = "; output(c1); cout << endl; 20 cout << "c2 = "; output(c2); cout << endl; 21 cout << "c3 = "; output(c3); cout << endl; 22 cout << "c4 = "; output(c4); cout << endl; 23 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 24 25 cout << endl; 26 27 cout << "复数运算测试: " << endl; 28 cout << "abs(c2) = " << abs(c2) << endl; 29 c1.add(c2); 30 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 31 cout << boolalpha; 32 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 33 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 34 c4 = add(c2, c3); 35 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 36 } 37 38 int main() { 39 test(); 40 }
运行结果:
任务3:
task3.cpp
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)1.使用了默认构造函数 Complex() {real=0;imag=0;} 2.使用普通构造函数 Complex(double r) {real=r;imag=0;} 和 Complex(double r,double i){real=r;imag=i;} 3.使用复制构造函数 Complex(const Complex&c){real=c.real;imag=c.imag;} 4.使用判断两个复数类是否相等的函数 5.使用复数类加法函数。
2)1.声明不同:此代码通过 Complex <数据类型> 类名称 (输入数据)的方法声明;用real() 和 imag() 代替了 get_real 和 get_imag;省略了输出函数output();通过(c1==c2)等式子可直接返回bool类型值;
复数类可直接进行加法运算。
2.可以将输出函数写进构造函数中以避免其反复出现,节约空间。
任务4:
Fraction.h
1 #pragma once 2 #include <iostream> 3 #include <string> 4 #include <cstdlib> 5 6 using namespace std; 7 8 class Fraction{ 9 public: 10 Fraction(); 11 Fraction(int u); 12 Fraction(int u,int d); 13 Fraction(const Fraction &f); 14 15 static const string doc; 16 17 private: 18 int up,down; 19 20 public: 21 int get_up() const; 22 int get_down() const; 23 Fraction negative(); 24 25 friend void output(Fraction f); 26 friend Fraction add(Fraction f1,Fraction f2); 27 friend Fraction sub(Fraction f1,Fraction f2); 28 friend Fraction mul(Fraction f1,Fraction f2); 29 friend Fraction div(Fraction f1,Fraction f2); 30 };
1 #include "Fraction.h" 2 3 using namespace std; 4 5 Fraction::Fraction() { 6 up=1; 7 down=1; 8 } 9 10 Fraction::Fraction(int u) { 11 up=u; 12 down=1; 13 } 14 15 Fraction::Fraction(int u,int d) { 16 up=u; 17 down=d; 18 } 19 20 Fraction::Fraction(const Fraction &f) { 21 up=f.up; 22 down=f.down; 23 } 24 25 const string Fraction::doc{"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算"}; 26 27 int Fraction::get_up() const { 28 29 if(!up && down) 30 return 0; 31 else if(down){ 32 int up1=up,down1=down; 33 int r=up1%down1; 34 while(r){ 35 up1=down1; 36 down1=r; 37 r=up1%down1; 38 }//最大公约数; 39 r=down1; 40 up1=up/r; 41 return up1; 42 } 43 } 44 int Fraction::get_down() const { 45 if(down){ 46 int up1=up,down1=down; 47 int r=up1%down1; 48 while(r){ 49 up1=down1; 50 down1=r; 51 r=up1%down1; 52 }//最大公约数; 53 r=down1; 54 down1=down/r; 55 return down1; 56 } 57 else{ 58 cout<<"分母不能为0"; 59 return 0; 60 } 61 } 62 Fraction Fraction::negative() { 63 Fraction f0; 64 f0.up=-up; 65 f0.down=down; 66 return f0; 67 } 68 69 void output(Fraction f){ 70 int up1=f.get_up(),down1=f.get_down(); 71 if(!up1 && down1) 72 cout<<0; 73 else if(down1){ 74 int flag=1; 75 if(up1*down1<0) 76 flag=-1; 77 78 up1=abs(up1); 79 down1=abs(down1); 80 cout<<flag*up1<<'/'<<down1; 81 } 82 83 } 84 85 Fraction add(Fraction f1,Fraction f2){ 86 Fraction f0; 87 int up1=f1.up,down1=f1.down,up2=f2.up,down2=f2.down; 88 89 int flag1=1,flag2=1; 90 if(up1*down1<0) 91 flag1=-1; 92 if(up2*down2<0) 93 flag2=-1; 94 95 up1=abs(up1); 96 up2=abs(up2); 97 down1=abs(down1); 98 down2=abs(down2); 99 100 int down_m; 101 int r=down1%down2; 102 while(r){ 103 down1=down2; 104 down2=r; 105 r=down1%down2; 106 }//最大公约数; 107 r=down2; 108 109 down1=abs(f1.down); 110 down2=abs(f2.down); 111 112 down_m=down1*down2/r;//最小公倍数 113 up1*=down_m/down1; 114 up2*=down_m/down2; //约分 115 116 f0.up=flag1*up1+flag2*up2; 117 f0.down=down_m; 118 return f0; 119 } 120 121 Fraction sub(Fraction f1,Fraction f2){ 122 Fraction f0=add(f1,f2.negative()); 123 return f0; 124 } 125 126 127 Fraction mul(Fraction f1,Fraction f2){ 128 Fraction f0; 129 f0.up=f1.up*f2.up; 130 f0.down=f1.down*f2.down; 131 return f0; 132 } 133 134 Fraction div(Fraction f1,Fraction f2){ 135 Fraction f0; 136 f0.up=f1.up*f2.down; 137 f0.down=f1.down*f2.up; 138 return f0; 139 }
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 }
运行结果:
任务5:
account.h
1 class SavingAccount{ 2 private: 3 int id; 4 double balance; 5 double rate; 6 int lastDate; 7 double accumulation; 8 static double total; 9 10 void record(int,double); 11 double accumulate(int date) const { 12 return accumulation+balance*(date-lastDate); 13 } 14 public: 15 SavingAccount(int,int,double); 16 int get_ID() const {return id;} 17 double getBalance() const {return balance;} 18 double getRate() const {return rate;} 19 static double getTotal() {return total;} 20 void deposit(int,double); 21 void withdraw(int,double); 22 void settle(int); 23 void show() const; 24 };
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 5 using namespace std; 6 7 double SavingAccount::total=0; 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 void SavingAccount::record(int date,double amount){ 13 accumulation=accumulate(date); 14 lastDate=date; 15 amount=floor(amount*100+0.5)/100; 16 balance+=amount; 17 total+=amount; 18 cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl; 19 } 20 void SavingAccount::deposit(int date,double amount){ 21 record(date,amount); 22 } 23 void SavingAccount::withdraw(int date,double amount){ 24 if(amount>getBalance()) 25 cout<<"Error: not enough money"<<endl; 26 else 27 record(date,-amount); 28 } 29 void SavingAccount::settle(int date){ 30 double interest=accumulate(date)*rate/365; 31 if(interest!=0) 32 record(date,interest); 33 accumulation=0; 34 } 35 void SavingAccount::show() const{ 36 cout<<"#"<<id<<"\tBalance: "<<balance; 37 }
1 #include "account.h" 2 #include <iostream> 3 using namespace std; 4 int main(){ 5 SavingAccount sa0(1,21325302,0.015); 6 SavingAccount sa1(1,58320212,0.015); 7 8 sa0.deposit(5,5000); 9 sa1.deposit(25,10000); 10 sa0.deposit(45,5500); 11 sa1.withdraw(60,4000); 12 sa0.settle(90); 13 sa1.settle(90); 14 sa0.show();cout<<endl; 15 sa1.show();cout<<endl; 16 cout<<"Total: "<<SavingAccount::getTotal()<<endl; 17 return 0; 18 }
运行结果:
问题:合理,但阅读起来不够简洁