2013-11-14 96 views
0

到目前爲止,我已經得到了如何在ruby中創建一個終端計算器程序?

puts "Enter a calculation: " 

calc = gets.chomp 

def add(a, b) 
    puts a + b 
end 

puts add(calc) 

現在我羞於承認,但我很爲難,我試着寫添加方法等...但我似乎無法總結我的頭轉過來計算並輸出正確的結果。

爲了簡化這一點,我該如何獲得增加工作的能力?

即用戶輸入的計算(2點的整數),程序將 計算,程序結果輸出,程序請求另一個計算 。

+0

爲什麼不告訴我們你試過的東西,並詢問有關混淆的特定部分? :) – CDub

+0

做:)雖然很尷尬。 –

+0

沒什麼好尷尬的 - 每個人都從某個地方開始。 :) – CDub

回答

2

想想你的問題一次一步。您希望用戶提供整數。所以,用一個簡單的提示開始,就像你已經這樣做:

puts "Enter your first value" 

現在從用戶獲得的價值:

firstVal = gets.chomp 

現在提供另一個提示,並獲得了第二個值。

puts "Enter your second value" 
secVal = gets.chomp 

和輸出結果:

puts "#{firstVal} + #{secVal} = #{firstVal.to_i + secVal.to_i}" 

有時只是寫出來簡單明瞭是最簡單的第一步。現在您可以創建一個添加功能來更有效地完成此操作。試試看,看看你是否有幸運!

編輯: 另外,我注意到你的add函數有兩個參數,但你只傳遞它一個。 爲了用兩個參數調用一個函數,需要兩個值來提供它。例如:

x = 5 
y = 2 

def add(a, b) 
    return a + b 
end 

puts add(x, y) 
+0

謝謝adback03,有時我傾向於過度複雜的東西:) –

+2

不要擔心它。編程當然不容易,特別是當你剛剛開始時。祝你好運! –

1

這裏有一個小巧的計算器我刮起來幫助你開始:

#! /usr/bin/ruby 

def add(a, b) 
    a + b 
end 

while(true) do 
    puts "Enter a calculation: " 

    # chomp off the \n at the end of the input 
    calc = gets.chomp 

    # quit if the user types exit 
    exit if calc.include?("exit") 

    # puts the result of the add function, taking input of calc "split" into two numbers 
    puts add(calc.split("").first.to_i, calc.split("").last.to_i) 
    # spacing 
    puts 
end 
+0

謝謝CDub,這不僅僅是有用的。 –

1

爲了擴大這項。如果你想繼續接受計算請求,你可以把這個過程放在一個循環中(在許多解決方案中)。

while true 
    puts 'Enter Val 1' 
    v1 = gets.chomp.to_i 
    puts 'Enter Val 2' 
    v2 = gets.chomp.to_i 
    puts "#{v1} + #{v2} = #{v1+v2} " 
end 
3

我覺得這種腳本是一個完美的case語句。下面是一個二進制運營商工作的第一關:

#! /usr/bin/ruby 

def calc 
    puts "Calculator 1.0 \nEnter 'q' to quit." 

    while true 
    print ">> " 
    str = gets.chomp.split(" ") # splits into array, rejects blanks 
    return if str[0] == 'q'  # quit if first element is 'q' 

    operand1 = str[0].to_i 
    operand2 = str[2].to_i 
    operator = str[1].to_sym 

    case operator 
    when :+ then puts operand1 + operand2 
    when :- then puts operand1 - operand2 
    when :* then puts operand1 * operand2 
    when :/ then puts operand1/operand2 
    when :% then puts operand1 % operand2 
    else 
     puts "invalid input" 
    end 
    end 

end 

if __FILE__ == $0  # if run as script, start calc: see http://j.mp/HOTGq8 
    calc 
end 

然後,在命令行:

$ ./calc.rb 
Calculator 1.0 
Enter 'q' to quit. 
>> 55/5 
11 
>> 90/10 
9 
>> 5 * 3093 
15465 

祝你好運!

這些都是偉大的資源,如果你是剛剛起步:RubyMonkCodecademy

+0

這太棒了!謝謝jmromer –

2

我知道這是一個年紀大一點的職位,但人們仍舊覺得這個答案,我想補充一下jkrmr上面說的。

jkrmr發佈的代碼很好,但不處理浮點計算,這是一個簡單的修復,所以我添加了functinoality。 :-)

#! /usr/bin/ruby 

def calc 
    puts "Calculator 1.1 \nEnter 'q' to quit." 

    while true 
    print ">> " 
    str = gets.chomp.split(" ") # splits into array, rejects blanks 
    return if str[0] == 'q'  # quit if first element is 'q' 
    if str[0].include? "." 
     operand1 = str[0].to_f 
    else 
     operand1 = str[0].to_i 
    end 

    operator = str[1].to_sym 

    if str[2].include? "." 
     operand2 = str[2].to_f 
    else 
     operand2 = str[2].to_i 
    end 

    case operator 
    when :+ then puts operand1 + operand2 
    when :- then puts operand1 - operand2 
    when :* then puts operand1 * operand2 
    when :/ then puts operand1/operand2 
    when :% then puts operand1 % operand2 
    else 
     puts "invalid input" 
    end 
    end 

end 

if __FILE__ == $0  # if run as script, start calc: see http://j.mp/HOTGq8 
    calc 
end