實驗二

22软件一班毛俊东發表於2024-10-29

實驗內容一:

t.h
點選檢視程式碼
#pragma once

#include <string>

// 類T: 宣告
class T {
// 物件屬性、方法
public:
    T(int x = 0, int y = 0);   // 普通建構函式
    T(const T &t);  // 複製建構函式
    T(T &&t);       // 移動建構函式
    ~T();           // 解構函式

    void adjust(int ratio);      // 按係數成倍調整資料
    void display() const;           // 以(m1, m2)形式顯示T類物件資訊

private:
    int m1, m2;

// 類屬性、方法
public:
    static int get_cnt();          // 顯示當前T類物件總數

public:
    static const std::string doc;       // 類T的描述資訊
    static const int max_cnt;           // 類T物件上限

private:
    static int cnt;         // 當前T類物件數目

// 類T友元函式宣告
    friend void func();
};

// 普通函式宣告
void func();

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

點選檢視程式碼
// 類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;
}

執行結果:

實驗內容二

Complex.h
點選檢視程式碼
#ifndef COMPLEX_H
#define COMPLEX_H

#include <cmath>
#include <string>

class Complex {
  double real;
  double imag;

public:
  static const std::string doc;

  Complex();
  Complex(double real);
  Complex(double real, double imag);
  Complex(const Complex &other);

  double get_real() const;
  double get_imag() const;

  void add(const Complex &other);
  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 void output(const Complex &c);
  friend double abs(const Complex &c);
};

#endif // COMPLEX_H

Complex.cpp
點選檢視程式碼
#include "Complex.h"
#include <iostream>

const std::string Complex::doc = "a simplified complex class";

Complex::Complex() : real(0), imag(0) {}

Complex::Complex(double real) : real(real), imag(0) {}

Complex::Complex(double real, double imag) : real(real), imag(imag) {}

Complex::Complex(const Complex &other) : real(other.real), imag(other.imag) {}

double Complex::get_real() const { return real; }

double Complex::get_imag() const { return imag; }

void Complex::add(const Complex &other) {
  real += other.real;
  imag += other.imag;
}

Complex add(const Complex &c1, const Complex &c2) {
  return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

bool is_equal(const Complex &c1, const Complex &c2) {
  return c1.real == c2.real && c1.imag == c2.imag;
}

bool is_not_equal(const Complex &c1, const Complex &c2) {
  return c1.real != c2.real || c1.imag != c2.imag;
}

void output(const Complex &c) {
  if (c.imag == 0) {
    std::cout << c.real;
  }
  else if (c.imag > 0) {
    std::cout << c.real << "+" << c.imag << "i";
  }
  else {
    std::cout << c.real << c.imag << "i";
  }
}

double abs(const Complex &c) {
  return std::sqrt(c.real * c.real + c.imag * c.imag);
}

task2.cpp

點選檢視程式碼
#include "Complex.h"
using std::cout;
using std::endl;
using std::boolalpha;

void test() {
    cout << "類成員測試: " << endl;
    cout << Complex::doc << endl;

    cout << endl;

    cout << "Complex物件測試: " << endl;
    Complex c1;
    Complex c2(3, -4);
    const Complex c3(3.5);
    Complex c4(c3);

    cout << "c1 = "; output(c1); cout << endl;
    cout << "c2 = "; output(c2); cout << endl;
    cout << "c3 = "; output(c3); cout << endl;
    cout << "c4 = "; output(c4); cout << endl;
    cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;

    cout << endl;

    cout << "複數運算測試: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1.add(c2);
    cout << "c1 += c2, c1 = "; output(c1); cout << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
    cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
    c4 = add(c2, c3);
    cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
}

int main() {
    test();
}

執行結果

實驗內容3

task3.cpp:
點選檢視程式碼
#include <iostream>
#include <complex>

using std::cout;
using std::endl;
using std::boolalpha;
using std::complex;

void test() {
    cout << "標準庫模板類comple測試: " << endl;
    complex<double> c1;
    complex<double> c2(3, -4);
    const complex<double> c3(3.5);
    complex<double> c4(c3);

    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c4 = " << c4 << endl;
    cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;
    cout << endl;

    cout << "複數運算測試: " << endl;
    cout << "abs(c2) = " << abs(c2) << endl;
    c1 += c2;
    cout << "c1 += c2, c1 = " << c1 << endl;
    cout << boolalpha;
    cout << "c1 == c2 : " << (c1 == c2) << endl;
    cout << "c1 != c3 : " << (c1 != c3) << endl;
    c4 = c2 + c3;
    cout << "c4 = c2 + c3, c4 = " << c4 << endl;
}

int main() {
    test();
}

執行結果:

實驗內容4

Fraction.h
點選檢視程式碼
#ifndef FRACTION_H
#define FRACTION_H

#include <iostream>
#include <numeric>
#include <stdexcept>
#include <string>

using std::string;

class Fraction {
private:
    int up;
    int down;

    void reduce();

public:
    static string doc;

    Fraction(int num = 0, int denom = 1);
    Fraction(const Fraction &f);

    Fraction negative() const;
    int get_up() const;
    int get_down() const;

    friend Fraction add(const Fraction &f1, const Fraction &f2);
    friend Fraction sub(const Fraction &f1, const Fraction &f2);
    friend Fraction mul(const Fraction &f1, const Fraction &f2);
    friend Fraction div(const Fraction &f1, const Fraction &f2);
    friend void output(const Fraction &f);
};

#endif

Fraction.cpp

點選檢視程式碼
#include "Fraction.h"
#include <cstdlib>

string Fraction::doc {"Fraction類 v 0.01版.\n目前僅支援分數物件的構造、輸出、加/減/乘/除運算."};

Fraction::Fraction(int num, int denom) : up(num), down(denom) {
    if (denom == 0) {
        std::cout << "分母不能為零" << std::endl;
        exit(0);
    }
    reduce();
}

Fraction::Fraction(const Fraction &f) : up(f.up), down(f.down) {}


int gcd(int a, int b) {

    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a ;
}

int abs(int a){
    if(a < 0){
        return -a;
    }
    else{
        return a;
    }
}

void Fraction::reduce() {

    int a = up;
    int b = down;

    int oka = 1;
    int okb = 1;

    if(a < 0){
        a *= -1;
        oka = 0;
    }
    if(b < 0){
        b *= -1;
        okb = 0;
    }

    int divi = gcd(a, b);
    up /= divi;
    down /= divi;

    if(down < 0){
        down *= -1;
        up *= -1;
    }
}

Fraction Fraction::negative() const {
    return Fraction(-up, down);
}

int Fraction::get_up() const {
    return up;
}

int Fraction::get_down() const {
    return down;
}

Fraction add(const Fraction &f1, const Fraction &f2) {
    int up_1;
    int down_1;

    down_1 = f1.get_down() * f2.get_down();
    up_1 = f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down();
    return Fraction(up_1 , down_1);
}

Fraction sub(const Fraction &f1, const Fraction &f2) {
    return add(f1, f2.negative());
}

Fraction mul(const Fraction &f1, const Fraction &f2) {
    return Fraction(f1.up * f2.up, f1.down * f2.down);
}

Fraction div(const Fraction &f1, const Fraction &f2) {
    return mul(f1, Fraction(f2.down, f2.up));
}

void output(const Fraction &f) {
    int ok = 1;

    if(f.get_down() < 0 || f.get_up() < 0){
        ok = 0;
        std::cout << '-';
    }

    int a = abs(f.get_up());
    int b = abs(f.get_down());

    if(b == 1){
        std::cout << a;
    }
    else{
        std::cout << a << "/" << b;
    }
}

task4.cpp

點選檢視程式碼
#include "Fraction.h"
#include <iostream>

using std::cout;
using std::endl;


void test1() {
    cout << "Fraction類測試: " << endl;
    cout << Fraction::doc << endl << endl;

    Fraction f1(5);
    Fraction f2(3, -4), f3(-18, 12);
    Fraction f4(f3);
    cout << "f1 = "; output(f1); cout << endl;
    cout << "f2 = "; output(f2); cout << endl;
    cout << "f3 = "; output(f3); cout << endl;
    cout << "f4 = "; output(f4); cout << endl;

    Fraction f5(f4.negative());
    cout << "f5 = "; output(f5); cout << endl;
    cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;

    cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
    cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
    cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
    cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
    cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
}

void test2() {
    Fraction f6(42, 55), f7(0, 3);
    cout << "f6 = "; output(f6); cout << endl;
    cout << "f7 = "; output(f7); cout << endl;
    cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
}

int main() {
    cout << "測試1: Fraction類基礎功能測試\n";
    test1();

    cout << "\n測試2: 分母為0測試: \n";
    test2();
}

執行結果:

實驗內容5

account.h
點選檢視程式碼
#pragma once
class SavingsAccount {
private:
    int id;
    double balance;
    double rate;
    int lastDate;
    double accumulation;
    static double total;
    void record(int date, double amount);
    double accumulate(int date) const {
        return accumulation + balance * (date - lastDate);
    }
public:
    SavingsAccount(int date, int id, double rate);
    int getId()const { return id; }
    double getBalance()const { return balance; }
    double getRate()const { return rate; }
    static double getTotal() { return total; }
    void deposit(int date, double amount);
    void withdraw(int date, double amount);
    void settle(int date);
    void show()const;
};

account.cpp

點選檢視程式碼
#include"account.h"
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total = 0;
SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
    cout << date << "\t#" << id << "is created" << endl;
}
void SavingsAccount::record(int date, double amount) {
    accumulation = accumulate(date);
    lastDate = date;
    amount = floor(amount * 100 + 0.5) / 100;
    balance += amount;
    total += amount;
    cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingsAccount::deposit(int date, double amount) {
    record(date, amount);
}
void SavingsAccount::withdraw(int date,double amount) {
    if (amount > getBalance())
        cout << "Error:not enough money" << endl;
    else {
        record(date, -amount);
    }
}
void SavingsAccount::settle(int date) {
    double interest = accumulate(date) * rate / 365;
    if (interest != 0)
        record(date, interest);
    accumulation = 0;
}
void SavingsAccount::show()const {
    cout << "#" << id << "\tBalance:" << balance;
}

task5.cpp

點選檢視程式碼
#include"account.h"
#include<iostream>
using namespace std;
int main() {
    SavingsAccount sa0(1, 21325302, 0.015);
    SavingsAccount sa1(1, 58320212, 0.015);
    sa0.deposit(5, 5000);
    sa1.deposit(25, 10000);
    sa0.deposit(45, 5500);
    sa1.withdraw(60, 4000);
    sa0.settle(90);
    sa1.settle(90);
    sa0.show(); cout << endl;S
    sa1.show(); cout << endl;
    cout << "Total: " << SavingsAccount::getTotal() << endl;
    return 0;
}

執行結果:

相關文章