wkingsnorth

C++

Nov 3rd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. // Rom nums c++.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. char input[99];
  9. //bool op = false;
  10. int i = 0;
  11. int num[99];
  12. char op;
  13.  
  14.  
  15.  
  16. int inp(char input[],int num[], int i)
  17. {
  18. //Converts array values to numbers
  19. int tot = 0; //total
  20. for (i = 0; i <= 99; i++)
  21. {
  22. switch (input[i])
  23. {
  24. case 'm':
  25. case 'M':
  26. num[i] = 1000;
  27. break;
  28. case 'D':
  29. case 'd':
  30. num[i] = 500;
  31. break;
  32. case 'C':
  33. case 'c':
  34. num[i] = 100;
  35. break;
  36. case 'L':
  37. case 'l':
  38. num[i] = 50;
  39. break;
  40. case 'X':
  41. case 'x':
  42. num[i] = 10;
  43. break;
  44. case 'V':
  45. case 'v':
  46. num[i] = 5;
  47. break;
  48. case 'I':
  49. case 'i':
  50. num[i] = 1;
  51. break;
  52. case 0:
  53. num[i] = 0;
  54. break;
  55. default:
  56. cout << "You have not input a valid numeral. Please input a Roman numeral using the following numbers in descending order. Maximum value 4000."
  57. << endl << "M = 1000 \n D = 500 \n C = 100 \n L = 50 \n X = 10 \n V = 5 \n I = 1\n";
  58. tot = -1; goto flop;
  59. break;
  60.  
  61. }
  62.  
  63. }
  64. //Code to output all input numerals and all the numbers they mean
  65. //for (i = 0; i <= 99; i++)
  66. //{
  67. // cout << input[i] << " - " << num[i] << endl;
  68. //}
  69.  
  70. //Checks for descending order
  71. for (i = 0; i <= 99; i++)
  72. {
  73. if (num[i] < num[i + 1])
  74. {
  75. cout << "The numeral must be input in descending order." << endl;
  76. tot = -1; goto flop;
  77. }
  78. }
  79. //checks for use of smaller numbers where a bigger number could be used
  80. int I = 0;
  81. int V = 0;
  82. int X = 0;
  83. int L = 0;
  84. int C = 0;
  85. int D = 0;
  86. int M = 0;
  87. for (i = 0; i <= 99; i++)
  88. {
  89.  
  90. if (num[i] == 1)
  91. {
  92. I = I + 1;
  93. }
  94. if (num[i] == 5)
  95. {
  96. V = V + 1;
  97. }
  98. if (num[i] == 10)
  99. {
  100. X = X + 1;
  101. }
  102. if (num[i] == 50)
  103. {
  104. L = L + 1;
  105. }
  106. if (num[i] == 100)
  107. {
  108. C = C + 1;
  109. }
  110. if (num[i] == 500)
  111. {
  112. D = D + 1;
  113. }
  114. if (num[i] == 1000)
  115. {
  116. M = M + 1;
  117. }
  118. }
  119. if ((I >= 5) || (V >= 2) || (X >= 5) || (L >= 2) || (C >= 5) || (D >= 2))
  120. {
  121. cout << "You have input more numerals than needed - for example, 5 'I's - where it would be appropriate to use a 'V' instead" << endl;
  122. goto flop; tot = -1;
  123. }
  124. //adds all the numbers up
  125.  
  126.  
  127. for (i = 0; i <= 99; i++)
  128. {
  129. tot = tot + num[i];
  130. }
  131.  
  132. cout << "Your number is: " << tot << endl;
  133. flop:
  134. return(tot);
  135.  
  136.  
  137.  
  138. //Asks if the user wishes to restart the program
  139. // cout << "Input 'start' to restart, or anything else to quit";
  140. // char end[99];
  141. // cin >> end; cout << endl;
  142. // if (end == "start"){ goto start; }
  143. // return 0;
  144. }
  145.  
  146.  
  147.  
  148.  
  149. int main()
  150. {
  151. //second input
  152. start:
  153. cout << "Please input your first number and press enter" << endl; //Prints a request for the input, and then starts a new line
  154. start2:
  155. cout << "Input: "; cin >> input; //Takes the input
  156. int num1 = inp(input, num, 0);
  157. if (num1 == -1) {goto start;}
  158.  
  159. //first input
  160. cout << "Please input your second number and press enter" << endl; //Prints a request for the input, and then starts a new line
  161. cout << "Input: "; cin >> input; //Takes the input
  162. int num2 = inp(input, num, 0);
  163. if (num2 == -1) {goto start;}
  164.  
  165. oper:
  166. cout << "Would you like to add or subtract?" << endl << "Input 'a' to add or 's' to subtract'" << endl;
  167. cin >> op;
  168. int total = 0;
  169. switch (op)
  170. {
  171. case 'a':
  172. case 'A':
  173. total = num1 + num2;
  174. break;
  175. case 's':
  176. case 'S':
  177. total = num1 - num2;
  178. if (total <= 0) {cout <<"The answer cannot be zero or negative." << endl << "The second number is subtracted from the first."; goto start;}
  179. break;
  180.  
  181. }
  182. cout << "The answer is " << total;
  183. char out[20];
  184. for (i = 0; total<=0; i++)
  185. {
  186. if(total>=1000) {total = total-1000; out[i] = 'M';}
  187. else if(total>=500) {total = total-500; out[i] = 'D';}
  188. else if(total>=100) {total = total-100; out[i] = 'C';}
  189. else if(total>=50) {total = total-50; out[i] = 'L';}
  190. else if(total>=10) {total = total-10; out[i] = 'X';}
  191. else if(total>=5) {total = total-5; out[i] = 'V';}
  192. else if(total>=1) {total = total-1; out[i] = 'I';}
  193. else {out[i] = ' ';}
  194.  
  195. }
  196. cout << " or ";
  197. for(i = 1; i <=20, i++)
  198. {cout << out[i];}
  199. cout endl;
  200. goto start;
  201. }
  202.  
  203.  
  204.  
  205.  
  206. int _tmain(int argc, _TCHAR* argv[])
  207. {
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment