编辑代码

import Foundation

// 定义一个函数,用于计算表达式的值  
func evaluate(_ expression: String) throws -> Double {  
    enum EvaluationError: Error {  
        case unbalancedParentheses  
        case invalidToken(String)  
        case divisionByZero  
    }  
  
    // 去除空格并将字符串分解为标记  
    let separator = CharacterSet.whitespaces.union(.newlines)  
    let tokens = expression.components(separatedBy: separator).filter { !$0.isEmpty }
    //let tokens = expression.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }  
      
    var values = [Double]()  // 数值栈  
    var ops = [Character]()  // 运算符栈  
      
    // 运算符优先级  
    let precedence: [Character: Int] = ["+": 1, "-": 1, "*": 2, "/": 2 , "^": 3]  
      
    // 应用运算符函数  
    func applyOp(_ op: Character, rhs: Double, lhs: Double) throws  -> Double {  
        switch op {  
        case "+": return lhs + rhs  
        case "-": return lhs - rhs  
        case "*": return lhs * rhs  
        case "/":  
            guard rhs != 0 else { throw EvaluationError.divisionByZero }  
            return lhs / rhs  
        case "^": return pows(lhs, rhs) // 使用pows函数进行乘方运算      
        default: fatalError("Unsupported operation")  
        }  
    }  
    func pows(_ base: Double, _ exponent: Double) -> Double {  
        let intPart = Int(exponent)  
 
        
        var result = 1.0  
        for _ in 0..<intPart {  
            result *= base  
        }    
        return result  
    }
    // 检查运算符的优先级  
    func hasPrecedence(op1: Character, over op2: Character) -> Bool {  
        guard let prec1 = precedence[op1], let prec2 = precedence[op2] else { return false }  
        return prec1 >= prec2  
    }  
      
    for token in tokens {
        if let value = Double(token) {  
            values.append(value)  
        } else if token == "(" {  
            ops.append("(")  
        } else if token == ")" {  
            while !ops.isEmpty && ops.last != "(" {  
                let op = ops.removeLast()  
                let rhs = values.removeLast()  
                let lhs = values.removeLast()
                do{
                    values.append(try applyOp(op, rhs: rhs, lhs: lhs))
                }  catch{
                    print("error:\(error)")
                }
                //values.append(try! applyOp(op, rhs: rhs, lhs: lhs))  
            }  
            if !ops.isEmpty && ops.last == "(" {  
                ops.removeLast()  
            } else {  
                throw EvaluationError.unbalancedParentheses  
            }  
        } else if let firstChar = token.first, precedence[firstChar] != nil {  
            // 运算符  
            while !ops.isEmpty && ops.last != "(" && hasPrecedence(op1:ops.last!, over: firstChar) {  
                let op = ops.removeLast()  
                let rhs = values.removeLast()  
                let lhs = values.removeLast()  
                do{
                    values.append(try applyOp(op, rhs: rhs, lhs: lhs))
                }  catch{
                    print("error:\(error)")
                }  
            }  
            ops.append(firstChar)  
        } else {  
            throw EvaluationError.invalidToken(token)  
        }  
    }  
      
    // 处理剩余的运算符  
    while !ops.isEmpty {  
        if ops.last == "(" {  
            throw EvaluationError.unbalancedParentheses  
        }  
        let op = ops.removeLast()  
        let rhs = values.removeLast()  
        let lhs = values.removeLast()  
        do{
            values.append(try applyOp(op, rhs: rhs, lhs: lhs))
        }catch{
           print("error:\(error)") 
        }
        
    }  
      
    guard values.count == 1 else { throw EvaluationError.unbalancedParentheses }  
    return values.first!  
}  



  
func applyOp_Complx(_ op: Character, rhs: Complex, lhs: Complex) -> Complex? {
    switch op {
    case "+": return lhs + rhs
    case "-": return lhs - rhs
    case "*": return lhs * rhs
    case "/": return lhs / rhs
    default: return nil
    }
}


// 打印复数  
func printComplex(_ c: Complex) {  
    print("复数:\(c.real) + \(c.imaginary)i")  
}  
  

// 从字符串解析复数  
func parseComplexFromString(_ input: String) -> Complex? {  
    let scanner = Scanner(string: input)  
    var realPart: Double = 0.0  
    var imaginaryPart: Double = 0.0  
    var hasPlusSign = false  
      
    // 扫描实部  
    if scanner.scanDouble(&realPart) && scanner.atEnd {  
        // 如果没有虚部,则直接返回实数作为复数(虚部为0)  
        return Complex(real: realPart, imaginary: 0.0)  
    }  
      
    // 扫描加号(如果有)  
    if scanner.scanString("+", into: nil) {  
        hasPlusSign = true  
    } else if scanner.scanString("-", into: nil) {  
        // 如果是减号,则将虚部的符号设置为负  
        imaginaryPart = -1.0  
    }  
      
    // 扫描虚部  
    if !hasPlusSign && !scanner.atEnd {  
        // 如果没有加号且不是字符串末尾,则意味着格式错误  
        return nil  
    }  
    if scanner.scanDouble(&imaginaryPart) && scanner.scanString("i", into: nil) && scanner.atEnd {  
        // 成功解析实部和虚部  
        return Complex(real: realPart, imaginary: imaginaryPart)  
    }  
      
    // 如果解析失败,则返回nil  
    return nil  
}  


// 示例使用  
repeat {  
    print("请选择操作:1 - 计算,2 - 复数, 0 - 退出")  
    if let operation = readLine(), let op = Int(operation) {  
        switch op {  
        case 1:  
            print("请输入一个数学表达式:")  
            if let input = readLine()?.trimmingCharacters(in: .whitespacesAndNewlines) {  
                do {  
                    let result = try evaluate(input)  
                    print("\(input) = \(result)")  
                } catch {  
                    print("表达式求值错误:\(error)")  
                }  
            } else {  
                print("未输入有效的表达式。")  
            }  
        case 2:  
            print("第一个复数")
            let complx1 = parseComplexFromString(readLine())
            print("操作")
            let op1 = readLine()
            print("第二个复数")
            let complx2 = parseComplexFromString(readLine())
            guard let complxResult = applyOp_Complx(op1, rhs: complx2, lhs: complx1) else {
                print("无效的操作。")
                continue
            }
            print("\(printComplex(complx1)) \(op1) \(printComplex(complx2)) = \(printComplex(complxResult))")
        case 0:  
            exit(EXIT_SUCCESS) // 输入0时退出程序  
        default:  
            print("无效的操作。")  
        }  
    } else {  
        print("未输入有效的操作。")  
    }  
} while true // 这里使用了 true 作为条件,表示无限循环,直到在循环体内退出

// 1 + 3 * ( 2 / 4 )
// Result = 2.5

// 24 / 0

// ( 15 - 90 ) * 14 - 1.75 * 80 / 0.2
// Result = - 1750

// 3 ^ 5
// Result = 243

//99 * 88
//99 * 88 = 8712.0