2013-11-14 43 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