Romasmi / cpp-practice

0 stars 0 forks source link

Замечания по программе Radix #5

Closed alexey-malov closed 6 years ago

alexey-malov commented 6 years ago
1>c:\teaching\ips\2018\oop\smirnov\cpp_oop_practice\lab_1\task_2\task_2.cpp(77): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
1>c:\teaching\ips\2018\oop\smirnov\cpp_oop_practice\lab_1\task_2\task_2.cpp(88): warning C4244: 'argument': conversion from 'double' to 'const unsigned int', possible loss of data
alexey-malov commented 6 years ago
unsigned int GetSum(const unsigned int a, const unsigned b)
{
    if (a >= numeric_limits<unsigned int>::max() - b)
    {
        throw overflow_error("Input number overflow");
        return 0;
    }
alexey-malov commented 6 years ago
// Будет ли переполнение при умножении беззнаковых чисел а и b
if (a == 0 || (std::numeric_limits<unsigned>::max() / a >= b))
{
  // a * b не вызовет переполнения
}
else
{
   // переполнение   
}
alexey-malov commented 6 years ago
alexey-malov commented 6 years ago
Romasmi commented 6 years ago

При входном числе FFFFFFFF (4294967295) программа выдаст ошибку, т.к. numeric_limits::max() = 4294967295 (Проверено выводов в output инструкцией cout << numeric_limits::max() << endl;)

alexey-malov commented 6 years ago
    if (str.length() <= 0)
    {
        wasError = true;
        return 0;
    }
alexey-malov commented 6 years ago
unsigned int GetMul(const unsigned int a, const unsigned b)
{
    if (a == 0 || ((numeric_limits<unsigned int>::max() / b + 1) >  a))
alexey-malov commented 6 years ago
        unsigned int currentDischargeNumber = LetterToNumber(str[i]);
alexey-malov commented 6 years ago
    return InInterval(letter, '0', '9') ? static_cast<int>(letter - '0') : static_cast<int>(letter) - 55;
const char AMinus10 = 'A' - 10;
alexey-malov commented 6 years ago
        unsigned int currentDischargeNumber = LetterToNumber(str[i]);
        if (currentDischargeNumber > radix)
        {
            wasError = true;
            return 0;
        }
alexey-malov commented 6 years ago
alexey-malov commented 6 years ago
Romasmi commented 6 years ago

Fixed by commit https://github.com/Romasmi/cpp_oop_practice/commit/95c9f55c466bac6616b498d881d2029726163fd9

alexey-malov commented 6 years ago
bool ValidStringNumber(string number)
{
    try
    {
        int tempNumber = stoi(number);
    }
    catch (const exception& e)
    {
        return false;
    }
}
alexey-malov commented 6 years ago