使用Qt和Interpreter设计模式开发计算器(附源码)


计算器软件其实有很多种,但是基本上都是模仿计算器,用鼠标点击按键来操作,这次我们反其道而行之,采用类似文本输入的操作方式。

功能

1.键盘输入算式,回车后计算结果。

2.根据当前输入的函数的一部分,自动找到备选函数。这时可以用上/下键选择需要的函数后,按空格键确定输入。在整个过程中一直可以表示函数的帮助信息。我们可以参考帮助信息,选择合适的函数。

3.支持三角函数,反三角函数,求和,平均值,乘方,开方,对数,当然还有包含嵌套的四则运算。

相关资源下载(包括可执行文件,可直接在WindowsXP,Windows7环境下执行及源代码,工程文件)下载地址:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /pub/2011/10/30/使用Qt和Interpreter设计模式开发计算器源码/

执行画面如下 

技术要点:

除了操作界面以外,实际我们是要做这样一个解析器,就面临这一个如何描述我们所面的需求的问题。在这里我们使用EBNF的一种形式,它由W3C定义。我们可以在XML Path Language (XPath) 2.0 (Second Edition)中找到它的细节。

一下是我们在计算器中输入的表达式的描述。

  1. [1]Expr::= AdditiveExpr   
  2.    
  3.   
  4. [2] AdditiveExpr::=MultiplicativeExpr ( ("+" | "-") MultiplicativeExpr )*  
  5.   
  6.    
  7.   
  8. [3]MultiplicativeExpr::= UnaryExpr ( ("*" | "/" | "%" ) UnaryExpr)*  
  9.   
  10.    
  11.   
  12. [4]UnaryExpr::=("-" | "+")* PrimaryExpr  
  13.   
  14.    
  15.   
  16. [5]PrimaryExpr::= NumericLiteral | ParenthesizedExpr | FunctionCall  
  17.   
  18.    
  19.   
  20. [6]NumericLiteral::=IntegerLiteral | DecimalLiteral | DoubleLiteral  
  21.   
  22.    
  23.   
  24. [7]ParenthesizedExpr::="(" Expr? ")"  
  25.   
  26.    
  27.   
  28. [8]FunctionCall::=FunctionName "(" (Expr(","Expr)*)? ")"  
  29.   
  30.    
  31.   
  32. [9]IntegerLiteral ::=Digits  
  33.   
  34.    
  35.   
  36. [10]DecimalLiteral ::=("." Digits) | (Digits "." [0-9]*)  
  37.   
  38.    
  39.   
  40. [11]DoubleLiteral::=(("." Digits) | (Digits ("." [0-9]*)?)) [eE] [+-]? Digits  
  41.   
  42.    
  43.   
  44. [12]Digits ::=[0-9]+  
  45.   
  46.    
  47.   
  48. [13] FunctionName=sinr  
  49.   
  50.                 |sind  
  51.   
  52.                 |cosr  
  53.   
  54.                 |sind  
  55.   
  56.                 |tanr  
  57.   
  58.                 |tand  
  59.   
  60.                 |asinr  
  61.   
  62.                 |asind  
  63.   
  64.                 |acosr  
  65.   
  66.                 |acosd  
  67.   
  68.                 |atanr  
  69.   
  70.                 |atand  
  71.   
  72.                 |power  
  73.   
  74.                 |root  
  • 1
  • 2
  • 3
  • 下一页

相关内容