运算符重载

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//运算符重载
#include <iostream>

class Complex {
private:
    double real;
    double imag;

public:
    // 构造函数
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 赋值运算符重载
    Complex& operator=(const Complex& other) {
        if (this != &other) {
            real = other.real;
            imag = other.imag;
        }
        return *this;
    }

    // 成员函数版本的算术运算符重载
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }

    Complex operator-(const Complex& other) const {
        return Complex(real - other.real, imag - other.imag);
    }

    Complex operator*(const Complex& other) const {
        return Complex(real * other.real - imag * other.imag,
                       real * other.imag + imag * other.real);
    }

    Complex operator/(const Complex& other) const {
        double denominator = other.real * other.real + other.imag * other.imag;
        if (denominator == 0.0) {
            throw std::invalid_argument("Division by zero");
        }
        return Complex((real * other.real + imag * other.imag) / denominator,
                       (imag * other.real - real * other.imag) / denominator);
    }

    // 成员函数版本的自增自减运算符重载
    // 前置自增
    Complex& operator++() {
        ++real;
        return *this;
    }

    // 后置自增
    Complex operator++(int) {
        Complex temp = *this;
        ++real;
        return temp;
    }

    // 前置自减
    Complex& operator--() {
        --real;
        return *this;
    }

    // 后置自减
    Complex operator--(int) {
        Complex temp = *this;
        --real;
        return temp;
    }

    // 友元函数版本的算术运算符重载
    friend Complex operator+(const Complex& a, const Complex& b);
    friend Complex operator-(const Complex& a, const Complex& b);
    friend Complex operator*(const Complex& a, const Complex& b);
    friend Complex operator/(const Complex& a, const Complex& b);

    // 友元函数版本的输入输出运算符重载
    friend std::ostream& operator<<(std::ostream& os, const Complex& c);
    friend std::istream& operator>>(std::istream& is, Complex& c);
};

// 友元函数版本的算术运算符实现
Complex operator+(const Complex& a, const Complex& b) {
    return Complex(a.real + b.real, a.imag + b.imag);
}

Complex operator-(const Complex& a, const Complex& b) {
    return Complex(a.real - b.real, a.imag - b.imag);
}

Complex operator*(const Complex& a, const Complex& b) {
    return Complex(a.real * b.real - a.imag * b.imag,
                   a.real * b.imag + a.imag * b.real);
}

Complex operator/(const Complex& a, const Complex& b) {
    double denominator = b.real * b.real + b.imag * b.imag;
    if (denominator == 0.0) {
        throw std::invalid_argument("Division by zero");
    }
    return Complex((a.real * b.real + a.imag * b.imag) / denominator,
                   (a.imag * b.real - a.real * b.imag) / denominator);
}

// 友元函数版本的输入输出运算符实现
std::ostream& operator<<(std::ostream& os, const Complex& c) {
    os << c.real << " + " << c.imag << "i";
    return os;
}

std::istream& operator>>(std::istream& is, Complex& c) {
    is >> c.real >> c.imag;
    return is;
}

int main() {
    Complex a(3.0, 4.0), b(1.0, 2.0);
    Complex result;

    // 成员函数版本的算术运算
    result = a + b;
    std::cout << "a + b = " << result << std::endl;
    result = a - b;
    std::cout << "a - b = " << result << std::endl;
    result = a * b;
    std::cout << "a * b = " << result << std::endl;
    result = a / b;
    std::cout << "a / b = " << result << std::endl;

    // 前置和后置自增自减
    std::cout << "a before ++: " << a << std::endl;
    std::cout << "a++: " << a++ << std::endl;
    std::cout << "a after a++: " << a << std::endl;

    std::cout << "++a: " << ++a << std::endl;

    std::cout << "a before --: " << a << std::endl;
    std::cout << "a--: " << a-- << std::endl;
    std::cout << "a after a--: " << a << std::endl;

    std::cout << "--a: " << --a << std::endl;

    // 友元函数版本的算术运算
    result = operator+(a, b);
    std::cout << "operator+(a, b) = " << result << std::endl;
    result = operator-(a, b);
    std::cout << "operator-(a, b) = " << result << std::endl;
    result = operator*(a, b);
    std::cout << "operator*(a, b) = " << result << std::endl;
    result = operator/(a, b);
    std::cout << "operator/(a, b) = " << result << std::endl;

    // 输入输出运算符
    std::cout << "Enter a new value for a (real and imaginary parts): ";
    std::cin >> a;
    std::cout << "New value of a: " << a << std::endl;

    return 0;
}

6.4反转数字

Write a function with the following header to display an integer in reverse order:

void reverse(int number)

For example, reverse(3456) displays 6543. Write a test program that prompithe user to enter an integer and displays its reversal

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

// Function to reverse the digits of an integer
void reverse(int number) {
    int reversedNumber = 0;

    // Extracting digits and forming the reversed number
    while (number != 0) {
        reversedNumber = reversedNumber * 10 + (number % 10);
        number /= 10;
    }

    // Display the reversed number
    cout << "Reversed Number: " << reversedNumber << endl;
}

// Main function to test the reverse function
int main() {
    int number;
    
    // Prompt the user to enter an integer
    cout << "Enter an integer: ";
    cin >> number;
    
    // Call the reverse function
    reverse(number);
    
    return 0;
}

6.12 公式算20值

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

double compute_series(int t)
{
    double temp = 0;
    printf("i\tm[i]\n");
    for (int i = 1; i <= t; i++)
    {
        temp += double(i) / (i + 1);
        printf("%d\t%.4f\n", i, temp);
    }
    return temp;
}

int main()
{
    compute_series(20);
    return 0;
}

6.13 公式取π

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include<cmath>
using namespace std;

// Function to compute the series approximation of pi
double compute_pi(int terms) {
    double pi = 0.0;
    for (int k = 0; k < terms; ++k) {
        pi += (pow(-1, k) / (2 * k + 1));
    }
    return pi * 4;
}

// Main function to display the table
int main() {
    cout << "i\tm(i)" << endl;
    cout << "-----------------" << endl;

    for (int i = 1; i <= 1000; i+=100) {
        double approximation = compute_pi(i);
          printf("%d\t%.4f\n", i, approximation);
    }

    return 0;
}

6.5 三个数字比大小

三个数字比大小

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

// 定义函数,按升序显示三个数字
void displaySortedNumbers(double num1, double num2, double num3) {
    double temp;
    // 使用简单的比较和交换来排序数字
    if (num1 > num2) {
        temp = num1;
        num1 = num2;
        num2 = temp;
    }
    if (num1 > num3) {
        temp = num1;
        num1 = num3;
        num3 = temp;
    }
    if (num2 > num3) {
        temp = num2;
        num2 = num3;
        num3 = temp;
    }

    // 显示排序后的数字
    cout << "按升序排列的数字是: " << num1 << ", " << num2 << ", " << num3 << endl;
}

int main() {
    double num1, num2, num3;

    // 提示用户输入三个数字
    cout << "请输入三个数字: ";
    cin >> num1 >> num2 >> num3;

    // 调用函数按升序显示数字
    displaySortedNumbers(num1, num2, num3);

    return 0;
}