Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExpressionEvaluator {
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- String line=in.nextLine();
- ArrayStack<Integer> stack=new ArrayStack<>(100);
- String broj="";
- boolean mnozenje=false;
- for(int i=0;i<line.length();i++){
- char znak=line.charAt(i);
- if(Character.isDigit(znak)){
- broj+=znak;
- if(i==line.length()-1 || !Character.isDigit(line.charAt(i+1))){
- stack.push(Integer.parseInt(broj));
- broj="";
- }
- }
- if((i==line.length()-1 || znak=='*' || znak=='+') && mnozenje){
- mnozenje=false;
- int num1=stack.pop();
- int num2=stack.pop();
- stack.push(num1*num2);
- }
- if(znak=='*'){
- mnozenje=true;
- }
- }
- int sum=0;
- while (!stack.isEmpty()){
- sum+=stack.pop();
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement