返回顶部

收藏

Quadratic formula (二次公式)

更多

[Ruby]代码

#PennyBoki @ </dream.in.code>
puts 'Enter the coefficients'
puts 'Enter a: '
STDOUT.flush              
a=gets.chomp.to_f                                 # user enters the coefficient a
puts 'Enter b: '
STDOUT.flush 
b=gets.chomp.to_f                                 # user enters the coefficient b
puts 'Enter c: '
STDOUT.flush 
c=gets.chomp.to_f                                 # user enters the coefficient c

D = b*b -4*a*c                                     # D is the discriminant

if D>=0                                                # if the discriminant is positive or 0
  x1=(-b-Math.sqrt(D))/(2*a)                 # x1 is the first solution
  x2=(-b+Math.sqrt(D))/(2*a)                # x2 is the second solution
  puts 'x1: '+x1.to_s+' x2: '+x2.to_s  # display the solutions
  else                                                  # if the discriminant is negative 
    x1=(-b)/(2*a)                                 # x1 is the real number of a complex number
    x2=(Math.sqrt(D*(-1)))/(2*a)           # x2 is the imaginary number of a complex number 
    puts 'x1: '+x1.to_s+'-i'+x2.to_s    # display the first solution
    puts 'x2: '+x1.to_s+'+i'+x2.to_s    # display the second solution
  end

标签:ruby/rails

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2015-09-30 09:04:13计算数字平均值 by p5soft
  2. 2015-09-29 09:03:51等级相关系数 Coefficient Of Rank Correlation by 李兰军
  3. 2015-09-24 14:33:50角度弧度转换代码 by 蟋蟀MM
  4. 2015-09-22 18:23:03数字转换成罗马数字 by 屁屁果
  5. 2015-09-22 09:12:52费马素性检验 by river0830
  6. 2015-09-21 09:03:53Brainfuck 解释器 by 云香水识
  7. 2015-09-18 09:34:24统计字母出现的次数 by lucasli
  8. 2015-09-11 18:30:16平均数/中间数 mean/median by zjbpku
  9. 2015-09-11 18:19:28Ruby 字符串回文判断 by 千万不要郁闷
  10. 2015-08-31 09:53:29约数求和 by 童学芬
  11. 2019-06-17 21:18:10PHP+jQuery.photoClip.js图片裁剪上传实例 by 素材火官网

发表评论