#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","/","","","","*","","","","-","","","","+","+/-","",".","="]
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 == ""
$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 != ""
$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 (("".."") === key)? "Gainsboro" : "Gray"
command do
case key
when ("".."")
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

最新文章

  1. C和指针 第五章 逻辑位移与算术位移
  2. Android--Activity(活动)
  3. MVC视图请求流程视图
  4. VS 2013 Preview 自定义 SharePoint 2013 列表 之 两个Bug
  5. 在Ubuntu-14.04.3配置并成功编译Android6_r1源码
  6. Andoid java文件中的Log检查工具
  7. STM32F0xx_TIM输出PWM配置详细过程
  8. Javascript null和undefined
  9. 在phalcon框架下,php接口规范以及接口实例
  10. Keras如何构造简单的CNN网络
  11. RSA算法详解及C语言实现
  12. Git常用命令清单
  13. webform 简单的服务器控件。
  14. JAVA 23种开发模式详解(代码举例)
  15. CSS基础入门
  16. SSE图像算法优化系列二十五:二值图像的Euclidean distance map(EDM)特征图计算及其优化。
  17. Java知多少(49)throw:异常的抛出
  18. stl集合算法
  19. iOS使用NSURLConnection发送同步和异步HTTP Request
  20. Mac 上 java 究竟在哪里,本文彻底让你搞清楚!

热门文章

  1. Codeforces Round #591
  2. C++标准库分析总结(四)——<Vector、Array、Forward_list设计原则>
  3. (转)supervisor
  4. GO make&new区别
  5. pl/sql test Window 参数为date
  6. 一键分享QQ、微信、微博等
  7. PHP fopen/file_get_contents与curl性能比较
  8. Python问题:error: Microsoft Visual C++ 9.0 is required
  9. android DownloadManager: java.lang.IllegalArgumentException: Not a file URI: content://
  10. nginx指定允许的IP访问