You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
4.6 KiB
170 lines
4.6 KiB
import ACTIONS from "./action"; |
|
|
|
|
|
const evaluate = state => { |
|
let { currentValue, lastValue, operator } = state; |
|
currentValue = parseFloat(currentValue); |
|
lastValue = parseFloat(lastValue); |
|
let ans = ""; |
|
switch (operator) { |
|
case "+": { |
|
ans = lastValue + currentValue; |
|
break; |
|
} |
|
|
|
case "-": { |
|
ans = lastValue - currentValue; |
|
break; |
|
} |
|
|
|
case "×": { |
|
ans = lastValue * currentValue; |
|
break; |
|
} |
|
|
|
case "÷": { |
|
ans = lastValue / currentValue; |
|
break; |
|
} |
|
default: |
|
break; |
|
} |
|
return ans.toString(); |
|
} |
|
|
|
const reducer = ( |
|
state = { |
|
lastValue: "", |
|
currentValue: "", |
|
operator: "", |
|
overWrite: false, |
|
}, action) => { |
|
switch (action.type) { |
|
case ACTIONS.ADD_DIGIT: { |
|
if (state.overWrite) { |
|
state.currentValue = ""; |
|
state.overWrite = false; |
|
} |
|
|
|
if (state.currentValue === '0' && action.digit === '0') |
|
return state; |
|
if (state.currentValue === '0' && action.digit !== '.') |
|
return { |
|
...state, |
|
currentValue: action.digit, |
|
} |
|
if (action.digit === '.' && state.currentValue.includes('.')) { |
|
return state; |
|
} |
|
if (action.digit === '.' && state.currentValue === "") { |
|
return { |
|
...state, |
|
currentValue: "0.", |
|
} |
|
} |
|
return { |
|
...state, |
|
currentValue: state.currentValue + action.digit, |
|
} |
|
} |
|
|
|
case ACTIONS.DELETE_DIGIT: |
|
{ |
|
if (state.overWrite) { |
|
return { |
|
...state, |
|
currentValue: "", |
|
overWrite: false, |
|
} |
|
} |
|
if (state.currentValue === '') { |
|
return state; |
|
} |
|
|
|
return { |
|
...state, |
|
currentValue: state.currentValue.slice(0, -1), // 删除最后一个元素 |
|
} |
|
} |
|
|
|
case ACTIONS.CHOOSE_OPERATION: |
|
{ |
|
if (state.currentValue === "" && state.lastValue === "") { |
|
return state; |
|
} |
|
if (state.lastValue === "") { |
|
return { |
|
...state, |
|
lastValue: state.currentValue, |
|
operator: action.operator, |
|
currentValue: "" |
|
} |
|
} |
|
|
|
if (state.currentValue === "") { |
|
return { |
|
...state, |
|
operator: action.operator, |
|
} |
|
} |
|
let ans = evaluate(state); |
|
if (ans === "Infinity") { |
|
return { |
|
...state, |
|
lastValue: "", |
|
operator: "", |
|
currentValue: "除数不能为0", |
|
overWrite: true |
|
} |
|
} |
|
|
|
return { |
|
...state, |
|
lastValue: ans, |
|
operator: action.operator, |
|
currentValue: "", |
|
} |
|
} |
|
|
|
case ACTIONS.EVALUATE: |
|
{ |
|
if (state.currentValue === "" |
|
|| state.lastValue === "" |
|
|| state.operator === "") |
|
return state; |
|
|
|
let ans = evaluate(state); |
|
if (ans === "Infinity") { |
|
return { |
|
...state, |
|
lastValue: "", |
|
operator: "", |
|
currentValue: "除数不能为0", |
|
overWrite: true |
|
} |
|
} |
|
|
|
return { |
|
...state, |
|
currentValue: ans, |
|
operator: "", |
|
lastValue: "", |
|
overWrite: true, |
|
} |
|
} |
|
|
|
case ACTIONS.CLEAR: |
|
{ |
|
return { |
|
currentValue: "", |
|
lastValue: "", |
|
operator: "", |
|
} |
|
} |
|
default: |
|
return state; |
|
} |
|
|
|
} |
|
|
|
export default reducer; |