Мой код выглядит как ввод пользователя в форме 'a / b' и выполняет дробную математику, а затем выводит результаты. Основная ошибка, которую я, похоже, получаю, - это не совпадение с 'operator ='. Ошибка указана в строках 39 и 40. Любая помощь приветствуется.
Здесь отображаются ошибки, которые я получаю: введите описание изображения здесь
main.cpp
// Add appropriate headers
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include "rational7.h"
using namespace std;
int main()
{
// ToDo: declare three rational objects using the default constructor
Rational a, b, c;
int length;
char answer='Y';
// Main loop to read in rationals and compute the sum
do {
cout << "Enter the number of fractions to add/multiply: ";
cin >> length;
Rational *fractions = new Rational[length];
for (int i = 0; i < length; i++)
{
fractions[i].set (1,i+1);
fractions[i].output();
}
Rational r_sum, r_product(1);
//tempsum = fractions[0];
//tempproduct = fractions[0];
for (int i = 0; i < length; i++)
{
r_sum = sum(r_sum, fractions[i]); // test r.sum
r_product = product(r_product, fractions[i]); // test
}
//simplify
//cout << "Sum of fractions: " << r_sum.output() << endl;
//cout << "Prod of fractions: " << r_product.output() << endl;
// ToDo: use your input member function to read the first rational
cout << "Enter two rationals to compare ";
a.input();
b.input();
a.output(); // Debug line
b.output(); // Debug line
while (a.getDenominator() == 0 || b.getDenominator() == 0)
{
cout << "wrong input. \n";
cout << "Enter a rational (p/q): \n";
a.input();
b.input();
}
// cout << "Total number of objects created = " << << endl;
cout << "\nTry again (Y/N)?\n";
cin >> answer;
cin.ignore(256,'\n');
} while (answer == 'y' || answer == 'Y');
return 0;
}
рациональный7.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
#include "rational7.h"
using namespace std;
// ToDO: Implement your class member functions below.
/*
Rational::Rational()
{
numerator = 0;
denominator = 1;
}
Rational::Rational(int num)
{
numerator = num;
denominator = 1;
}
Rational::Rational(int num, int den)
{
Rational r;
numerator = num;
denominator = den;
}
*/
int Rational::counter = 0;
//Three constructors to one
Rational::Rational(int num, int den)
{
numerator = num;
denominator = den;
}
void sum(const Rational &a, const Rational &b)
//void Rational::sum(const Rational &a, const Rational &b)
{
Rational temp;
int num = 0;
int den = 0;
//num = (a.getNumerator()*b.getDenominator() + a.getDenominator()*b.getNumerator());
//den = (a.getDenominator()*b.getDenominator());
num = (a.numerator*b.denominator + a.denominator*b.numerator);
den = (a.denominator*b.denominator);
temp.numerator = num;
temp.denominator = den;
//return temp;
}
//void Rational::product(const Rational &a, const Rational &b)
void product(const Rational &a, const Rational &b)
{
Rational temp;
//int num = (a.getNumerator()*b.getNumerator());
//int den = (a.getDenominator()*b.getDenominator());
int num = (a.numerator*b.numerator);
int den = (a.denominator*b.denominator);
temp.numerator = num;
temp.denominator = den;
//return temp;
}
void Rational::input()
{
string in;
getline(cin, in);
//cin >> in;
// find the index position of /
int indx = in.find("/");
// seperate the numerator
numerator = atoi(in.substr(0, indx).c_str());
// seperate the denominator
denominator = atoi(in.substr(indx+1, in.length()).c_str());
//if(denominator != 0)
Rational(numerator, denominator);
}
void Rational::output()const
{
cout << numerator << "/" << denominator;
}
void Rational::set(int num, int den)
{
numerator = num;
denominator = den;
}
// Two getter functions
int Rational::getNumerator()const
{
return numerator;
}
int Rational::getDenominator()const
{
return denominator;
}
/*
void simplify(int num, int den)
{
gcd(num, den);
}
*/
/*
int gcd (int a, int b)
{
while (a!=0 && b!=0)
{
a = a % b;
if (a!=0)
b = b % a;
}
if (a==0)
return b;
if (b==0)
return a;
}
*/
/*
bool isEqual(const Rational &a, const Rational &b)
{
if (a.numerator * b.denominator == a.denominator * b.numerator)
{
return true;
}
else
{
return false;
}
}
*/
int Rational::getObjectCounter()
{
return counter;
}
рациональный7.ч
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
using namespace std;
/* KEEP THIS COMMENT
* class Rational
* represents a Rational number. Remember rational means ratio-nal
* which means there is a numerator and denominator having
* integer values. Using good ADT techniques, we have made member
* variable private (also known as instance variables) and made member
* functions public.
*/
class Rational
{
private:
int numerator;
int denominator;
static int counter;
//void simplify(int num, int den);
public:
/*
// ToDo: Default Constructor
Rational();
// ToDo: Constructor that takes int numerator
Rational(int num);
// ToDo: Constructor that takes int numerator and int denominator
Rational(int num, int den);
*/
// Three constructors to one
Rational(int num = 0, int den = 1);
// ToDo: Member function to read a rational in the form: n/d
void input();
// ToDo: Member function to write a rational as n/d
void output()const;
// ToDo: declare an accessor function to get the numerator
int getNumerator()const;
// ToDo: declare an accessor function to get the denominator
int getDenominator()const;
// object counter 'global'
static int getObjectCounter();
// ToDo: delcare a function called Sum that takes two rational objects
// sets the current object to the sum of the given objects using the
// formula: a/b + c/d = ( a*d + b*c)/(b*sd)
friend void sum(const Rational &a, const Rational &b);
friend void product(const Rational &a, const Rational &b);
void set(int num, int den);
//bool isEqual(const Rational &a, const Rational &b);
};