忘记密码   免费注册 查看新帖 | 论坛精华区

ChinaUnix.net

  平台 论坛 博客 认证专区 大话IT 视频 徽章 文库 沙龙 自测 下载 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
最近访问板块 发新帖
查看: 4170 | 回复: 0

ruby编写的简单计算器 [复制链接]

论坛徽章:
0
发表于 2015-07-08 14:46 |显示全部楼层
用wxruby编写的一个简单的图形界面计算器:
1.初步实现了+ - × / 平方根的功能
2.MC MR MS M+功能没有做
目前功能都比较简单,还有许多功能需要优化,供初学者交流
[Ruby]代码
  1. begin
  2.   require 'rubygems'
  3. rescue LoadError
  4. end
  5. require 'wx'
  6. include Wx

  7. $op_x = ""
  8. $op_y = ""
  9. $result = 0
  10. $operator = nil
  11. $point_flag = nil

  12. class CacuFrame < Frame
  13.   #��ʼ������
  14.    
  15.   def initialize()
  16.     super(nil, -1, 'Caculator', :size => [340,380], :style => MINIMIZE_BOX|SYSTEM_MENU|CAPTION|CLOSE_BOX|CLIP_CHILDREN)
  17.      
  18.     #�����������ؼ��������ť�¼�
  19.     @my_panel = Panel.new(self)
  20.     @my_textbox = TextCtrl.new(@my_panel, -1, '0', :size => [320,30])
  21.     @button_num1 = Button.new(@my_panel, -1, '1',Point.new(80,220), Size.new(50,50))
  22.     evt_button(@button_num1.get_id()) {|event| num1_button_click(event)}
  23.     @button_num2 = Button.new(@my_panel, -1, '2',Point.new(140,220), Size.new(50,50))
  24.     evt_button(@button_num2.get_id()) {|event| num2_button_click(event)}
  25.     @button_num3 = Button.new(@my_panel, -1, '3',Point.new(200,220), Size.new(50,50))
  26.     evt_button(@button_num3.get_id()) {|event| num3_button_click(event)}
  27.     @button_num4 = Button.new(@my_panel, -1, '4',Point.new(80,160), Size.new(50,50))
  28.     evt_button(@button_num4.get_id()) {|event| num4_button_click(event)}
  29.     @button_num5 = Button.new(@my_panel, -1, '5',Point.new(140,160), Size.new(50,50))
  30.     evt_button(@button_num5.get_id()) {|event| num5_button_click(event)}
  31.     @button_num6 = Button.new(@my_panel, -1, '6',Point.new(200,160), Size.new(50,50))
  32.     evt_button(@button_num6.get_id()) {|event| num6_button_click(event)}
  33.     @button_num7 = Button.new(@my_panel, -1, '7',Point.new(80,100), Size.new(50,50))
  34.     evt_button(@button_num7.get_id()) {|event| num7_button_click(event)}
  35.     @button_num8 = Button.new(@my_panel, -1, '8',Point.new(140,100), Size.new(50,50))
  36.     evt_button(@button_num8.get_id()) {|event| num8_button_click(event)}
  37.     @button_num9 = Button.new(@my_panel, -1, '9',Point.new(200,100), Size.new(50,50))
  38.     evt_button(@button_num9.get_id()) {|event| num9_button_click(event)}
  39.     @button_num0 = Button.new(@my_panel, -1, '0',Point.new(80,280), Size.new(110,50))
  40.     evt_button(@button_num0.get_id()) {|event| num0_button_click(event)}
  41.     @button_clear = Button.new(@my_panel, -1, 'C',Point.new(20,40), Size.new(50,50))
  42.     evt_button(@button_clear.get_id()) {|event| clear_button_click(event)}
  43.     @button_back = Button.new(@my_panel, -1, '<--',Point.new(80,40), Size.new(50,50))
  44.     evt_button(@button_back.get_id()) {|event| back_button_click(event)}
  45.     @button_squ = Button.new(@my_panel, -1, '^0.5',Point.new(140,40), Size.new(50,50))
  46.     evt_button(@button_squ.get_id()) {|event| squ_button_click(event)}
  47.     @button_div = Button.new(@my_panel, -1, '/',Point.new(200,40), Size.new(50,50))
  48.     evt_button(@button_div.get_id()) {|event| div_button_click(event)}
  49.     @button_mul = Button.new(@my_panel, -1, 'X',Point.new(260,40), Size.new(50,50))
  50.     evt_button(@button_mul.get_id()) {|event| mul_button_click(event)}
  51.     @button_subtract = Button.new(@my_panel, -1, '-',Point.new(260,100), Size.new(50,50))
  52.     evt_button(@button_subtract.get_id()) {|event| subtract_button_click(event)}
  53.     @button_add = Button.new(@my_panel, -1, '+',Point.new(260,160), Size.new(50,50))
  54.     evt_button(@button_add.get_id()) {|event| add_button_click(event)}
  55.     @button_result = Button.new(@my_panel, -1, '=',Point.new(260,220), Size.new(50,110))
  56.     evt_button(@button_result.get_id()) {|event| result_button_click(event)}
  57.     @button_point = Button.new(@my_panel, -1, '.',Point.new(200,280), Size.new(50,50))
  58.     evt_button(@button_point.get_id()) {|event| point_button_click(event)}
  59.     @button_MC = Button.new(@my_panel, -1, 'MC',Point.new(20,100), Size.new(50,50))
  60.     @button_MR = Button.new(@my_panel, -1, 'MR',Point.new(20,160), Size.new(50,50))
  61.     @button_MS = Button.new(@my_panel, -1, 'MS',Point.new(20,220), Size.new(50,50))
  62.     @button_Madd = Button.new(@my_panel, -1, 'M+',Point.new(20,280), Size.new(50,50))
  63.      
  64.     #���в���
  65.     @my_panel_sizer = BoxSizer.new(VERTICAL)
  66.     @my_panel.set_sizer(@my_panel_sizer)
  67.     @my_panel.sizer.add(@my_textbox, 0, GROW|ALL, 2)
  68.      
  69.     show()   
  70.      
  71.   end
  72.    
  73.   def num1_button_click(event)
  74.     if $operator != nil
  75.       $op_y << "1"   
  76.       @my_textbox.set_value($op_y)
  77.     else
  78.       $op_x << "1"
  79.       @my_textbox.set_value($op_x)
  80.     end
  81.   end
  82.    
  83.   def num2_button_click(event)
  84.     if $operator != nil
  85.       $op_y << "2"
  86.       @my_textbox.set_value($op_y)
  87.     else
  88.       $op_x << "2"
  89.       @my_textbox.set_value($op_x)
  90.     end
  91.   end
  92.    
  93.   def num3_button_click(event)
  94.     if $operator != nil
  95.       $op_y << "3"
  96.       @my_textbox.set_value($op_y)
  97.     else
  98.       $op_x << "3"
  99.       @my_textbox.set_value($op_x)
  100.     end
  101.   end
  102.    
  103.   def num4_button_click(event)
  104.     if $operator != nil
  105.       $op_y << "4"
  106.       @my_textbox.set_value($op_y)
  107.     else
  108.       $op_x << "4"
  109.       @my_textbox.set_value($op_x)
  110.     end
  111.   end
  112.    
  113.   def num5_button_click(event)
  114.     if $operator != nil
  115.       $op_y << "5"
  116.       @my_textbox.set_value($op_y)
  117.     else
  118.       $op_x << "5"
  119.       @my_textbox.set_value($op_x)
  120.     end
  121.   end
  122.    
  123.   def num6_button_click(event)
  124.     if $operator != nil
  125.       $op_y << "6"
  126.       @my_textbox.set_value($op_y)
  127.     else
  128.       $op_x << "6"
  129.       @my_textbox.set_value($op_x)
  130.     end
  131.   end
  132.    
  133.   def num7_button_click(event)
  134.     if $operator != nil
  135.       $op_y << "7"
  136.       @my_textbox.set_value($op_y)
  137.     else
  138.       $op_x << "7"
  139.       @my_textbox.set_value($op_x)
  140.     end
  141.   end
  142.    
  143.   def num8_button_click(event)
  144.     if $operator != nil
  145.       $op_y << "8"
  146.       @my_textbox.set_value($op_y)
  147.     else
  148.       $op_x << "8"
  149.       @my_textbox.set_value($op_x)
  150.     end
  151.   end
  152.    
  153.   def num9_button_click(event)
  154.     if $operator != nil
  155.       $op_y << "9"
  156.       @my_textbox.set_value($op_y)
  157.     else
  158.       $op_x << "9"
  159.       @my_textbox.set_value($op_x)
  160.     end
  161.   end
  162.    
  163.   def num0_button_click(event)
  164.     if $operator != nil
  165.       $op_y << "0"
  166.       @my_textbox.set_value($op_y)
  167.     else
  168.       $op_x << "0"
  169.       @my_textbox.set_value($op_x)
  170.     end
  171.   end
  172.    
  173.   def squ_button_click(event)
  174.     if $operator != nil
  175.       if ($op_y.to_f >= 0)
  176.         $op_y = Math.sqrt($op_y.to_f).to_s
  177.       else
  178.         $op_y = (-Math.sqrt($op_y.to_f.abs)).to_s
  179.       end
  180.       @my_textbox.set_value($op_y)
  181.     else
  182.       if ($op_x.to_f >= 0)
  183.         $op_x = Math.sqrt($op_x.to_f).to_s
  184.       else
  185.         $op_x = (-Math.sqrt($op_x.to_f.abs)).to_s
  186.       end
  187.       @my_textbox.set_value($op_x)
  188.     end
  189.   end
  190.      
  191.   def clear_button_click(event)
  192.     $op_x = ""
  193.     $op_y = ""
  194.     $result = 0
  195.     $operator = nil
  196.     $point_flag = nil
  197.     @my_textbox.set_value("0")
  198.   end
  199.    
  200.   def back_button_click(event)
  201.     if $operator != nil
  202.       $op_y = $op_y.chop
  203.       @my_textbox.set_value($op_y)
  204.     else
  205.       $op_x = $op_x.chop
  206.       @my_textbox.set_value($op_x)
  207.     end
  208.   end
  209.    
  210.   def point_button_click(event)
  211.     str = /\./
  212.     if $operator != nil
  213.       if ( ( $op_y =~ str ) == nil )
  214.         $op_y << "."
  215.         @my_textbox.set_value($op_y)
  216.       end
  217.     else
  218.       if ( ($op_x =~ str) == nil )
  219.         $op_x << "."
  220.         @my_textbox.set_value($op_x)
  221.       end
  222.     end
  223.   end
  224.    
  225.   def div_button_click(event)
  226.     $operator = "/"
  227.     $point_flag = nil
  228.   end
  229.    
  230.   def mul_button_click(event)
  231.     $operator = "X"
  232.     $point_flag = nil
  233.   end
  234.    
  235.   def subtract_button_click(event)
  236.     $operator = "-"
  237.     $point_flag = nil
  238.   end
  239.    
  240.   def add_button_click(event)
  241.     $operator = "+"
  242.     $point_flag = nil
  243.   end
  244.    
  245.   def result_button_click(event)
  246.     case $operator
  247.       when "+":
  248.         $result = $op_x.to_f + $op_y.to_f
  249.       when "-":
  250.         $result = $op_x.to_f - $op_y.to_f
  251.       when "X":
  252.         $result = $op_x.to_f * $op_y.to_f
  253.       when "/":
  254.         $result = $op_x.to_f / $op_y.to_f
  255.       else
  256.         #�ϱ�������Ϣ����ݳ�ʼ��
  257.         err_info = MessageDialog.new(nil,"error happen!", "error info")
  258.         err_info.show_modal()
  259.     end
  260.      
  261.     #��ʾ������
  262.     @my_textbox.set_value("#{$result}")
  263.      
  264.     #�������������ֵ��op_x,op_y����
  265.     $op_x = $result.to_s
  266.     $op_y = ""
  267.     $operator = nil
  268.     $point_flag = nil
  269.   end
  270.    
  271. end

  272. class CacuApp < App
  273.   def on_init
  274.     CacuFrame.new
  275.   end
  276.    
  277. end

  278. CacuApp.new.main_loop()
复制代码
caculator.rb
  1. begin
  2.   require 'rubygems'
  3. rescue LoadError
  4. end
  5. require 'wx'
  6. include Wx

  7. $op_x = ""
  8. $op_y = ""
  9. $result = 0
  10. $operator = nil
  11. $point_flag = nil

  12. class CacuFrame < Frame
  13.   #��ʼ������
  14.    
  15.   def initialize()
  16.     super(nil, -1, 'Caculator', :size => [340,380], :style => MINIMIZE_BOX|SYSTEM_MENU|CAPTION|CLOSE_BOX|CLIP_CHILDREN)
  17.      
  18.     #�����������ؼ��������ť�¼�
  19.     @my_panel = Panel.new(self)
  20.     @my_textbox = TextCtrl.new(@my_panel, -1, '0', :size => [320,30])
  21.     @button_num1 = Button.new(@my_panel, -1, '1',Point.new(80,220), Size.new(50,50))
  22.     evt_button(@button_num1.get_id()) {|event| num1_button_click(event)}
  23.     @button_num2 = Button.new(@my_panel, -1, '2',Point.new(140,220), Size.new(50,50))
  24.     evt_button(@button_num2.get_id()) {|event| num2_button_click(event)}
  25.     @button_num3 = Button.new(@my_panel, -1, '3',Point.new(200,220), Size.new(50,50))
  26.     evt_button(@button_num3.get_id()) {|event| num3_button_click(event)}
  27.     @button_num4 = Button.new(@my_panel, -1, '4',Point.new(80,160), Size.new(50,50))
  28.     evt_button(@button_num4.get_id()) {|event| num4_button_click(event)}
  29.     @button_num5 = Button.new(@my_panel, -1, '5',Point.new(140,160), Size.new(50,50))
  30.     evt_button(@button_num5.get_id()) {|event| num5_button_click(event)}
  31.     @button_num6 = Button.new(@my_panel, -1, '6',Point.new(200,160), Size.new(50,50))
  32.     evt_button(@button_num6.get_id()) {|event| num6_button_click(event)}
  33.     @button_num7 = Button.new(@my_panel, -1, '7',Point.new(80,100), Size.new(50,50))
  34.     evt_button(@button_num7.get_id()) {|event| num7_button_click(event)}
  35.     @button_num8 = Button.new(@my_panel, -1, '8',Point.new(140,100), Size.new(50,50))
  36.     evt_button(@button_num8.get_id()) {|event| num8_button_click(event)}
  37.     @button_num9 = Button.new(@my_panel, -1, '9',Point.new(200,100), Size.new(50,50))
  38.     evt_button(@button_num9.get_id()) {|event| num9_button_click(event)}
  39.     @button_num0 = Button.new(@my_panel, -1, '0',Point.new(80,280), Size.new(110,50))
  40.     evt_button(@button_num0.get_id()) {|event| num0_button_click(event)}
  41.     @button_clear = Button.new(@my_panel, -1, 'C',Point.new(20,40), Size.new(50,50))
  42.     evt_button(@button_clear.get_id()) {|event| clear_button_click(event)}
  43.     @button_back = Button.new(@my_panel, -1, '<--',Point.new(80,40), Size.new(50,50))
  44.     evt_button(@button_back.get_id()) {|event| back_button_click(event)}
  45.     @button_squ = Button.new(@my_panel, -1, '^0.5',Point.new(140,40), Size.new(50,50))
  46.     evt_button(@button_squ.get_id()) {|event| squ_button_click(event)}
  47.     @button_div = Button.new(@my_panel, -1, '/',Point.new(200,40), Size.new(50,50))
  48.     evt_button(@button_div.get_id()) {|event| div_button_click(event)}
  49.     @button_mul = Button.new(@my_panel, -1, 'X',Point.new(260,40), Size.new(50,50))
  50.     evt_button(@button_mul.get_id()) {|event| mul_button_click(event)}
  51.     @button_subtract = Button.new(@my_panel, -1, '-',Point.new(260,100), Size.new(50,50))
  52.     evt_button(@button_subtract.get_id()) {|event| subtract_button_click(event)}
  53.     @button_add = Button.new(@my_panel, -1, '+',Point.new(260,160), Size.new(50,50))
  54.     evt_button(@button_add.get_id()) {|event| add_button_click(event)}
  55.     @button_result = Button.new(@my_panel, -1, '=',Point.new(260,220), Size.new(50,110))
  56.     evt_button(@button_result.get_id()) {|event| result_button_click(event)}
  57.     @button_point = Button.new(@my_panel, -1, '.',Point.new(200,280), Size.new(50,50))
  58.     evt_button(@button_point.get_id()) {|event| point_button_click(event)}
  59.     @button_MC = Button.new(@my_panel, -1, 'MC',Point.new(20,100), Size.new(50,50))
  60.     @button_MR = Button.new(@my_panel, -1, 'MR',Point.new(20,160), Size.new(50,50))
  61.     @button_MS = Button.new(@my_panel, -1, 'MS',Point.new(20,220), Size.new(50,50))
  62.     @button_Madd = Button.new(@my_panel, -1, 'M+',Point.new(20,280), Size.new(50,50))
  63.      
  64.     #���в���
  65.     @my_panel_sizer = BoxSizer.new(VERTICAL)
  66.     @my_panel.set_sizer(@my_panel_sizer)
  67.     @my_panel.sizer.add(@my_textbox, 0, GROW|ALL, 2)
  68.      
  69.     show()   
  70.      
  71.   end
  72.    
  73.   def num1_button_click(event)
  74.     if $operator != nil
  75.       $op_y << "1"   
  76.       @my_textbox.set_value($op_y)
  77.     else
  78.       $op_x << "1"
  79.       @my_textbox.set_value($op_x)
  80.     end
  81.   end
  82.    
  83.   def num2_button_click(event)
  84.     if $operator != nil
  85.       $op_y << "2"
  86.       @my_textbox.set_value($op_y)
  87.     else
  88.       $op_x << "2"
  89.       @my_textbox.set_value($op_x)
  90.     end
  91.   end
  92.    
  93.   def num3_button_click(event)
  94.     if $operator != nil
  95.       $op_y << "3"
  96.       @my_textbox.set_value($op_y)
  97.     else
  98.       $op_x << "3"
  99.       @my_textbox.set_value($op_x)
  100.     end
  101.   end
  102.    
  103.   def num4_button_click(event)
  104.     if $operator != nil
  105.       $op_y << "4"
  106.       @my_textbox.set_value($op_y)
  107.     else
  108.       $op_x << "4"
  109.       @my_textbox.set_value($op_x)
  110.     end
  111.   end
  112.    
  113.   def num5_button_click(event)
  114.     if $operator != nil
  115.       $op_y << "5"
  116.       @my_textbox.set_value($op_y)
  117.     else
  118.       $op_x << "5"
  119.       @my_textbox.set_value($op_x)
  120.     end
  121.   end
  122.    
  123.   def num6_button_click(event)
  124.     if $operator != nil
  125.       $op_y << "6"
  126.       @my_textbox.set_value($op_y)
  127.     else
  128.       $op_x << "6"
  129.       @my_textbox.set_value($op_x)
  130.     end
  131.   end
  132.    
  133.   def num7_button_click(event)
  134.     if $operator != nil
  135.       $op_y << "7"
  136.       @my_textbox.set_value($op_y)
  137.     else
  138.       $op_x << "7"
  139.       @my_textbox.set_value($op_x)
  140.     end
  141.   end
  142.    
  143.   def num8_button_click(event)
  144.     if $operator != nil
  145.       $op_y << "8"
  146.       @my_textbox.set_value($op_y)
  147.     else
  148.       $op_x << "8"
  149.       @my_textbox.set_value($op_x)
  150.     end
  151.   end
  152.    
  153.   def num9_button_click(event)
  154.     if $operator != nil
  155.       $op_y << "9"
  156.       @my_textbox.set_value($op_y)
  157.     else
  158.       $op_x << "9"
  159.       @my_textbox.set_value($op_x)
  160.     end
  161.   end
  162.    
  163.   def num0_button_click(event)
  164.     if $operator != nil
  165.       $op_y << "0"
  166.       @my_textbox.set_value($op_y)
  167.     else
  168.       $op_x << "0"
  169.       @my_textbox.set_value($op_x)
  170.     end
  171.   end
  172.    
  173.   def squ_button_click(event)
  174.     if $operator != nil
  175.       if ($op_y.to_f >= 0)
  176.         $op_y = Math.sqrt($op_y.to_f).to_s
  177.       else
  178.         $op_y = (-Math.sqrt($op_y.to_f.abs)).to_s
  179.       end
  180.       @my_textbox.set_value($op_y)
  181.     else
  182.       if ($op_x.to_f >= 0)
  183.         $op_x = Math.sqrt($op_x.to_f).to_s
  184.       else
  185.         $op_x = (-Math.sqrt($op_x.to_f.abs)).to_s
  186.       end
  187.       @my_textbox.set_value($op_x)
  188.     end
  189.   end
  190.      
  191.   def clear_button_click(event)
  192.     $op_x = ""
  193.     $op_y = ""
  194.     $result = 0
  195.     $operator = nil
  196.     $point_flag = nil
  197.     @my_textbox.set_value("0")
  198.   end
  199.    
  200.   def back_button_click(event)
  201.     if $operator != nil
  202.       $op_y = $op_y.chop
  203.       @my_textbox.set_value($op_y)
  204.     else
  205.       $op_x = $op_x.chop
  206.       @my_textbox.set_value($op_x)
  207.     end
  208.   end
  209.    
  210.   def point_button_click(event)
  211.     str = /\./
  212.     if $operator != nil
  213.       if ( ( $op_y =~ str ) == nil )
  214.         $op_y << "."
  215.         @my_textbox.set_value($op_y)
  216.       end
  217.     else
  218.       if ( ($op_x =~ str) == nil )
  219.         $op_x << "."
  220.         @my_textbox.set_value($op_x)
  221.       end
  222.     end
  223.   end
  224.    
  225.   def div_button_click(event)
  226.     $operator = "/"
  227.     $point_flag = nil
  228.   end
  229.    
  230.   def mul_button_click(event)
  231.     $operator = "X"
  232.     $point_flag = nil
  233.   end
  234.    
  235.   def subtract_button_click(event)
  236.     $operator = "-"
  237.     $point_flag = nil
  238.   end
  239.    
  240.   def add_button_click(event)
  241.     $operator = "+"
  242.     $point_flag = nil
  243.   end
  244.    
  245.   def result_button_click(event)
  246.     case $operator
  247.       when "+":
  248.         $result = $op_x.to_f + $op_y.to_f
  249.       when "-":
  250.         $result = $op_x.to_f - $op_y.to_f
  251.       when "X":
  252.         $result = $op_x.to_f * $op_y.to_f
  253.       when "/":
  254.         $result = $op_x.to_f / $op_y.to_f
  255.       else
  256.         #�ϱ�������Ϣ����ݳ�ʼ��
  257.         err_info = MessageDialog.new(nil,"error happen!", "error info")
  258.         err_info.show_modal()
  259.     end
  260.      
  261.     #��ʾ������
  262.     @my_textbox.set_value("#{$result}")
  263.      
  264.     #�������������ֵ��op_x,op_y����
  265.     $op_x = $result.to_s
  266.     $op_y = ""
  267.     $operator = nil
  268.     $point_flag = nil
  269.   end
  270.    
  271. end

  272. class CacuApp < App
  273.   def on_init
  274.     CacuFrame.new
  275.   end
  276.    
  277. end

  278. CacuApp.new.main_loop()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

  

北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:1101082001
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP