import cs1.*;
//==============================================================
// 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[])
 {
  char  operand1, operand2; // operands entered as characters
  char  operator;
  int   operand1Value, operand2Value; // operands in numeric form
  int   result;  
  System.out.println ("Enter 3-character postfix expression: ");
  operand1 = Keyboard.readChar();
  System.out.println (" operand1 is " + operand1); // echo input
  operand2 = Keyboard.readChar();
  System.out.println (" operand2 is " + operand2);  // echo input
  operator = Keyboard.readChar();
  System.out.println (" operator is " + operator); // echo input

  // decode first operand (convert it to a number IF POSSIBLE 
  if (operand1 >= 'a' && operand1 <= 'f') // if operand1 between 'a' and 'f'
  {  // convert to number: 'a' is 10 so add 10 & ASCII 'a' value
     operand1Value = operand1 + 10 - 'a';
     System.out.println ("operand1Value is " + operand1Value); // show numeric value
	} 
  else if (operand1 >= '0' && operand1 <= '9') 
    {  // convert to number by subtracting ASCII '0' value
    operand1Value = operand1 - '0';
	 System.out.println ("operand1Value is " + operand1Value); // show numeric value
    }
  else
    {
    operand1Value = 0;  // prevents message saying operand1Value may not have value 
    System.out.println ("operand1Value is " + operand1Value);
    System.out.println("Program Aborted: operand1 " + operand1 + 
	  " not `0'-`9' or `a'-`f'.");   // operand1 wasn't in valid range
	 System.exit(1);  // 1 indicates WHERE error occurred
    }
	
  // now decode 2nd operand (convert it to a number	 
  if (operand2 >= 'a' && operand2 <= 'f')  // see above explanations
   {  
	 operand2Value = 10 + operand2 - 'a';
    System.out.println ("operand2Value is " + operand2Value);
   }
  else if (operand2 >= '0' && operand2 <= '9')
   {  
	 operand2Value = operand2 - '0';
	 System.out.println ("operand2Value is " + operand2Value);
   }
  else
    {
	 operand2Value = 0;
    System.out.println ("operand2Value is " + operand2Value);
    System.out.println ("Program Aborted: operand2 "
	 + operand2 + " not `0'-`9' or `a'-`f'.");
    System.exit(2);  // shows WHERE error occurred
    }

 // check that operator is one of two allowed: ( + or -) and IF SO perform operation 
 // Note we're using operandValues
  if (operator == '+')  
  		result = operand1Value + operand2Value; 
  else if (operator == '-')
    result = operand1Value - operand2Value;
  else
    {
     result = 0;
     System.out.println ("Program Aborted: operator " + operator + " not `+' or `-'.");
     System.exit(3);  // indicates WHERE program aborted
    }
	 
	// note that we're printing original operands not their numeric values here 
  System.out.println (operand1 + " " + operator + " " + operand2 + " = " + result);
 }
 
}
