• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

rubytk实现简易计算器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#encding:GBK
require 'tk'

module Const
  WIDTH_OF_PANEL = 370
  HEIGHT_OF_PANEL = 520
  SIZE_OF_BUTTON_H = 60
  SIZE_OF_BUTTON_W = 80
  HEIGHT_OF_DISPLAY_FRAME = 90
  HEIGHT_OF_VALUE_LABEL = 30
  HEIGHT_OF_OPERATION_LABEL = 20
  BORDER_WIDTH = 2
  PADX = 10
  PADY = 10
  OP_ARRAY = ["%","sqrt","x^2","1/x","CE","C","DEL","/","7","8","9","*","4","5","6","-","1","2","3","+","+/-","0",".","="]
end

include Const

$value_tk_var = TkVariable.new(0)
$operate_tk_var = TkVariable.new
$Font = TkFont.new('arial 18')

class ButtonInfor
  attr_accessor :x, :y
  protected :x=, :y=
  def initialize(x=0,y=0)
    @x , @y = x , y
  end
end

module Work
  def number(num)
    if $value_tk_var.value == "0"
      $value_tk_var.value = num
    elsif $value_tk_var.value == '-0'
      $value_tk_var.value = $value_tk_var.value.chop + num
      puts "bbbb"
    elsif $value_tk_var.value != "0"
      $value_tk_var.value += num
    end
  end

  def point
    unless /\./ =~ $value_tk_var.value
      $value_tk_var.value += "."
    end
  end

  def calcu( key )
    unless (/\+/ =~ $operate_tk_var.value || /-/ =~ $operate_tk_var.value || /\*/ =~ $operate_tk_var.value || /\// =~ $operate_tk_var.value)
      $operate_tk_var.value = $value_tk_var.value + " " + key
      $value_tk_var.value = 0
    else
      $operate_tk_var.value = $operate_tk_var.value.chop + key
    end
  end

  def equl
    operator_ = $operate_tk_var.value[$operate_tk_var.value.size-1]
    $operate_tk_var.value = $operate_tk_var.value.chop
    num2 = (/\./ =~ $value_tk_var.value) ? $value_tk_var.value.to_f : $value_tk_var.value.to_i
    num1 = (/\./ =~ $operate_tk_var.value) ? $operate_tk_var.value.to_f : $operate_tk_var.value.to_i
    case operator_
    when "+"
      $value_tk_var.value = ( num1 + num2 ).to_s
    when "-"
      $value_tk_var.value = ( num1 - num2 ).to_s
    when "*"
      $value_tk_var.value = ( num1 * num2 ).to_s
    when "/"
      if num2 == 0 || num2 == 0.0
        $value_tk_var.value = "CANNOT DEVIDED BY 0!"
      else
        $value_tk_var.value = ( num1 / num2 ).to_s
      end
    end
    $operate_tk_var.value = nil
  end

  def clear(op)
    $value_tk_var.value = 0
    $operate_tk_var.value = nil if (op == "C")
  end

  def del
    $value_tk_var.value = $value_tk_var.value.chop
  end

  def sqrt_
    $value_tk_var.value = Math.sqrt($value_tk_var.value.to_f)
  end

  def sq_
    $value_tk_var.value = $value_tk_var.value.to_f * $value_tk_var.value.to_f
  end

  def devide_one
    $value_tk_var.value = 1/($value_tk_var.value.to_f)
  end

  def negate
    if $value_tk_var.value[0] != '-'
      $value_tk_var.value = "-" + $value_tk_var.value
    else
      $value_tk_var.value = $value_tk_var.value.reverse.chop.reverse
    end
  end

  def get_pos_hash
    tmp = Hash.new
    (0..23).each do |i|
      tmp[OP_ARRAY[i]]=ButtonInfor.new((i%4)*(PADX+SIZE_OF_BUTTON_W),(i/4)*(PADY+SIZE_OF_BUTTON_H))
    end
    return tmp
  end

  module_function :number , :point , :clear , :calcu , :equl , :del , :sqrt_ , :sq_ , :devide_one , :get_pos_hash ,:negate
end

#创建主窗口
root = TkRoot.new do
  title "SHH's Calculator";
  geometry WIDTH_OF_PANEL.to_s + "x" + HEIGHT_OF_PANEL.to_s;
end

operat_frame = TkFrame.new do
  relief 'groove'
  pack('fill' => 'x')
  borderwidth BORDER_WIDTH
  padx PADX
  pady PADY
  place:'height' => HEIGHT_OF_PANEL-HEIGHT_OF_DISPLAY_FRAME , 'width' => WIDTH_OF_PANEL , 'x' => 0 , 'y' => HEIGHT_OF_DISPLAY_FRAME
  background "LightCyan"
end

display_frame = TkFrame.new do
  relief 'groove'
  pack('fill' => 'x')
  borderwidth BORDER_WIDTH
  padx PADX
  pady PADY
  place:'height' => HEIGHT_OF_DISPLAY_FRAME , 'width' => WIDTH_OF_PANEL , 'x' => 0 , 'y' => 0
  background "lightblue"
end

value_label = TkLabel.new(display_frame) do
  relief 'groove'
  pack('fill' => 'x')
  borderwidth BORDER_WIDTH
  textvariable
  font $Font
  place:'height' => HEIGHT_OF_VALUE_LABEL , 'width' => WIDTH_OF_PANEL - PADX * 2 , 'x' => 0 , 'y' => 0
end

operation_label = TkLabel.new(display_frame) do
  relief 'groove'
  pack('fill' => 'x')
  borderwidth BORDER_WIDTH
  textvariable
  font TkFont.new('arial 10')
  place:'height' => HEIGHT_OF_OPERATION_LABEL , 'width' => WIDTH_OF_PANEL * 0.618  , 'x' => WIDTH_OF_PANEL * (1-0.618) - PADX * 2 , 'y' => HEIGHT_OF_VALUE_LABEL + PADY
end

value_label['textvariable'] = $value_tk_var
operation_label['textvariable'] = $operate_tk_var

Work.get_pos_hash().each do |key,value|
  TkButton.new(operat_frame) do
    font "Consolas 15"
    place:'height' => SIZE_OF_BUTTON_H , 'width' => SIZE_OF_BUTTON_W , 'x' => value.x , 'y' => value.y
    text key
    background (("0".."9") === key)? "Gainsboro" : "Gray"
    command do
      case key
      when ("0".."9")
        Work.number(key)
      when "."
        Work.point()
      when "+","-","*","/"
        Work.calcu(key)
      when "="
        Work.equl()
      when "CE","C"
        Work.clear(key)
      when "DEL"
        Work.del()
      when "sqrt"
        Work.sqrt_()
      when "x^2"
        Work.sq_()
      when "1/x"
        Work.devide_one()
      when "+/-"
        Work.negate()
      end
    end
  end
end

Tk.mainloop

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ruby+watir--问题记录发布时间:2022-07-14
下一篇:
Ruby日文手册翻译1发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap