Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.03 KB | None | 0 0
  1. //Created by Cody Smith
  2. //This program calculates roman numbers and prints them out.
  3. package roman_calculator;
  4.  
  5. import java.util.*;
  6. public class roman_calculator
  7. {
  8. // scan can now be used anywhere within this class
  9. Scanner scan = new Scanner(System.in);
  10. boolean doCalculation()
  11.     {
  12.     char operator;
  13.     String soperand1, soperand2;
  14.    
  15.     int ioperand1, ioperand2, iresult;
  16.     String sres;
  17.     operator = getOperator();
  18.     if (operator == 'q')
  19.         return false;
  20.     else
  21.     soperand1=getOperand(1);
  22.     soperand2=getOperand(2);
  23.     ioperand1=convert_from_Roman(soperand1);
  24.     //System.out.println("ioperand 1 " + ioperand1);
  25.     ioperand2=convert_from_Roman(soperand2);
  26.     //System.out.println("ioperand 2 " + ioperand2);
  27.     // call doArithmetic and print out the result using
  28.     // convert_to_Roman to generate Roman Numeral output.
  29.     iresult = doArithmetic(ioperand1, ioperand2,operator);
  30.     sres = convert_to_Roman(iresult);
  31.     System.out.println("sResult " + sres);
  32.     return true;
  33.     }
  34.    
  35.    
  36.  
  37. // This routine prompts the user with
  38. // Operator: + - * / q for quit
  39. // If none of these are entered, this routine complains and
  40. // prompts the user again. Otherwise the operator is returned.
  41. char getOperator()
  42.     {
  43.     char operator='q';
  44.     // ***********************************
  45.     // Logic to get a correct operator from the user goes here
  46.     // ***********************************
  47.     boolean operator_is_good=false;
  48.             do
  49.             {
  50.                 System.out.print("Operator: + - * / q for quit: ");
  51.                 String strOperator = scan.nextLine();
  52.                 strOperator = strOperator.trim();
  53.                     if (strOperator.length() == 0)
  54.                     continue; // Need to try this again with no input
  55.                     operator = strOperator.charAt(0);
  56.                     operator_is_good=false;
  57.                 switch (operator)
  58.                     {
  59.                     case 'q':
  60.                     case '+':
  61.                     case '-':
  62.                     case '*':
  63.                     case '/':
  64.                     operator_is_good = true;
  65.                     break;
  66.                     default:
  67.                     System.out.println("Your operator is bad ... try again:");
  68.                     break;
  69.                     }
  70.             } while (!operator_is_good);
  71.             return operator;
  72.     }
  73.  
  74. // This routine prompts the user for either operand1 or operand2
  75. // depending on the value of which. This routine uppercases the
  76. // input and calls convert_from_Roman to create an integer.
  77. // If the input is invalid ( negative return from convert_from_Roman)
  78. // then complain and prompt the user again.
  79. String getOperand(int op)
  80.     {
  81.    
  82.     System.out.println("Enter operand "+ op);
  83.     String input, sOperand;
  84.     char ch;
  85.         boolean operand_is_bad;
  86.         do
  87.         {
  88.             operand_is_bad=false;
  89.             input = scan.nextLine();
  90.             input = input.trim();
  91.             if (input.length() == 0)
  92.                     operand_is_bad=true;
  93.             sOperand = "";
  94.             for (int i=0; i < input.length(); i++)
  95.             {
  96.                 ch = input.charAt(i);
  97.                 switch(ch)
  98.                 {
  99.                 case 'I':
  100.                 case 'V':
  101.                 case 'X':
  102.                 case 'L':
  103.                 case 'C':
  104.                 case 'D':
  105.                 case 'M':
  106.                     sOperand = sOperand + ch;
  107.                     operand_is_bad = false;
  108.                     break;
  109.                 default:
  110.                     operand_is_bad=true;
  111.                     System.out.println("Your last input was bad, try again " + sOperand);
  112.                     break;
  113.                 }
  114.                    
  115.             }
  116.            
  117.                        
  118.         } while (operand_is_bad);
  119. //return Integer.parseInt(input);
  120.         return sOperand;
  121.     }
  122.  
  123. // Routine to convert an integer to a Roman Numeral String.
  124. // When you do this routine, you might find it handy to
  125. // create a utility routine that looks like:
  126. // String addRomanDigit(String starting, int num, char digit)
  127. String convert_to_Roman(int value)
  128.     {
  129.     String romanNum = "";
  130.     int i, ii, itemp, iremainder;
  131.    
  132.     int [] iarray  = new int[7];
  133.     iarray[0] =1000;
  134.     iarray[1] =500;
  135.     iarray[2] =100;
  136.     iarray[3] =50;
  137.     iarray[4] =10;
  138.     iarray[5] =5;
  139.     iarray[6] =1;
  140.     //System.out.println("i array " + iarray[0]);
  141.    
  142.     for (i=0; i<=6; i++)
  143.     {
  144.        
  145.     itemp = value/iarray[i];
  146.     iremainder = value % iarray[i];
  147.     value = iremainder;
  148.     if ((itemp >= 1) &&  (i==0))
  149.     {
  150.         for (ii=1; ii <=itemp; ii++)
  151.         {
  152.             romanNum = romanNum + "M";
  153.         }
  154.                
  155.     }
  156.     if ((itemp >= 1) &&  (i==1))
  157.     {
  158.         for (ii=1; ii <=itemp; ii++)
  159.         {
  160.             romanNum = romanNum + "D";
  161.         }
  162.                
  163.     }
  164.     if ((itemp >= 1) &&  (i==2))
  165.     {
  166.         for (ii=1; ii <=itemp; ii++)
  167.         {
  168.             romanNum = romanNum + "C";
  169.         }
  170.                
  171.     }
  172.     if ((itemp >= 1) &&  (i==3))
  173.     {
  174.         for (ii=1; ii <=itemp; ii++)
  175.         {
  176.             romanNum = romanNum + "L";
  177.         }
  178.                
  179.     }
  180.     if ((itemp >= 1) &&  (i==4))
  181.     {
  182.         for (ii=1; ii <=itemp; ii++)
  183.         {
  184.             romanNum = romanNum + "X";
  185.         }
  186.                
  187.     }
  188.     if ((itemp >= 1) &&  (i==5))
  189.     {
  190.         for (ii=1; ii <=itemp; ii++)
  191.         {
  192.             romanNum = romanNum + "V";
  193.         }
  194.                
  195.     }
  196.     if ((itemp >= 1) &&  (i==6))
  197.     {
  198.         for (ii=1; ii <=itemp; ii++)
  199.         {
  200.             romanNum = romanNum + "I";
  201.         }
  202.                
  203.     }
  204.    
  205.     }
  206.     return romanNum;
  207.     }
  208.  
  209. // Convert Roman Numeral String to an integer. If the
  210. // Roman Numeral String is invalid, return -1.
  211. int convert_from_Roman(String value1)
  212.     {
  213.     int i, ival, ilen;
  214.     char ch;
  215.     ival =0;
  216.     i=0;
  217.     ilen = value1.length() -1;
  218.     //System.out.println("length " + ilen);
  219.     while (i <= ilen)
  220.     {
  221.         ch = value1.charAt(i);
  222.         switch(ch)
  223.         {
  224.         case 'I':
  225.             ival=ival+1;
  226.             break;
  227.         case 'V':
  228.             ival=ival+5;
  229.             break;
  230.         case 'X':
  231.             ival=ival+10;
  232.             break;
  233.         case 'L':
  234.             ival=ival+50;
  235.             break;
  236.         case 'C':
  237.             ival=ival+100;
  238.             break;
  239.         case 'D':
  240.             ival=ival+500;
  241.             break;
  242.         case 'M':
  243.             ival=ival+1000;
  244.             break;
  245.         default:
  246.                 break;
  247.         }
  248.         i+=1;
  249.         // System.out.println("ival " + ival);
  250.         // System.out.println("i  " + i);
  251.     }
  252.    
  253.         return ival;
  254.     }
  255.  
  256.    
  257.  
  258. // Perform the arithmetic indicated by the operator (+ - * /)
  259. // and return answer
  260. int doArithmetic(int operand1, int operand2, char operator)
  261.     {
  262.     int answer;
  263.     // ***********************************
  264.     // Your logic to compute the answer
  265.     // ***********************************
  266.     switch(operator)
  267.             {
  268.                 case '+':
  269.                 answer = operand1 + operand2;
  270.                 break;
  271.                 case '-':
  272.                 answer = operand1 - operand2;
  273.                 break;
  274.                 case '*':
  275.                 answer = operand1 * operand2;
  276.                 break;
  277.                 case '/':
  278.                 answer = operand1 / operand2;
  279.                 break;
  280.                
  281.                 default:
  282.                 System.out.println("We shouldn't get here in doArithmentic!!!!" + operator);
  283.                 answer = 0;
  284.                 break;
  285.             }
  286.    
  287.     return answer;
  288.     }
  289.  
  290. //Main method
  291. public static void main(String[] args) {
  292.     roman_calculator rc = new roman_calculator();
  293.     boolean bContinue =true;
  294.     while (bContinue)
  295.         {
  296.         bContinue =rc.doCalculation();
  297.         }
  298.     System.out.println("Finished Roman Computations");
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement