赞助商

wxRuby编写的简单计算器

发布时间:2019-07-11 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了wxRuby编写的简单计算器脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#=====================================================
# █ 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,'0',Point.new(15,5),Size.new(290,30))
    @button_num1 = Button.new(@my_panel,'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,'2',Point.new(135,50))
    evt_button(@button_num2.get_id()) {|event| num2_button_click(event)}
    @button_num3 = Button.new(@my_panel,'3',Point.new(195,50))
    evt_button(@button_num3.get_id()) {|event| num3_button_click(event)}
    @button_num4 = Button.new(@my_panel,'4',160),50))
    evt_button(@button_num4.get_id()) {|event| num4_button_click(event)}
    @button_num5 = Button.new(@my_panel,'5',50))
    evt_button(@button_num5.get_id()) {|event| num5_button_click(event)}
    @button_num6 = Button.new(@my_panel,'6',50))
    evt_button(@button_num6.get_id()) {|event| num6_button_click(event)}
    @button_num7 = Button.new(@my_panel,'7',100),50))
    evt_button(@button_num7.get_id()) {|event| num7_button_click(event)}
    @button_num8 = Button.new(@my_panel,'8',50))
    evt_button(@button_num8.get_id()) {|event| num8_button_click(event)}
    @button_num9 = Button.new(@my_panel,'9',50))
    evt_button(@button_num9.get_id()) {|event| num9_button_click(event)}
    @button_num0 = Button.new(@my_panel,280),Size.new(110,50))
    evt_button(@button_num0.get_id()) {|event| num0_button_click(event)}
    @button_clear = Button.new(@my_panel,'C',40),50))
    evt_button(@button_clear.get_id()) {|event| clear_button_click(event)}
    @button_back = Button.new(@my_panel,'Back',50))
    evt_button(@button_back.get_id()) {|event| back_button_click(event)}
    @button_sqrt = Button.new(@my_panel,'Sqrt',50))
    evt_button(@button_sqrt.get_id()) {|event| sqrt_button_click(event)}
    @button_div = Button.new(@my_panel,'/',50))
    evt_button(@button_div.get_id()) {|event| div_button_click(event)}
    @button_mul = Button.new(@my_panel,'*',Point.new(255,50))
    evt_button(@button_mul.get_id()) {|event| mul_button_click(event)}
    @button_sub = Button.new(@my_panel,'-',50))
    evt_button(@button_sub.get_id()) {|event| sub_button_click(event)}
    @button_add = Button.new(@my_panel,'+',50))
    evt_button(@button_add.get_id()) {|event| add_button_click(event)}
    @button_result = Button.new(@my_panel,'=',110))
    evt_button(@button_result.get_id()) {|event| result_button_click(event)}
    @button_point = Button.new(@my_panel,'.',50))
    evt_button(@button_point.get_id()) {|event| point_button_click(event)}
    @button_MC = Button.new(@my_panel,'MC',50))
	evt_button(@button_MC.get_id()) {|event| MC_button_click(event)}
    @button_MR = Button.new(@my_panel,'MR',50))
	evt_button(@button_MR.get_id()) {|event| MR_button_click(event)}
    @button_MS = Button.new(@my_panel,'MS',50))
	evt_button(@button_MS.get_id()) {|event| MS_button_click(event)}
    @button_Madd = Button.new(@my_panel,'M+',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,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 [email protected] 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()

总结

以上是脚本之家为你收集整理的wxRuby编写的简单计算器全部内容,希望文章能够帮你解决wxRuby编写的简单计算器所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:76874919,请注明来意。
标签: