public class ArithmeticAddition extends ArithmeticExpression {
public double Evaluate(double dblFValue,double dblSValue){
return dblFValue+dblSValue;
}
}
public class ArithmeticDivision extends ArithmeticExpression {
public double Evaluate(double dblFValue, double dblSValue) {
if (dblSValue != 0)
return dblFValue/dblSValue;
else
return 0;
}
}
public abstract class ArithmeticExpression {
ArithmeticExpression(){}
public ArithmeticExpression(double dblFValue,double dblSValue) {
}
private double _dblValue;
public double getValue(){
return this._dblValue;
}
public void setValue(double dblValue){
this._dblValue= dblValue;
}
public abstract double Evaluate(double dblFValue,double dblSValue);
public void Display(String expression){
System.out.println("Expression :\t"+expression);
}
}
public class ArithmeticMultiplication extends ArithmeticExpression{
public double Evaluate(double dblFValue, double dblSValue) {
return dblFValue*dblSValue;
}
}
public class ArithmeticSubstraction extends ArithmeticExpression {
public double Evaluate(double dblFValue, double dblSValue) {
return dblFValue- dblSValue;
}
}
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scnRead =new Scanner (System.in);
System.out.println("Enter :");
String strExpression = scnRead.nextLine();
int ParseExpression = new Calculator().ParseExpression(strExpression);
System.out.println(ParseExpression);
}
private int ParseExpression(String expression)
{
int winningLevel = 0;
byte winningTokenWeight = 0;
int winningPos = 0;
int level = 0;
int pos = 0;
for (char token :expression.toCharArray())
{
pos++;
switch (token)
{
case '(':
level++;
break;
case ')':
level--;
break;
}
if (level <= winningLevel)
{
if (OperatorWeight(token) > winningTokenWeight)
{
winningLevel = level;
winningTokenWeight = OperatorWeight(token);
winningPos = pos;
}
}
}
return winningPos;
}
private byte OperatorWeight(char value)
{
switch (value)
{
case '+':
case '-':
return 3;
case '*':
return 2;
case '/':
return 1;
default:
return 0;
}
}
}
No comments:
Post a Comment