Задача.
image_2020-10-15_141330.jpg
Посмотрите, пожалуйста, код. Есть ли здесь хоть что-то правильное? На данном этапе имеется 2 ошибки в Subtraction и Multiplication, связанные с other (две последние реализации).
Pair.h
Код:
class Pair{
public:
long int NumberFirst;
unsigned short int NumberSecond;
virtual void display() = 0;
virtual double getDouble();
virtual void Addition(const Pair& other);
virtual void Subtraction(const Pair& other);
virtual void Multiplication(const Pair& other);
};
class Fraction: public Pair{
public:
Fraction();
Fraction(long int _fNumberFirst, unsigned short int _fNumberSecond);
void Addition(const Pair& other);
void Subtraction(const Pair& other);
void Multiplication(const Pair& other);
void Comparison(Fraction first, Fraction second);
void display();
void getObject(double a);
double getDouble();
};
Pair.cpp
Код:
#include "Pair.h"
#include <cmath>
#include <iostream>
double Pair::getDouble(){ return 0.0; }
void Pair::Addition(const Pair& other){}
void Pair::Subtraction(const Pair& other){}
void Pair::Multiplication(const Pair& other){}
Fraction::Fraction() {
NumberFirst = 0;
NumberSecond = 0;
}
Fraction::Fraction(long int _fNumberFirst, unsigned short int _fNumberSecond) {
NumberFirst = _fNumberFirst;
NumberSecond = _fNumberSecond;
}
void Fraction::Addition(const Pair& other) {
Fraction TEMP;
TEMP.NumberFirst = (*this).NumberFirst + other.NumberFirst;
TEMP.NumberSecond = (*this).NumberSecond + other.NumberSecond;
if (TEMP.NumberSecond >= 100) {
TEMP.NumberSecond -= 100;
TEMP.NumberFirst++;
}
return;
}
void Fraction::display() {
std::cout << NumberFirst << "." << NumberSecond << std::endl;
}
void Fraction::getObject(double a) {
if (static_cast<int>(a / 0.0001) % 10 != 0) {
NumberSecond = static_cast<int>(abs(a * 10000)) % 10000;
}
else if (static_cast<int>(a / 0.001) % 10 != 0) {
NumberSecond = static_cast<int>(abs(a * 1000)) % 1000;
}
else if (static_cast<int>(a / 0.01) % 10 != 0) {
NumberSecond = static_cast<int>(abs(a * 100)) % 100;
}
else if (static_cast<int>(a / 0.1) % 10 != 0) {
NumberSecond = static_cast<int>(abs(a * 10)) % 10;
}
else if (static_cast<int>(a / 1) % 10 != 0) {
NumberSecond = static_cast<int>(abs(a));
}
NumberFirst = static_cast<long int>(a);
}
double Fraction::getDouble(){
double var{ 0 };
var += this->NumberFirst;
if (NumberSecond / 100000 != 0) {
if (NumberFirst > 0) var += static_cast<int>(NumberSecond) * 0.000001;
else var -= static_cast<int>(NumberSecond) * 0.000001;
}
else if (NumberSecond / 10000 != 0) {
if (NumberFirst > 0) var += static_cast<int>(NumberSecond) * 0.00001;
else var -= static_cast<int>(NumberSecond) * 0.00001;
}
else if (NumberSecond / 1000 != 0) {
if (NumberFirst > 0) var += static_cast<int>(NumberSecond) * 0.0001;
else var -= static_cast<int>(NumberSecond) * 0.0001;
}
else if (NumberSecond / 100 != 0) {
if (NumberFirst > 0) var += static_cast<int>(NumberSecond) * 0.001;
else var -= static_cast<int>(NumberSecond) * 0.001;
}
else if (NumberSecond / 10 != 0) {
if (NumberFirst > 0) var += static_cast<int>(NumberSecond) * 0.01;
else var -= static_cast<int>(NumberSecond) * 0.01;
}
else if (NumberSecond != 0) {
if (NumberFirst > 0) var += static_cast<int>(NumberSecond) * 0.1;
else var -= static_cast<int>(NumberSecond) * 0.1;
}
return var;
}
void Fraction::Subtraction(const Pair& other) {
Fraction TEMP;
double var;
var = (*this).getDouble() - other.getDouble();
TEMP.getObject(var);
}
void Fraction::Multiplication(const Pair& other) {
Fraction TEMP;
double i;
i = (*this).getDouble() * other.getDouble();
TEMP.getObject(i);
}
void Fraction::Comparison(Fraction first, Fraction second){
if (first.NumberFirst == NumberFirst && second.NumberSecond == NumberSecond) std::cout << "The numbers are equal." << std::endl;
else {
if ((first.NumberFirst - second.NumberSecond) > 0) std::cout << "The number first is greater than the number second." << std::endl;
else std::cout << "The number first is less than the number second." << std::endl;
}
}