@Controller
public class CaculatorController {
@Autowired
private CaculatorService caculatorService;
@RequestMapping(value="welcome")
public String welcome() {
return "Caculator";
}
@RequestMapping(value="caculatorResult",method = RequestMethod.POST)
@ResponseBody
public BigDecimal caculatorResult(@RequestParam("inputStr")String inputStr) {
CaculatorDto caculatorDto = new CaculatorDto();
caculatorDto.setInputStr(inputStr);
caculatorDto = caculatorService.getResult(caculatorDto);
System.out.println(caculatorDto.getResult());
return caculatorDto.getResult();
}
}
==========================================================
@Service
public class CaculatorServiceImpl implements CaculatorService{
@Override
public CaculatorDto getResult(CaculatorDto caculatorDto) {
ArrayList postFixArray = infixToPostFix(getToken(caculatorDto.getInputStr()));
BigDecimal result = getCaculator(postFixArray);
caculatorDto.setResult(result);
return caculatorDto;
}
@Override
public ArrayList getToken(String beforeToken) {
int beginIndex=0,endIndex = 0;
ArrayList tokenArray = new ArrayList();
HashMap<String,Integer> operMap = new HashMap<>();
operMap.put("*", 3);
operMap.put("/", 3);
operMap.put("^", 3);
operMap.put("%", 3);
operMap.put("^", 3);
operMap.put("+", 2);
operMap.put("-", 2);
operMap.put("(", 1);
operMap.put(")", 0);
//10+2/3
for(int i=0;i<beforeToken.length();i++) {
if(operMap.containsKey(String.valueOf(beforeToken.charAt(i)))) {
endIndex = i;
if(!beforeToken.substring(beginIndex, endIndex).isEmpty()) {
tokenArray.add(beforeToken.substring(beginIndex, endIndex));
}
tokenArray.add(String.valueOf(beforeToken.charAt(i)));
beginIndex = endIndex +1;
}else {
if(i+1 == beforeToken.length()) {
tokenArray.add(beforeToken.substring(beginIndex, i+1));
}
}
}
return tokenArray;
}
@Override
public ArrayList infixToPostFix(ArrayList infixArray) {
Stack operStack = new Stack();
ArrayList postFixArray = new ArrayList();
HashMap<String,Integer> operMap = new HashMap<>();
operMap.put("*", 3);
operMap.put("/", 3);
operMap.put("^", 3);
operMap.put("%", 3);
operMap.put("^", 3);
operMap.put("+", 2);
operMap.put("-", 2);
operMap.put("(", 1);
operMap.put(")", 0);
for(Object o : infixArray) {
if(o.equals("(")) {
operStack.push(o);
}else if(o.equals(")")) {
while(!operStack.peek().equals("(")) {
Object temp = operStack.pop();
if(!temp.equals("(")) {
postFixArray.add(temp);
}
}
operStack.pop();
}else if(operMap.containsKey(o)) {
if(operStack.isEmpty()) {
operStack.push(o);
}else {
if(operMap.get(operStack.peek())>=operMap.get(o)) {
postFixArray.add(operStack.pop());
operStack.push(o);
}else {
operStack.push(o);
}
}
}else {
BigDecimal bigDecimal = new BigDecimal(o.toString());
postFixArray.add(bigDecimal);
}
}
while(!operStack.isEmpty()) {
postFixArray.add(operStack.pop());
}
return postFixArray;
}
@Override
public BigDecimal getCaculator(ArrayList postFixArray) {
Stack valueStack = new Stack();
for(Object o : postFixArray) {
if(o instanceof BigDecimal) {
valueStack.push((BigDecimal) o);
}else if(o.equals("+")) {
BigDecimal num1 = valueStack.pop();
BigDecimal num2 = valueStack.pop();
valueStack.push(num2.add(num1));
}else if(o.equals("-")) {
BigDecimal num1 = valueStack.pop();
BigDecimal num2 = valueStack.pop();
valueStack.push(num2.subtract(num1));
}else if(o.equals("/")) {
BigDecimal num1 = valueStack.pop();
BigDecimal num2 = valueStack.pop();
valueStack.push(num2.divide(num1));
}else if(o.equals("%")) {
BigDecimal num1 = valueStack.pop();
BigDecimal num2 = valueStack.pop();
valueStack.push(num2.remainder(num1));
}else if(o.equals("*")) {
BigDecimal num1 = valueStack.pop();
BigDecimal num2 = valueStack.pop();
valueStack.push(num2.multiply(num1));
}else if(o.equals("^")) {
BigDecimal num1 = valueStack.pop();
BigDecimal num2 = valueStack.pop();
valueStack.push(num2.pow(num1.intValue()));
}
}
return valueStack.pop();
}
}
'SH.Basic > 자료구조' 카테고리의 다른 글
자료구조 - 스택(Stack) (0) | 2018.07.02 |
---|