<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      OOP實驗二

      任務1

      源碼:

       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();
      // 類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;
      }
       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 }

       

      結果:

      問:t.h中,普通函數 func 作為類X的友元,在類的內部聲明了友元關系。在類外部,去掉

      line36,重新編譯,是否能正確運行。如果能,回答說明可以去掉line36。如果不能,以截圖
      形式給出編譯報錯信息,分析可能的原因。

      答:不能

       在類內部的聲明不會提醒main函數普通函數func的存在。

      問:t.h中,line9-12給出了各種構造函數、析構函數。總結各種構造函數的功能,以及它們與析

      構函數的調用時機。

      答:

      普通構造函數:通過數值創建對象實例。

      復制構造函數:利用一個已知的對象實例給新實例賦值。

      移動構造函數:在處理臨時對象是時提高效率。

      析構函數:釋放實例占用的內存。

      問:t.cpp中,line13-15,調整到t.h,重新編譯,程序能否正確編譯運行。

      答:不能。

      任務2

      源碼:

       

        1 //.h:
        2 #pragma once
        3 
        4 #include<iostream>
        5 #include<cmath>
        6 
        7 using std::cout;
        8 using std::endl;
        9 using std::boolalpha;
       10 
       11 void test();
       12 
       13 class Complex {
       14 private:
       15     double real, imag;
       16 public:
       17     static std::string doc;
       18     Complex(double r = 0, double i = 0) :real{ r }, imag{ i } {}
       19     Complex(const Complex& c) :real{ c.get_real() }, imag{ c.get_imag() } {}
       20     double get_real() const {
       21         return real;
       22     }
       23     double get_imag() const {
       24         return imag;
       25     }
       26     void add(const Complex c) {
       27         real += c.get_real();
       28         imag += c.get_imag();
       29     }
       30     friend Complex add(const Complex c1, const Complex c2);
       31     friend bool is_equal(const Complex& c1, const Complex& c2);
       32     friend bool is_not_equal(const Complex& c1, const Complex& c2);
       33     friend void output(const Complex& c);
       34     friend double abs(const Complex& c);
       35 };
       36 
       37 Complex add(const Complex c1, const Complex c2);
       38 bool is_equal(const Complex& c1, const Complex& c2);
       39 bool is_not_equal(const Complex& c1, const Complex& c2);
       40 void output(const Complex& c);
       41 double abs(const Complex& c);
       42 
       43 //.cpp:
       44 #include"Complex.h"
       45 
       46 std::string Complex::doc = "a simple complex class";
       47 
       48 void test() {
       49     cout << "類成員測試: " << endl;
       50     cout << Complex::doc << endl;
       51 
       52     cout << endl;
       53 
       54     cout << "Complex對象測試: " << endl;
       55     Complex c1;
       56     Complex c2(3, -4);
       57     const Complex c3(3.5);
       58     Complex c4(c3);
       59 
       60     cout << "c1 = "; output(c1); cout << endl;
       61     cout << "c2 = "; output(c2); cout << endl;
       62     cout << "c3 = "; output(c3); cout << endl;
       63     cout << "c4 = "; output(c4); cout << endl;
       64     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
       65 
       66     cout << endl;
       67 
       68     cout << "復數運算測試: " << endl;
       69     cout << "abs(c2) = " << abs(c2) << endl;
       70     c1.add(c2);
       71     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
       72     cout << boolalpha;
       73     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
       74     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
       75     c4 = add(c2, c3);
       76     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
       77 }
       78 Complex add(const Complex c1,const Complex c2) {
       79     Complex c3;
       80     c3.add(c2);
       81     c3.add(c1);
       82     return c3;
       83 }
       84 bool is_equal(const Complex& c1, const Complex& c2) {
       85     if (c1.get_imag() == c2.get_imag() && c1.get_real() == c2.get_real())
       86     {
       87         return true;
       88     }
       89     else
       90     {
       91         return false;
       92     }
       93 }
       94 
       95 bool is_not_equal(const Complex& c1, const Complex& c2) {
       96     if (c1.get_imag() == c2.get_imag() && c1.get_real() == c2.get_real())
       97     {
       98         return false;
       99     }
      100     else
      101     {
      102         return true;
      103     }
      104 }
      105 
      106 void output(const Complex& c) {
      107     if (c.get_imag() >= 0) {
      108         cout << c.get_real() << '+' << c.get_imag() << 'i' << endl;
      109     }
      110     else {
      111         cout << c.get_real() << '-' << abs(c.get_imag()) << 'i' << endl;
      112     }
      113 }
      114 double abs(const Complex& c) {
      115     return sqrt(c.get_imag() * c.get_imag() + c.get_real() * c.get_real());
      116 
      117 }
      118 
      119 //.main:
      120 #include"Complex.h"
      121 int main() {
      122     test();
      123 }

       

      結果:

       

       

      任務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 }

       

      結果:

       

      任務4

      源碼:

       

        1 //.h
        2 #pragma once
        3 #include <iostream>
        4 
        5 using std::cout;
        6 using std::endl;
        7 
        8 int measure(int x, int y);
        9 class Fraction {
       10 private:
       11     int up;
       12     int down;
       13 public:
       14     static std::string doc;
       15     Fraction(int u, int d = 1){
       16         int mea = measure(u, d);
       17         up = u / mea;
       18         down = d / mea;
       19         if (down < 0)
       20         {
       21             down = -down;
       22             up = -up;
       23         }
       24     }
       25     int get_up() const {
       26         return up;
       27     }
       28     int get_down() const {
       29         return down;
       30     }
       31     Fraction negative() {
       32         Fraction f(-up, down);
       33         return f;
       34     }
       35     friend void output(const Fraction& f);
       36     friend Fraction add(const Fraction f1, const Fraction f2);
       37     friend Fraction sub(const Fraction f1, const Fraction f2);
       38     friend Fraction mul(const Fraction f1, const Fraction f2);
       39     friend Fraction div(const Fraction f1, const Fraction f2);
       40 };
       41 
       42 Fraction add(const Fraction f1, const Fraction f2);
       43 Fraction sub(const Fraction f1, const Fraction f2);
       44 Fraction mul(const Fraction f1, const Fraction f2);
       45 Fraction div(const Fraction f1, const Fraction f2);
       46 void output(const Fraction& f);
       47 
       48 //.cpp
       49 #include "Fraction.h"
       50 
       51 std::string Fraction::doc = "Fraction類 v 0.01版.\n目前僅支持分數對象的構造、輸出、加/減/乘/除運算\n";
       52 Fraction add(const Fraction f1, const Fraction f2) {
       53     int up1, up2, down1, down2, up, down;
       54     up1 = f1.up;
       55     up2 = f2.up;
       56     down1 = f1.down;
       57     down2 = f2.down;
       58     up = up1 * down2 + up2 * down1;
       59     down = down1 + down2;
       60     int mea = measure(up, down);
       61     Fraction f(up / mea, down / mea);
       62     return f;
       63 }
       64 Fraction sub(const Fraction f1, const Fraction f2) {
       65     int up1, up2, down1, down2, up, down;
       66     up1 = f1.up;
       67     up2 = f2.up;
       68     down1 = f1.down;
       69     down2 = f2.down;
       70     up = up1 * down2 - up2 * down1;
       71     down = down1 + down2;
       72     int mea = measure(up, down);
       73     Fraction f(up / mea, down / mea);
       74     return f;
       75 }
       76 Fraction mul(const Fraction f1, const Fraction f2) {
       77     int up1, up2, down1, down2, up, down;
       78     up1 = f1.up;
       79     up2 = f2.up;
       80     down1 = f1.down;
       81     down2 = f2.down;
       82     up = up1 * up2;
       83     down = down1 * down2;
       84     int mea = measure(up, down);
       85     Fraction f(up / mea, down / mea);
       86     return f;
       87 }
       88 Fraction div(const Fraction f1, const Fraction f2) {
       89     if (f2.up == 0)
       90     {
       91         Fraction error(0, 0);
       92         return error;
       93     }
       94     int up1, up2, down1, down2, up, down;
       95     up1 = f1.up;
       96     up2 = f2.up;
       97     down1 = f1.down;
       98     down2 = f2.down;
       99     up = up1 * down2;
      100     down = up2 * down1;
      101     int mea = measure(up, down);
      102     Fraction f(up / mea, down / mea);
      103     return f;
      104 }
      105 void output(const Fraction& f) {
      106     if (f.down != 0)
      107     {
      108         if (f.up == 0 || f.down == 1)
      109         {
      110             cout << f.up;
      111         }
      112         else
      113         {
      114             cout << f.up << "/" << f.down;
      115         }
      116     }
      117     else 
      118     {
      119         cout << "分母不能為0";
      120     }
      121 }
      122 int measure(int x, int y)
      123 {
      124     if (y == 0)
      125     {
      126         return 1;
      127     }
      128     int z = y;
      129     while (x % y != 0)
      130     {
      131         z = x % y;
      132         x = y;
      133         y = z;
      134     }
      135     return z;
      136 }
      137 
      138 //.main
      139 #include"Fraction.h"
      140 
      141 void test1() {
      142     cout << "Fraction類測試: " << endl;
      143     cout << Fraction::doc << endl << endl;
      144 
      145     Fraction f1(5);
      146     Fraction f2(3, -4), f3(-18, 12);
      147     Fraction f4(f3);
      148     cout << "f1 = "; output(f1); cout << endl;
      149     cout << "f2 = "; output(f2); cout << endl;
      150     cout << "f3 = "; output(f3); cout << endl;
      151     cout << "f4 = "; output(f4); cout << endl;
      152 
      153     Fraction f5(f4.negative());
      154     cout << "f5 = "; output(f5); cout << endl;
      155     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
      156 
      157     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
      158     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
      159     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
      160     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
      161     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
      162 }
      163 
      164 void test2() {
      165     Fraction f6(42, 55), f7(0, 3);
      166     cout << "f6 = "; output(f6); cout << endl;
      167     cout << "f7 = "; output(f7); cout << endl;
      168     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
      169 }
      170 
      171 int main() {
      172     cout << "測試1: Fraction類基礎功能測試\n";
      173     test1();
      174 
      175     cout << "\n測試2: 分母為0測試: \n";
      176     test2();
      177 }

       

      結果:

       

      任務5

      源碼:

       1 //.h
       2 #ifndef __ACCOUNT_H__
       3 #define __ACCOUNT_H__
       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     void record(int date, double amount);
      13     double accumulate(int date)const {
      14         return accumulation + balance * (date - lastDate);
      15     }
      16 public:
      17     SavingsAccount(int date, int id, double rate);
      18     int getId()const { return id; }
      19     double getBalance()const { return balance; }
      20     double getRate()const { return rate; }
      21     static double getTotal() { return total; }
      22     void deposit(int date, double amount);
      23     void withdraw(int date, double amount);
      24     void settle(int date);
      25     void show()const;
      26 };
      27 #endif // !__ACCOUNT__
      28 
      29 //.cpp
      30 #include"account.h"
      31 #include<cmath>
      32 #include<iostream>
      33 using namespace std;
      34 double SavingsAccount::total = 0;
      35 SavingsAccount::SavingsAccount(int date, int id, double rate)
      36     :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
      37     cout << date << "\t#" << id << " is created" << endl;
      38 }
      39 void SavingsAccount::record(int date, double amount) {
      40     accumulation = accumulate(date);
      41     lastDate = date;
      42     amount = floor(amount * 100 + 0.5) / 100;
      43     balance += amount;
      44     total += amount;
      45     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
      46 }
      47 void SavingsAccount::deposit(int date, double amount) {
      48     record(date, amount);
      49 }
      50 void SavingsAccount::withdraw(int date, double amount) {
      51     if (amount > getBalance())
      52         cout << "Error:not enough money" << endl;
      53     else
      54         record(date, -amount);
      55 }
      56 void SavingsAccount::settle(int date) {
      57     double interest = accumulate(date) * rate / 365;
      58     if (interest != 0)
      59         record(date, interest);
      60     accumulation = 0;
      61 }
      62 void SavingsAccount::show() const  {
      63     cout << "#" << id << "\tBalance:" << balance;
      64 }
      65 
      66 //.main
      67 #include"account.h"
      68 #include<iostream>
      69 using namespace std;
      70 int main() {
      71     SavingsAccount sa0(1, 2132502, 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 }

       

      結果:

       

      posted @ 2024-10-25 22:05  阿瓜不瓜  閱讀(15)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 不卡一区二区国产在线| 西丰县| 91久久亚洲综合精品成人| 新巴尔虎右旗| 久久午夜无码免费| 欧美寡妇xxxx黑人猛交| 国内精品免费久久久久电影院97| 国产亚洲真人做受在线观看| 亚洲色最新高清AV网站| 欧美人与动牲交精品| 昭苏县| 久章草在线毛片视频播放| 乱人伦人妻中文字幕不卡| 日本亚洲一区二区精品| 亚洲自偷自拍熟女另类| 久久久综合九色合综| 亚洲中文字幕无码一久久区| 最新亚洲人成网站在线影院| 新昌县| 思思热在线视频精品| 欧美激情综合色综合啪啪五月| 日韩激情无码av一区二区| 国产片AV国语在线观看手机版| 正阳县| 国产成人理论在线视频观看| 他掀开裙子把舌头伸进去添视频| 欧美人与动欧交视频| 丁香婷婷在线视频| 日韩秘 无码一区二区三区| 成年女人免费碰碰视频| 国产午夜福利视频合集| 国产精品一区二区三区卡| 国产小嫩模无套中出视频| 成人免费A级毛片无码片2022| 久久99精品久久99日本| 亚洲午夜香蕉久久精品| 蜜桃成人无码区免费视频网站| 日韩丝袜亚洲国产欧美一区| av激情亚洲男人的天堂| 国产午夜精品福利91| 免费AV片在线观看网址|