歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用Qt和Interpreter設計模式開發計算器(附源碼)

使用Qt和Interpreter設計模式開發計算器(附源碼)

日期:2017/3/1 11:09:05   编辑:Linux編程

計算器軟件其實有很多種,但是基本上都是模仿計算器,用鼠標點擊按鍵來操作,這次我們反其道而行之,采用類似文本輸入的操作方式。

功能

1.鍵盤輸入算式,回車後計算結果。

2.根據當前輸入的函數的一部分,自動找到備選函數。這時可以用上/下鍵選擇需要的函數後,按空格鍵確定輸入。在整個過程中一直可以表示函數的幫助信息。我們可以參考幫助信息,選擇合適的函數。

3.支持三角函數,反三角函數,求和,平均值,乘方,開方,對數,當然還有包含嵌套的四則運算。

相關資源下載(包括可執行文件,可直接在WindowsXP,Windows7環境下執行及源代碼,工程文件)下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/2011/10/30/使用Qt和Interpreter設計模式開發計算器源碼/

執行畫面如下

技術要點:

除了操作界面以外,實際我們是要做這樣一個解析器,就面臨這一個如何描述我們所面的需求的問題。在這裡我們使用EBNF的一種形式,它由W3C定義。我們可以在XML Path Language (XPath) 2.0 (Second Edition)中找到它的細節。

一下是我們在計算器中輸入的表達式的描述。

  1. [1]Expr::= AdditiveExpr
  2. [2] AdditiveExpr::=MultiplicativeExpr ( ("+" | "-") MultiplicativeExpr )*
  3. [3]MultiplicativeExpr::= UnaryExpr ( ("*" | "/" | "%" ) UnaryExpr)*
  4. [4]UnaryExpr::=("-" | "+")* PrimaryExpr
  5. [5]PrimaryExpr::= NumericLiteral | ParenthesizedExpr | FunctionCall
  6. [6]NumericLiteral::=IntegerLiteral | DecimalLiteral | DoubleLiteral
  7. [7]ParenthesizedExpr::="(" Expr? ")"
  8. [8]FunctionCall::=FunctionName "(" (Expr(","Expr)*)? ")"
  9. [9]IntegerLiteral ::=Digits
  10. [10]DecimalLiteral ::=("." Digits) | (Digits "." [0-9]*)
  11. [11]DoubleLiteral::=(("." Digits) | (Digits ("." [0-9]*)?)) [eE] [+-]? Digits
  12. [12]Digits ::=[0-9]+
  13. [13] FunctionName=sinr
  14. |sind
  15. |cosr
  16. |sind
  17. |tanr
  18. |tand
  19. |asinr
  20. |asind
  21. |acosr
  22. |acosd
  23. |atanr
  24. |atand
  25. |power
  26. |root
Copyright © Linux教程網 All Rights Reserved