Advertisement
MadCortez

Untitled

Nov 19th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <set>
  5.  
  6. const char SYM[7] = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
  7. const int NUM[7] = { 1, 5, 10, 50, 100, 500, 1000 };
  8. const int USE_CONSOLE = 1;
  9. const int USE_FILE = 2;
  10.  
  11. using namespace std;
  12.  
  13. string getUserInputFromConsole();
  14. string getUserInputFromFile(string path);
  15. bool checkPath(string path);
  16. string getUserInputPath();
  17. int inputMethod();
  18. void printInConsole(int value, string s);
  19. string getUserOutputPath();
  20. void printInFile(string path, int value, string s);
  21. int outputMethod();
  22. void start();
  23. void printTask();
  24. string userInput();
  25. void resultPrint(int value, string s);
  26.  
  27. string getUserInputFromConsole() {
  28.     string s;
  29.     cout << "Введите строку, содержащую до 4х цифровых символов" << endl;
  30.     cin >> s;
  31.     return s;
  32. }
  33.  
  34. string getUserInputFromFile(string path) {
  35.     string s;
  36.     ifstream file(path);
  37.     file.open(path);
  38.     file.clear();
  39.     file >> s;
  40.     file.close();
  41.     return s;
  42. }
  43.  
  44. bool checkPath(string path) {
  45.     ifstream file(path);
  46.     if (file.is_open()) {
  47.         cout << path << " найден" << endl;
  48.         return true;
  49.     }
  50.     else {
  51.         cout << path << " не найден" << endl;
  52.         return false;
  53.     }
  54. }
  55.  
  56. string getUserInputPath() {
  57.     string path;
  58.     bool isNotValid = false;
  59.     do {
  60.         cout << "Введите абсолютный путь к файлу с входными данными" << endl;
  61.         cin >> path;
  62.     } while (!checkPath(path));
  63.     return path;
  64. }
  65.  
  66. int inputMethod() {
  67.     int method;
  68.     cout << "Каким способом хотите ввести данные?" << endl;
  69.     cout << "1 - с помощью консоли" << endl;
  70.     cout << "2 - с помощью файла" << endl;
  71.     do {
  72.         cin >> method;
  73.         if (method != 1 && method != 2)
  74.             cout << "Введите 1 или 2";
  75.     } while (method != 2 && method != 1);
  76.     return method;
  77. }
  78.  
  79. void printInConsole(int value, string s) {
  80.     cout << "Введённое число: " << value << endl;
  81.     cout << "Данное число в римской системе счисления: " << s;
  82. }
  83.  
  84. string getUserOutputPath() {
  85.     string path;
  86.     cout << "Введите абсолютный путь к файлу для вывода результата" << endl;
  87.     cin >> path;
  88.     cout << "Результат работа помещён в файл";
  89.     return path;
  90. }
  91.  
  92. void printInFile(string path, int value, string s) {
  93.     ofstream file(path);
  94.     file << "Введённое число: " << value << endl;
  95.     file << "Данное число в римской системе счисления: " << s;
  96. }
  97.  
  98. int outputMethod() {
  99.     int method;
  100.     cout << "Куда хотите вывести результат?" << endl;
  101.     cout << "1 - в консоль" << endl;
  102.     cout << "2 - в файл" << endl;
  103.     do {
  104.         cin >> method;
  105.         if (method != 1 && method != 2)
  106.             cout << "Введите 1 или 2";
  107.     } while (method != 2 && method != 1);
  108.     return method;
  109. }
  110.  
  111. string deleteNotSym(string s) {
  112.     const set<char> SYM = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  113.     int i = 0;
  114.     while (i < s.length()) {
  115.         if (s[i] < '0' || s[i] > '9')
  116.             s.erase(i, 1);
  117.         else
  118.             i++;
  119.     }
  120.     return s;
  121. }
  122.  
  123. string convertToRim(int a) {
  124.     string s;
  125.     int i = 6;
  126.     while (a > 0) {
  127.         while (a / NUM[i] > 0) {
  128.             a -= NUM[i];
  129.             s += SYM[i];
  130.         }
  131.     i--;
  132.     }
  133.     return s;
  134. }
  135.  
  136. string check4Same(string s) {
  137.     int i = 0;
  138.     int temp;
  139.     while (i < s.length() - 2) {
  140.         if (s[i] == s[i + 1] && s[i + 1] == s[i + 2] && s[i + 2] == s[i + 3]) {
  141.             for (int j = 0; j < 7; j++)
  142.                 if (SYM[j] == s[i])
  143.                     temp = j;
  144.             s.erase(i + 1, 3);
  145.             s.insert(i + 1, 1, SYM[temp + 1]);
  146.         }
  147.         i++;
  148.     }
  149.     return s;
  150. }
  151.  
  152. string fixNotValid(string s) {
  153.     int i = 0;
  154.     int temp, temp1;
  155.     while (i < s.length() - 1) {
  156.         if (s[i] == s[i + 2]) {
  157.             for (int j = 0; j < 7; j++)
  158.                 if (SYM[j] == s[i])
  159.                     temp = j;
  160.             for (int j = 0; j < 7; j++)
  161.                 if (SYM[j] == s[i + 1])
  162.                     temp1 = j;
  163.             if (temp == temp1 + 1) {
  164.                 s.erase(i, 3);
  165.                 s.insert(i, 1, SYM[temp1]);
  166.                 s.insert(i + 1, 1, SYM[temp + 1]);
  167.             }
  168.         }
  169.         i++;
  170.     }
  171.     return s;
  172. }
  173.  
  174. string getUserInput() {
  175.     string s;
  176.     short method = inputMethod();
  177.     if (method == USE_CONSOLE)
  178.         s = getUserInputFromConsole();
  179.     else {
  180.         string path = getUserInputPath();
  181.         s = getUserInputFromFile(path);
  182.     }
  183.     return s;
  184. }
  185.  
  186. void resultPrint(int value, string s) {
  187.     short method = outputMethod();
  188.     if (method == 1)
  189.         printInConsole(value, s);
  190.     else {
  191.         string path = getUserOutputPath();
  192.         printInFile(path, value, s);
  193.     }
  194. }
  195.  
  196. void printTask() {
  197.     cout << "Данная программа переводит введёное число в римскую систему счисления" << endl;
  198. }
  199.  
  200. void start() {
  201.     string s;
  202.     set<int> a;
  203.     int value;
  204.     printTask();
  205.     s = getUserInput();
  206.     s = deleteNotSym(s);
  207.     value = stoi(s);
  208.     s = convertToRim(value);
  209.     s = check4Same(s);
  210.     s = fixNotValid(s);
  211.     resultPrint(value, s);
  212. }
  213.  
  214. int main()
  215. {
  216.     setlocale(LC_ALL, "Russian");
  217.     start();
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement