index.js 6.3 KB

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