Ruby编写的简单计算器

用wxruby编写的一个简单的图形界面计算器:

1.初步实现了+ - × / 平方根的功能

2.MC MR MS M+功能没有做

目前功能都比较简单,还有许多功能需要优化,供初学者交流

[Ruby]代码

begin
  require 'rubygems'
rescue LoadError
end
require 'wx'
include Wx

$op_x = ""
$op_y = ""
$result = 0
$operator = nil
$point_flag = nil

class CacuFrame < Frame
  #��ʼ������
 
  def initialize()
    super(nil, -1, 'Caculator', :size => [340,380], :style => MINIMIZE_BOX|SYSTEM_MENU|CAPTION|CLOSE_BOX|CLIP_CHILDREN)
   
    #�����������ؼ��������ť�¼�
    @my_panel = Panel.new(self)
    @my_textbox = TextCtrl.new(@my_panel, -1, '0', :size => [320,30])
    @button_num1 = Button.new(@my_panel, -1, '1',Point.new(80,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(140,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(200,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(80,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(140,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(200,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(80,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(140,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(200,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(80,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(20,40), Size.new(50,50))
    evt_button(@button_clear.get_id()) {|event| clear_button_click(event)}
    @button_back = Button.new(@my_panel, -1, '<--',Point.new(80,40), Size.new(50,50))
    evt_button(@button_back.get_id()) {|event| back_button_click(event)}
    @button_squ = Button.new(@my_panel, -1, '^0.5',Point.new(140,40), Size.new(50,50))
    evt_button(@button_squ.get_id()) {|event| squ_button_click(event)}
    @button_div = Button.new(@my_panel, -1, '/',Point.new(200,40), Size.new(50,50))
    evt_button(@button_div.get_id()) {|event| div_button_click(event)}
    @button_mul = Button.new(@my_panel, -1, 'X',Point.new(260,40), Size.new(50,50))
    evt_button(@button_mul.get_id()) {|event| mul_button_click(event)}
    @button_subtract = Button.new(@my_panel, -1, '-',Point.new(260,100), Size.new(50,50))
    evt_button(@button_subtract.get_id()) {|event| subtract_button_click(event)}
    @button_add = Button.new(@my_panel, -1, '+',Point.new(260,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(260,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(200,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(20,100), Size.new(50,50))
    @button_MR = Button.new(@my_panel, -1, 'MR',Point.new(20,160), Size.new(50,50))
    @button_MS = Button.new(@my_panel, -1, 'MS',Point.new(20,220), Size.new(50,50))
    @button_Madd = Button.new(@my_panel, -1, 'M+',Point.new(20,280), Size.new(50,50))
   
    #���в���
    @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 num1_button_click(event)
    if $operator != nil
      $op_y << "1"    
      @my_textbox.set_value($op_y)
    else
      $op_x << "1"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num2_button_click(event)
    if $operator != nil
      $op_y << "2"
      @my_textbox.set_value($op_y)
    else
      $op_x << "2"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num3_button_click(event)
    if $operator != nil
      $op_y << "3"
      @my_textbox.set_value($op_y)
    else
      $op_x << "3"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num4_button_click(event)
    if $operator != nil
      $op_y << "4"
      @my_textbox.set_value($op_y)
    else
      $op_x << "4"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num5_button_click(event)
    if $operator != nil
      $op_y << "5"
      @my_textbox.set_value($op_y)
    else
      $op_x << "5"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num6_button_click(event)
    if $operator != nil
      $op_y << "6"
      @my_textbox.set_value($op_y)
    else
      $op_x << "6"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num7_button_click(event)
    if $operator != nil
      $op_y << "7"
      @my_textbox.set_value($op_y)
    else
      $op_x << "7"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num8_button_click(event)
    if $operator != nil
      $op_y << "8"
      @my_textbox.set_value($op_y)
    else
      $op_x << "8"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num9_button_click(event)
    if $operator != nil
      $op_y << "9"
      @my_textbox.set_value($op_y)
    else
      $op_x << "9"
      @my_textbox.set_value($op_x)
    end
  end
 
  def num0_button_click(event)
    if $operator != nil
      $op_y << "0"
      @my_textbox.set_value($op_y)
    else
      $op_x << "0"
      @my_textbox.set_value($op_x)
    end
  end
 
  def squ_button_click(event)
    if $operator != nil
      if ($op_y.to_f >= 0)
        $op_y = Math.sqrt($op_y.to_f).to_s
      else
        $op_y = (-Math.sqrt($op_y.to_f.abs)).to_s
      end
      @my_textbox.set_value($op_y)
    else
      if ($op_x.to_f >= 0)
        $op_x = Math.sqrt($op_x.to_f).to_s
      else
        $op_x = (-Math.sqrt($op_x.to_f.abs)).to_s
      end
      @my_textbox.set_value($op_x)
    end
  end
   
  def clear_button_click(event)
    $op_x = ""
    $op_y = ""
    $result = 0
    $operator = nil
    $point_flag = nil
    @my_textbox.set_value("0")
  end
 
  def back_button_click(event)
    if $operator != nil
      $op_y = $op_y.chop
      @my_textbox.set_value($op_y)
    else
      $op_x = $op_x.chop
      @my_textbox.set_value($op_x)
    end
  end
 
  def point_button_click(event)
    str = /\./
    if $operator != nil
      if ( ( $op_y =~ str ) == nil )
        $op_y << "."
        @my_textbox.set_value($op_y)
      end
    else
      if ( ($op_x =~ str) == nil )
        $op_x << "."
        @my_textbox.set_value($op_x)
      end
    end
  end
 
  def div_button_click(event)
    $operator = "/"
    $point_flag = nil
  end
 
  def mul_button_click(event)
    $operator = "X"
    $point_flag = nil
  end
 
  def subtract_button_click(event)
    $operator = "-"
    $point_flag = nil
  end
 
  def add_button_click(event)
    $operator = "+"
    $point_flag = nil
  end
 
  def result_button_click(event)
    case $operator
      when "+":
        $result = $op_x.to_f + $op_y.to_f
      when "-":
        $result = $op_x.to_f - $op_y.to_f
      when "X":
        $result = $op_x.to_f * $op_y.to_f
      when "/":
        $result = $op_x.to_f / $op_y.to_f
      else
        #�ϱ�������Ϣ����ݳ�ʼ��
        err_info = MessageDialog.new(nil,"error happen!", "error info")
        err_info.show_modal()
    end
   
    #��ʾ������
    @my_textbox.set_value("#{$result}")
   
    #�������������ֵ��op_x,op_y����
    $op_x = $result.to_s
    $op_y = ""
    $operator = nil
    $point_flag = nil
  end
 
end

class CacuApp < App
  def on_init
    CacuFrame.new
  end
 
end

CacuApp.new.main_loop()

caculator.rb ~ 8KB    点击这里下载

begin
  require 'rubygems'
rescue LoadError
end
require 'wx'
include Wx

$op_x = ""
$op_y = ""
$result = 0
$operator = nil
$point_flag = nil

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfyfd.html