Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions HW_02_01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <cstdint>
#include <cstdlib>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем этот заголовочный файл?

#include <iostream>

uint32_t value_digits_quantity(uint32_t value);
uint32_t value_summ_digits(uint32_t value);
Copy link
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uint32_t quantity = 1;
uint32_t quantity{1};


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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (; value; value /= 10) {
while ((value /= 10) > 0) {

summ += value % 10;
}

return summ;
}
125 changes: 125 additions & 0 deletions HW_02_02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <bits/c++config.h>
#include <bits/stdint-uintn.h>
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если вы тут изменяете value то почему ее просто не менять в value_summ_n_digits?

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а почему не false?

}

*summ = 0;
Copy link
Owner

Choose a reason for hiding this comment

The 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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему не ссылка ?

if (!value) {
return 0;
}

*value /= pow(10, n);

return 1;
}
112 changes: 112 additions & 0 deletions HW_02_03.cpp
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); }
Copy link
Owner

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я не думаю что тут уместна функция. Мое мнение она тут явно лишняя, зачем наворачивать на простую строку
value /= 10; целую функцию


/**
* @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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (abs_value) {
while (abs_value > 0) {

это даст больше понимания что это целочисленная переменная а не 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;
}
Loading