Golang程序 从有理数中获取分子

Golang程序 从有理数中获取分子

在这篇文章中,我们将讨论如何从一个有理数中获取分子。

语法

func NewRat(a, b int64) *Rat
func (x *Rat) Num() *Int

NewRat() 函数将两个整数作为输入参数,并返回一个a/b形式的有理数,这里a是有理数的分子,b是分母。

Go语言中的 Num() 函数将有理数的分子作为结果以整数格式返回。

方法一

Go语言程序从一个有理数中获取分子。

算法

  • 第1步 – 导入fmt和big包。

  • 第2步–调用main()函数。

  • 第3步–初始化一个变量来存储有理数。使用big.NewRat()来创建有理数。

  • 第4步 – 使用num()函数得到有理数的分子。

  • 第5步 – 存储结果并将其打印在屏幕上

例子

使用库函数获取有理数的分子的源代码如下

// Including the main package
package main

// Importing fmt and math/big
import (
   "fmt"
   "math/big"
)
// fmt package allows us to print anything on the screen
// big defined in math package allows us to use various predefined methods like Num()

// Calling main
func main() {
   fmt.Println("Golang Program to get the numerator of a rational number")

   // NewRat creates a new Rational number with numerator a and denominator b
   number := big.NewRat(10, 16)

   // Printing the result on the screen
   fmt.Println("The rational number is", number)

   // getting the numerator of the rational number
   // storing the result in a result variable
   result := number.Num()

   // printing the numerator on the screen
   fmt.Println("The numerator of rational number", number, "is:", result)
}

输出

Golang Program to get the numerator of a rational number
The rational number is 5/8
The numerator of rational number 5/8 is: 5

说明

  • 首先我们需要导入允许我们打印任何东西的fmt包和使用各种预定义方法的big包。

  • 调用main()函数。

  • 使用big包中定义的NewRat()函数创建一个有理数,向该函数提供两个输入作为参数。这些输入的方式是:第一个输入被视为有理数的分子,第二个输入是其分母。

  • 然后将数字存储在一个名为number的变量中。

  • 现在我们需要得到这个数字的分子并将其打印在屏幕上。

  • 为了得到分子,请使用大软件包中的Num()函数。这个函数以int格式返回分母的结果。

  • 将结果的值存储在一个变量中。

  • 使用println()函数在屏幕上打印结果。

方法二

Go语言程序使用两个函数从一个有理数中获得分子。

算法

  • 第1步–导入fmt、big和rand包。

  • 第2步 – 创建一个getNumerator()函数。

  • 第3步–调用main()函数。

  • 第4步–调用getNumerator()函数。

  • 第5步–初始化一个变量,在其中存储有理数。使用big.NewRat()来创建有理数。

  • 第6步 – 使用Num()函数来获得有理数的分子。

  • 第7步 – 返回这个值。

  • 第8步 – 存储结果并将其打印在屏幕上。

例子

下面是使用两个函数获取有理数分子的源代码。

// Including the main package
package main

// Importing fmt and math/big
import (
   "fmt"
   "math/big"
   "math/rand"
)
// fmt package allows us to print anything on the screen. 
// big package allows us to use various predefined mathematical methods like a rat 
// rand package allows us to generate random numbers.


// Initializing the getNumerator() function.
func getNumerator() *big.Int {
   // NewRat creates a new Rational number with numerator a and denominator b
   number := big.NewRat(int64(rand.Intn(200)), int64(rand.Intn(200)))

   // Printing the result
   fmt.Println("The rational number is", number)

   // getting the numerator of the rational number
   // storing the result in a result variable
   result := number.Num()

   // returning the result
   return result
}
// Calling main
func main() {
   fmt.Println("Golang Program to get the numerator of a rational number")

   // calling the getNumerator() function
   result := getNumerator()

   // printing the numerator on the screen
   fmt.Println("The numerator of rational number is:", result)
}

输出

Golang Program to get the numerator of a rational number
The rational number is 27/29
The numerator of rational number is: 27

说明

  • 导入 fmt、big 和 rand 包。

  • fmt包允许我们在屏幕上打印任何东西。big包允许我们使用各种预定义的数学方法,如rat,rand包允许我们生成随机数。

  • 创建并定义getNumerator()函数。这个函数将生成有理数并返回其分子。

  • 在这里,为了生成有理数,我们使用了NewRat()方法。我们使用RandInt()函数来生成200以内的随机整数值,这些整数值以分子和分母的形式输入到这个函数中。

  • 我们将以这种方式生成的32位整数转换为64位,因为NewRat()接受64位数值作为输入。

  • 使用Num()函数得到分子的值,并将结果存储在一个变量中。

  • 返回这个值。

  • 使用fmt.Printf()函数在屏幕上打印最终结果。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程