Advertisement
MadCortez

Untitled

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