编辑代码

import Foundation

struct ComplexNumber {
    var real: Double
    var imaginary: Double
    
    init(real: Double, imaginary: Double) {
        self.real = real
        self.imaginary = imaginary
    }
    
    static func + (lhs: ComplexNumber, rhs: ComplexNumber) -> ComplexNumber {
        return ComplexNumber(real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary)
    }
    
    static func - (lhs: ComplexNumber, rhs: ComplexNumber) -> ComplexNumber {
        return ComplexNumber(real: lhs.real - rhs.real, imaginary: lhs.imaginary - rhs.imaginary)
    }
    
    static func * (lhs: ComplexNumber, rhs: ComplexNumber) -> ComplexNumber {
        let realPart = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary
        let imaginaryPart = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real
        return ComplexNumber(real: realPart, imaginary: imaginaryPart)
    }
    
    static func / (lhs: ComplexNumber, rhs: ComplexNumber) -> ComplexNumber {
        let denominator = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary
        let realPart = (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / denominator
        let imaginaryPart = (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / denominator
        return ComplexNumber(real: realPart, imaginary: imaginaryPart)
    }
    
    func description() -> String {
        if imaginary >= 0 {
            return "\(real)+\(imaginary)i"
        } else {
            return "\(real)\(imaginary)i"
        }
    }
}

func calculate() {
    print("请选择计算类型:")
    print("1. 实数(含整数)计算")
    print("2. 复数计算")
    print("3. 正弦函数计算")
    print("4. 无穷精度的整数计算")
    print("输入#结束运行")

    while let input = readLine() {
        if input == "#" {
            break
        }

        switch input {
        case "1":
            print("请输入表达式:")
            if let expression = readLine() {
                let result = evaluateRealExpression(expression)
                print("结果:\(result)")
            }
        case "2":
            print("请输入复数表达式(格式:a+bi 运算符 c+di):")
            if let complexExpression = readLine() {
                let result = evaluateComplexExpression(complexExpression)
                print("结果:\(result.description())")
            }
        case "3":
            print("请输入角度(度):")
            if let degreeString = readLine(),
               let degree = Double(degreeString) {
                let result = sin(degree * .pi / 180)
                print("sin(\(degree)) = \(result)")
            } else {
                print("无效输入")
            }
        case "4":
            print("请输入表达式:")
            if let expression = readLine() {
                let result = evaluateBigIntExpression(expression)
                print("结果:\(result)")
            }
        default:
            print("无效选择")
        }
    }
}

// 实数(含整数)计算
func evaluateRealExpression(_ expression: String) -> Double {
    let operators: [Character] = ["+", "-", "*", "/"]
    var operatorIndex: String.Index?
    var op: Character?
    
    for (index, char) in expression.enumerated() {
        if operators.contains(char) {
            operatorIndex = expression.index(expression.startIndex, offsetBy: index)
            op = char
            break
        }
    }
    
    if let index = operatorIndex, let op = op {
        let lhs = Double(expression[..<index]) ?? 0
        let rhs = Double(expression[expression.index(after: index)...]) ?? 0
        
        switch op {
        case "+":
            return lhs + rhs
        case "-":
            return lhs - rhs
        case "*":
            return lhs * rhs
        case "/":
            return lhs / rhs
        default:
            return 0
        }
    }
    
    return 0
}

// 无穷精度的整数计算
func evaluateBigIntExpression(_ expression: String) -> String {
    // 示例:仅支持加法
    let components = expression.split(separator: "+")
    if components.count == 2 {
        let operand1 = components[0]
        let operand2 = components[1]
        let result = addBigIntegers(String(operand1), String(operand2))
        return result
    } else {
        return "无效的表达式"
    }
}

func addBigIntegers(_ num1: String, _ num2: String) -> String {
    var result = ""
    var carry = 0
    let num1 = Array(num1.reversed())
    let num2 = Array(num2.reversed())
    let maxLength = max(num1.count, num2.count)

    for i in 0..<maxLength {
        let digit1 = i < num1.count ? Int(String(num1[i])) ?? 0 : 0
        let digit2 = i < num2.count ? Int(String(num2[i])) ?? 0 : 0
        let sum = digit1 + digit2 + carry
        result.append(String(sum % 10))
        carry = sum / 10
    }

    if carry > 0 {
        result.append(String(carry))
    }

    return String(result.reversed())
}

// 复数计算
func evaluateComplexExpression(_ expression: String) -> ComplexNumber {
    let operators: [Character] = ["+", "-", "*", "/"]
    var op: Character = "+"
    let operands = expression.split { operators.contains($0) }
    
    if operands.count == 2 {
        for char in expression {
            if operators.contains(char) {
                op = char
                break
            }
        }
        
        let lhs = parseComplexNumber(String(operands[0]))
        let rhs = parseComplexNumber(String(operands[1]))
        
        switch op {
        case "+":
            return lhs + rhs
        case "-":
            return lhs - rhs
        case "*":
            return lhs * rhs
        case "/":
            return lhs / rhs
        default:
            return ComplexNumber(real: 0, imaginary: 0)
        }
    } else {
        print("无效的复数表达式")
        return ComplexNumber(real: 0, imaginary: 0)
    }
}

func parseComplexNumber(_ expression: String) -> ComplexNumber {
    var realPart = 0.0
    var imaginaryPart = 0.0
    
    let components = expression.split(separator: "+")
    if components.count == 2 {
        realPart = Double(components[0]) ?? 0
        imaginaryPart = Double(components[1].replacingOccurrences(of: "i", with: "")) ?? 0
    } else {
        let componentsMinus = expression.split(separator: "-")
        if componentsMinus.count == 2 && componentsMinus[0].isEmpty {
            realPart = -(Double(componentsMinus[1]) ?? 0)
        }
    }

    return ComplexNumber(real: realPart, imaginary: imaginaryPart)
}

// 开始计算
calculate()