From ed9e84dc227a2208c5be5344f04e8aa55fbc1148 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Wed, 17 Dec 2025 14:26:19 +0100 Subject: [PATCH] 1st challange done! --- 6th_day/1/main.cpp | 77 +++++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/6th_day/1/main.cpp b/6th_day/1/main.cpp index c48d2ed..f4a561b 100644 --- a/6th_day/1/main.cpp +++ b/6th_day/1/main.cpp @@ -9,37 +9,72 @@ vector> get_input(const string &FILE_NAME) { ifstream file(FILE_NAME); vector> result; - vector local_vec; - - size_t pos = 0; string s; + while (getline(file, s)) { - while ((pos = s.find(" ")) != string::npos) { + size_t pos; + size_t i = 0; + + while ((pos = s.find(' ')) != string::npos) { if (pos == 0) { s.erase(0, 1); continue; } - local_vec.push_back(s.substr(0, pos)); - s.erase(0, pos); - } - local_vec.push_back(s); - result.push_back(local_vec); - local_vec.erase(local_vec.end()); - } + string word = s.substr(0, pos); + s.erase(0, pos + 1); + if (i >= result.size()) { + result.emplace_back(); + } + + result[i].push_back(word); + i++; + } + if (!s.empty()) { + if (i >= result.size()) { + result.emplace_back(); + } + result[i].push_back(s); + } + } return result; } -int main() { - return 0; - const string &FILE_NAME = "example.txt"; - vector> input = get_input(FILE_NAME); - - for (vector vec : input) { - cout << "Vec: " << endl; - for (string s : vec) { - cout << s << endl; - ; +long long process_operation(vector &input) { + char operation = input.back()[0]; + input.pop_back(); + long long sum; + switch (operation) { + case '+': + sum = 0; + cout << "Now we are adding numbers: "; + for (string value : input) { + cout << value << " "; + sum += stoll(value); } + cout << endl; + break; + case '*': + sum = 1; + cout << "Now we are multiplying numbers: "; + for (string value : input) { + cout << value << " "; + sum *= stoll(value); + } + cout << endl; + break; } + cout << "The sum is " << sum << endl; + return sum; +} + +int main() { + const string FILE_NAME = "input.txt"; + vector> input = get_input(FILE_NAME); + long long sum = 0; + for (vector group : input) { + sum += process_operation(group); + } + cout << sum; + return 0; }