0

In the next code I can input a number n, next to this I can input n pair of numbers with a operator.

Input

4
1 2 +
3 2 -
4 6 *
2 1 .

Pairs means --> numerator and denominator --> n/d

At the end we must input a ''.'' char to do the operation and output should be:

Output

13/3

Problem is I cant find a way to store numbers as object in vector, do operation and print desired result.

#include<iostream>
#include <stdio.h>
#include <vector>
using namespace std;

class rational
{
    int n,d;
public:
    rational() : n(1), d(1) {};
    ~rational(){};
    void setn(int nn){ n = nn;}
    void setd(int dn) { d = dn; }
    void getData()
    {
        cin>>n;
        cin>>d;

        while(d==0)
        {
            break;
        }

        while(d<0)
        {
            n *= -1;
            d *= -1;
        }
    }

int GCD(int n1, int remainder)
{
    if(remainder==0)
        return(n1);
    else { return(GCD(remainder,n1%remainder)); }
}
void printoper()
{
    cout << n << " " << d << endl;
}
void reduce(int &n,int &d)
{
    int rdc = 0;
    if(d>n)
        rdc = GCD(d,n);
    else if(d<n)
        rdc = GCD(n,d);
    else
        rdc = GCD(n,d);
    n /= rdc;
    d /= rdc;
    cout<<n<<"/"<<d<<endl;
}

void operator +(rational c1)
{
rational temp;
temp.n=(n*c1.d)+(c1.n*d);
temp.d=c1.d*d;
reduce(temp.n,temp.d);
}

void operator -(rational c1)
{
rational temp;
temp.n=(n*c1.d)-(c1.n*c1.d);
temp.d=c1.d*d;
reduce(temp.n,temp.d);
}

void operator *(rational c1)
{
rational temp;
temp.n=n*c1.n;
temp.d=d*c1.d;
reduce(temp.n,temp.d);
}

void operator /(rational c1)
{
rational temp;
temp.n=n*c1.d;
temp.d=d*c1.n;
if(temp.d<0)
{
    temp.n *= -1;
    temp.d *= -1;
}
reduce(temp.n,temp.d);
}

};

int main()
{
    vector<rational> list;
            int n;
            int d;
            rational *p1;
    int x;
    char cr;
    cin>>x;
    for(int contador=0; contador<x; contador++){
            if(contador<x){
                    cin>>n;
                    cin>>d;
                    cin>>cr;
                    p1=new rational;
                    list.push_back(*p1);
                    cin.get();
                    }
            }
    vector<rational>::iterator it;
    for ( it = list.begin(); it != list.end(); ++it ) {
                        it->printoper();
                 }
cin.get();
return 0;
}

I would appreciate any help.

EDITED

I've modified my code to:

#include<iostream>
#include<stdio.h>
using namespace std;

class rational
{
    int n,d;
public:
    rational() : n(1), d(1) {};
    ~rational(){};

    void getData()
    {
        cout<<"\nEnter a numerator: ";
        cin>>n;
        cout<<"Enter a denominator: ";
        cin>>d;
        cout<<endl;

        while(d==0)
        {
            cout<<"Please enter a denominator: ";
            cin>>d;
        }

        while(d<0)
        {
            n *= -1;
            d *= -1;
        }
    }

int GCD(int n1, int remainder)
{
    if(remainder==0)
        return(n1);
    else { return(GCD(remainder,n1%remainder)); }
}

void reduce(int &n,int &d)
{
    int rdc = 0;
    if(d>n)
        rdc = GCD(d,n);
    else if(d<n)
        rdc = GCD(n,d);
    else
        rdc = GCD(n,d);
    n /= rdc;
    d /= rdc;
    cout<<n<<"/"<<d<<endl;
}

void operator +(rational c1)
{
rational temp;
temp.n=(n*c1.d)+(c1.n*d);
temp.d=c1.d*d;
reduce(temp.n,temp.d);
}

void operator -(rational c1)
{
rational temp;
temp.n=(n*c1.d)-(c1.n*c1.d);
temp.d=c1.d*d;
reduce(temp.n,temp.d);
}

void operator *(rational c1)
{
rational temp;
temp.n=n*c1.n;
temp.d=d*c1.d;
reduce(temp.n,temp.d);
}

};

int main()
{
    rational c1, c2;
    int n,x;
    char cr;
    cin>>x;
    for(int contador=0; contador<x; contador++){
            if(contador<x){
                            c1.getData();
                            c2.getData ();
                    }
            }
    cin>>cr;
    switch(cr)
    {
        case '+':
            c1+c2;
            cin.get();
        return 0;
        case '-':
            c1-c2;
            cin.get();
        return 0;
        case '*':
            c1*c2;
            cin.get();
        return 0;
        default:
            cout<<endl;
        return 0;
    }

cin.get();
return 0;
}

Now I can't find a way to do operations for n fractions (n/d)

Erick.1993
  • 63
  • 6
  • 3
    `p1=new rational;` -- This causes a memory leak, and there is no need for dynamic memory allocation here. Just do `list.push_back(rational());` – PaulMcKenzie May 28 '18 at 01:36
  • 2
    Also, you could add a constructor or change your default `rational` constructor to take an `n` and `d` instead of just `setn` and `setd`. But what exactly are you having an issue with? Is it that you can't set the `n` and `d` values in `rational`? – PaulMcKenzie May 28 '18 at 01:46
  • I've modified code to this : https://www.ideone.com/19lwkS but now i can't find a way to do operations to ''n'' number of fractions. – Erick.1993 May 28 '18 at 04:05
  • Your operators are... a bit unusual. E.g. for `rational::operator +()` I would expect that it returns the result. In your case, it computes `temp` and then... Leaving the operator body the contents of `temp` goes away and is lost. Wouldn't it make more sense to return `temp`? Once you return results from you arithmetic operators, you might add output to your `switch (cr)` cases. And if you want to perform multiple operations in a loop... Wouldn't it make sense to put the whole `switch` into a loop? There are three kinds available in C++ (not counting ugly things like `if (!end) goto start;`). – Scheff's Cat May 28 '18 at 08:18
  • I just realized that `rational::reduce()` does the output. Sorry, my fault. I really hadn't expected to find it there. For all that, I wouldn't consider this as bad design. Instead, I would recommend to separate output from `rational::reduce()`. E.g. you could provide a `std::ostream& operator<<(std::ostream &out, const rational &value)` for output. – Scheff's Cat May 28 '18 at 08:23

0 Answers0