guguabiu

导航

实验2

任务1:

t.h:

 1 #pragma once
 2 #include <string>
 3 class T {
 4 public:
 5     T(int x = 0, int y = 0);  
 6     T(const T &t);  
 7     T(T &&t);       
 8     ~T();           
 9 
10     void adjust(int ratio);     
11     void display() const;        
12 private:
13     int m1, m2;
14 public:
15     static int get_cnt();         
16 public:
17     static const std::string doc;       
18     static const int max_cnt;           
19 
20 private:
21     static int cnt;        
22     friend void func();
23 };
24 void func();
View Code

t.cpp:

 1 #include "t.h"
 2 #include <iostream>
 3 #include <string>
 4 using std::cout;
 5 using std::endl;
 6 using std::string;
 7 
 8 const std::string T::doc{"a simple class sample"};
 9 const int T::max_cnt = 999;
10 int T::cnt = 0;
11 
12 T::T(int x, int y): m1{x}, m2{y} { 
13     ++cnt; 
14     cout << "T constructor called.\n";
15 } 
16 T::T(const T &t): m1{t.m1}, m2{t.m2} {
17     ++cnt;
18     cout << "T copy constructor called.\n";
19 }
20 T::T(T &&t): m1{t.m1}, m2{t.m2} {
21     ++cnt;
22     cout << "T move constructor called.\n";
23 }    
24 T::~T() {
25     --cnt;
26     cout << "T destructor called.\n";
27 }           
28 void T::adjust(int ratio) {
29     m1 *= ratio;
30     m2 *= ratio;
31 }    
32 void T::display() const {
33     cout << "(" << m1 << ", " << m2 << ")" ;
34 }     
35 int T::get_cnt() {
36    return cnt;
37 }
38 void func() {
39     T t5(42);
40     t5.m2 = 2049;
41     cout << "t5 = "; t5.display(); cout << endl;
42 }
View Code

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 }
View Code

 

问题1:可以去掉line36;

问题2:普通构造函数:用于创建对象时初始化对象的成员变量;当一个对象被创建时被调用;

             复制构造函数:用于通过另一个同类型对象初始化一个新对象;当一个对象被复制时被调用;

             移动构造函数:用于通过临时对象初始化一个新对象;当一个对象被移动时被调用;

问题3:可以正确编译运行。

任务2:

Complex.h:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <string>
 4 
 5 class Complex {
 6 public:
 7     static const std::string doc; 
 8 private:
 9     double real; 
10     double imag; 
11 
12 public:
13     Complex(); 
14     Complex(double real); 
15     Complex(double real, double imag); 
16     Complex(const Complex& c); 
17     double get_real() const; 
18     double get_imag() const; 
19     void add(const Complex& c); 
20 
21     friend Complex add(const Complex& c1, const Complex& c2); 
22     friend bool is_equal(const Complex& c1, const Complex& c2); 
23     friend bool is_not_equal(const Complex& c1, const Complex& c2); 
24     friend void output( const Complex& c); 
25     friend double abs(const Complex& c); 
26 
27 
28 };

Complex.cpp:

 1 #include "Complex.h"
 2 #include<iostream>
 3 #include<string>
 4 
 5 using std::cout;
 6 using std::endl;
 7 using std::string;
 8 
 9 
10 const std::string Complex::doc = "a simplified complex class";
11 
12 Complex::Complex() : real(0), imag(0) {}
13 Complex::Complex(double real) : real(real), imag(0) {}
14 Complex::Complex(double real, double imag) : real(real), imag(imag) {}
15 Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {}
16 
17 double Complex::get_real() const {
18     return real;
19 }
20 
21 double Complex::get_imag() const {
22     return imag;
23 }
24 
25 void Complex::add(const Complex& c) {
26     real += c.real;
27     imag += c.imag;
28 }
29 
30 Complex add(const Complex& c1, const Complex& c2) {
31     return Complex(c1.real + c2.real, c1.imag + c2.imag);
32 }
33 
34 
35 bool is_equal(const Complex& c1, const Complex& c2) {
36     return (c1.real == c2.real) && (c1.imag == c2.imag);
37 }
38 
39 bool is_not_equal(const Complex& c1, const Complex& c2) {
40     return !is_equal(c1, c2);
41 }
42 
43 void output(const Complex& c) {
44     if(c.imag>=0)
45     {
46         cout << c.real << "+" << c.imag << "i";
47     }
48     else{
49         cout << c.real << "-"  << (-1)*c.imag <<"i";
50     }
51 }
52 
53 double abs(const Complex& c) {
54     return std::sqrt(c.real * c.real + c.imag * c.imag);
55 }

task2.cpp:

 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     return 0;
42 }
View Code

 

任务3:

 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 }
View Code

 端口:构造函数:complex<double> c1;complex<double> c2(3,-4);complex<double> c3(3.5);complex<double> c4(c3);

           成员函数:c4.real();c4.imag();

           全局函数:abs();

直接使用complex类内置的功能和运算符重载,比如负数的加法和比较操作都是通过运算符重载直接完成的,相比于任务2需要调用额外的函数要简洁的多。

任务4:

Fraction.h:

 1 #pragma once
 2 #include <string>
 3 
 4 class Fraction {
 5 public:
 6     static const std::string doc;  
 7 
 8     Fraction(int up = 0, int down = 1);    
 9     Fraction(const Fraction &f);           
10 
11     int get_up() const;                    
12     int get_down() const;                  
13     Fraction negative() const;             
14 
15     friend Fraction add(const Fraction &f1, const Fraction &f2);
16     friend Fraction sub(const Fraction &f1, const Fraction &f2);
17     friend Fraction mul(const Fraction &f1, const Fraction &f2);
18     friend Fraction div(const Fraction &f1, const Fraction &f2);
19     friend void output(const Fraction &f);
20 
21 private:
22     int up, down;                          
23     void simplify();                       
24     int gcd(int a, int b) const;           
25 };
View Code

Fraction.cpp:

 1 #include "Fraction.h"
 2 #include <iostream>
 3 #include <cmath>
 4 
 5 const std::string Fraction::doc = "Fraction类 v 0.01版. 目前仅支持分数对象的构造、输出、加/减/乘/除运算.";
 6 
 7 int Fraction::gcd(int a, int b) const {
 8     return b == 0 ? a : gcd(b, a % b);
 9 }
10 
11 Fraction::Fraction(int up, int down) : up(up), down(down) {
12     if (down == 0) {
13         std::cerr << "分母不能为0" << std::endl;
14         exit(EXIT_FAILURE);
15     }
16     simplify();
17 }
18 
19 int Fraction::get_up() const {
20     return up;
21 }
22 
23 int Fraction::get_down() const {
24     return down;
25 }
26 
27 Fraction::Fraction(const Fraction &f) : up(f.up), down(f.down) {}
28 
29 Fraction Fraction::negative() const {
30     return Fraction(-up, down);
31 }
32 
33 void Fraction::simplify() {
34     int g = gcd(abs(up), abs(down));
35     up /= g;
36     down /= g;
37     if (down < 0) {
38         up = -up;
39         down = -down;
40     }
41 }
42 
43 Fraction add(const Fraction &f1, const Fraction &f2) {
44     int up = f1.up * f2.down + f2.up * f1.down;
45     int down = f1.down * f2.down;
46     return Fraction(up, down);
47 }
48 
49 Fraction sub(const Fraction &f1, const Fraction &f2) {
50     int up = f1.up * f2.down - f2.up * f1.down;
51     int down = f1.down * f2.down;
52     return Fraction(up, down);
53 }
54 
55 Fraction mul(const Fraction &f1, const Fraction &f2) {
56     return Fraction(f1.up * f2.up, f1.down * f2.down);
57 }
58 
59 Fraction div(const Fraction &f1, const Fraction &f2) {
60     if (f2.up == 0) {
61         std::cerr << "分母不能为0" << std::endl;
62         exit(EXIT_FAILURE);
63     }
64     return Fraction(f1.up * f2.down, f1.down * f2.up);
65 }
66 
67 void output(const Fraction &f) {
68     if (f.down == 1) {
69         std::cout << f.up; 
70     } else {
71         std::cout << f.up << "/" << f.down; 
72     }
73 }
View Code

task4.cpp:

 1 #include "Fraction.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 void test1() {
 8     cout << "Fraction类测试: " << endl;
 9     cout << Fraction::doc << endl << endl;
10 
11     Fraction f1(5);
12     Fraction f2(3, -4), f3(-18, 12);
13     Fraction f4(f3);
14 
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 
23     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
24 
25     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
26     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
27     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
28     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
29 
30     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
31 }
32 
33 void test2() {
34     Fraction f6(42, 55), f7(0, 3);
35     cout << "f6 = "; output(f6); cout << endl;
36     cout << "f7 = "; output(f7); cout << endl;
37     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
38 }
39 
40 int main() {
41     cout << "测试1: Fraction类基础功能测试\n";
42     test1();
43     cout << "\n测试2: 分母为0测试: \n";
44     test2();
45     return 0;
46 }
View Code

 

任务5:

account.h:

 1 #ifndef __ACCOUNT_H__
 2 #define __ACCOUNT_H__
 3 
 4 class SavingsAccount {
 5 private:
 6     int id;                 
 7     double balance;         
 8     double rate;            
 9     int lastDate;           
10     double accumulation;     
11     static double total;     
12 
13     void record(int date, double amount); 
14     double accumulate(int date) const {   
15         return accumulation + balance * (date - lastDate);
16     }
17 
18 public:
19     SavingsAccount(int date, int id, double rate); 
20     int getId() const { return id; }               
21     double getBalance() const { return balance; }   
22     double getRate() const { return rate; }         
23     static double getTotal() { return total; }      
24 
25     void deposit(int date, double amount);           
26     void withdraw(int date, double amount);          
27     void settle(int date);                            
28     void show() const;                               
29 };
30 
31 #endif // __ACCOUNT_H__
View Code

account.cpp:

 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 double SavingsAccount::total = 0;
 7 SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),balance(0),rate(rate),lastDate(date),accumulation(0){
 8     cout << date <<"\t#" << id << " is created" <<endl;
 9 }
10 void SavingsAccount::record(int date,double amount){
11     accumulation = accumulate(date);
12     lastDate = date;
13     amount = floor(amount*100+0.5)/100;
14     balance+=amount;
15     total+=amount;
16     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
17 }
18 void SavingsAccount::deposit(int date,double amount){
19     record(date,amount);
20 }
21 void SavingsAccount::withdraw(int date,double amount){
22     if(amount>getBalance())
23         cout << "Error:not enough money" << endl;
24     else
25         record(date,-amount);
26 }
27 void SavingsAccount::settle(int date){
28     double interest = accumulate(date)*rate/365;
29     if(interest!=0)
30         record(date,interest);
31     accumulation = 0;
32 }
33 void SavingsAccount::show() const {
34     cout << "#" << id << "\tBalance: " << balance;
35 }
View Code

5_11.cpp:

 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main(){
 5     SavingsAccount sa0(1,21325302,0.015);
 6     SavingsAccount sa1(1,58320212,0.015);
 7     sa0.deposit(5,5000);
 8     sa1.deposit(25,10000);
 9     sa0.deposit(45,5500);
10     sa1.withdraw(60,4000);
11     sa0.settle(90);
12     sa1.settle(90);
13     sa0.show();cout << endl;
14     sa1.show();cout << endl;
15     cout << "Total:" << SavingsAccount::getTotal() << endl;
16 }
View Code

合理。

我觉得在deposit和withraw函数中应该再加入负数检查来检测amount是否为负数。

 

posted on 2024-10-29 17:36  絮尘。  阅读(5)  评论(0编辑  收藏  举报