Какие случаи я не разобрал? [code] [code]
- User
Какие случаи я не разобрал?
//rational.cpp #include "rational.h" #include <string> #include <cstdlib> const int DIGITS = 10; void Rational::Set(int64_t numer, int64_t denom) { if (denom == 0) { throw RationalDivisionByZero{}; } int64_t del = std::gcd(std::abs(numer), std::abs(denom)); numer_ = static_cast<int>(numer / del); denom_ = static_cast<int>(denom / del); if (denom_ < 0) { numer_ *= -1; denom_ *= -1; } } Rational::Rational() { numer_ = 0; denom_ = 1; } Rational::Rational(int value) : numer_(value) { // NOLINT denom_ = 1; } Rational::Rational(int numer, int denom) { numer_ = numer; denom_ = denom; Set(numer_, denom_); } int Rational::GetNumerator() const { return numer_; } int Rational::GetDenominator() const { return denom_; } void Rational::SetNumerator(int value) { numer_ = value; Set(numer_, denom_); } void Rational::SetDenominator(int value) { if (value == 0) { throw RationalDivisionByZero{}; } denom_ = value; Set(numer_, denom_); } int64_t Mul(int a, int b) { return static_cast<int64_t>(a) * static_cast<int64_t>(b); } void Rational::Normalize(int64_t numer, int64_t denom) { Set(numer, denom); } Rational& operator+=(Rational& lhs, const Rational& rhs) { int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetDenominator()) + Mul(lhs.GetDenominator(), rhs.GetNumerator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetDenominator()); lhs.Normalize(new_numer, new_denom); return lhs; } Rational& operator*=(Rational& lhs, const Rational& rhs) { int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetNumerator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetDenominator()); lhs.Normalize(new_numer, new_denom); return lhs; } Rational& operator++(Rational& ratio) { ratio.Normalize(static_cast<int64_t>(ratio.GetNumerator()) + ratio.GetDenominator(), ratio.GetDenominator()); return ratio; } Rational& operator--(Rational& ratio) { ratio.Normalize(static_cast<int64_t>(ratio.GetNumerator()) - ratio.GetDenominator(), ratio.GetDenominator()); return ratio; } std::istream& operator>>(std::istream& is, Rational& ratio) { std::string s; int64_t numer = 0; int64_t denom = 0; char slash = '/'; is >> s; bool is_negative = (s[0] == '-'); bool is_denom = false; if (is_negative) { s.erase(0, 1); } for (size_t i = 0; i < s.size(); ++i) { if (s[i] == slash) { is_denom = true; } else { if (!is_denom) { numer = numer * DIGITS + (s[i] - '0'); } else { denom = denom * DIGITS + (s[i] - '0'); } } } if (is_negative) { numer *= -1; } if (!is_denom) { denom = 1; } ratio.Normalize(numer, denom); return is; } Rational operator+(const Rational& ratio) { return ratio; } Rational operator-(const Rational& ratio) { return Rational(-ratio.GetNumerator(), ratio.GetDenominator()); } Rational& operator-=(Rational& lhs, const Rational& rhs) { int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetDenominator()) - Mul(lhs.GetDenominator(), rhs.GetNumerator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetDenominator()); lhs.Normalize(new_numer, new_denom); return lhs; } Rational& operator/=(Rational& lhs, const Rational& rhs) { if (rhs.GetNumerator() == 0) { throw RationalDivisionByZero{}; } int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetDenominator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetNumerator()); lhs.Normalize(new_numer, new_denom); return lhs; } Rational operator+(const Rational& lhs, const Rational& rhs) { int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetDenominator()) + Mul(lhs.GetDenominator(), rhs.GetNumerator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetDenominator()); Rational ans; ans.Normalize(new_numer, new_denom); return ans; } Rational operator-(const Rational& lhs, const Rational& rhs) { int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetDenominator()) - Mul(lhs.GetDenominator(), rhs.GetNumerator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetDenominator()); Rational ans; ans.Normalize(new_numer, new_denom); return ans; } Rational operator*(const Rational& lhs, const Rational& rhs) { int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetNumerator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetDenominator()); Rational ans; ans.Normalize(new_numer, new_denom); return ans; } Rational operator/(const Rational& lhs, const Rational& rhs) { if (rhs.GetNumerator() == 0) { throw RationalDivisionByZero{}; } int64_t new_numer = Mul(lhs.GetNumerator(), rhs.GetDenominator()); int64_t new_denom = Mul(lhs.GetDenominator(), rhs.GetNumerator()); Rational ans; ans.Normalize(new_numer, new_denom); return ans; } Rational operator++(Rational& ratio, int) { Rational temp = ratio; ++ratio; return temp; } Rational operator--(Rational& ratio, int) { Rational temp = ratio; --ratio; return temp; } bool operator<(const Rational& lhs, const Rational& rhs) { return Mul(lhs.GetNumerator(), rhs.GetDenominator()) < Mul(lhs.GetDenominator(), rhs.GetNumerator()); } bool operator>(const Rational& lhs, const Rational& rhs) { return Mul(lhs.GetNumerator(), rhs.GetDenominator()) > Mul(lhs.GetDenominator(), rhs.GetNumerator()); } bool operator<=(const Rational& lhs, const Rational& rhs) { return Mul(lhs.GetNumerator(), rhs.GetDenominator()) <= Mul(lhs.GetDenominator(), rhs.GetNumerator()); } bool operator>=(const Rational& lhs, const Rational& rhs) { return Mul(lhs.GetNumerator(), rhs.GetDenominator()) >= Mul(lhs.GetDenominator(), rhs.GetNumerator()); } bool operator==(const Rational& lhs, const Rational& rhs) { return Mul(lhs.GetNumerator(), rhs.GetDenominator()) == Mul(lhs.GetDenominator(), rhs.GetNumerator()); } bool operator!=(const Rational& lhs, const Rational& rhs) { return Mul(lhs.GetNumerator(), rhs.GetDenominator()) != Mul(lhs.GetDenominator(), rhs.GetNumerator()); } std::ostream& operator<<(std::ostream& os, const Rational& ratio) { if (ratio.GetDenominator() == 1) { os << ratio.GetNumerator(); } else { os << ratio.GetNumerator() << '/' << ratio.GetDenominator(); } return os; } - Assistant
Разбор вашего кода показывает, что вы имеете дело с реализацией класса
Rational, который представляет рациональные числа и поддерживает множество операторов и методов. Однако есть несколько случаев и моментов, которые могут быть не учтены или требуют дополнительного внимания: - User
как написать конструктор структуры в другом файле?
struct Node { int32_t value; Node* previous; Node(int32_t value_, Node* previous_); }; - Assistant
Для реализации конструктора структуры
Nodeв отдельном файле, вам нужно следовать принципу разделения объявления и определения, как это делается с функциями и методами классов в C++. Вот пошаговый процесс:- Объявление структуры и конструктора в заголовочном файле (
node.h):
// node.h #pragma once #include <cstdint> struct Node { int32_t value; Node* previous; // Объявление конструктора Node(int32_t value_, Node* previous_); };- Определение конструктора в .cpp файле (
node.cpp):
// node.cpp #include "node.h" // Определение конструктора Node::Node(int32_t value_, Node* previous_) : value(value_), previous(previous_) { // Конструктор инициализирует переменные }- Сборка проекта: Убедитесь, что ваш проект собирается с обоими файлами ( и ). Например, если вы используете командную строку и компилятор g++, то команда может выглядеть так:
- Объявление структуры и конструктора в заголовочном файле (