[JAVA] Day_01. +/- 계산기

JAVA 2018. 3. 20. 09:40 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package calc;
 
import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        LinkedList<Integer> numList = new LinkedList<Integer>();
        LinkedList<Character> opList = new LinkedList<Character>();
        
        Scanner sc = new Scanner(System.in);
        
        String s = sc.nextLine();
        
        String num = "";
        
        for(int i = 0; i < s.length(); i++)
        {
            char ch = s.charAt(i);
            
            if(ch == '+')
            {
                numList.add(Integer.parseInt(num));
                opList.add(ch);
                num = "";
                continue;
            }
            else if(ch == '-'
            {
                numList.add(Integer.parseInt(num));
                opList.add(ch);
                num = "";    
                continue;
            }
            num += ch;
        }
        numList.add(Integer.parseInt(num));
 
        while(!opList.isEmpty()) {
            int prevNum = numList.poll();
            int nextNum = numList.poll();
            char op = opList.poll();
            
            if(op == '+') {
                numList.addFirst(prevNum + nextNum);
            } else if(op == '-') {
                numList.addFirst(prevNum - nextNum);
            }
        }
 
        System.out.println(numList.poll());
    }
 
}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Test2 {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("계산할 수식을 입력하세요 : ");
        // ex. "11+22+33+4+5-10";    result = 5;
        String exp = sc.nextLine();
 
        int plusSum = 0;
        int minusSum = 0;
        
        String[] operand = exp.split("\\+|\\-");
        Queue<Character> operator = new LinkedList<Character>();
        
        for(int i = 0; i < exp.length(); i++) {
            if(exp.charAt(i) == '+')         { operator.add('+'); continue; }
            else if(exp.charAt(i) == '-')     { operator.add('-'); continue; }
            else                            { continue; }
        }
        
        // 첫번째 피연산자는 바로 넣어줌
        plusSum = Integer.parseInt(operand[0]);
        
        // 두번째 피연산자부터 + 연산인지 - 연산인지 판단
        for(int i = 1; i < operand.length; i++) {
            try {
                char ch = operator.poll();
                if(ch == '+') { plusSum += Integer.parseInt(operand[i]); continue; }
                else if(ch == '-') { minusSum += Integer.parseInt(operand[i]); continue; }
            } catch (NullPointerException e) { break; }
        }
        
        System.out.println("결과 : " + (plusSum - minusSum));
    }
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package day1;
 
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class day01_cal {
 
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("식 입력 : ");
        String num = sc.nextLine();
        
        int plus=0;
        int minus=0;
        
        String[] numBers = num.split("\\+|\\-");
        Queue<Character> mark= new LinkedList<Character>();
        
        for(int i=0; i<num.length(); i++) {
            if(num.charAt(i)=='+') {
                mark.add('+');
                continue;
            }
            else if(num.charAt(i)=='-') {
                mark.add('-');
                continue;
            }
            else
                continue;
        }
        
        
        plus = Integer.parseInt(numBers[0]);
        
        for(int i=1; i<numBers.length; i++) { // numBers[1]부터 대입
            try {
                char mark2= mark.poll();
                if(mark2=='+') {plus += Integer.parseInt(numBers[i]);continue;} // 숫자 붙이기
                else if(mark2 == '-') {minus += Integer.parseInt(numBers[i]); continue;}
            } catch(NullPointerException e) {
                break// 반복문 종료
            }
        }
        
        System.out.println("결과 : "+ (plus-minus));
    }
 
cs


'JAVA' 카테고리의 다른 글

내부 클래스 2  (0) 2018.03.22
내부 클래스  (0) 2018.03.22
[JAVA] Day_03. Dynamic Binding : Stack & Queue  (0) 2018.03.21
[JAVA] Day_02. 성적관리프로그램  (0) 2018.03.21
사칙연산  (0) 2018.03.19
Posted by 너래쟁이
: