본문 바로가기

개인/개인공부

2024-03-05-한줄연산 계산기

package main.java.variable;

 

import java.util.ArrayList;

import java.util.Scanner;

import java.util.regex.Pattern;

 

 

public class Calculator {

public static void SimpleCalculator()

{

Scanner scner=new Scanner(System.in); //사용자에게 입력받을 때 쓰일 스캐너

System.out.println("======입력창======\n");

System.out.println("입력<-");

String inputdata=scner.next();

 

//숫자와 기호 외에 문자가 들어간경우

if(Pattern.matches("^[a-zA-Z]*$", inputdata)||

Pattern.matches("^[$@$^()|\\~#!?&]*$",inputdata))

{

System.out.println("======결과창======\n");

System.out.println("입력한 값 :"+inputdata);

System.out.println("정상적인 연산 수식이 아닙니다!");

 

}

else

{

System.out.println("======결과창======\n");

System.out.print("="+Calculation(inputdata));

//System.out.println(numbers);

//System.out.println(operators);

}

}

 

private static Double Calculation(String _all)

{

boolean firstcutting=false;

boolean lastcutting=false;

 

ArrayList<String> numbers= new ArrayList<>();

//inputdata에서 연산 기호를 제외한 숫자 배열 만들기

if(_all.contains("+")||_all.contains("-")||_all.contains("*")

||_all.contains("/")||_all.contains("%"))

{

 

 

//split은 |로 추가 조건 가능. split 안의 연산 기호 앞에는 \\를 붙여야 사용가능

String[] cutting=_all.split("\\+|\\-|\\*|\\/|\\%");

 

for(int i=0;i<cutting.length;i++)

{

numbers.add(cutting[i]);

}

 

//inputdata에서 숫자를 제외한 연산 기호 배열 만들기

//1. inputdata에서 0~9까지의 숫자는 모두 " "빈칸으로 대체 2. 빈칸을 기준으로 자르기

String[] cutting2 = (_all.replaceAll("[0-9]", " ")).split(" ");

ArrayList<String> operators= new ArrayList<>();

for(int j=0;j<cutting2.length;j++)

{

 

//두자리수 이상의 수가 있는 경우 빈칸이 여러개 나와 빈칸이 저장될 수 있기 때문에 저장된 빈칸은 제외

if(!cutting2[j].equals(""))

{

operators.add(cutting2[j]);

}

}

 

//곱하기, 나누기, 나머지를 미리 연산

while(!firstcutting)

{

//연산기호 배열에서 우선순위로 계산

if(operators.contains("*")||operators.contains("/")||operators.contains("%"))

{

// *가 가장 우선 계산 될 때

OrderOfOperators(operators,numbers,"*","/","%");

// /가 가장 우선 계산 될 때

OrderOfOperators(operators,numbers,"/","*","%");

// %가 가장 우선 계산 될 때

OrderOfOperators(operators,numbers,"%","/","*");

}

else

{

//더 이상 곱하기, 나누기, 나머지 연산식이 없을때 반복 종료

firstcutting=true;

}

}

 

//더하기, 빼기 연산

while(!lastcutting)

{

if(operators.contains("+")||operators.contains("-"))

{

//마지막 기호 %는 어차피 제외되는 조건문에서 걸러지기 때문에 함수 재활용

// +가 가장 우선 계산 될 때

OrderOfOperators(operators,numbers,"+","-","%");

// -가 가장 우선 계산 될 때

OrderOfOperators(operators,numbers,"-","+","%");

}

else

{

//더 이상 더하기, 빼기 연산식이 없을때 반복 종료

lastcutting=true;

}

}

}

else

{

return Double.parseDouble(_all);

}

 

return Double.parseDouble(numbers.get(0));

}

 

 

//기호배열, 숫자배열, 비교기호1,비교기호2,비교기호3

private static void OrderOfOperators(ArrayList<String> _o,ArrayList<String> _n,String _a,String _b,String _c)

{

double frontnum=0; //기호 앞의 수

double behindnum=0; //기호 뒤의 수

int order=_o.indexOf(_a); //기호 배열에서의 기호 순서

 

//System.out.println(Double.parseDouble(_n.get(order)));

 

//기호의 indexof()값이 작을수록 앞에 있는 것)

if(_o.contains(_a))

{

if(_o.contains(_b))

{

if(_o.contains(_c))

{

if(_o.indexOf(_a)<_o.indexOf(_b)

&&_o.indexOf(_a)<_o.indexOf(_c))

{

//기호 앞 번호는 기호의 순서와 같음

frontnum=Double.parseDouble(_n.get(order));

//기호 뒤 번호는 기호의 순서+1과 같음

behindnum=Double.parseDouble(_n.get(order+1));

 

//확인용 출력문

//System.out.println(frontnum+_o.get(order)+behindnum+"="+microCalculator(frontnum,behindnum,_a));

 

//사용한 기호는 배열에서 삭제

_o.remove(order);

//사용한 숫자의 첫번째 값을 연산한 값으로 변경

_n.set(order,Double.toString(microCalculator(frontnum,behindnum,_a)));

//두번째 값은 삭제

_n.remove(order+1);

 

 

}

}

else

{

if(_o.indexOf(_a)<_o.indexOf(_b))

{

//기호 앞 번호는 기호의 순서와 같음

frontnum=Double.parseDouble(_n.get(order));

//기호 뒤 번호는 기호의 순서+1과 같음

behindnum=Double.parseDouble(_n.get(order+1));

 

//확인용 출력문

//System.out.println(frontnum+_o.get(order)+behindnum+"="+microCalculator(frontnum,behindnum,_a));

 

//사용한 기호는 배열에서 삭제

_o.remove(order);

//사용한 숫자의 첫번째 값을 연산한 값으로 변경

_n.set(order,Double.toString(microCalculator(frontnum,behindnum,_a)));

//두번째 값은 삭제

_n.remove(order+1);

}

}

}

else if(_o.contains(_c))

{

if(_o.indexOf(_a)<_o.indexOf(_c))

{

//기호 앞 번호는 기호의 순서와 같음

frontnum=Double.parseDouble(_n.get(order));

//기호 뒤 번호는 기호의 순서+1과 같음

behindnum=Double.parseDouble(_n.get(order+1));

 

//확인용 출력문

//System.out.println(frontnum+_o.get(order)+behindnum+"="+microCalculator(frontnum,behindnum,_a));

 

//사용한 기호는 배열에서 삭제

_o.remove(order);

//사용한 숫자의 첫번째 값을 연산한 값으로 변경

_n.set(order,Double.toString(microCalculator(frontnum,behindnum,_a)));

//두번째 값은 삭제

_n.remove(order+1);

}

}

else //해당 기호만 있는 경우

{

//기호 앞 번호는 기호의 순서와 같음

frontnum=Double.parseDouble(_n.get(order));

//기호 뒤 번호는 기호의 순서+1과 같음

behindnum=Double.parseDouble(_n.get(order+1));

 

//확인용 출력문

//System.out.println(frontnum+_o.get(order)+behindnum+"="+microCalculator(frontnum,behindnum,_a));

 

//사용한 기호는 배열에서 삭제

_o.remove(order);

//사용한 숫자의 첫번째 값을 연산한 값으로 변경

_n.set(order,Double.toString(microCalculator(frontnum,behindnum,_a)));

//두번째 값은 삭제

_n.remove(order+1);

}

}

}

 

//두 숫자와 기호의 연산

private static double microCalculator(double _n1, double _n2,String _oper)

{

if(_oper == "+")

{

return _n1 + _n2;

}

else if(_oper =="-")

{

return _n1 - _n2;

}

else if(_oper == "*")

{

return _n1 * _n2;

}

else if(_oper == "/")

{

return _n1 / _n2;

}

else if(_oper == "%")

{

return _n1 % _n2;

}

else

{

return 0;

}

 

}

}

SimpleCalculator.txt
0.01MB

'개인 > 개인공부' 카테고리의 다른 글

[java/eclipse] 자바 동적 배열 선언  (0) 2024.03.12
[Java/eclipse] TCP 소켓 서버 연결  (0) 2024.03.12
산출물  (0) 2024.03.05
[jsp] jsp파일 간의 변수 공유  (1) 2024.02.27
[jsp] 다른 화면으로 값 넘기는 방법  (0) 2024.02.23