#include <stdio.h>

#include <cstdlib>

#include "Rational.h"

using namespace std;

//funtions

void getConstraints(int &, int &, FILE *);

void getInput(Rational**, int, int, FILE *);

//global variables

char inFileName[16], outFileName[16];

int main()

{

int rows = 0;

int cols = 0;

cout << "Please enter input file name (max 15 chars):\n";

cin >> inFileName;

FILE *fp;

if((fp=fopen(inFileName,"r"))==NULL)

{

cout<< "Error opening file.";

}

getConstraints(rows, cols, fp);

if(rows != 0 && cols != 0)

{

Rational **a = new Rational *[rows];

for(int k=0; k<rows; k++)

a[k] = new Rational [cols];

getInput(a, rows, cols, fp);

for(int k = 0; k < rows; k++)

delete[] a[k];

delete[] a;

}

return 0;

}

void getConstraints(int &R, int &C, FILE *f)

{

fscanf(f, "%d", &R);

R++;

fscanf(f, "%d", &C);

C = C*2 + 1;

return;

}

void getInput(Rational** a, int R, int C, FILE *f)

{

int row, col;

char c1, c2;

bool negOne = false;

row = R;

col = C;

Rational temp;

col = col/2 - 1;

a[0][0] = 0;

for(int j = 1; j <= col; j++)

{

temp.input(f);

a[0][j] = temp;

}

for(int i = 1; i < row; i++)

{

temp.input(f);

a[i][0] = temp;

fscanf(f, "%c%c ", &c1, &c2);

if(c1 == '<')

negOne = true;

for(int j = 1; j <= col; j++)

{

temp.input(f);

a[i][j] = temp;

}

if(negOne == true)

{

for(int j = 0; j <= col; j++)

{

a[i][j] = a[i][j] * -1;

}

}

negOne = false;

}

return;

}

#include <fstream>

#include <iostream>

#include <cstdlib>

#include <stdio.h>

using namespace std;

//Class for Rational Numbers

class Rational

{

public:

//constructors

Rational();

Rational(int numerator, int denominator);

Rational(int numerator);

void operator=(const Rational&);

//member functions

friend Rational operator+(const Rational&, const Rational&);

friend Rational operator-(const Rational&, const Rational&);

friend Rational operator*(const Rational&, const Rational&);

friend Rational operator/(const Rational&, const Rational&);

friend Rational operator<(const Rational&, const Rational&);

friend Rational operator>(const Rational&, const Rational&);

void reduce();

void input(FILE *);

void output(ostream& out_s);

//accessor functions

int Numerator() {return numerator;}

int Denominator() {return denominator;}

private:

//member variables

int numerator;

int denominator;

};

Rational::Rational() : numerator(0), denominator(1)

{

}

Rational::Rational(int num, int dem)

{

numerator = num;

denominator = dem;

}

Rational::Rational(int num)

{

numerator = num;

denominator = 1;

}

Rational operator+(const Rational& r1, const Rational& r2)

{

Rational r3;

r3.numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;

r3.denominator = r1.denominator * r2.denominator;

r3.reduce();

return r3;

}

Rational operator-(const Rational& r1, const Rational& r2)

{

Rational r3;

r3.numerator = r1.numerator * r2.denominator - r2.numerator * r1.denominator;

r3.denominator = r1.denominator * r2.denominator;

r3.reduce();

return r3;

}

Rational operator*(const Rational& r1, const Rational& r2)

{

Rational r3;

r3.numerator = r1.numerator * r2.numerator;

r3.denominator = r1.denominator * r2.denominator;

r3.reduce();

return r3;

}

Rational operator/(const Rational& r1, const Rational& r2)

{

Rational r3;

r3.numerator = r1.numerator * r2.denominator;

r3.denominator = r1.denominator * r2.numerator;

r3.reduce();

return r3;

}

Rational operator<(const Rational& r1, const Rational& r2)

{

if(r1.numerator * r2.denominator < r2.numerator * r1.denominator)

return true;

else

return false;

}

Rational operator>(const Rational& r1, const Rational& r2)

{

if(r1.numerator * r2.denominator > r2.numerator * r1.denominator)

return true;

else

return false;

}

void Rational::reduce()

{

int n = 2;

bool div = true;

bool div2 = false;

while(div == true)

{

//checks if divisible

for(int i = 2; i < numerator || div2 == true; i++)

{

if(numerator % i == 0 && denominator % i == 0)

div2 = true;

}

//if divisible do division

if(div2 == true)

{

while(numerator % n != 0 && denominator %n != 0)

n++;

numerator = numerator/n;

denominator = denominator/n;

}

else

return;

div2 = false;

n = 2;

}

return;

}

void Rational::output(ostream& out_s)

{

if(denominator != 1)

out_s << numerator << "/" << denominator;

else

out_s << numerator;

}

void Rational::input(FILE *f)

{

if((f=fopen("matrix.txt","r"))==NULL)

{

cout<< "Error opening file.";

return;

}

fscanf(f, "%d/%d' '", &numerator, &denominator);

}

void Rational::operator =(const Rational &a)

{

numerator = a.numerator;

denominator = a.denominator;

return;

}

4

4

-1 -4 -3 -3

-1 <= 1 1 0 -1

2 >= 1 -1 -1 -1

-1 >= 1 1 0 -1

-2 >= -1 1 -1 1