package main import ( "fmt" "math" "strconv" "strings" "unicode" ) // Define all token…
- User
package main
import ( "fmt" "math" "strconv" "strings" "unicode" )
// Define all token types type TokenType int
const ( EOF TokenType = iota Number Plus Minus Multiply Divide Power Sqrt LeftParen RightParen )
// Token structure type Token struct { Type TokenType Value string }
// Function to check if a rune is a space func isSpace(r rune) bool { return unicode.IsSpace(r) }
// Function to check if a rune is a digit func isDigit(r rune) bool { return unicode.IsDigit(r) }
// Function to check if a rune is an operator func isOperator(r rune) bool { return strings.ContainsRune("+-*/^", r) }
// Function to convert an expression string into tokens
func tokenize(expr string) ([]Token, error) { var tokens []Token for len(expr) > 0 { expr = strings.TrimSpace(expr) if len(expr) == 0 { break } var t Token switch { case expr[0] == '+': t = Token{Plus, "+"} expr = expr[1:] case expr[0] == '-': t = Token{Minus, "-"} expr = expr[1:] case expr[0] == '': t = Token{Multiply, ""} expr = expr[1:] case expr[0] == '/': t = Token{Divide, "/"} expr = expr[1:] case expr[0] == '(': t = Token{LeftParen, "("} expr = expr[1:] case expr[0] == ')': t = Token{RightParen, ")"} expr = expr[1:] case expr[0] == '^': t = Token{Power, "^"} expr = expr[1:] case strings.HasPrefix(expr, "sqrt"): t = Token{Sqrt, "sqrt"} expr = expr[len("sqrt"):] case isDigit(rune(expr[0])): num := strings.TrimLeftFunc(expr, isDigit) t = Token{Number, expr[:len(expr)-len(num)]} expr = num
default: return nil, fmt.Errorf("Неизвестный символ: %c", expr[0]) } tokens = append(tokens, t) } return tokens, nil}
// Function to get the precedence of an operator func precedence(op TokenType) int { switch op { case Power: return 4 case Sqrt: return 3 case Multiply, Divide: return 2 case Plus, Minus: return 1 case LeftParen, RightParen: return 0 default: return 0 } }
// Function to evaluate an expression given its tokens func eval(tokens []Token) (float64, error) { var stack []float64 var operatorStack []Token
for _, t := range tokens { switch t.Type { case Number: value, err := strconv.ParseFloat(t.Value, 64) if err != nil { return 0, err } stack = append(stack, value) case Plus, Minus, Multiply, Divide, Power, Sqrt: for len(operatorStack) > 0 && precedence(t.Type) <= precedence(operatorStack[len(operatorStack)-1].Type) { var op Token op, operatorStack = operatorStack[len(operatorStack)-1], operatorStack[:len(operatorStack)-1] if op.Type == Sqrt { if len(stack) < 1 { return 0, fmt.Errorf("Invalid expression: %s", t.Value) } value := stack[len(stack)-1] if value < 0 { return 0, fmt.Errorf("Cannot take square root of a negative number: %v", value) } stack = stack[:len(stack)-1] stack = append(stack, math.Sqrt(value)) } else { if len(stack) < 2 { return 0, fmt.Errorf("Invalid expression: %s", t.Value) } right, left := stack[len(stack)-1], stack[len(stack)-2] stack = stack[:len(stack)-2] switch op.Type { case Plus: stack = append(stack, left+right) case Minus: stack = append(stack, left-right) case Multiply: stack = append(stack, left*right) case Divide: stack = append(stack, left/right) case Power: stack = append(stack, math.Pow(left, right)) } } } operatorStack = append(operatorStack, t) case LeftParen: operatorStack = append(operatorStack, t) case RightParen: for len(operatorStack) > 0 && operatorStack[len(operatorStack)-1].Type != LeftParen { var op Token op, operatorStack = operatorStack[len(operatorStack)-1], operatorStack[:len(operatorStack)-1] if op.Type == Sqrt { if len(stack) < 1 { return 0, fmt.Errorf("Invalid expression: %s", t.Value) } value := stack[len(stack)-1] if value < 0 { return 0, fmt.Errorf("Cannot take square root of a negative number: %v", value) } stack = stack[:len(stack)-1] stack = append(stack, math.Sqrt(value)) } else { if len(stack) < 2 { return 0, fmt.Errorf("Invalid expression: %s", t.Value) } right, left := stack[len(stack)-1], stack[len(stack)-2] stack = stack[:len(stack)-2] switch op.Type { case Plus: stack = append(stack, left+right) case Minus: stack = append(stack, left-right) case Multiply: stack = append(stack, left*right) case Divide: stack = append(stack, left/right) case Power: stack = append(stack, math.Pow(left, right)) } } } if len(operatorStack) == 0 || operatorStack[len(operatorStack)-1].Type != LeftParen { return 0, fmt.Errorf("Mismatched parentheses") } operatorStack = operatorStack[:len(operatorStack)-1] } } for len(operatorStack) > 0 { var op Token op, operatorStack = operatorStack[len(operatorStack)-1], operatorStack[:len(operatorStack)-1] if op.Type == Sqrt { if len(stack) < 1 { return 0, fmt.Errorf("Invalid expression: %s", op.Value) } value := stack[len(stack)-1] if value < 0 { return 0, fmt.Errorf("Cannot take square root of a negative number: %v", value) } stack = stack[:len(stack)-1] stack = append(stack, math.Sqrt(value)) } else { if len(stack) < 2 { return 0, fmt.Errorf("Invalid expression: %s", op.Value) } right, left := stack[len(stack)-1], stack[len(stack)-2] stack = stack[:len(stack)-2] switch op.Type { case Plus: stack = append(stack, left+right) case Minus: stack = append(stack, left-right) case Multiply: stack = append(stack, left*right) case Divide: stack = append(stack, left/right) case Power: stack = append(stack, math.Pow(left, right)) } } } if len(stack) != 1 { return 0, fmt.Errorf("Invalid expression") } return stack[0], nil - Assistant