-
Notifications
You must be signed in to change notification settings - Fork 5
Serohin serhii hw 02 #39
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: main
Are you sure you want to change the base?
Changes from all commits
c1a42b8
2191db8
ae6cefa
8488069
7212170
4aee82f
733080e
db63f83
b2fb3e3
8768896
049237f
2c8d925
70b355a
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,59 @@ | ||||||
| #include <cstdint> | ||||||
| #include <cstdlib> | ||||||
| #include <iostream> | ||||||
|
|
||||||
| uint32_t value_digits_quantity(uint32_t value); | ||||||
| uint32_t value_summ_digits(uint32_t value); | ||||||
|
Owner
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. а зачем тут делать обьявление а внизу определение? |
||||||
|
|
||||||
| int main() { | ||||||
| uint32_t value{}; | ||||||
|
|
||||||
| std::cout << "Please, enter unsigned integer value" << std::endl; | ||||||
| std::cout << "-->"; | ||||||
| std::cin >> value; | ||||||
|
|
||||||
| uint32_t quantity = value_digits_quantity(value); | ||||||
|
|
||||||
| std::cout << "The value " << value << " consists of " << quantity << " digits" | ||||||
| << std::endl; | ||||||
|
|
||||||
| uint32_t summ = value_summ_digits(value); | ||||||
| std::cout << "Summ of digits is " << summ << std::endl; | ||||||
|
|
||||||
| auto average = static_cast<float>(summ) / quantity; | ||||||
| std::cout << "The average is " << average << std::endl; | ||||||
|
|
||||||
| return EXIT_SUCCESS; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @brief This function returns quantity of digits for given value | ||||||
| * | ||||||
| * @param value [input] Value for calculation | ||||||
| * @return uint32_t Quantity of digits | ||||||
| */ | ||||||
| uint32_t value_digits_quantity(uint32_t value) { | ||||||
| uint32_t quantity = 1; | ||||||
|
Owner
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.
Suggested change
|
||||||
|
|
||||||
| while (value /= 10) { | ||||||
| ++quantity; | ||||||
| } | ||||||
|
|
||||||
| return quantity; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @brief This function calculates summ for given value | ||||||
| * | ||||||
| * @param value [input] Given value for calculation | ||||||
| * @return uint32_t Summ | ||||||
| */ | ||||||
| uint32_t value_summ_digits(uint32_t value) { | ||||||
| uint32_t summ = 0; | ||||||
|
|
||||||
| for (; value; value /= 10) { | ||||||
|
Owner
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.
Suggested change
|
||||||
| summ += value % 10; | ||||||
| } | ||||||
|
|
||||||
| return summ; | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #include <bits/c++config.h> | ||
| #include <bits/stdint-uintn.h> | ||
|
Owner
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. это за такие магические заголовочные файлы? |
||
| #include <cmath> | ||
| #include <cstdint> | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <math.h> | ||
|
|
||
| /** | ||
| * @brief This constant sets quantity of digits of ticket number that was | ||
| * entered by user | ||
| * | ||
| */ | ||
| constexpr uint32_t ALLOWWED_TICKET_DIGITS_QUANTITY{6}; | ||
|
|
||
| /** | ||
| * @brief This constant defines a halves of ticket's nimber to check that ticket | ||
| * is lucky | ||
| * | ||
| */ | ||
| constexpr uint32_t TICKET_PART_TO_COMPARE{ALLOWWED_TICKET_DIGITS_QUANTITY / 2}; | ||
|
|
||
| uint32_t value_digits_quantity(uint32_t value); | ||
| bool value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ); | ||
| bool value_skip_n_digits(uint32_t *value, uint32_t n); | ||
|
|
||
| int main() { | ||
|
|
||
| uint32_t value{}; | ||
|
|
||
| std::cout << "Please, enter unsigned integer value" << std::endl; | ||
| std::cout << "-->"; | ||
| std::cin >> value; | ||
|
|
||
| uint32_t quantity = value_digits_quantity(value); | ||
|
|
||
| std::cout << "The value " << value << " consists of " << quantity << " digits" | ||
| << std::endl; | ||
|
|
||
| if (quantity != ALLOWWED_TICKET_DIGITS_QUANTITY) { | ||
| std::cout << "Value have to consist " << ALLOWWED_TICKET_DIGITS_QUANTITY | ||
| << " digits!" << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
|
|
||
| uint32_t part1{}; | ||
| uint32_t part2{}; | ||
|
|
||
| value_summ_n_digits(value, TICKET_PART_TO_COMPARE, &part1); | ||
|
Owner
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. а почему part1 не сделать как возвращаемое значение ? |
||
| std::cout << "part1: " << part1 << std::endl; | ||
| value_skip_n_digits(&value, TICKET_PART_TO_COMPARE); | ||
|
Owner
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. если вы тут изменяете |
||
| std::cout << "value: " << value << std::endl; | ||
| value_summ_n_digits(value, TICKET_PART_TO_COMPARE, &part2); | ||
| std::cout << "part2: " << part2 << std::endl; | ||
|
|
||
| /*Checking for ticket's lucky*/ | ||
| if (part1 == part2) { | ||
| std::cout << "My congratulations, You have got a lucky ticket!" | ||
| << std::endl; | ||
| } else { | ||
| std::cout << "No big deal, no luck with this ticket." << std::endl; | ||
| } | ||
|
|
||
| return EXIT_SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * @brief This function returns quantity of digits for given value | ||
| * | ||
| * @param value [input] Value for calculation | ||
| * @return uint32_t Quantity of digits | ||
| */ | ||
| uint32_t value_digits_quantity(uint32_t value) { | ||
| uint32_t quantity = 1; | ||
|
|
||
| while (value /= 10) { | ||
| ++quantity; | ||
| } | ||
|
|
||
| return quantity; | ||
| } | ||
|
|
||
| /** | ||
| * @brief This function calculates summ of first n digits for given value | ||
| * | ||
| * @param value [input] Value for calculation | ||
| * @param quantity [input] Quantity of digits for addition | ||
| * @param summ [input/output] Pointer to variable, where summ will be | ||
| * written | ||
| * @return true | ||
| * @return false | ||
| */ | ||
| bool value_summ_n_digits(uint32_t value, uint32_t quantity, uint32_t *summ) { | ||
|
|
||
| if (!summ || (value_digits_quantity(value) < quantity)) { | ||
| return 0; | ||
|
Owner
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. а почему не false? |
||
| } | ||
|
|
||
| *summ = 0; | ||
|
Owner
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 (quantity--) { | ||
| *summ += value % 10; | ||
| value /= 10; | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * @brief This function trimms n first digits for given value | ||
| * | ||
| * @param value [input/output] pointer to variable for trimming | ||
| * @param n [input] Number of first digits that will deleted from value | ||
| * @return true | ||
| * @return false | ||
| */ | ||
| bool value_skip_n_digits(uint32_t *value, uint32_t n) { | ||
|
Owner
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 (!value) { | ||
| return 0; | ||
| } | ||
|
|
||
| *value /= pow(10, n); | ||
|
|
||
| return 1; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||
| #include <bits/stdint-uintn.h> | ||||||
| #include <cmath> | ||||||
| #include <cstdbool> | ||||||
| #include <cstdint> | ||||||
| #include <cstdlib> | ||||||
| #include <iostream> | ||||||
|
|
||||||
| uint32_t value_digits_quantity(uint32_t value); | ||||||
| bool value_trim_the_last_digit(uint32_t *const value); | ||||||
| uint32_t value_get_the_last_digit(uint32_t value); | ||||||
|
|
||||||
| int value_reverce(int value); | ||||||
|
|
||||||
| int main() { | ||||||
|
|
||||||
| int value{}; | ||||||
|
|
||||||
| std::cout << "Please, enter integer value" << std::endl; | ||||||
| std::cout << "-->"; | ||||||
| std::cin >> value; | ||||||
|
|
||||||
| int reverse = value_reverce(value); | ||||||
|
|
||||||
| std::cout << "Reverse value is " << reverse << std::endl; | ||||||
|
|
||||||
| return EXIT_SUCCESS; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @brief This function returns quantity of digits for given value | ||||||
| * | ||||||
| * @param value Input value for calculation | ||||||
| * @return uint32_t Quantity of digits | ||||||
| */ | ||||||
| uint32_t value_digits_quantity(uint32_t value) { | ||||||
| uint32_t quantity = 1; | ||||||
|
|
||||||
| while (value /= 10) { | ||||||
| ++quantity; | ||||||
| } | ||||||
|
|
||||||
| return quantity; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @brief This function returns the last digit for given value | ||||||
| * | ||||||
| * @param value [input] Value for calculation | ||||||
| * @return uint32_t The last digit | ||||||
| */ | ||||||
| uint32_t value_get_the_last_digit(uint32_t value) { return (value % 10); } | ||||||
|
Owner
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. я не думаю что тут нужна функция для одной простой инструкции |
||||||
|
|
||||||
| /** | ||||||
| * @brief This function trims the last digit for given number | ||||||
| * | ||||||
| * @param value [input/output] Pointer to external variable | ||||||
| * @return true | ||||||
| * @return false | ||||||
| */ | ||||||
| bool value_trim_the_last_digit(uint32_t *const value) { | ||||||
| if (!value) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| *value /= 10; | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
Comment on lines
+60
to
+68
Owner
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. я не думаю что тут уместна функция. Мое мнение она тут явно лишняя, зачем наворачивать на простую строку |
||||||
|
|
||||||
| /** | ||||||
| * @brief This function changes the order of given number | ||||||
| * | ||||||
| * @param value [input] Number for calculation | ||||||
| * @param order [input] New order for given number | ||||||
| * @return uint32_t Number with changed order | ||||||
| */ | ||||||
| uint32_t value_change_order(uint32_t value, uint32_t order) { | ||||||
| return static_cast<uint32_t>(value * pow(10, order)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @brief This function returns value with reverse digits order | ||||||
| * | ||||||
| * @param value Value for calculation | ||||||
| * @return int Reverse value | ||||||
| */ | ||||||
| int value_reverce(int value) { | ||||||
| int reverse = 0; | ||||||
| bool is_signed = (value < 0); | ||||||
| // std::cout << "log: is signed = "<< is_signed << std::endl; | ||||||
|
|
||||||
| uint32_t abs_value = static_cast<uint32_t>(abs(value)); | ||||||
| // std::cout << "log: abs = "<< abs_value << std::endl; | ||||||
|
|
||||||
| uint32_t value_order = value_digits_quantity(abs_value); | ||||||
| // std::cout << "log: order = "<< value_order << std::endl; | ||||||
| // std::cout << "---------------"<< std::endl; | ||||||
|
|
||||||
| while (abs_value) { | ||||||
|
Owner
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.
Suggested change
это даст больше понимания что это целочисленная переменная а не bool переменная |
||||||
| auto last_digit = value_get_the_last_digit(abs_value); | ||||||
| // std::cout << "log: the last is = "<< last_digit << std::endl; | ||||||
|
|
||||||
| reverse += value_change_order(last_digit, --value_order); | ||||||
| // std::cout << "log: reverse = "<< reverse << std::endl; | ||||||
|
|
||||||
| value_trim_the_last_digit(&abs_value); | ||||||
| // std::cout << "log: abs = "<< abs_value << std::endl; | ||||||
| // std::cout << "---------------"<< std::endl; | ||||||
| } | ||||||
|
|
||||||
| return is_signed ? -reverse : reverse; | ||||||
| } | ||||||
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.
а зачем этот заголовочный файл?