Hi,
I am trying to separately compile a program in C++ using UNIX and pico editor, however I am constantly getting errors. Here is my code. Any help would be appreciated.
Header File
C++ File
Main
I am trying to separately compile a program in C++ using UNIX and pico editor, however I am constantly getting errors. Here is my code. Any help would be appreciated.
Header File
#ifndef RATIONAL_H;
#define RATIONAL_H;
using namespace std;
class rational {
friend ostream& operator<<(ostream &, rational &);
friend istream& operator>>(istream &, rational &);
public:
rational();
rational(int, int);
bool operator<(rational &);
// post-condition: if calling obj is smaller, returns true; returns false otherwise
bool operator>(const rational &);
// post-condition: if calling obj is greater, returns true; returns false otherwise
private:
int GCD();
int num;
int denom;
};
#endif
C++ File
using namespace std;
#include <iostream>
#include <fstream> // for declaring ifstream and oftream objects
#include <cstdlib> // for exit(1).
using namespace std;
class rational {
friend ostream& operator<<(ostream &, rational &);
friend istream& operator>>(istream &, rational &);
public:
rational();
rational(int, int);
bool operator<(rational &);
// post-condition: if calling obj is smaller, returns true; returns false otherwise
bool operator>(const rational &);
// post-condition: if calling obj is greater, returns true; returns false otherwise
private:
int GCD();
int num;
int denom;
};
rational::rational()
{
num = 0; denom = 1;
}
rational::rational(int n, int d)
{
if (d == 0)
{
cout << "Denominator cannot be zero, program is terminated." << endl;
exit(1);
}
num = n;
denom = d;
}
int rational::GCD()
{
int n1 = abs(num), n2 = abs(denom), remainder;
remainder = n1 % n2;
while(remainder != 0)
{
n1 = n2;
n2 = remainder;
remainder = n1 % n2;
}
return n2;
}
bool rational::operator>(const rational &obj)
{
double value1, value2; // value1: double equivalent of calling object; value2 double equivlent of parameter obj
value1 = static_cast<double>(num) / static_cast<double>(denom);
value2 = static_cast<double>(obj.num) / static_cast<double>(obj.denom);
if (value1 > value2)
return true;
else
return false;
}
bool rational::operator<(rational &obj)
{
double value1, value2; // value1: double equivalent of calling object; value2 double equivlent of parameter obj
value1 = static_cast<double>(num) / static_cast<double>(denom);
value2 = static_cast<double>(obj.num) / static_cast<double>(obj.denom);
if (value1 < value2)
return true;
else
return false;
}
ostream& operator<<(ostream &out, rational &obj)
{
int gcd = obj.GCD();
obj.num /= gcd;
obj.denom /= gcd;
out << obj.num << "/" << obj.denom;
return out;
}
istream& operator>>(istream &in, rational &obj)
{
in >> obj.num >> obj.denom;
return in;
}
//**********************************************************************
void selectionSort(rational a[], int number_used);
void swap_values(rational & v1, rational & v2);
int index_of_smallest(rational a[], int start_index, int number_used);
void selectionSort(rational a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{
index_of_next_smallest = index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
}
}
void swap_values(rational & v1, rational & v2)
{
rational temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest(rational a[], int start_index, int number_used)
{
rational min = a[start_index];
int index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
return index_of_min;
}
//**********************************************************************
int main()
{
const int SIZE = 10;
rational r[SIZE];
int n = 0;
ifstream fin;
ofstream fout;
for(int i = 0; i < 10; i++)
{
cout<<"Please Enter 10 Rational Numbers"<<endl;
cin>>r[SIZE];
}
while(fin >> r[n])
n++;
cout << "\n\nBefore sorting, the list of rationals are: " << endl;
fout << "\n\nBefore sorting, the list of rationals are: " << endl;
for (int i = 0; i < n; i++)
{
cout << r[i] << " ";
fout << r[i] << " ";
}
selectionSort(r, n);
cout << "\n\nAfter sorting, the list of rationals are: " << endl;
fout << "\n\nAfter sorting, the list of rationals are: " << endl;
for (int i = 0; i < n; i++)
{
cout << r[i] << " ";
fout << r[i] << " ";
}
system("PAUSE");
cout << endl << endl;
return 0;
}
Main
using namespace std;
int main()
{
const int SIZE = 10;
rational r[SIZE];
int n = 0;
ifstream fin;
ofstream fout;
for(int i = 0; i < 10; i++)
{
cout<<"Please Enter 10 Rational Numbers"<<endl;
cin>>r[SIZE];
}
while(fin >> r[n])
n++;
cout << "\n\nBefore sorting, the list of rationals are: " << endl;
fout << "\n\nBefore sorting, the list of rationals are: " << endl;
for (int i = 0; i < n; i++)
{
cout << r[i] << " ";
fout << r[i] << " ";
}
selectionSort(r, n);
cout << "\n\nAfter sorting, the list of rationals are: " << endl;
fout << "\n\nAfter sorting, the list of rationals are: " << endl;
for (int i = 0; i < n; i++)
{
cout << r[i] << " ";
fout << r[i] << " ";
}
system("PAUSE");
cout << endl << endl;
return 0;