-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostfix.java
More file actions
71 lines (63 loc) · 1.55 KB
/
Postfix.java
File metadata and controls
71 lines (63 loc) · 1.55 KB
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
/**
*
*/
import java.util.*;
/**
* @author M.NAVEEN
* RANDOM CODER'S
* Tech/Project Lead Android Club
*/
public class Postfix
{ String str;
static Scanner nav=new Scanner(System.in);
public Postfix(String str)
{
this.str=str;
}
public int addition(int a,int b)
{
return a+b;
}
public int sub(int a,int b)
{
return a-b;
}
public int Multiplication(int a,int b)
{
return a*b;
}
public int Division(int a,int b)
{
return a/b;
}
public static void main(String[] args)
{
System.out.println("Enter a String with valid expression a+b");
String s=nav.next();
String temp[]=s.split("");
Postfix p=new Postfix(s);
for(int i=0;i<temp.length;i++)
{
if(temp[i].equals("+"))
{ String split[]=s.split("\\+");
System.out.println("sum "+ p.addition(Integer.parseInt(split[0]),Integer.parseInt(split[1])));
break;
}
if(temp[i].equals("-"))
{ String split[]=s.split("\\-");
System.out.println("sub "+ p.sub(Integer.parseInt(split[0]),Integer.parseInt(split[1])));
break;
}
if(temp[i].equals("*"))
{ String split[]=s.split("\\*");
System.out.println("Multiplication "+p.Multiplication(Integer.parseInt(split[0]),Integer.parseInt(split[1])));
break;
}
if(temp[i].equals("/"))
{ String split[]=s.split("\\/");
System.out.println("Division " +p.Division(Integer.parseInt(split[0]),Integer.parseInt(split[1])));
break;
}
}
}
}