index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //index.js
  2. var app = getApp()
  3. var util = require('../../utils/util.js');
  4. var calc = require('../../utils/calc.js');
  5. var core = require('../../utils/core.js');
  6. Page({
  7. data: {
  8. layout:[
  9. [{
  10. opt:"bindViewTapOpt",
  11. type:"warn",
  12. id:"of",
  13. value:"ON/OFF",
  14. },
  15. {
  16. opt:"bindViewTapOpt",
  17. type:"warn",
  18. id:"s",
  19. value:"高级",
  20. },
  21. {
  22. opt:"bindViewTapOpt",
  23. type:"warn",
  24. id:"c",
  25. value:"C",
  26. },
  27. {
  28. opt:"bindViewTapOpt",
  29. type:"warn",
  30. id:"ac",
  31. value:"AC",
  32. }],
  33. [{
  34. opt:"bindViewTapInput",
  35. type:"default",
  36. id:"7",
  37. value:"7",
  38. },
  39. {
  40. opt:"bindViewTapInput",
  41. type:"default",
  42. id:"8",
  43. value:"8",
  44. },
  45. {
  46. opt:"bindViewTapInput",
  47. type:"default",
  48. id:"9",
  49. value:"9",
  50. },
  51. {
  52. opt:"bindViewTapOpt",
  53. type:"primary",
  54. id:"÷",
  55. value:"÷",
  56. }],
  57. [{
  58. opt:"bindViewTapInput",
  59. type:"default",
  60. id:"4",
  61. value:"4",
  62. },
  63. {
  64. opt:"bindViewTapInput",
  65. type:"default",
  66. id:"5",
  67. value:"5",
  68. },
  69. {
  70. opt:"bindViewTapInput",
  71. type:"default",
  72. id:"6",
  73. value:"6",
  74. },
  75. {
  76. opt:"bindViewTapOpt",
  77. type:"primary",
  78. id:"×",
  79. value:"×",
  80. }],
  81. [{
  82. opt:"bindViewTapInput",
  83. type:"default",
  84. id:"1",
  85. value:"1",
  86. },
  87. {
  88. opt:"bindViewTapInput",
  89. type:"default",
  90. id:"2",
  91. value:"2",
  92. },
  93. {
  94. opt:"bindViewTapInput",
  95. type:"default",
  96. id:"3",
  97. value:"3",
  98. },
  99. {
  100. opt:"bindViewTapOpt",
  101. type:"primary",
  102. id:"-",
  103. value:"-",
  104. }],
  105. [{
  106. opt:"bindViewTapInput",
  107. type:"default",
  108. id:"0",
  109. value:"0",
  110. },
  111. {
  112. opt:"bindViewTapInput",
  113. type:"default",
  114. id:".",
  115. value:".",
  116. },
  117. {
  118. opt:"bindViewTapOpt",
  119. type:"warn",
  120. id:"=",
  121. value:"=",
  122. },
  123. {
  124. opt:"bindViewTapOpt",
  125. type:"primary",
  126. id:"+",
  127. value:"+",
  128. }]],
  129. lines: [
  130. "",
  131. "",
  132. "",
  133. "",
  134. 0
  135. ],
  136. power:1,
  137. line:"------------------------------------------------",
  138. infix:"0"
  139. },
  140. onLoad: function () {
  141. //core.showToast();
  142. //core.showModal();
  143. //core.showActionSheet();
  144. this.clear();
  145. wx.setNavigationBarTitle({
  146. title: '微信计算器'
  147. })
  148. },
  149. onReady: function() {
  150. wx.showNavigationBarLoading();
  151. wx.hideNavigationBarLoading();
  152. },
  153. onShow: function() {
  154. // Do something when page ready.
  155. },
  156. onHide: function() {
  157. // Do something when page hide.
  158. },
  159. onUnload: function() {
  160. // Do something when page close.
  161. },
  162. onPullDownRefresh: function() {
  163. // Do something when pull down.
  164. },
  165. onReachBottom: function() {
  166. // Do something when page reach bottom.
  167. },
  168. clear:function(){
  169. this.output([0]);
  170. },
  171. clearAll:function(){
  172. this.output(["","","","",0],true);
  173. this.setData({
  174. infix: "",
  175. });
  176. },
  177. calculator:function(){
  178. var infix = this.data.infix;
  179. var res = calc.calculate(infix);
  180. this.makeInfix("0",true);
  181. return res;
  182. },
  183. bindViewTapInput:function (e){
  184. var content = e.currentTarget.id;
  185. this.getNumber(content);
  186. },
  187. bindViewTapOpt:function (e){
  188. var content = e.currentTarget.id;
  189. //console.log(content);
  190. switch(content){
  191. case 'c':
  192. this.clear();
  193. break;
  194. case 's':
  195. wx.navigateTo({
  196. url: '/pages/super/index?id=1'
  197. })
  198. break;
  199. case 'of':
  200. this.setData({
  201. power:0-this.data.power
  202. });
  203. this.clearAll();
  204. break;
  205. case 'ac':
  206. this.clearAll();
  207. break;
  208. case '=':
  209. this.makeInfix();
  210. var res = this.calculator();
  211. this.output([this.data.line,res],true);
  212. break;
  213. default:
  214. this.makeInfix(content);
  215. this.output([content],true);
  216. break;
  217. }
  218. },
  219. makeInfix:function(content,flash){
  220. //生成中缀表达式
  221. content = content ? content : "";
  222. var oldInfix = this.data.infix.toString();
  223. var lastNum = this.data.lines[(this.data.lines.length-1)].toString();
  224. if(oldInfix == "0"){
  225. oldInfix = "";
  226. }
  227. if(!flash){
  228. content = oldInfix+lastNum+content;
  229. }
  230. console.log(content);
  231. this.setData({
  232. infix:content
  233. });
  234. },
  235. getNumber:function(content){
  236. var lines = this.data.lines;
  237. var move = false;
  238. var old = lines[(lines.length-1)].toString();
  239. var newNum;
  240. if(content=="." && old.indexOf(".")!=-1){
  241. //当原字符串有小数点时再输入小数点无反应
  242. return false;
  243. }
  244. if(content=="0" && old=="0"){
  245. //当原字符串等于0再输入0无反应
  246. return false;
  247. }
  248. if(isNaN(old) && old != "0."){
  249. //当原字符串是非数字即操作符(+-×÷)时新字符串另起一行为新数字
  250. //需要排除 0. 这个不是字符,但是需要继续拼接
  251. if(content=="."){
  252. content = "0.";
  253. }
  254. newNum = content;
  255. //另起一行的标志
  256. move = true;
  257. }else{
  258. if(old == "0" && content != "."){
  259. //当原字符串是0的时候又不是小数,需要把0去掉
  260. old = "";
  261. }
  262. newNum = old+content;
  263. }
  264. this.output([newNum],move);
  265. },
  266. output:function(content,move){
  267. var lines = this.data.lines;
  268. if(move){
  269. for(var index in content){
  270. lines.shift();
  271. lines.push(content[index]);
  272. }
  273. }else{
  274. lines[(lines.length-1)] = content[0];
  275. }
  276. if(this.data.power <= 0){
  277. lines = ["","","","",""];
  278. }
  279. this.setData({
  280. lines:lines
  281. });
  282. //console.log(this.data);
  283. }
  284. })