Advertisement
Alyks

Untitled

Oct 23rd, 2019
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <regex>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> makeSameLength(string num1, string num2) {
  9.     int lengthDiff = num1.length() - num2.length();
  10.     int zeroesCount = abs(lengthDiff);
  11.     for(int i = 0; i < zeroesCount; i++) {
  12.         if(num1.length() < num2.length())
  13.             num1 = "0" + num1;
  14.         else
  15.             num2 = "0" + num2;
  16.     }
  17.     vector<string> arr = {num1, num2};
  18.     return arr;
  19. }
  20.  
  21. string sum(string num1, string num2) {
  22.     vector<string> nums = makeSameLength(num1, num2);
  23.     num1 = nums[0];
  24.     num2 = nums[1];
  25.     int memory = 0;
  26.     string sequence = "";
  27.     for(int i = num1.length() - 1; i >= 0; i--) {
  28.         int el1 = num1[i]-'0';
  29.         int el2 = num2[i]-'0';
  30.         int result = el1 + el2 + memory;
  31.         int units = result % 10;
  32.         if (i != 0) {
  33.             memory = result / 10;
  34.             sequence = to_string(units) + sequence;
  35.         } else
  36.             sequence = to_string(result) + sequence;
  37.     }
  38.     return sequence;
  39. }
  40.  
  41. string getNumberFromSeq(vector<string> seq) {
  42.     string result = "0";
  43.     for(string el : seq) {
  44.         if(el.find(" ") != -1)
  45.             el = el.erase(0, 1);
  46.         result = sum(result, el);
  47.     }
  48.     return result;
  49. }
  50.  
  51. void showSequence(vector<string> seq) {
  52.     for(string el : seq)
  53.         cout << el;
  54.     cout << endl;
  55. }
  56.  
  57. vector<string> split(string str, string delimiter) {
  58.     vector<string> result;
  59.     int pos = 0;
  60.     while(pos != -1) {
  61.         pos = str.find(delimiter);
  62.         result.push_back(str.substr(0, pos));
  63.         str.erase(0, pos + delimiter.length());
  64.     }
  65.     return result;
  66. }
  67.  
  68. int main() {
  69.     cout << "Данная программа находит последовательность, представляющую сумму двух натуральных чисел, заданных своими последовательностями\n" << endl;
  70.     bool notCorrect = true;
  71.     bool noException;
  72.     while(notCorrect) {
  73.         noException = true;
  74.         cout << "Введите путь до файла, в котором находятся последовательности чисел" << endl;
  75.         string filePath;
  76.         cin >> filePath;
  77.         if(filePath.find(".txt") == -1)
  78.             filePath += ".txt";
  79.  
  80.         string seq1 = "";
  81.         string seq2 = "";
  82.         string num1 = "0";
  83.         string num2 = "0";
  84.  
  85.         ifstream inputFile;
  86.         inputFile.open(filePath);
  87.         if(inputFile) {
  88.             getline(inputFile, seq1);
  89.             getline(inputFile, seq2);
  90.             inputFile.close();
  91.         } else {
  92.             cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла." << endl;
  93.             noException = false;
  94.         }
  95.  
  96.         if(noException) {
  97.             vector<string> arr1 = split(seq1, ",");
  98.             vector<string> arr2 = split(seq2, ",");
  99.             cout << "Последовательности, записанные в файле:" << endl;
  100.             showSequence(arr1);
  101.             showSequence(arr2);
  102.             num1 = getNumberFromSeq(arr1);
  103.             num2 = getNumberFromSeq(arr2);
  104.             cout << "Получившиеся числа: " << num1 << " и " << num2 << endl;
  105.             string sequence = sum(num1, num2);
  106.             regex pattern("^0");
  107.             sequence = regex_replace(sequence, pattern, "");
  108.             regex numPattern("^\\d+$");
  109.             if(regex_match(num1, numPattern) && regex_match(num2, numPattern)) {
  110.                 string resultStr = "Результат: " + sequence;
  111.                 cout << resultStr << endl;
  112.                 ofstream outputFile;
  113.                 outputFile.open("output.txt");
  114.                 outputFile << resultStr;
  115.                 cout << "Результат сохранен в файл output.txt" << endl;
  116.                 notCorrect = false;
  117.             } else {
  118.                 cout << "Вы должны вводить последовательность для натуральных чисел" << endl;
  119.             }
  120.         }
  121.     }
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement