Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Created by Cody Smith
- //This program calculates roman numbers and prints them out.
- package roman_calculator;
- import java.util.*;
- public class roman_calculator
- {
- // scan can now be used anywhere within this class
- Scanner scan = new Scanner(System.in);
- boolean doCalculation()
- {
- char operator;
- String soperand1, soperand2;
- int ioperand1, ioperand2, iresult;
- String sres;
- operator = getOperator();
- if (operator == 'q')
- return false;
- else
- soperand1=getOperand(1);
- soperand2=getOperand(2);
- ioperand1=convert_from_Roman(soperand1);
- //System.out.println("ioperand 1 " + ioperand1);
- ioperand2=convert_from_Roman(soperand2);
- //System.out.println("ioperand 2 " + ioperand2);
- // call doArithmetic and print out the result using
- // convert_to_Roman to generate Roman Numeral output.
- iresult = doArithmetic(ioperand1, ioperand2,operator);
- sres = convert_to_Roman(iresult);
- System.out.println("sResult " + sres);
- 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()
- {
- char operator='q';
- // ***********************************
- // Logic to get a correct operator from the user goes here
- // ***********************************
- boolean operator_is_good=false;
- do
- {
- System.out.print("Operator: + - * / q for quit: ");
- String strOperator = scan.nextLine();
- strOperator = strOperator.trim();
- if (strOperator.length() == 0)
- continue; // Need to try this again with no input
- operator = strOperator.charAt(0);
- operator_is_good=false;
- switch (operator)
- {
- case 'q':
- case '+':
- case '-':
- case '*':
- case '/':
- operator_is_good = true;
- break;
- default:
- System.out.println("Your operator is bad ... try again:");
- break;
- }
- } while (!operator_is_good);
- return operator;
- }
- // 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.
- String getOperand(int op)
- {
- System.out.println("Enter operand "+ op);
- String input, sOperand;
- char ch;
- boolean operand_is_bad;
- do
- {
- operand_is_bad=false;
- input = scan.nextLine();
- input = input.trim();
- if (input.length() == 0)
- operand_is_bad=true;
- sOperand = "";
- for (int i=0; i < input.length(); i++)
- {
- ch = input.charAt(i);
- switch(ch)
- {
- case 'I':
- case 'V':
- case 'X':
- case 'L':
- case 'C':
- case 'D':
- case 'M':
- sOperand = sOperand + ch;
- operand_is_bad = false;
- break;
- default:
- operand_is_bad=true;
- System.out.println("Your last input was bad, try again " + sOperand);
- break;
- }
- }
- } while (operand_is_bad);
- //return Integer.parseInt(input);
- return sOperand;
- }
- // 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 romanNum = "";
- int i, ii, itemp, iremainder;
- int [] iarray = new int[7];
- iarray[0] =1000;
- iarray[1] =500;
- iarray[2] =100;
- iarray[3] =50;
- iarray[4] =10;
- iarray[5] =5;
- iarray[6] =1;
- //System.out.println("i array " + iarray[0]);
- for (i=0; i<=6; i++)
- {
- itemp = value/iarray[i];
- iremainder = value % iarray[i];
- value = iremainder;
- if ((itemp >= 1) && (i==0))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "M";
- }
- }
- if ((itemp >= 1) && (i==1))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "D";
- }
- }
- if ((itemp >= 1) && (i==2))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "C";
- }
- }
- if ((itemp >= 1) && (i==3))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "L";
- }
- }
- if ((itemp >= 1) && (i==4))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "X";
- }
- }
- if ((itemp >= 1) && (i==5))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "V";
- }
- }
- if ((itemp >= 1) && (i==6))
- {
- for (ii=1; ii <=itemp; ii++)
- {
- romanNum = romanNum + "I";
- }
- }
- }
- return romanNum;
- }
- // Convert Roman Numeral String to an integer. If the
- // Roman Numeral String is invalid, return -1.
- int convert_from_Roman(String value1)
- {
- int i, ival, ilen;
- char ch;
- ival =0;
- i=0;
- ilen = value1.length() -1;
- //System.out.println("length " + ilen);
- while (i <= ilen)
- {
- ch = value1.charAt(i);
- switch(ch)
- {
- case 'I':
- ival=ival+1;
- break;
- case 'V':
- ival=ival+5;
- break;
- case 'X':
- ival=ival+10;
- break;
- case 'L':
- ival=ival+50;
- break;
- case 'C':
- ival=ival+100;
- break;
- case 'D':
- ival=ival+500;
- break;
- case 'M':
- ival=ival+1000;
- break;
- default:
- break;
- }
- i+=1;
- // System.out.println("ival " + ival);
- // System.out.println("i " + i);
- }
- return ival;
- }
- // Perform the arithmetic indicated by the operator (+ - * /)
- // and return answer
- int doArithmetic(int operand1, int operand2, char operator)
- {
- int answer;
- // ***********************************
- // Your logic to compute the answer
- // ***********************************
- switch(operator)
- {
- case '+':
- answer = operand1 + operand2;
- break;
- case '-':
- answer = operand1 - operand2;
- break;
- case '*':
- answer = operand1 * operand2;
- break;
- case '/':
- answer = operand1 / operand2;
- break;
- default:
- System.out.println("We shouldn't get here in doArithmentic!!!!" + operator);
- answer = 0;
- break;
- }
- return answer;
- }
- //Main method
- public static void main(String[] args) {
- roman_calculator rc = new roman_calculator();
- boolean bContinue =true;
- while (bContinue)
- {
- bContinue =rc.doCalculation();
- }
- System.out.println("Finished Roman Computations");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement