/1935_후위 표기식2.swift Secret
Created
November 19, 2023 06:28
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum OperType: Character { | |
case plus = "+" | |
case minus = "-" | |
case multiply = "*" | |
case divisor = "/" | |
func calculate(_ n1: Double, _ n2: Double) -> Double { | |
switch self { | |
case .plus: return n1 + n2 | |
case .minus: return n2 - n1 | |
case .multiply: return n1 * n2 | |
case .divisor: return n2 / n1 | |
} | |
} | |
} | |
let n = Int(readLine()!)! | |
let command = readLine()! | |
var dict: [Character: Double] = [:] | |
var numStack: [Double] = [] | |
for i in 0..<n { | |
dict[Character(UnicodeScalar(i + 65)!)] = Double(readLine()!)! | |
} | |
for cmd in command { | |
if let oper = OperType(rawValue: cmd) { | |
numStack.append(oper.calculate(numStack.removeLast(), numStack.removeLast())) | |
} else { | |
numStack.append(dict[cmd, default: 0]) | |
} | |
} | |
print(String(format: "%.2f", numStack.last!)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment