Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <regex>
- using namespace std;
- vector<string> makeSameLength(string num1, string num2) {
- int lengthDiff = num1.length() - num2.length();
- int zeroesCount = abs(lengthDiff);
- for(int i = 0; i < zeroesCount; i++) {
- if(num1.length() < num2.length())
- num1 = "0" + num1;
- else
- num2 = "0" + num2;
- }
- vector<string> arr = {num1, num2};
- return arr;
- }
- string sum(string num1, string num2) {
- vector<string> nums = makeSameLength(num1, num2);
- num1 = nums[0];
- num2 = nums[1];
- int memory = 0;
- string sequence = "";
- for(int i = num1.length() - 1; i >= 0; i--) {
- int el1 = num1[i]-'0';
- int el2 = num2[i]-'0';
- int result = el1 + el2 + memory;
- int units = result % 10;
- if (i != 0) {
- memory = result / 10;
- sequence = to_string(units) + sequence;
- } else
- sequence = to_string(result) + sequence;
- }
- return sequence;
- }
- string getNumberFromSeq(vector<string> seq) {
- string result = "0";
- for(string el : seq) {
- if(el.find(" ") != -1)
- el = el.erase(0, 1);
- result = sum(result, el);
- }
- return result;
- }
- void showSequence(vector<string> seq) {
- for(string el : seq)
- cout << el;
- cout << endl;
- }
- vector<string> split(string str, string delimiter) {
- vector<string> result;
- int pos = 0;
- while(pos != -1) {
- pos = str.find(delimiter);
- result.push_back(str.substr(0, pos));
- str.erase(0, pos + delimiter.length());
- }
- return result;
- }
- int main() {
- cout << "Данная программа находит последовательность, представляющую сумму двух натуральных чисел, заданных своими последовательностями\n" << endl;
- bool notCorrect = true;
- bool noException;
- while(notCorrect) {
- noException = true;
- cout << "Введите путь до файла, в котором находятся последовательности чисел" << endl;
- string filePath;
- cin >> filePath;
- if(filePath.find(".txt") == -1)
- filePath += ".txt";
- string seq1 = "";
- string seq2 = "";
- string num1 = "0";
- string num2 = "0";
- ifstream inputFile;
- inputFile.open(filePath);
- if(inputFile) {
- getline(inputFile, seq1);
- getline(inputFile, seq2);
- inputFile.close();
- } else {
- cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла." << endl;
- noException = false;
- }
- if(noException) {
- vector<string> arr1 = split(seq1, ",");
- vector<string> arr2 = split(seq2, ",");
- cout << "Последовательности, записанные в файле:" << endl;
- showSequence(arr1);
- showSequence(arr2);
- num1 = getNumberFromSeq(arr1);
- num2 = getNumberFromSeq(arr2);
- cout << "Получившиеся числа: " << num1 << " и " << num2 << endl;
- string sequence = sum(num1, num2);
- regex pattern("^0");
- sequence = regex_replace(sequence, pattern, "");
- regex numPattern("^\\d+$");
- if(regex_match(num1, numPattern) && regex_match(num2, numPattern)) {
- string resultStr = "Результат: " + sequence;
- cout << resultStr << endl;
- ofstream outputFile;
- outputFile.open("output.txt");
- outputFile << resultStr;
- cout << "Результат сохранен в файл output.txt" << endl;
- notCorrect = false;
- } else {
- cout << "Вы должны вводить последовательность для натуральных чисел" << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement