-
Notifications
You must be signed in to change notification settings - Fork 3
Пестов Владимир Алексеевич #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include <iostream> | ||
| #include <random> | ||
|
|
||
| int16_t GenerateRandomNumber() { | ||
| std::random_device random; | ||
|
|
||
| std::default_random_engine engine(random()); | ||
| std::uniform_int_distribution<int16_t> uniform_dist(0, 9); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Отлично, что используете более современный подход. |
||
|
|
||
| return uniform_dist(engine); | ||
| } | ||
|
|
||
| int16_t ReceiveUserInput() { | ||
| int16_t inp{}; | ||
|
|
||
| std::cout << "Guess a number from 0 to 9: "; | ||
| std::cin >> inp; | ||
|
|
||
| return inp; | ||
| } | ||
|
|
||
| int main() { | ||
| auto random_number = GenerateRandomNumber(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно сделать константой |
||
|
|
||
| while (true) { | ||
| auto user_input = ReceiveUserInput(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В рамках одной итерации тоже может быть константой) |
||
|
|
||
| if (user_input == random_number) { | ||
| std::cout << "Correct!" << std::endl; | ||
| break; | ||
| } | ||
|
|
||
| std::cout << "Wrong!" << std::endl; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А почему для хранения чисел от 0 до 9 вы выбрали именно 2 байтное знаковое? Если эконоить на размере, то можно же выбрать более подходящий тип
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Потому что int8_t начинал парситься в char, что заставляло
либо использовать
inp - '0'в качестве числа, либо принимать int и делать static_castОба варианта показались не очень красивыми, решил оставить этот (хотя, конечно, в таком виде неочевидно, почему 16)