EvilZone
Programming and Scripting => Scripting Languages => : Dark Nebulae October 23, 2012, 05:22:27 PM
-
So guys, its my first program in ruby.
print "===========================================================\n"
print "============ Simple Calculator Menu=========================\n"
print "============================================================="
print "\n\n"
puts " 1.)Addition \t 2.)Subtraction \n 3.)Multiplication \t 4.)Division \n 5.)Power \t 6.)Square Root"
c=Integer(gets.chomp)
if c == 1
print "Enter any integer: "
a=Integer(gets.chomp)
print "Enter another integer: "
b=Integer(gets.chomp)
d=a+b
print "The sum is: "+d.to_s
elsif c == 2
print "Enter any integer: "
a=Integer(gets.chomp)
print "Enter another number: "
b=Integer(gets.chomp)
d=a-b
print "The difference is: "+d.to_s
elsif c == 3
print "Enter a number: "
a=Integer(gets.chomp)
print "Enter another number: "
b=Integer(gets.chomp)
d=a*b
print "The product is "+d.to_s
elsif c == 4
print "Enter a number: "
a=Integer(gets.chomp)
print "Enter another number: "
b=Integer(gets.chomp)
d=a/b
print "The quotient after dividing "+a.to_s+" by "+b.to_s+" is "+d.to_s
elsif c == 5
print "Enter the number: "
a=Integer(gets.chomp)
print "Enter the power: "
b=Integer(gets.chomp)
d=a**b
print ""+a.to_s+" raised to power "+b.to_s+" is "+d.to_s
elsif c == 6
print "Enter the number: "
a=Integer(gets.chomp)
d=a**0.5
print "The square root of "+a.to_s+" is "+d.to_s
else
print "Wrong choice"
end
-
First of all, this is not a "program", it's a Script.
Second of all, it's acceptable as a first script. But if you keep up with such code formatting (ugly as hell) it will not be acceptable and people here won't take it nicely.
I am sure Ruby, as every other language, has specific Ruby conventions, but general code conventions apply to every (real) coding language.
One of your mistakes are missplaced spaces,
bad indentation,
lack of spaces (b=Integer(gets.chomp) ;; d=a+b),
variable names ar horrible - use words instead of letters,
no grouping for logical statements (elsif c == 5)...
This is acceptable as a working (I assume it's working, can't run it) solution. Correct the errors I pointed out and you'll do much better.
Waiting for your other scripts :)
-
variable names ar horrible - use words instead of letters,[/size]
I have used appropriate symbols.
c for choice, a for first input(as it is the first alphabet of English language),b for second input(the reason is same as that in case of a),now after that c is chosen therefore I used d to store the result.It is very simple and compact and no one should have problem with it.
[/size]no grouping for logical statements (elsif c == 5)...[/color][/size]
I recently started learning ruby language and therefore it is correct.
-
*sight* some people should not post at all...
You clearly did not understand what I was trying to explain, but I don't care anymore... also the colors in your post does not make it any better to read. If you're going to post like such, at least don't make mistakes with formatting and stuff...
-
*sight* some people should not post at all...
You clearly did not understand what I was trying to explain, but I don't care anymore... also the colors in your post does not make it any better to read. If you're going to post like such, at least don't make mistakes with formatting and stuff...
I used the different colors to highlight what I want to say.
-
quick rewrite, might help
# ruby has puts(print + newline), use it
# also has heredocs
puts <<EOS
================================================
============ Simple Calculator Menu ============
================================================
1) Addition
2) Substraction
3) Multiplication
4) Division
5) Exponent
6) Root
EOS
# AFAIK ruby doesn't have a prompt/stdin get
# ARGF(args/stdin) is the way to go anyways
# programs that ask for input usually suck
def get(prompt)
print prompt
STDOUT.flush
gets.chomp
end
# until is between 1 - 6
until (opt = get('enter option: ').to_i).between?(1,6)
puts "unknown option: #{opt}"
end
a = get("first number: ").to_f
b = get("second number: ").to_f
# use switch/case
result = case opt
when 1 then a + b
when 2 then a - b
when 3 then a * b
when 4 then a / b
when 5 then a**b
when 6 then a**(1/b)
end
puts "result: #{result}"
-
I have used appropriate symbols.
c for choice, a for first input(as it is the first alphabet of English language),b for second input(the reason is same as that in case of a),now after that c is chosen therefore I used d to store the result.It is very simple and compact and no one should have problem with it.I recently started learning ruby language and therefore it is correct.
If you didn't wish to ask for opinions, why post the code?