Advertisement
Hugo24

4.6

Jan 20th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main() {
  6. char input[10];
  7. char rimDigits[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
  8. int c, index, i, sum = 0, arabDigits[] = {1, 5, 10, 50, 100, 500, 1000};
  9. cout << "4.6: Введите римское число: " << endl;
  10. cin >> input;
  11. c = strlen(input);
  12.  
  13. if (c >= 4) {
  14. if (input[c - 1] == input[c - 2] && input[c - 3] && input[c - 4]) {
  15. printf("Ошибка в написании!\n");
  16. return 0;
  17. }
  18. }
  19.  
  20. i = c - 1;
  21. for (int j = 0; j < 7; j++) {
  22. if (input[i] == rimDigits[j]) {
  23. index = j;
  24. sum = arabDigits[j];
  25. }
  26. }
  27.  
  28. for (i = i - 1; i >= 0; i--) {
  29.  
  30. for (int j = 0; j < 7; j++) {
  31. if (input[i] == rimDigits[j])
  32. if (j >= index) {
  33. sum += arabDigits[j];
  34. index = j;
  35. }
  36. else if (j < index) {
  37. sum -= arabDigits[j];
  38. index = j;
  39. }
  40. }
  41. }
  42. printf("Число = %d\n", sum);
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement