Recent Posts
None | 20 sec ago
C++ | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Lua | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
By Anonymous on the 9th of Feb 2010 09:33:01 PM
Download |
Raw |
Embed |
Report
import java.util.Scanner;
import java.util.Stack;
public class Calculator {
int x,y;
if(a.equals("+") || a.equals("-"))x = 1;
else x = 2;
if(b.equals("+") || b.equals("-"))y = 1;
else y = 2;
if(x<y)return true;
else return false;
}
Scanner sc = new Scanner(ex);
Stack<String> operators = new Stack<String>();
while(sc.hasNext()){
token = sc.next();
try{
sb.append(" "+token+" ");
if(operators.isEmpty()) operators.push(token);
else{
if(priority(operators.peek(), token)){
operators.push(token);
}
else{
sb.append(" "+operators.pop()+" ");
operators.push(token);
}
}
}
}
while(!operators.isEmpty()){
sb.append(" "+operators.pop()+" ");
}
return sb.toString();
}
public static float calculate
(float a,
float b,
String o
){
if(o.equals("+"))return a+b;
else if(o.equals("-"))return a-b;
else if(o.equals("/"))return b/a; //nevem tocno zakaj mora bit tukaj obrnjeno :P
else return a*b;
}
public static float evaluate
(String ex
){
float result=0;
Stack<Float> calc = new Stack<Float>();
ex = infixToPostfix(ex);
Scanner sc = new Scanner(ex);
while(sc.hasNext()){
token = sc.next();
try{
calc.
push(Float.
parseFloat(token
));
calc.push(calculate(calc.pop(), calc.pop(), token));
}
}
result = calc.pop();
return result;
}
public static void main
(String[] args
){
Scanner sc
= new Scanner
(System.
in);
System.
out.
println("Navodila: Dovoljeni operatorji so +,-,*,/.\nOperande in operatorje locite s presledki.\nZa izhod vpisite \"x\".");
do{
System.
out.
println("Rezultat: "+evaluate
(sc.
nextLine()));
}while(!input.equals("x"));
}
}
Submit a correction or amendment below.
Make A New Post