这是计算器程序,如何编写适用于未知数量输入的代码?

huangapple go评论74阅读模式
英文:

This is Calculator Program, How to code for unknown number of inputs?

问题

这个程序是用来进行不同的计算的。目前它只能对主函数中给定的两个数字进行基本运算,我正在尝试升级它,使其能够计算多于两个数字的情况,以及根据输入计算未知数量的数字。

package main

import (
	"fmt"
)

func add(n int, m int) int {
	sum := n + m
	return sum
}

func sub(n int, m int) int {
	diff := n - m
	return diff
}

func mul(n float32, m float32) float32 {
	pro := n * m
	return pro
}

func div(n float32, m float32) float32 {
	quo := n / m
	return quo
}

func main() {
	fmt.Println(add(4, 6))
	fmt.Println(sub(4, 6))
	fmt.Println(mul(4, 6))
	fmt.Println(div(6, 4))
}

请注意,这只是一个示例程序,目前只能处理两个数字。要使其能够处理更多数字,您需要对函数进行修改,并使用循环或递归来处理未知数量的数字。

英文:

This program is to do different calculations.right now it is only doing the basic operations for two numbers given in main, I'm trying to upgrade it to calculate for more than 2 numbers and more like for unknown number of number given in input.

package main

import (
	"fmt"
)

func add (n int , m int) int {
	 sum := n + m
	 return sum
}
func sub (n int , m int) int {
	 diff := n - m
	 return diff
}
func mul (n float32 , m float32) float32 {
	 pro := n * m
	 return pro
}

func div (n float32 , m float32) float32 {
	 quo := n / m
	 return quo
}

func main() {
	fmt.Println(add(4,6))
	fmt.Println(sub(4,6))
	fmt.Println(mul(4,6))
	fmt.Println(div(6,4))
}

答案1

得分: 2

你可以使用...来接受任意数量的参数,以下是你的程序的翻译版本:

package main

import (
	"fmt"
)

func add(m ...int) int {
	sum := 0
	for _, i := range m {
		sum += i
	}
	return sum
}
func sub(m ...int) int {
	sub := m[0]
	for _, i := range m[1:] {
		sub -= i
	}
	return sub
}
func mul(m ...float32) float32 {
	c := float32(1)
	for _, i := range m {
		c *= i
	}
	return c
}
func div(m ...float32) float32 {
	quo := m[0]
	for _, i := range m[1:] {
		quo /= i
	}
	return quo
}

func main() {
	fmt.Println(add(4, 6))
	fmt.Println(sub(4, 6))
	fmt.Println(mul(4, 6))
	fmt.Println(div(6, 4))
}

这是Go Playground的链接:https://play.golang.org/p/dWrMa-GdGj

英文:

You may use ... to accept arbitery number of arguements,
Here is how your program will be then

package main

import (
	"fmt"
)

func add(m ...int) int {
	sum := 0
	for _, i := range m {
		sum += i
	}
	return sum
}
func sub(m ...int) int {
	sub := m[0]
	for _, i := range m[1:] {
		sub -= i
	}
	return sub
}
func mul(m ...float32) float32 {
	c := float32(1)
	for _, i := range m {
		c *= i
	}
	return c
}
func div(m ...float32) float32 {
	quo := m[0]
	for _, i := range m[1:] {
		quo /= i
	}
	return quo
}

func main() {
	fmt.Println(add(4, 6))
	fmt.Println(sub(4, 6))
	fmt.Println(mul(4, 6))
	fmt.Println(div(6, 4))
}

Here is go play link : https://play.golang.org/p/dWrMa-GdGj

答案2

得分: 0

你的问题不太清楚,所以我会做一些假设。我假设你想要一个能够执行 add(1, 2, 3) 并返回 1 + 2 + 3 的函数。

有两种方法可以实现这个功能,一种是使用切片,另一种是使用 ...T 参数类型(参考 Go 规范)。

使用切片的实现方式将接受一个数字的切片,然后使用迭代方法对切片进行操作。例如,对于一个加法函数:

func add(numbers []int) int {
    sum := 0
    for _, number := range numbers {
        sum += number
    }
    return sum
}

你可以这样调用它:add([]int{1, 2, 3}),它将返回 1 + 2 + 3 的结果。

使用 ...T 参数类型几乎相同,它将多个参数转换为切片。

func add(numbers ...int) int {
    sum := 0
    for _, number := range numbers {
        sum += number
    }
    return sum
}

不同的是调用方式。要么是 add(1, 2, 3),要么如果你仍然想使用切片,可以是 add([]int{1, 2, 3}...)

你应该能够将这些概念应用到其他操作中。

英文:

Your question isn't too clear, so I'm going to make some assumptions. I'm assuming you want something that can do add(1, 2, 3) that will return 1 + 2 + 3.

There are two ways of doing this, either using slices, or using the ...T argument type. (Refer to the Go specifications).

An implementation that makes use of slices will accept a slice of numbers, and then use iterative methods to perform actions on the slice. For example for an add function:

func add(numbers []int) int {
	sum := 0
	for _, number := range numbers {
		sum += number
	}
	return sum
}

You can then call it like add([]int{1, 2, 3}) which will return the result of 1 + 2 + 3.

Using the ...T argument type is almost the same, as it basically turns multiple arguments into a slice.

func add(numbers ...int) int {
	sum := 0
	for _, number := range numbers {
		sum += number
	}
	return sum
}

Except calling it becomes different. Either: add(1, 2, 3) or if you would still like to use a slice, add([]int{1, 2, 3}...).

You should be able to apply these concepts to your other operations.

huangapple
  • 本文由 发表于 2017年1月2日 13:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/41421782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定