Home > Backend Development > Golang > Implement a basic arithmetic operator using Go language

Implement a basic arithmetic operator using Go language

王林
Release: 2023-12-23 14:52:20
Original
700 people have browsed it

Implement a basic arithmetic operator using Go language

Go language is an open source, statically typed compiled language that has received widespread attention and use for its simplicity, efficiency and easy expansion. This article will introduce how to use Go language to write a simple four-arithmetic calculator and provide specific code examples.

First, we need to define several basic data structures to represent operational expressions and operators. We can use structures to represent operators and operands, and use enumeration types to limit the value range of operators.

type Operator int

const (
    Add Operator = iota
    Subtract
    Multiply
    Divide
)

type Operand struct {
    value float64
}

type Expression struct {
    operator Operator
    leftOperand  Operand
    rightOperand Operand
}
Copy after login

Next, we can implement a function to parse the operation expression entered by the user and return an Expression object.

func parseExpression(input string) (Expression, error) {
    // 解析输入字符串并生成Expression对象
    // 如果输入不合法则返回错误信息
}
Copy after login

Then, we can implement a calculation function to calculate the result based on the given Expression object.

func calculate(expression Expression) float64 {
    switch expression.operator {
    case Add:
        return expression.leftOperand.value + expression.rightOperand.value
    case Subtract:
        return expression.leftOperand.value - expression.rightOperand.value
    case Multiply:
        return expression.leftOperand.value * expression.rightOperand.value
    case Divide:
        return expression.leftOperand.value / expression.rightOperand.value
    default:
        return 0
    }
}
Copy after login

Finally, we can write a main function to process user input and call parsing and calculation functions to output the results.

func main() {
    fmt.Println("欢迎使用四则运算计算器!")
    fmt.Println("请输入一个四则运算表达式:")
    
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        input := scanner.Text()
        
        expression, err := parseExpression(input)
        if err != nil {
            fmt.Println("输入无效,请重新输入")
            continue
        }
        
        result := calculate(expression)
        fmt.Println("结果是:", result)
    }
    
    if err := scanner.Err(); err != nil {
        fmt.Println("读取输入失败:", err)
    }
}
Copy after login

The above is a simple example code of a four-arithmetic calculator written in Go language. By defining data structures and implementing parsing and calculation functions, we can easily process user input and output results. Of course, this example can be further extended and optimized, such as adding more operators and supporting complex operation expressions, but the basic framework and ideas are the same.

To summarize, Go language is a powerful programming language that can be used to easily write various applications, including four arithmetic operations calculators. I hope the sample code and explanations in this article can be helpful to you.

The above is the detailed content of Implement a basic arithmetic operator using Go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template