逆ポーランド記法電卓 Java
import java.io.*;
enum OPERATION
{
PLUS,
MINUS,
MULTIPLY,
DIVIDE,
INVERSION,
CLEAR,
ENTER,
EXIT
}
class CalcStack
{
private static final int StackSize = 10;
private double[] mValues;
private int mCount;
public CalcStack()
{
mValues = new double[StackSize];
mCount = 0;
}
public void Push(double value) throws Exception
{
if (mCount >= StackSize)
throw new Exception("Stack がオーバーフローしました。");
mValues[mCount++] = value;
}
public double Pop() throws Exception
{
if (mCount <= 0)
throw new Exception("Stack が空です。");
return mValues[--mCount];
}
public int Count()
{
return mCount;
}
public double ElementAt(int idx) throws Exception
{
if (idx < 0 || idx >= mCount)
throw new Exception("パラメータが正しくありません。");
return mValues[mCount - idx - 1];
}
}
class RpnCalc
{
private CalcStack mStack = new CalcStack();
private OPERATION Input() throws Exception
{
BufferedReader br = new BufferedReader(
new InputStreamReader( System.in ) );
String input = br.readLine();
String numbers = "";
OPERATION ope = OPERATION.ENTER;
for( int i = 0 ; i < input.length() ; i++ )
{
char ch = input.charAt(i);
if (('0' <= ch && ch <= '9') || ch == '.')
{
numbers += ch;
}
else
{
if (ch == '+')
ope = OPERATION.PLUS;
else if (ch == '-')
ope = OPERATION.MINUS;
else if (ch == '*')
ope = OPERATION.MULTIPLY;
else if (ch == '/')
ope = OPERATION.DIVIDE;
else if (ch == '!')
ope = OPERATION.INVERSION;
else if (ch == 'c'|| ch == 'C')
ope = OPERATION.CLEAR;
else if (ch == 'q' || ch == 'Q')
ope = OPERATION.EXIT;
break;
}
}
if (numbers.length() > 0)
mStack.Push(Double.parseDouble(numbers));
return ope;
}
private void ShowCalculator() throws Exception
{
final String body = "\n"
+ "*** RPN Calculator *******************************\n"
+ "* +:加 -:減 *:乗 /:除 !:符号 c:削除 q:終了 *\n"
+ "* *";
System.out.println( body );
for (int i = mStack.Count(); i > 0; i--)
System.out.printf("* %2d: %f\n", i, mStack.ElementAt(i - 1) );
System.out.print(">");
}
public void Run()
{
for (; ; )
{
OPERATION operation = OPERATION.EXIT;
try
{
ShowCalculator();
operation = Input();
if (operation == OPERATION.INVERSION || operation == OPERATION.CLEAR)
{
double n = mStack.Pop();
if (operation == OPERATION.INVERSION)
mStack.Push(n * (-1.0));
}
else if (operation == OPERATION.PLUS || operation == OPERATION.MINUS
|| operation == OPERATION.MULTIPLY || operation == OPERATION.DIVIDE)
{
double n1 = mStack.Pop();
double n2 = mStack.Pop();
if (operation == OPERATION.PLUS)
mStack.Push(n1 + n2);
else if (operation == OPERATION.MINUS)
mStack.Push(n2 - n1);
else if (operation == OPERATION.MULTIPLY)
mStack.Push(n1 * n2);
else if (operation == OPERATION.DIVIDE)
{
if (n1 == 0.0)
System.out.println("\nDivide by Zero!");
else
mStack.Push(n2 / n1);
}
}
else if (operation == OPERATION.EXIT)
{
break;
}
}
catch (Exception ex)
{
System.out.println("\n" + ex.getMessage());
}
}
}
public static void main( String[] args )
{
RpnCalc calc = new RpnCalc();
calc.Run();
}
}