letsdoitjava

Roman Calculator

Jul 6th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. // Roman Calculator, this routine either returns false if the user wants to quit or it does on Roman calculation.
  2. // Renee Waggoner
  3. // July 8, 2019
  4. // Special Requirements: None
  5. package roman_calculator;
  6. import java.util.*;
  7.  
  8. public class RomanCalculator {
  9.     Scanner scan = new Scanner(System.in);
  10.  
  11.     boolean doCalculation() {
  12.         char operator;
  13.         // prompt user for operator till user enters one of +, *, -, / or q
  14.         while (true) {
  15.             System.out.print("Operator: + - * / q for quit: ");
  16.             operator = getOperator();
  17.             if (operator == 'q')
  18.             {
  19.                 return false;
  20.             }
  21.             // if operator is either + - / * then break
  22.             // Note: I tried to not use break in this code but it kept coming up, so I relented.
  23.             if (operator == '+' || operator == '*' || operator == '/' || operator == '-')
  24.                 break;
  25.             // else prompt user to reenter operator
  26.             System.out.println("Your operand is bad ... try again:");
  27.         }
  28.         int operand1, operand2;
  29.         operand1 = getOperand(1);
  30.         operand2 = getOperand(2);
  31.         int answer = doArithmetic(operand1, operand2, operator, operator);
  32.         System.out.println("Answer = " + convert_to_Roman(answer));
  33.         return true;
  34.     }
  35.     // This routine prompts the user with
  36.     // Operator: + - * / q for quit
  37.     // If none of these are entered, this routine complains and
  38.     // prompts the user again. Otherwise the operator is returned.
  39.     char getOperator() {
  40.         String operator1;
  41.         operator1 = scan.next();
  42.         return operator1.charAt(0);
  43.     }
  44.     // This routine prompts the user for either operand1 or operand2
  45.     // depending on the value of which. This routine uppercases the
  46.     // input and calls convert_from_Roman to create an integer.
  47.     // If the input is invalid ( negative return from convert_from_Roman)
  48.     // then complain and prompt the user again.
  49.     int getOperand(int which) {
  50.         int upperCase = 0;
  51.         String operand;
  52.         // prompt user for operand which till user enters all in romans
  53.         while (true) {
  54.             System.out.print("Enter operand" + which + ": ");
  55.             operand = scan.next();
  56.             operand = operand.toUpperCase();
  57.             // this variable is the index of string where it has bad character
  58.             int bad = -1;
  59.             // check every character of operand if it is roman
  60.             for (int i = 0; i < operand.length(); i++) {
  61.                 // if current character is not roman then set bad to i and break
  62.                 // Note: broke it up here because it was giving me a headache, but booleans are my favorite
  63.                 if (operand.charAt(i) != 'M' && operand.charAt(i) != 'D' && operand.charAt(i) !=
  64.                        
  65.                         'C' && operand.charAt(i) != 'L' && operand.charAt(i) != 'X' && operand.charAt(i) != 'V'&& operand.charAt(i) != 'I')
  66.                 {
  67.                     bad = i;
  68.                     break;
  69.                 }
  70.             }
  71.             // if bad character is unchanged that means bad character is not encountered
  72.             if (bad == -1)
  73.                 break;
  74.             System.out.println(operand + " has a bad character at position: " + bad);
  75.         }
  76.         upperCase = convert_from_Roman(operand);
  77.         return upperCase;
  78.     }
  79.     // Routine to convert an integer to a Roman Numeral String.
  80.     // When you do this routine, you might find it handy to
  81.     // create a utility routine that looks like:
  82.     // String addRomanDigit(String starting, int num, char digit)
  83.     String convert_to_Roman(int value) {
  84.         String num = "";
  85.         while (value > 0)
  86.         {
  87.             if (value >= 1000)
  88.             {
  89.                 num = num + "M";
  90.                 value = value - 1000;
  91.                
  92.             } else if (value >= 500) {
  93.                 num = num + "D";
  94.                 value = value - 500;
  95.                
  96.             } else if (value >= 100) {
  97.                 num = num + "C";
  98.                 value = value - 100;
  99.  
  100.             } else if (value >= 50) {
  101.                 num = num + "L";
  102.                 value = value - 50;
  103.  
  104.             } else if (value >= 10) {
  105.                 num = num + "X";
  106.                 value = value - 10;
  107.  
  108.             } else if (value >= 5) {
  109.                 num = num + "V";
  110.                 value = value - 5;
  111.  
  112.             } else if (value >= 1) {
  113.                 num = num + "I";
  114.                 value = value - 1;
  115.             }
  116.         }
  117.         num = num + "";
  118.         return num;
  119.     }
  120.     // Convert Roman Numeral String to an integer. If the
  121.     // Roman Numeral String is invalid, return -1.
  122.     int convert_from_Roman(String value) {
  123.         int total = 0;
  124.         for (int i = 0; i < value.length(); i++) {
  125.  
  126.             if (value.charAt(i) == 'M') {
  127.                 total += 1000;
  128.  
  129.             } else if (value.charAt(i) == 'D') {
  130.                 total += 500;
  131.  
  132.             } else if (value.charAt(i) == 'C') {
  133.                 total += 100;
  134.  
  135.             } else if (value.charAt(i) == 'L') {
  136.                 total += 50;
  137.  
  138.             } else if (value.charAt(i) == 'X') {
  139.                 total += 10;
  140.  
  141.             } else if (value.charAt(i) == 'V') {
  142.                 total += 5;
  143.  
  144.             } else if (value.charAt(i) == 'I') {
  145.                 total += 1;
  146.  
  147.             } else {
  148.                 total = -1;
  149.             }
  150.         }
  151.         return total;
  152.     }
  153.     int invalid_operand_position(String value, int result, int num1, int num2) {
  154.         {
  155.             for (int i = 0; i < value.length(); i++) {
  156.                 if (num1 < 0) {
  157.                     System.out.println("Invalid operand at position: " + i);
  158.                     return Integer.MIN_VALUE;
  159.                 }
  160.                 if (num2 < 0) {
  161.                     System.out.println("Invalid operand at position: " + i);
  162.                     return Integer.MIN_VALUE;
  163.                 }
  164.                 if (num1 >= num2) {
  165.                     result = result + num1;
  166.                 } else {
  167.                     result = result + num2 - num1;
  168.                     i++;
  169.                 }
  170.             }
  171.             return result;
  172.         }
  173.     }
  174.     // Perform the arithmetic indicated by the operator (+ - * /)
  175.     // and return answer
  176.     int doArithmetic(int operand1, int operand2, int choiceFromUser, char operator) {
  177.         if (operator == '+') {
  178.             choiceFromUser = operand1 + operand2;
  179.         } else if (operator == '-') {
  180.             choiceFromUser = operand1 - operand2;
  181.         } else if (operator == '*') {
  182.             choiceFromUser = operand1 * operand2;
  183.         } else if (operator == '/') {
  184.             choiceFromUser = operand1 / operand2;
  185.         }
  186.         return choiceFromUser;
  187.     }
  188.     public static void main(String[] args) {
  189.         RomanCalculator rc = new RomanCalculator();
  190.         while (rc.doCalculation()) {
  191.             System.out.println();
  192.         }
  193.         System.out.println("Finished Roman Computations");
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment