CliooilC 2023-05-06 16:37 采纳率: 0%
浏览 260
已结题

怎么用c++编写一个复数计算器?包括加减乘除、幂次方、取模、辐角、共轭。实部和虚部都是实数范围。

比如(3.5+222i)^(-3)+0.12*arg(-2+5.1i)-|33+4.667i|/cjg(3-4i)=?
其中arg是辐角,cjg取共轭,||是取模。实数范围内,需要用逆波兰。
不可以用c++自带的complex类,需要自己定义。
因初学这一部分,没有足够的练习,不太能上手这一类题目。希望可以给出较详细的代码,或者提供一个比较具体的思路指导。

  • 写回答

12条回答 默认 最新

  • QWQ.qwq 2023-05-06 19:32
    关注

    部分引用GPT(有的实在是不会,没办法)
    我帮你写一个:

    #include <iostream>
    #include <string>
    #include <stack>
    #include <cmath>
    
    using namespace std;
    
    class Complex {
    public:
        double real;
        double imaginary;
        // 构造函数
        Complex(double real = 0, double imaginary = 0) {
            this->real = real;
            this->imaginary = imaginary;
        }
        // 重载运算符
        Complex operator + (const Complex& c) const {
            return Complex(real + c.real, imaginary + c.imaginary);
        }
        Complex operator - (const Complex& c) const {
            return Complex(real - c.real, imaginary - c.imaginary);
        }
        Complex operator * (const Complex& c) const {
            return Complex(real * c.real - imaginary * c.imaginary, real * c.imaginary + imaginary * c.real);
        }
        Complex operator / (const Complex& c) const {
            double denominator = c.real * c.real + c.imaginary * c.imaginary;
            return Complex((real * c.real + imaginary * c.imaginary) / denominator, (imaginary * c.real - real * c.imaginary) / denominator);
        }
        Complex operator ^ (int n) const {
            Complex result = *this;
            for (int i = 1; i < n; i++) {
                result = result * (*this);
            }
            return result;
        }
        double abs() const {
            return sqrt(real * real + imaginary * imaginary);
        }
        double arg() const {
            return atan2(imaginary, real);
        }
        Complex conjugate() const {
            return Complex(real, -imaginary);
        }
    };
    
    // 操作符优先级
    int Priority(char op) {
        if (op == '+' || op == '-') {
            return 1;
        } else if (op == '*' || op == '/') {
            return 2;
        } else if (op == '^') {
            return 3;
        } else {
            return 0;
        }
    }
    
    // 四则运算
    Complex Calculate(Complex a, Complex b, char op) {
        switch (op) {
        case '+':
            return a + b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            return a / b;
        case '^':
            return a ^ (int)b.real;
        default:
            return Complex();
        }
    }
    
    // 逆波兰表达式求值
    Complex Evaluate(const string& expression) {
        stack<char> opStack;
        stack<Complex> numStack;
        int index = 0;
        while (index < expression.length()) {
            if (isdigit(expression[index]) || expression[index] == '.') {
                double realPart = stod(expression.substr(index), &index);
                double imaginaryPart = 0;
                if (expression[index] == 'i') {
                    index++;
                    imaginaryPart = realPart;
                    realPart = 0;
                }
                numStack.push(Complex(realPart, imaginaryPart));
            } else if (expression[index] == '(') {
                opStack.push('(');
                index++;
            } else if (expression[index] == ')') {
                while (!opStack.empty() && opStack.top() != '(') {
                    char op = opStack.top();
                    opStack.pop();
                    Complex b = numStack.top();
                    numStack.pop();
                    Complex a = numStack.top();
                    numStack.pop();
                    numStack.push(Calculate(a, b, op));
                }
                opStack.pop(); // 弹出左括号
                index++;
            } else if (expression[index] == '+' || expression[index] == '-' || expression[index] == '*' || expression[index] == '/' || expression[index] == '^') {
                while (!opStack.empty() && Priority(opStack.top()) >= Priority(expression[index])) {
                    char op = opStack.top();
                    opStack.pop();
                    Complex b = numStack.top();
                    numStack.pop();
                    Complex a = numStack.top();
                    numStack.pop();
                    numStack.push(Calculate(a, b, op));
                }
                opStack.push(expression[index]);
                index++;
            } else {
                index++; // 忽略空格和无效字符
            }
        }
        while (!opStack.empty()) {
            char op = opStack.top();
            opStack.pop();
            Complex b = numStack.top();
            numStack.pop();
            Complex a = numStack.top();
            numStack.pop();
            numStack.push(Calculate(a, b, op));
        }
        return numStack.top();
    }
    
    int main() {
        string expression = "3.5+222i -3 ^ 0.12 -2 5.1i arg * 33 4.667i abs 3 -4i cjg /";
        Complex result = Evaluate(expression);
        cout << result.real << " + " << result.imaginary << "i" << endl;
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 系统已结题 5月14日
  • 提问应符合社区要求 5月7日
  • 请回答用户的提问 5月6日
  • 创建了问题 5月6日