Haskell 程式計算商和餘數
在本教程中,我們將討論編寫一個程式來計算 Haskell 程式語言中的商和餘數。
商和餘數是兩個數量相除的結果。
例如 - 31/5 的商和餘數分別為 6 和 1。這個數字可以表示為 31(被除數)= 5(除數)* 6(商)+ 1(餘數)。
在本教程中,我們將看到四種不同的方法來編寫一個程式來計算商和餘數。
使用內建函式div 和 mod 計算商和餘數的 Haskell 程式
使用內建運算子計算商和餘數的 Haskell 程式。
使用遞迴函式計算餘數的 Haskell 程式。
使用遞迴函式計算商的 Haskell 程式。
演算法步驟
初始化或輸入被除數和除數。
實現計算商和餘數的邏輯。
列印或顯示計算結果。
示例 1
使用內建函式計算商和餘數的 Haskell 程式。
main :: IO() main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 print ("Entered Dividend and Divisor are:") print (show dividend ++"and"++ show divisor) -- computing quotient using function div and loading into a variable let quotient = div dividend divisor -- computing remainder using funcion mod and loading into a variable let remainder = mod dividend divisor -- printing the quotient and remainder print ("So the Quotient and Remainder are:") print (show quotient ++"and"++ show remainder)
輸出
"Entered Dividend and Divisor are:" "31and5" "So the Quotient and Remainder are:" "6and1"
在上面的程式中,我們宣告並初始化了兩個變數dividend 和 divisor。我們使用函式 div 計算了商,該函式接受兩個引數並返回計算出的除法的商。我們使用函式 mod 計算了餘數,該函式接受兩個引數並返回計算出的除法的餘數。最後,我們使用 print 函式列印了商和餘數。注意:show 是一個函式,它以數字作為引數,將其解析為字串,並返回字串。“++” 是連線兩個字串的運算子。
示例 2
使用運算子計算商和餘數的 Haskell 程式
main :: IO() main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 print ("Entered Dividend and Divisor are:") print (show dividend ++"and"++ show divisor) -- computing quotient using division operator let quotient = floor (dividend / divisor) -- computing remainder let remainder = (round dividend) - (round divisor)*quotient -- printing the quotient and remainder print ("So the Quotient and Remainder are:") print (show quotient ++"and"++ show remainder)
輸出
"Entered Dividend and Divisor are:" "31.0 and 5.0" "So the Quotient and Remainder are:" "6 and 1"
在上面的程式中,我們宣告並初始化了兩個變數dividend 和 divisor。我們使用除法運算子('/')計算了商。由於返回的數字可能是小數,因此我們使用函式 floor 來提取小於該數字的最大整數,它是除法的商。floor 是一個函式,它接受一個數字並返回小於或等於該數字的最大整數。當我們使用 let 關鍵字初始化數字時,數字將儲存為浮點數。因此,函式round 用於將浮點數型別轉換為整數型別。我們透過從被除數中減去除數*商來計算餘數。最後,我們列印了商和餘數。
示例 3
使用遞迴函式計算餘數的 Haskell 程式
-- function declaration remainder :: Int->Int->Int -- function definition remainder dividend divisor = if divisor >= dividend then dividend else remainder (dividend-divisor) divisor main :: IO () main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 -- invoking remainder function and printing result print ("The remainder is:") print (remainder dividend divisor)
輸出
"The remainder is:" 1
在上面的程式中,我們聲明瞭一個名為 remainder 的函式,它接受兩個整數引數並返回一個整數。在其函式定義中,我們接受兩個引數被除數和除數。如果被除數大於除數,則使用引數(被除數-除數)和除數對其自身進行遞迴呼叫。否則返回被除數。返回的被除數是餘數。在主函式中,我們呼叫此函式,並列印返回的餘數。
示例 4
使用遞迴函式計算商的 Haskell 程式
-- function declaration quotient :: Int->Int->Int->Int -- function definition quotient dividend divisor cnt = if divisor >= dividend then cnt else quotient (dividend-divisor) divisor cnt+1 main :: IO () main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 -- invoking quotient function and printing result print ("The quotient is:") print (quotient dividend divisor 0)
輸出
"The quotient is:" 6
在上面的程式中,聲明瞭函式 quotient,它接受三個整數引數並返回一個整數。在其函式定義中,接受三個引數被除數、除數和 cnt。如果被除數大於除數,則使用引數(被除數-除數)、除數和(cnt+1)對其自身進行遞迴呼叫。否則返回 cnt 值。該函式返回除數可以從被除數中減去多少次,即商。在主函式中,我們使用被除數、除數和初始計數 0 呼叫了 quotient 函式。最後,我們列印了返回的結果。
結論
在本教程中,我們討論了在 Haskell 程式語言中實現一個計算商和餘數的程式。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP