Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package roman_calculator;
- import java.util.*;
- public class romanCalculator
- {
- // scan can now be used anywhere within this class
- Scanner scan = new Scanner(System.in);
- // This routine either returns false if the use wants to quit,
- // or it does one Roman Calculator calculation
- boolean doCalculation()
- {
- char operand,operator = getOperator() ;
- // Call getOperator to get either - + * / or q.
- // If q is returned, we return a false.
- if (operator == 'q')
- return false;
- // If the operator is + - * or / then call
- // getOperand(1) for the first operand and
- // call getOperand(2) for the second operand
- int operand1 = getOperand(1),operand2 = getOperand(2);
- int answer = doArithmetic (operand1,operand2, operator);
- String sAnswer = convert_to_Roman(answer);
- System.out.println("Answer = "+answer);
- // call doArithmetic and print out the result using
- // convert_to_Roman to generate Roman Numeral output.
- return true;
- }
- // This routine prompts the user with
- // Operator: + - * / q for quit
- // If none of these are entered, this routine complains and
- // prompts the user again. Otherwise the operator is returned.
- char getOperator()
- {
- System.out.println("Enter operation: + - * / q (q ==> quit) : ");
- String input = scan.next();
- return 0;
- }
- // This routine prompts the user for either operand1 or operand2
- // depending on the value of which. This routine uppercases the
- // input and calls convert_from_Roman to create an integer.
- // If the input is invalid ( negative return from convert_from_Roman)
- // then complain and prompt the user again.
- int getOperand(int which)
- {
- System.out.println("Enter operand" + which);
- String op = scan.next();
- return convert_from_Roman(op);
- }
- // Routine to convert an integer to a Roman Numeral String.
- // When you do this routine, you might find it handy to
- // create a utility routine that looks like:
- //String addRomanDigit(String starting, int num, char digit)
- String convert_to_Roman(int value)
- {
- String roman = "";
- while (value > 0 && value < 4000)
- switch (value)
- {
- case 1000:
- roman +='M';
- case 500:
- roman +='D';
- case 100:
- roman +='C';
- case 50:
- roman +='L';
- case 10:
- roman +='X';
- case 5:
- roman +='V';
- case 1:
- roman +='I';
- break;
- }
- return roman;
- }
- // Convert Roman Numeral String to an integer. If the
- // Roman Numeral String is invalid, return -1.
- int convert_from_Roman(String value)
- {
- String s = value.toUpperCase();
- s = s.trim();
- int len = s.length();
- int total = 0;
- for(int i = len; i > 0; i++)
- {
- char c = value.charAt(i);
- switch(c)
- {
- case 'M':
- total += 1000;
- case 'D':
- total += 500;
- case 'C':
- total += 100;
- case 'L':
- total += 50;
- case 'X':
- total += 10;
- case 'V':
- total += 5;
- case 'I':
- total += 1;
- break;
- }
- }
- return total;
- }
- // Perform the arithmetic indicated by the operator (+ - * /)
- void doArithmetic(int operand1, int operand2, char operator)
- {
- switch (operator)
- {
- case '+':
- return;
- case '-':
- return;
- case '*':
- return;
- case '/':
- return;
- }
- }
- public static void main(String[] args)
- {
- romanCalculator rc = new romanCalculator();
- while (rc.doCalculation())
- {
- System.out.println(); // blank line
- }
- System.out.println("Finished Roman Computations");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement