index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React, { Component } from 'react';
  2. import Assets from '@src/components/Assets';
  3. import './index.less';
  4. export default class Calculator extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = { value: '0' };
  8. this.valueList = [];
  9. this.typeList = [];
  10. this.change = false;
  11. }
  12. clear() {
  13. this.valueList = [];
  14. this.typeList = [];
  15. this.setState({ value: '0' });
  16. }
  17. del() {
  18. const { value } = this.state;
  19. if (value.length > 1) {
  20. this.setState({ value: value.substr(0, value.length - 1) });
  21. } else if (value !== '0') {
  22. this.setState({ value: '0' });
  23. }
  24. }
  25. f(a, b, c) {
  26. switch (c) {
  27. case '+':
  28. return a + b;
  29. case '-':
  30. return a - b;
  31. case '*':
  32. return a * b;
  33. case '/':
  34. return a / b;
  35. default:
  36. return 0;
  37. }
  38. }
  39. compute() {
  40. let { value } = this.state;
  41. for (let i = 0; i < this.typeList.length; i += 1) {
  42. if (i === 0) {
  43. value = this.f(this.valueList[0], this.valueList[1], this.typeList[0]);
  44. } else if (this.valueList[i + 1] !== undefined) {
  45. value = this.f(value, this.valueList[i + 1], this.typeList[i]);
  46. }
  47. }
  48. return value;
  49. }
  50. input(text) {
  51. const { value } = this.state;
  52. switch (text) {
  53. case '=':
  54. if (this.change && this.typeList.length > 0) {
  55. this.valueList.push(value);
  56. this.setState({ value: this.compute() });
  57. this.change = false;
  58. }
  59. break;
  60. case '+':
  61. case '-':
  62. case '*':
  63. case '/':
  64. if (this.change) {
  65. this.valueList.push(value);
  66. this.typeList.push(text);
  67. this.change = false;
  68. if (this.valueList.length > 1) {
  69. console.log(this);
  70. this.setState({ value: this.compute() });
  71. }
  72. }
  73. break;
  74. case '.':
  75. if (this.change && value[value.length - 1] !== '.') {
  76. this.setState({ value: `${value}${text}` });
  77. } else if (!this.change) {
  78. this.change = true;
  79. }
  80. break;
  81. case 0:
  82. if (this.change && value !== '0') {
  83. this.setState({ value: `${value}${text}` });
  84. } else if (!this.change) {
  85. this.change = true;
  86. this.setState({ value: '0' });
  87. }
  88. break;
  89. default:
  90. if (this.change) {
  91. if (value !== '0') {
  92. this.setState({ value: `${value}${text}` });
  93. } else {
  94. this.setState({ value: text });
  95. }
  96. } else {
  97. this.setState({ value: text });
  98. this.change = true;
  99. }
  100. break;
  101. }
  102. }
  103. render() {
  104. const { show } = this.props;
  105. const { value } = this.state;
  106. return (
  107. <div hidden={!show} className="calculator">
  108. <div className="line">
  109. <div className="block block-1 value">{value}</div>
  110. </div>
  111. <div className="line">
  112. <div className="block block-2 left" onClick={() => this.del()}>
  113. <Assets name="eliminate_icon" />
  114. </div>
  115. <div className="block block-3 right" onClick={() => this.clear()}>
  116. Clear
  117. </div>
  118. </div>
  119. <div className="line">
  120. <div className="block block-1 left" onClick={() => this.input(7)}>
  121. 7
  122. </div>
  123. <div className="block block-1 center" onClick={() => this.input(8)}>
  124. 8
  125. </div>
  126. <div className="block block-1 center" onClick={() => this.input(9)}>
  127. 9
  128. </div>
  129. <div className="block block-2 right" onClick={() => this.input('/')}>
  130. /
  131. </div>
  132. </div>
  133. <div className="line">
  134. <div className="block block-1 left" onClick={() => this.input(4)}>
  135. 4
  136. </div>
  137. <div className="block block-1 center" onClick={() => this.input(5)}>
  138. 5
  139. </div>
  140. <div className="block block-1 center" onClick={() => this.input(6)}>
  141. 6
  142. </div>
  143. <div className="block block-2 right" onClick={() => this.input('*')}>
  144. *
  145. </div>
  146. </div>
  147. <div className="line">
  148. <div className="block block-1 left" onClick={() => this.input(1)}>
  149. 1
  150. </div>
  151. <div className="block block-1 center" onClick={() => this.input(2)}>
  152. 2
  153. </div>
  154. <div className="block block-1 center" onClick={() => this.input(3)}>
  155. 3
  156. </div>
  157. <div className="block block-2 right" onClick={() => this.input('-')}>
  158. -
  159. </div>
  160. </div>
  161. <div className="line">
  162. <div className="block block-1 left" onClick={() => this.input(0)}>
  163. 0
  164. </div>
  165. <div className="block block-1 center" onClick={() => this.input('.')}>
  166. .
  167. </div>
  168. <div className="block block-1 center" onClick={() => this.input('=')}>
  169. =
  170. </div>
  171. <div className="block block-2 right" onClick={() => this.input('+')}>
  172. +
  173. </div>
  174. </div>
  175. </div>
  176. );
  177. }
  178. }