我是靠谱客的博主 感动戒指,最近开发中收集的这篇文章主要介绍Ruby生成加减乘法作业,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

最近,小朋友的学校要练习加减乘法,家长们需要自己出题。刚好看了点Ruby的语法,凑合着编写了个自动生成器: 前半部分加减法,后半部分加减乘。

 

 

file=File.new(File.join("D:","/projworkspace/ruby_cases/2.txt"),"w") 

SP = '      '

sum = ''

100.times do |x|
  #srand(1234)

  # pattern
  # 0: a + b + c =R  
  # 1: a + b - c =R
  # 2: a - b + c =R
  # 3: a - b - c =R
  op = rand(4)    

  a = rand(10...100)
  b = rand(10...100)
  #cc = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90]
  cc = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90]
  r = rand(0...cc.length())
  c = cc[r]

  exp = ""
  if op == 0  
    m = a + b
    while m + c > 100 || a%10 + b%10 < 10  #|| m%10 + c%10 < 10
      a = rand(10...100)
      b = rand(10...100)
      m = a + b

      r = rand(0...cc.length())
      c = cc[r]
    end


    exp = "#{a} + #{b} + #{c} = #{SP}"

  elsif op == 1
    m = a + b
    while m > 100 || m - c < 0 || a%10 + b%10 < 10  || m%10 > c%10 #|| m%10 + c%10 < 10
      a = rand(10...100)
      b = rand(10...100)
      m = a + b

      r = rand(0...cc.length())
      c = cc[r]
    end


    exp = "#{a} + #{b} - #{c} = #{SP}"    

  elsif op == 2
    m = a - b
    while m < 0 || m + c > 100 || a%10 - b%10 > 0 || m%10 + c%10 < 10   #|| m%10 + c%10 < 10
      a = rand(10...100)
      b = rand(10...100)
      m = a - b

      r = rand(0...cc.length())
      c = cc[r]
    end


    exp = "#{a} - #{b} + #{c} = #{SP}"    
  elsif op == 3
    m = a - b
    while m < 0 || m < c || a%10 - b%10 > 0 || m%10 > c%10   #|| m%10 + c%10 < 10
      a = rand(10...100)
      b = rand(10...100)
      m = a - b

      r = rand(0...cc.length())
      c = cc[r]
    end


    exp = "#{a} - #{b} - #{c} = #{SP}"   
  end
    



  sum += exp

  if (x+1)%3 == 0
    file.puts(sum)
    sum = ""
  end
end


sum = ""

50.times do |x|
  #srand(1234)

  # pattern
  # 0: a*b + c =R  
  # 1: c + a*b =R
  # 2: a*b - c =R
  # 3: c - a*b =R
  op = rand(3)    

  a = rand(1...7)
  b = rand(1...7)
  m = a*b

  cc = [1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90]
  r = rand(0...cc.length())
  c = cc[r]

  exp = ""
  if op == 0 || op == 1
    while m + c > 100 || m%10 + c%10 < 10
      a = rand(1...7)
      b = rand(1...7)
      m = a*b

      r = rand(0...cc.length())
      c = cc[r]
    end

    if op == 0 || op == 1
      exp = "#{a} x #{b} + #{c} = #{SP}"
    else
      exp = "#{c} + #{a} x #{b} = #{SP}"
    end

  elsif op == 2
    while m < c || m%10 >= c%10
      a = rand(1...7)
      b = rand(1...7)
      m = a*b

      r = rand(0...cc.length())
      c = cc[r]
    end

    exp = "#{a} x #{b} - #{c} = #{SP}"
  elsif op == 3
    while c < m || c%10 >= m%10
      a = rand(1...7)
      b = rand(1...7)
      m = a*b

      r = rand(0...cc.length())
      c = cc[r]
    end    
    exp = "#{c} - #{a} x #{b} = #{SP}"
  end
    



  sum += exp

  if (x+1)%3 == 0
    file.puts(sum)
    sum = ""
  end
end



 


 

 

最后

以上就是感动戒指为你收集整理的Ruby生成加减乘法作业的全部内容,希望文章能够帮你解决Ruby生成加减乘法作业所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部