使用庫函式獲取商和餘數的Go語言程式


在本文中,我們將討論如何使用Go語言的庫函式來獲取商和餘數。

語法

func Div(a, b, c uint) (q, r uint)
func Remainder(q, r float64) float64

div() 函式接受三個無符號整數作為引數,並在計算除法過程後分別返回商和餘數。

remainder() 函式接受兩個64位浮點值作為引數,並返回進行除法運算後的餘數(同樣為64位浮點型)。

以下是使用庫函式獲取除法商和餘數的原始碼,已編譯並執行。

使用Div()函式求兩個數的商

演算法

  • 步驟1 − 匯入fmt和bits包。

  • 步驟2 − 初始化並定義division()函式。

  • 步驟3 − 開始main()函式。

  • 步驟4 − 初始化變數,並將被除數和除數的值儲存在其中。

  • 步驟5 − 透過將被除數和除數的值作為引數傳遞給division函式來呼叫它。

  • 步驟6 − 使用bits.Div()預定義庫函式執行除法過程。

  • 步驟7 − 返回商和餘數的值。

  • 步驟8 − 在螢幕上列印結果。

示例

使用庫函式獲取兩個數除法商的Go語言程式 12. 使用庫函式獲取商和餘數的Go語言程式

package main import ( "fmt" "math/bits" ) // fmt package allows us to print anything on the screen. // bits sub package defined in math package is used to perform bitwise operations on unsigned integer data types. // initializing and defining the division() function func division(dividend, divisor uint) (quotient, remainder uint) { // Finding quotient and remainder Using Div() function quotient, remainder = bits.Div(0, dividend, divisor) // returning the results return quotient, remainder } // calling the main() function func main() { // initializing the variables. var dividend, divisor, quotient, remainder uint // assigning the values of dividend. dividend = 50 // assigning the values of divisor. divisor = 7 // calling the division() function and storing the results in quotient and remainder variables quotient, remainder = division(dividend, divisor) // printing the Quotient on the screen fmt.Println("The Quotient of ", dividend, "/", divisor, "is:") fmt.Println(quotient) // printing the Remainder on the screen fmt.Println("The Remainder of ", dividend, "/", divisor, "is:") fmt.Println(remainder) }

輸出

The Quotient of 50 / 7 is:
7
The Remainder of 50 / 7 is:
1

程式碼描述

  • 首先,我們匯入fmt和bits包,它們分別允許我們列印任何內容和執行按位操作。

  • 初始化並定義division()函式,該函式將包含執行除法過程的邏輯。

  • 此函式接受兩個無符號整數值作為引數並返回結果。

  • 我們在這裡使用了無符號整數型別的變數,因為bits.Div()函式接受無符號整數值。

  • 此方法接受三個引數並返回兩個值。為了計算兩個數的商,我們這裡將其中一個值用作零。

  • 此函式返回的值分別是商和餘數的值。

  • 將此函式返回的值儲存在單獨的變數中並返回它們。

  • 啟動main()函式。

  • 初始化無符號整數型別的變數,並將被除數和除數的值儲存在其中。

  • 呼叫division()函式並將被除數和除數作為引數傳遞給它。

  • 將函式返回的結果儲存在分別名為商和餘數的單獨變數中。

  • 使用fmt.Println()函式在螢幕上列印結果。

使用Remainder()函式求兩個數的商

演算法

  • 步驟1 − 匯入fmt和bits包。

  • 步驟2 − 初始化並定義division()函式。

  • 步驟3 − 開始main()函式。

  • 步驟4 − 初始化變數,並將被除數和除數的值儲存在其中。

  • 步驟5 − 透過將被除數和除數的值作為引數傳遞給division函式來呼叫它。

  • 步驟6 − 使用math.Remainder()預定義庫函式執行除法過程。

  • 步驟7 − 返回商和餘數的值。

  • 步驟8 − 在螢幕上列印結果。

示例

Go語言中還有一個庫函式可以用來計算兩個整數相除的餘數。本示例將討論此方法。

package main import ( "fmt" "math" ) // math package enables us to use other predefined mathematical functions. // initializing and defining the division() function func division(dividend, divisor float64) (remainder float64) { // Finding the remainder Using math.Remainder() function // storing the results in remainder variable remainder = math.Remainder(dividend, divisor) // returning the results return remainder } // calling the main() function func main() { // initializing the variables. var dividend, divisor, remainder float64 // storing the value of dividend dividend = 100.0 // storing the value of divisor divisor = 3.0 // calling the division() function remainder = division(dividend, divisor) // printing the Remainder on the screen fmt.Println("The Remainder of ", dividend, "/", divisor, "is:") fmt.Println(remainder) }

輸出

The Remainder of 100 / 3 is:
1

程式碼描述

  • 首先,我們匯入fmt和math包,它們分別允許我們列印任何內容和使用預定義的數學運算。

  • 初始化並定義division()函式,該函式將包含執行除法過程的邏輯。

  • 此函式接受兩個64位浮點值作為引數並返回結果。

  • 我們在這裡使用了64位浮點型變數,因為math.Remainder()函式接受64位浮點型值。

  • math.Remainder()函式返回的值是餘數,因此將其儲存在單獨的變數中。

  • 返回此值。

  • 啟動main()函式。

  • 初始化相應的變數,並將被除數和除數的值儲存在其中。

  • 呼叫division()函式並將被除數和除數作為引數傳遞給它。

  • 將函式返回的結果儲存在單獨的變數中並列印其值。

結論

我們已經成功編譯並執行了Go語言程式,該程式將使用庫函式以及示例來獲取除法結果和餘數。

更新於:2022年10月25日

瀏覽量:543

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.