Advertisement
Guest User

roman Calc

a guest
Mar 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. package roman_calculator;
  2.  
  3. import java.util.*;
  4.  
  5. public class romanCalculator
  6. {
  7. // scan can now be used anywhere within this class
  8. Scanner scan = new Scanner(System.in);
  9.  
  10. // This routine either returns false if the use wants to quit,
  11. // or it does one Roman Calculator calculation
  12. boolean doCalculation()
  13. {
  14. char operand,operator = getOperator() ;
  15.  
  16. // Call getOperator to get either - + * / or q.
  17. // If q is returned, we return a false.
  18.  
  19. if (operator == 'q')
  20. return false;
  21.  
  22. // If the operator is + - * or / then call
  23. // getOperand(1) for the first operand and
  24. // call getOperand(2) for the second operand
  25.  
  26. int operand1 = getOperand(1),operand2 = getOperand(2);
  27. int answer = doArithmetic (operand1,operand2, operator);
  28. String sAnswer = convert_to_Roman(answer);
  29. System.out.println("Answer = "+answer);
  30.  
  31.  
  32. // call doArithmetic and print out the result using
  33. // convert_to_Roman to generate Roman Numeral output.
  34.  
  35. return true;
  36. }
  37.  
  38. // This routine prompts the user with
  39. // Operator: + - * / q for quit
  40. // If none of these are entered, this routine complains and
  41. // prompts the user again. Otherwise the operator is returned.
  42. char getOperator()
  43. {
  44. System.out.println("Enter operation: + - * / q (q ==> quit) : ");
  45. String input = scan.next();
  46. return 0;
  47. }
  48.  
  49. // This routine prompts the user for either operand1 or operand2
  50. // depending on the value of which. This routine uppercases the
  51. // input and calls convert_from_Roman to create an integer.
  52. // If the input is invalid ( negative return from convert_from_Roman)
  53. // then complain and prompt the user again.
  54. int getOperand(int which)
  55. {
  56. System.out.println("Enter operand" + which);
  57. String op = scan.next();
  58. return convert_from_Roman(op);
  59.  
  60. }
  61.  
  62.  
  63. // Routine to convert an integer to a Roman Numeral String.
  64. // When you do this routine, you might find it handy to
  65. // create a utility routine that looks like:
  66. //String addRomanDigit(String starting, int num, char digit)
  67. String convert_to_Roman(int value)
  68. {
  69. String roman = "";
  70. while (value > 0 && value < 4000)
  71. switch (value)
  72. {
  73. case 1000:
  74. roman +='M';
  75. case 500:
  76. roman +='D';
  77. case 100:
  78. roman +='C';
  79. case 50:
  80. roman +='L';
  81. case 10:
  82. roman +='X';
  83. case 5:
  84. roman +='V';
  85. case 1:
  86. roman +='I';
  87. break;
  88. }
  89. return roman;
  90. }
  91.  
  92.  
  93. // Convert Roman Numeral String to an integer. If the
  94. // Roman Numeral String is invalid, return -1.
  95. int convert_from_Roman(String value)
  96. {
  97. String s = value.toUpperCase();
  98. s = s.trim();
  99. int len = s.length();
  100. int total = 0;
  101.  
  102. for(int i = len; i > 0; i++)
  103. {
  104. char c = value.charAt(i);
  105. switch(c)
  106. {
  107. case 'M':
  108. total += 1000;
  109. case 'D':
  110. total += 500;
  111. case 'C':
  112. total += 100;
  113. case 'L':
  114. total += 50;
  115. case 'X':
  116. total += 10;
  117. case 'V':
  118. total += 5;
  119. case 'I':
  120. total += 1;
  121. break;
  122. }
  123. }
  124. return total;
  125. }
  126.  
  127. // Perform the arithmetic indicated by the operator (+ - * /)
  128. void doArithmetic(int operand1, int operand2, char operator)
  129. {
  130. switch (operator)
  131. {
  132. case '+':
  133. return;
  134. case '-':
  135. return;
  136. case '*':
  137. return;
  138. case '/':
  139. return;
  140. }
  141. }
  142. public static void main(String[] args)
  143. {
  144. romanCalculator rc = new romanCalculator();
  145. while (rc.doCalculation())
  146. {
  147. System.out.println(); // blank line
  148. }
  149. System.out.println("Finished Roman Computations");
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement