wxRuby编写的简单计算器

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#=====================================================
# █ wxRuby Calculator Sample Code
#=====================================================

begin
  require 'rubygems' 
rescue LoadError
end

require 'wx'
include Wx
include Win32

$op_a = ""
$op_b = ""
$result = 0
$memory = 0
$operator = nil
$point_flag = nil
Calculator_File_About = ID_ABOUT
Calculator_File_Quit = ID_EXIT
Calculator_Edit_Copy = ID_COPY
Calculator_Edit_Paste = ID_PASTE

class CacuFrame < Frame
  
  def initialize()
    super(nil, -1, 'Calculator', :size => [330,390], :style => MINIMIZE_BOX|SYSTEM_MENU|CAPTION|CLOSE_BOX|CLIP_CHILDREN)   
    @my_panel = Panel.new(self) 
	add_menu_bar
  	evt_menu(Calculator_File_Quit) {on_quit}
  	evt_menu(Calculator_File_About) {on_about}	
	evt_menu(Calculator_Edit_Copy) {on_copy}
  	evt_menu(Calculator_Edit_Paste) {on_paste}	
    @my_textbox = TextCtrl.new(@my_panel,-1, '0',Point.new(15,5),Size.new(290,30))
    @button_num1 = Button.new(@my_panel, -1, '1',Point.new(75,220), Size.new(50,50))
    evt_button(@button_num1.get_id()) {|event| num1_button_click(event)}
    @button_num2 = Button.new(@my_panel, -1, '2',Point.new(135,220), Size.new(50,50))
    evt_button(@button_num2.get_id()) {|event| num2_button_click(event)}
    @button_num3 = Button.new(@my_panel, -1, '3',Point.new(195,220), Size.new(50,50))
    evt_button(@button_num3.get_id()) {|event| num3_button_click(event)}
    @button_num4 = Button.new(@my_panel, -1, '4',Point.new(75,160), Size.new(50,50))
    evt_button(@button_num4.get_id()) {|event| num4_button_click(event)}
    @button_num5 = Button.new(@my_panel, -1, '5',Point.new(135,160), Size.new(50,50))
    evt_button(@button_num5.get_id()) {|event| num5_button_click(event)}
    @button_num6 = Button.new(@my_panel, -1, '6',Point.new(195,160), Size.new(50,50))
    evt_button(@button_num6.get_id()) {|event| num6_button_click(event)}
    @button_num7 = Button.new(@my_panel, -1, '7',Point.new(75,100), Size.new(50,50))
    evt_button(@button_num7.get_id()) {|event| num7_button_click(event)}
    @button_num8 = Button.new(@my_panel, -1, '8',Point.new(135,100), Size.new(50,50))
    evt_button(@button_num8.get_id()) {|event| num8_button_click(event)}
    @button_num9 = Button.new(@my_panel, -1, '9',Point.new(195,100), Size.new(50,50))
    evt_button(@button_num9.get_id()) {|event| num9_button_click(event)}
    @button_num0 = Button.new(@my_panel, -1, '0',Point.new(75,280), Size.new(110,50))
    evt_button(@button_num0.get_id()) {|event| num0_button_click(event)}
    @button_clear = Button.new(@my_panel, -1, 'C',Point.new(15,40), Size.new(50,50))
    evt_button(@button_clear.get_id()) {|event| clear_button_click(event)}
    @button_back = Button.new(@my_panel, -1, 'Back',Point.new(75,40), Size.new(50,50))
    evt_button(@button_back.get_id()) {|event| back_button_click(event)}
    @button_sqrt = Button.new(@my_panel, -1, 'Sqrt',Point.new(135,40), Size.new(50,50))
    evt_button(@button_sqrt.get_id()) {|event| sqrt_button_click(event)}
    @button_div = Button.new(@my_panel, -1, '/',Point.new(195,40), Size.new(50,50))
    evt_button(@button_div.get_id()) {|event| div_button_click(event)}
    @button_mul = Button.new(@my_panel, -1, '*',Point.new(255,40), Size.new(50,50))
    evt_button(@button_mul.get_id()) {|event| mul_button_click(event)}
    @button_sub = Button.new(@my_panel, -1, '-',Point.new(255,100), Size.new(50,50))
    evt_button(@button_sub.get_id()) {|event| sub_button_click(event)}
    @button_add = Button.new(@my_panel, -1, '+',Point.new(255,160), Size.new(50,50))
    evt_button(@button_add.get_id()) {|event| add_button_click(event)}
    @button_result = Button.new(@my_panel, -1, '=',Point.new(255,220), Size.new(50,110))
    evt_button(@button_result.get_id()) {|event| result_button_click(event)}
    @button_point = Button.new(@my_panel, -1, '.',Point.new(195,280), Size.new(50,50))
    evt_button(@button_point.get_id()) {|event| point_button_click(event)}
    @button_MC = Button.new(@my_panel, -1, 'MC',Point.new(15,100), Size.new(50,50))
	evt_button(@button_MC.get_id()) {|event| MC_button_click(event)}
    @button_MR = Button.new(@my_panel, -1, 'MR',Point.new(15,160), Size.new(50,50))
	evt_button(@button_MR.get_id()) {|event| MR_button_click(event)}
    @button_MS = Button.new(@my_panel, -1, 'MS',Point.new(15,220), Size.new(50,50))
	evt_button(@button_MS.get_id()) {|event| MS_button_click(event)}
    @button_Madd = Button.new(@my_panel, -1, 'M+',Point.new(15,280), Size.new(50,50))
	evt_button(@button_Madd.get_id()) {|event| Madd_button_click(event)}
    
    @my_panel_sizer = BoxSizer.new(VERTICAL)
    @my_panel.set_sizer(@my_panel_sizer)
    @my_panel.sizer.add(@my_textbox, 0, GROW|ALL, 2)
	
    show()   
    
  end

  def add_menu_bar
    menu_file = Menu.new
    menu_file.append(Calculator_File_About, "&About\tCtrl-A", "Show about dialog")
    menu_file.append_separator()
    menu_file.append(Calculator_File_Quit, "E&xit\tAlt-X", "Quit self program")
	menu_edit = Menu.new
	menu_edit.append(Calculator_Edit_Copy, "&Copy\tCtrl-C", "Copy result")
	menu_edit.append(Calculator_Edit_Paste, "&Paste\tCtrl-V", "Paste result")
    menu_bar = MenuBar.new
    menu_bar.append(menu_file, "&File")
  	menu_bar.append(menu_edit, "&Edit")
    set_menu_bar(menu_bar)
  end
  
  def on_about
    message_box("wxRuby Calculator Sample\nby xpxxp@bmy 2012-8-1\n" ,"About Calculator", OK | ICON_INFORMATION, self)
  end
  
  def on_quit
    close(true)
  end
  
  # copy data 
  def on_copy
	  Clipboard.set_data(@my_textbox.value)
  end
  
  # paste data
  def on_paste
    @my_textbox.set_value(Clipboard.data)
	$operator != nil
	$op_a = @my_textbox.value
  end
  
  def num1_button_click(event)
    if $operator != nil
      $op_b << "1"     
      @my_textbox.set_value($op_b)
    else
      $op_a << "1"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num2_button_click(event)
    if $operator != nil
      $op_b << "2"
      @my_textbox.set_value($op_b)
    else
      $op_a << "2"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num3_button_click(event)
    if $operator != nil
      $op_b << "3"
      @my_textbox.set_value($op_b)
    else
      $op_a << "3"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num4_button_click(event)
    if $operator != nil
      $op_b << "4"
      @my_textbox.set_value($op_b)
    else
      $op_a << "4"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num5_button_click(event)
    if $operator != nil
      $op_b << "5"
      @my_textbox.set_value($op_b)
    else
      $op_a << "5"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num6_button_click(event)
    if $operator != nil
      $op_b << "6"
      @my_textbox.set_value($op_b)
    else
      $op_a << "6"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num7_button_click(event)
    if $operator != nil
      $op_b << "7"
      @my_textbox.set_value($op_b)
    else
      $op_a << "7"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num8_button_click(event)
    if $operator != nil
      $op_b << "8"
      @my_textbox.set_value($op_b)
    else
      $op_a << "8"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num9_button_click(event)
    if $operator != nil
      $op_b << "9"
      @my_textbox.set_value($op_b)
    else
      $op_a << "9"
      @my_textbox.set_value($op_a)
    end
  end
  
  def num0_button_click(event)
    if $operator != nil
      $op_b << "0"
      @my_textbox.set_value($op_b)
    else
      $op_a << "0"
      @my_textbox.set_value($op_a)
    end
  end
  
  def sqrt_button_click(event)
    if $operator != nil
      if ($op_b.to_f >= 0)
        $op_b = Math.sqrt($op_b.to_f).to_s
      else
	    $op_b = "Error:Radicand must be >=0!"
      end
      @my_textbox.set_value($op_b)
    else
      if ($op_a.to_f >= 0)
        $op_a = Math.sqrt($op_a.to_f).to_s
      else
        $op_a = "Error:Radicand must be >=0!"
      end
      @my_textbox.set_value($op_a)
    end
	  $op_a = ""
      $op_b = ""
      $result = 0
      $operator = nil
      $point_flag = nil
  end
    
  def clear_button_click(event)
    $op_a = ""
    $op_b = ""
    $result = 0
    $operator = nil
    $point_flag = nil
    @my_textbox.set_value("0")
  end
  
  def back_button_click(event)
    if $operator != nil
      $op_b = $op_b.chop
      @my_textbox.set_value($op_b)
    else
      $op_a = $op_a.chop
      @my_textbox.set_value($op_a)
    end
  end
  
  def point_button_click(event)
    str = /\./
    if $operator != nil
      if ( ( $op_b =~ str ) == nil )
        $op_b << "."
        @my_textbox.set_value($op_b)
      end
    else
      if ( ($op_a =~ str) == nil )
        $op_a << "."
        @my_textbox.set_value($op_a)
      end
    end
  end
  
  def div_button_click(event)
       result_display
	   $operator = "/"
  end
  
  def mul_button_click(event)
       result_display
	   $operator = "*"
  end
  
  def sub_button_click(event)
       result_display
	   $operator = "-"
  end
  
  def add_button_click(event)
       result_display
	   $operator = "+"
  end
  
  # clear memory
  def MC_button_click(event)
    $memory = 0
	$op_a = ""
    $op_b = ""
    $operator = nil
    $point_flag = nil
  end
  
  # show memory
  def MR_button_click(event)
    @my_textbox.set_value($memory.to_s)
	$op_a = ""
    $op_b = ""
    $operator = nil
    $point_flag = nil
  end
  
  # save data to memory, memory will be overwritten
  def MS_button_click(event)
    $memory = @my_textbox.value.to_f
	$op_a = ""
    $op_b = ""
    $operator = nil
    $point_flag = nil
  end
  
  # add data to memory and save the result to memory
  def Madd_button_click(event)
    $memory += @my_textbox.value.to_f
	$op_a = ""
    $op_b = ""
    $operator = nil
    $point_flag = nil
  end

  def result_button_click(event)
    result_display
	$op_a = ""
  end

  def result_display
    case $operator
      when "+":
        $result = $op_a.to_f + $op_b.to_f
      when "-":
        $result = $op_a.to_f - $op_b.to_f
      when "*":
        $result = $op_a.to_f * $op_b.to_f
      when "/":
        $result = $op_a.to_f / $op_b.to_f
	  else
		$result = @my_textbox.value.to_f
    end
    @my_textbox.set_value("#{$result}")
    $op_a = $result.to_s
    $op_b = ""
	$operator = nil
	$point_flag = nil
  end
  
end

class CacuApp < App
  def on_init
    CacuFrame.new
  end
  
end

CacuApp.new.main_loop()