cpp 实验2

 1 t.h
 2 #pragma once
 3 #include<string>
 4 using namespace std;
 5 class T {
 6 
 7 public:
 8     T(int x = 0, int y = 0);
 9     T(const T& t);
10     T(T&& t);
11     ~T();
12     void adjust(int ratio);
13     void display() const;
14 private:
15     int m1, m2;
16 public:
17     static const string doc;
18     static const int max_cnt; \
19         static int get_cnt();
20 private:
21     static int cnt;
22     friend void func();
23 };
24 void func();
25 
26 
27 t.cpp
28 #include"t.h"
29 #include<iostream>
30 using namespace std;
31 const string T::doc{"a simple class sample"};
32 const int T::max_cnt = 999;
33 int T::cnt = 0;
34 T::T(int x, int y) :m1{ x }, m2{ y } {
35     cnt++;
36     cout << "T constructor called.\n";
37 }
38 T::T(const T& t) :m1{ t.m1 }, m2{ t.m2 } {
39     cnt++;
40     cout << "T copy constructor called.\n";
41 }
42 T::T(T&& t) :m1{ t.m1 }, m2{ t.m2 } {
43     cnt++;
44     cout << "T move constructor called.\n";
45 }
46 T::~T() {
47     cnt--;
48     cout << "T destructor called.\n";
49 }
50 void T::adjust(int ratio) {
51     m1 *= ratio;
52     m2 *= ratio;
53 }
54 void T::display() const {
55     cout << "(" << m1 << "," << m2 << ")";
56 }
57 int T::get_cnt() {
58     return cnt;
59 }
60 void func() {
61     T t5(42);
62     t5.m2 = 2049;
63     cout << "t5="; t5.display(); cout << endl;
64 }
65 
66 task1.cpp
67 #include"t.h"
68 #include<iostream>
69 using namespace std;
70 void test();
71 int main() {
72     test();
73     test();
74     cout << "\nmain: \n";
75     cout << "T objects'current count: " << T::get_cnt() << endl;
76 }
77 void test() {
78     cout << "test class T: \n";
79     cout << "T info: " << T::doc << endl;
80     cout << "T objects'max count: " << T::max_cnt << endl;
81     cout << "T objects'current count: " << T::get_cnt() << endl << endl;
82     T t1;
83     cout << "t1 = "; t1.display(); cout << endl;
84     T t2(3, 4);
85     cout << "t2 = "; t2.display(); cout << endl;
86     T t3(t2);
87     t3.adjust(2);
88     cout << "t3 = "; t3.display(); cout << endl;
89     T t4(std::move(t2));
90     cout << "t3 = "; t4.display(); cout << endl;
91     cout << "T objects'current count: " << T::get_cnt() << endl;
92     func();
93 }
task1

1.在函数定义的时候没有指明所属类。
2.(1)简单构造函数,在没有实参,或者实参为整形数的时候调用。(2)复制构造函数在,实参为为类对象的引用的时候调用。(3)移动构造函数,当实参含有move的时候使用。
析构函数在程序运行结束,空间被系统回收时会调用。
3.不能。

  1 complex.h
  2 #pragma once
  3 #include<iostream>
  4 #include<cmath>
  5 using namespace std;
  6 class Complex {
  7 public:
  8     Complex(double r = 0, double i = 0);
  9     Complex(const Complex& c);
 10     double get_real() const;
 11     double get_imag() const;
 12     void add(Complex& c);
 13     friend Complex add(const Complex& c1, const Complex& c2);
 14     friend bool is_equal(Complex& c1, Complex& c2);
 15     friend bool is_not_equal(const Complex& c1, const Complex& c2);
 16     friend void output(const Complex& c);
 17     friend double abs(Complex& c);
 18     const static string doc();
 19 private:
 20     double real;
 21     double imag;
 22 };
 23 Complex add(const Complex& c1, const Complex& c2);
 24 bool is_equal(Complex& c1, Complex& c2);
 25 bool is_not_equal(const Complex& c1, const Complex& c2);
 26 void output(const Complex& c);
 27 double abs(Complex& c);
 28 
 29 complex.cpp
 30 #include"Complex.h"
 31 Complex::Complex(double r, double i) :real(r), imag(i) {}
 32 Complex::Complex(const Complex& c):real{c.real},imag{c.imag}{}
 33 double Complex::get_real() const {
 34     return real;
 35 }
 36 double Complex::get_imag()const {
 37 
 38     return imag;
 39 
 40 }
 41 void Complex::add(Complex& c) {
 42     real += c.get_real();
 43     imag += c.get_imag();
 44 }
 45 Complex add(const Complex& c1, const Complex& c2) {
 46 
 47     Complex c3(c1.get_real() + c2.get_real(), c1.get_imag() + c2.get_imag());
 48 
 49     return c3;
 50 
 51 }
 52 bool is_equal(Complex& c1, Complex& c2) {
 53 
 54     if ((c1.get_real() == c2.get_real()) && (c1.get_imag() == c2.get_imag())) {
 55 
 56         return true;
 57 
 58     }
 59 
 60     else {
 61 
 62         return false;
 63 
 64     }
 65 
 66 }
 67 bool is_not_equal(const Complex& c1, const Complex& c2) {
 68 
 69     if ((c1.get_real() == c2.get_real()) && (c1.get_imag() == c2.get_imag())) {
 70         return false;
 71     }
 72     return true;
 73 }
 74 void output(const Complex& c) {
 75     if (c.get_imag() >= 0)
 76         cout << c.get_real() << "+" << c.get_imag() << "i" << endl;
 77     else
 78         cout << c.get_real() << c.get_imag() << "i" << endl;
 79 }
 80 double abs(Complex& c) {
 81 
 82     return sqrt(c.get_real() * c.get_real() + c.get_imag() * c.get_imag());
 83 
 84 }
 85 const string Complex::doc() {
 86 
 87     return "a simplified complex class";
 88 
 89 }
 90 
 91 task2.cpp
 92 #include"Complex.h"
 93 void test() {
 94 
 95     cout << "类成员测试: " << endl;
 96 
 97     cout << Complex::doc() << endl;
 98 
 99 
100 
101     cout << endl;
102 
103 
104 
105     cout << "Complex对象测试: " << endl;
106 
107     Complex c1;
108 
109     Complex c2(3, -4);
110 
111     const Complex c3(3.5);
112 
113     Complex c4(c3);
114 
115 
116 
117     cout << "c1 = "; output(c1); cout << endl;
118 
119     cout << "c2 = "; output(c2); cout << endl;
120 
121     cout << "c3 = "; output(c3); cout << endl;
122 
123     cout << "c4 = "; output(c4); cout << endl;
124 
125     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
126 
127 
128 
129     cout << endl;
130 
131 
132 
133     cout << "复数运算测试: " << endl;
134 
135     cout << "abs(c2) = " << abs(c2) << endl;
136 
137     c1.add(c2);
138 
139     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
140 
141     cout << boolalpha;
142 
143     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
144 
145     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
146 
147     c4 = add(c2, c3);
148 
149     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
150 
151 }
152 int main() {
153     test();
154 }
task2

 1 #include<iostream>
 2 #include<complex>
 3 using namespace std;
 4 void test() {
 5     cout << "标准库模板类comple测试: " << endl;
 6     complex<double> c1;
 7     complex<double> c2(3, -4);
 8     const complex<double> c3(3.5);
 9     complex<double> c4(c3);
10     cout << "c1 = " << c1 << endl;
11     cout << "c2 = " << c2 << endl;
12     cout << "c3 = " << c3 << endl;
13     cout << "c4 = " << c4 << endl;
14     cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() <<
15         endl;
16     cout << endl;
17     cout << "复数运算测试: " << endl;
18     cout << "abs(c2)=" << abs(c2) << endl;
19     c1 += c2;
20     cout << "c1 += c2, c1 = " << c1 << endl;
21     cout << boolalpha;
22     cout << "c1 == c2 : " << (c1 == c2) << endl;
23     cout << "c1 != c3 : " << (c1 != c3) << endl;
24     c4 = c2 + c3;
25     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
26 
27 }
28 int main() {
29     test();
30 }
task3

  1 fraction.h
  2 #include<iostream>
  3 using namespace std;
  4 class Fraction {
  5 public:
  6     Fraction(int fz=0, int fm = 1);
  7     Fraction(const Fraction& f);
  8     int get_up()const;
  9     int get_down()const;
 10     Fraction negative() const;
 11     friend void output(const Fraction &f);
 12     friend Fraction add(const Fraction& f1, const Fraction& f2);
 13     friend Fraction sub(const Fraction& f1, const Fraction& f2);
 14     friend Fraction mul(const Fraction& f1, const Fraction& f2);
 15     friend Fraction div(const Fraction& f1,const Fraction& f2);
 16     static string doc();
 17 private:
 18     int up;
 19     int down;
 20 };
 21 void output(const Fraction& f);
 22 Fraction add(const Fraction& f1,const Fraction& f2);
 23 Fraction sub(const Fraction& f1,const Fraction& f2);
 24 Fraction mul(const Fraction& f1,const Fraction& f2);
 25 Fraction div(const Fraction& f1,const Fraction& f2);
 26 
 27 fraction.cpp
 28 #include"Fraction.h"
 29     int gcd(int x, int y) {
 30         if (x == 0 || y == 0) {
 31             return x ? y == 0 : y;
 32         }
 33         if (x%y == 0)
 34             return y;
 35         else
 36             return gcd(y, x % y);
 37     }
 38     Fraction::Fraction(int fz,int fm):up(fz),down(fm){
 39         int flag = 0;
 40         if (up * down < 0) {
 41             flag = 1;
 42         }
 43         fz = abs(up);
 44         fm = abs(down);
 45         int t = gcd(fz, fm);
 46         fz /= t;
 47         fm /= t;
 48         if (flag == 1) {
 49             up = fz * -1;
 50             down = fm;
 51         }
 52         else {
 53             up = fz;
 54             down = fm;
 55         }
 56     
 57     }
 58     Fraction::Fraction(const Fraction &f):up(f.up),down(f.down){
 59         int flag = 0;
 60         if (up * down < 0) {
 61             flag = 1;
 62         }
 63         int fz = abs(up);
 64         int fm = abs(down);
 65         int t = gcd(fz, fm);
 66         fz /= t;
 67         fm /= t;
 68         if (flag == 1) {
 69             up = -fz;
 70             down = fm;
 71         }
 72         else {
 73             up = fz;
 74             down = fm;
 75         }
 76 
 77 task4.cpp
 78 #include "Fraction.h"
 79 void test1() {
 80     cout << "Fraction类测试: " << endl;
 81     cout << Fraction::doc() << endl << endl;
 82     Fraction f1(5);
 83     Fraction f2(3, -4), f3(-18, 12);
 84     Fraction f4(f3);
 85     cout << "f1 = "; output(f1); cout << endl;
 86     cout << "f2 = "; output(f2); cout << endl;
 87     cout << "f3 = "; output(f3); cout << endl;
 88     cout << "f4 = "; output(f4); cout << endl;
 89     Fraction f5(f4.negative());
 90     cout << "f5 = "; output(f5); cout << endl;
 91     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " <<
 92         f5.get_down() << endl;
 93     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
 94     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
 95     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
 96     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
 97     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
 98 
 99 }
100 void test2() {
101     Fraction f6(42, 55), f7(0, 3);
102     cout << "f6 = "; output(f6); cout << endl;
103     cout << "f7 = "; output(f7); cout << endl;
104     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
105 }
106 int main() {
107     cout << "测试1: Fraction类基础功能测试\n";
108     test1();
109     cout << "\n测试2: 分母为0测试: \n";
110     test2();
111 }
task4

 1 account.h
 2 #pragma once
 3 class SavingsAccount {
 4 private:
 5     int id;
 6     double balance;
 7     double rate;
 8     int lastDate;
 9     double accumulation;
10     static double total;
11     void record(int date, double amount);
12     double accumulate(int date) const {
13         return accumulation + balance * (date - lastDate);
14     }
15 public:
16     SavingsAccount(int date, int id, double rate);
17     int getId()const { return id; }
18     double getBalance()const { return balance; }
19     double getRate()const { return rate; }
20     static double getTotal() { return total; }
21     void deposit(int date, double amount);
22     void withdraw(int date, double amount);
23     void settle(int date);
24     void show()const;
25 };
26 
27 
28 account.cpp
29 #include"account.h"
30 #include<cmath>
31 #include<iostream>
32 using namespace std;
33 double SavingsAccount::total = 0;
34 SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
35     cout << date << "\t#" << id << "is created" << endl;
36 }
37 void SavingsAccount::record(int date, double amount) {
38     accumulation = accumulate(date);
39     lastDate = date;
40     amount = floor(amount * 100 + 0.5) / 100;
41     balance += amount;
42     total += amount;
43     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
44 }
45 void SavingsAccount::deposit(int date, double amount) {
46     record(date, amount);
47 }
48 void SavingsAccount::withdraw(int date,double amount) {
49     if (amount > getBalance())
50         cout << "Error:not enough money" << endl;
51     else {
52         record(date, -amount);
53     }
54 }
55 void SavingsAccount::settle(int date) {
56     double interest = accumulate(date) * rate / 365;
57     if (interest != 0)
58         record(date, interest);
59     accumulation = 0;
60 }
61 void SavingsAccount::show()const {
62     cout << "#" << id << "\tBalance:" << balance;
63 }
64 
65 
66 task5.cpp
67 #include"account.h"
68 #include<iostream>
69 using namespace std;
70 int main() {
71     SavingsAccount sa0(1, 21325302, 0.015);
72     SavingsAccount sa1(1, 58320212, 0.015);
73     sa0.deposit(5, 5000);
74     sa1.deposit(25, 10000);
75     sa0.deposit(45, 5500);
76     sa1.withdraw(60, 4000);
77     sa0.settle(90);
78     sa1.settle(90);
79     sa0.show(); cout << endl;
80     sa1.show(); cout << endl;
81     cout << "Total: " << SavingsAccount::getTotal() << endl;
82     return 0;
83 }
View Code

 

posted @ 2024-10-22 22:50  泽康郁  阅读(62)  评论(0编辑  收藏  举报