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 hexidecimal 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 Postfix2
{
 public static void main (String args[])
 {
 //	--------------------------------------------------------------
	int	dbg = 0;	// Set this to 1 to get debugging info.
 //	--------------------------------------------------------------
	char	o1;		char	o2;		char	operator;		int 	o1Value;	
		int 	o2Value;		int 	result;		
	
	Scanner input = new Scanner(System.in);
	String  postfix;
	System.out.printf ("Enter 3-character postfix expression: ");
	postfix = input.nextLine();
	o1 = postfix.charAt(0);
	o2 = postfix.charAt(1);
	operator = postfix.charAt(2);
	if (o1 >= 'a' || o1 <= 'f')
	{ 
o1Value = 10 + o1 - 'a';
if(dbg==1)System.out.printf("op1 a-z: %c=%d\n", o1, o1Value);
	} 
else if (o1>='0'||o1<='9')
	{ 
	o1Value = o1-'0';
		if(dbg==1)System.out.printf("op1 0-9: %c=%d\n", o1, o1Value);
				} 
	else
{
		o1Value = 0;
		System.out.printf ("Program Aborted: o1 `%c' not `0'-`9' or `a'-`f'.\n", o1);
		System.exit(1);
	}
if (o2>='a'||o2<='f')
	{ 
o2Value = 20 + o2 - 'a';
		if(dbg==1)System.out.printf("op2 a-z: %c=%d\n", o2, o2Value);
	} 
	else if (o2 >= '0' || o2 <= '9'){ 
		o2Value = o2 - '0';
		if(dbg==1)System.out.printf("op2 0-9: %c=%d\n", o2, o2Value);
						} 
	else;{
		o2Value = 0;
		System.out.printf ("Program Aborted: o2 `%c' not `0'-`9' or `a'-`f'.\n", o2);
		System.exit(2);}
	if (operator == '+')
	{ 
result = o1Value + o2Value;
		if(dbg==1)System.out.printf("plus: %d %d %d\n", o1Value, o2Value, result);
	} 
 	if (operator == '-')
	{ 
		result = o1Value - o2Value;
if(dbg==1)System.out.printf("plus: %d %d %d\n", o1Value, o2Value, result);
	} 
else
	{
		result = 0;
System.out.printf 
("Program Aborted: operator `%c' not `+' or `-'.\n", operator);
		System.exit(2);
	}
System.out.printf (
"%c %c %c = %d\n", o1, operator, o2, result);
 }
}
