index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. this.setState({ value: this.compute() });
  70. }
  71. }
  72. break;
  73. case '.':
  74. if (this.change && value[value.length - 1] !== '.') {
  75. this.setState({ value: `${value}${text}` });
  76. } else if (!this.change) {
  77. this.change = true;
  78. }
  79. break;
  80. case 0:
  81. if (this.change && value !== '0') {
  82. this.setState({ value: `${value}${text}` });
  83. } else if (!this.change) {
  84. this.change = true;
  85. this.setState({ value: '0' });
  86. }
  87. break;
  88. default:
  89. if (this.change) {
  90. if (value !== '0') {
  91. this.setState({ value: `${value}${text}` });
  92. } else {
  93. this.setState({ value: text });
  94. }
  95. } else {
  96. this.setState({ value: text });
  97. this.change = true;
  98. }
  99. break;
  100. }
  101. }
  102. render() {
  103. const { show } = this.props;
  104. const { value } = this.state;
  105. return (
  106. <div hidden={!show} className="calculator">
  107. <div className="line">
  108. <div className="block block-1 value">{value}</div>
  109. </div>
  110. <div className="line">
  111. <div className="block block-2 left" onClick={() => this.del()}>
  112. <Assets name="eliminate_icon" />
  113. </div>
  114. <div className="block block-3 right" onClick={() => this.clear()}>
  115. Clear
  116. </div>
  117. </div>
  118. <div className="line">
  119. <div className="block block-1 left" onClick={() => this.input(7)}>
  120. 7
  121. </div>
  122. <div className="block block-1 center" onClick={() => this.input(8)}>
  123. 8
  124. </div>
  125. <div className="block block-1 center" onClick={() => this.input(9)}>
  126. 9
  127. </div>
  128. <div className="block block-2 right" onClick={() => this.input('/')}>
  129. /
  130. </div>
  131. </div>
  132. <div className="line">
  133. <div className="block block-1 left" onClick={() => this.input(4)}>
  134. 4
  135. </div>
  136. <div className="block block-1 center" onClick={() => this.input(5)}>
  137. 5
  138. </div>
  139. <div className="block block-1 center" onClick={() => this.input(6)}>
  140. 6
  141. </div>
  142. <div className="block block-2 right" onClick={() => this.input('*')}>
  143. *
  144. </div>
  145. </div>
  146. <div className="line">
  147. <div className="block block-1 left" onClick={() => this.input(1)}>
  148. 1
  149. </div>
  150. <div className="block block-1 center" onClick={() => this.input(2)}>
  151. 2
  152. </div>
  153. <div className="block block-1 center" onClick={() => this.input(3)}>
  154. 3
  155. </div>
  156. <div className="block block-2 right" onClick={() => this.input('-')}>
  157. -
  158. </div>
  159. </div>
  160. <div className="line">
  161. <div className="block block-1 left" onClick={() => this.input(0)}>
  162. 0
  163. </div>
  164. <div className="block block-1 center" onClick={() => this.input('.')}>
  165. .
  166. </div>
  167. <div className="block block-1 center" onClick={() => this.input('=')}>
  168. =
  169. </div>
  170. <div className="block block-2 right" onClick={() => this.input('+')}>
  171. +
  172. </div>
  173. </div>
  174. </div>
  175. );
  176. }
  177. }