Created
December 19, 2020 14:42
-
-
Save jin-x/900a2e926b90293c06e6f83e2d200616 to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #252
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <forward_list> | |
using std::cout; | |
using std::endl; | |
using std::string; | |
using std::forward_list; | |
void show_list(const string& title, const forward_list<int>& num) | |
{ | |
cout << title << ": { "; | |
for (int x : num) { | |
cout << x << ' '; | |
} | |
cout << "}\n"; | |
} | |
void inc_num_list(forward_list<int>& num) | |
{ | |
auto not9 = num.begin(); // на случай, если в списке все 9-ки | |
for (auto it = num.begin(); it != num.end(); ++it) { | |
if (*it != 9) { not9 = it; } // последний элемент, не равный 9 | |
} | |
if (*not9 == 9) { // если в списке все девятки | |
num.push_front(1); // и добавляем 1 к началу | |
} | |
for (auto it = not9; it != num.end(); ++it) { | |
if (*it == 9) { *it = 0; } // 9-ки преобразуем в нули*/ | |
else { ++*it; } // остальные увеличиваем числа на 1 | |
} | |
} | |
int main() | |
{ | |
forward_list<int> num = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; | |
show_list("\nBefore", num); | |
inc_num_list(num); | |
show_list(" After", num); | |
num = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }; | |
show_list("\nBefore", num); | |
inc_num_list(num); | |
show_list(" After", num); | |
num = { 7, 5, 9, 8, 9 }; | |
show_list("\nBefore", num); | |
inc_num_list(num); | |
show_list(" After", num); | |
num = { 1, 2, 4, 9, 9, 9, 9 }; | |
show_list("\nBefore", num); | |
inc_num_list(num); | |
show_list(" After", num); | |
num = { 9, 9, 9 }; | |
show_list("\nBefore", num); | |
inc_num_list(num); | |
show_list(" After", num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment