Advertisement
MadCortez

Untitled

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