-
Notifications
You must be signed in to change notification settings - Fork 0
/
Foobar_lab6.java
96 lines (87 loc) · 1.72 KB
/
Foobar_lab6.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class Foobar_lab6 {
public class Node{
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public void stack(int data) {
if(size==0) {
Node s=new Node();
s.data=data;
s.next=null;
head=tail=s;
size++;
}
else {
Node s=new Node();
s.data=data;
s.next=null;
tail.next=s;
s=tail;
size++;
}
}
public int popout() {
if(size>1) {
Node temp=head;
Node temp2=head.next;
while(temp2!=tail) {
temp=temp2;
temp2=temp2.next;
}
int q=temp2.data;
System.out.println(q);
temp.next=null;
temp=tail;
size--;
return q;
}
}
public void dis() {
if(size>0) {
Node temp=head;
while(temp!=null) {
System.out.println(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
Foobar_lab6 sta= new Foobar_lab6();
String s=scn.nextLine();
for(int i=0;i<s.length();i++) {
if(s.charAt(i)=='+' || s.charAt(i)=='-' || s.charAt(i)=='*') {
char g=s.charAt(i);
if(g=='+'){
int d=sta.popout();
int e=sta.popout();
int f=d+e;
sta.stack(f);
}
else if(g=='-') {
int d=sta.popout();
int e=sta.popout();
int f=d-e;
sta.stack(f);
}
else if(g=='*') {
int d=sta.popout();
int e=sta.popout();
int f=d*e;
sta.stack(f);
}
}
else if (s.charAt(i)=='0' || s.charAt(i)=='1' || s.charAt(i)=='2' || s.charAt(i)=='3' || s.charAt(i)=='4' || s.charAt(i)=='5' || s.charAt(i)=='6' || s.charAt(i)=='7' || s.charAt(i)=='8' || s.charAt(i)=='9') {
char d=s.charAt(i);
int n=Integer.parseInt(String.valueOf(d));
sta.stack(n);
}
}
sta.dis();
}
}