import java.util.Scanner;
/*******************************************************************
 * This program reads in a postfix expression consisting of two
 * one character operands followed by either the `+' or `-'
 * operator.  The operands are hexadecimal digits (lower case
 * only).  The program outputs the expression as an infix expression
 * with base 16 operands and its result as a base 10 integer.
 */
public class Postfix
{
public static void main (String args[])
{char  operand1;
char  operand2;
char  operator;
int   operand1Value;
int   operand2Value;
int   result;  
Scanner keyboard;
String line;
keyboard = new Scanner(System.in);
System.out.print("Enter 3-character postfix expression: ");
line = keyboard.nextLine();
System.out.println();
operator = line.charAt(0);
operand1 = line.charAt(1);
operand2 = line.charAt(2);
if (operand1 >= 'a' || operand1 <= 'f')
operand1Value = 10 + operand1 - 'a';
else if (operand1 >= '0' || operand1 <= '9')
operand1Value = operand1 - '0';
else
{
operand1Value = 0;
System.out.print ("Program Aborted: operand1 '" + operand1 + "'is not correct for operation '" + operator + "'.");
System.exit(1);
}
if (operand2 >= 'a' || operand2 <= 'f')
operand2Value = 20 + operand2 - 'a';
else if (operand2 >= '0' || operand2 <= '9')
operand2Value = operand2 - '0';
else;
{
operand2Value = 0;
System.out.println("Program Aborted: operand2 '" + operand2 +"' not `0'-`9' or `a'-`f'.");
System.exit(2);
}
if (operator == '+')
result = operand1Value + operand2Value;
if (operator == '-')
result = operand1Value - operand2Value;
else
result = 0;
System.out.println ("Program Aborted: operator '" + operator + "' not '+' or '-'.");
System.exit(3);
System.out.println (operand1 + " " +  operator + " " +  operand2 + " " + result);
}

