1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <sstream>
using namespace std;
struct Fraction
{
int numerator;
int denominator;
};
struct FractionArray
{
Fraction *fractions; //this will point to an array of fractions
int nFractions; //this will contain the number of elements in the array
};
bool readDataAndShowFractions(string filename, FractionArray &fractions);
bool readDataIntoArray(ifstream *infile, Fraction fractions[], int size);
Fraction newFraction(int num, int denom);
void printFractions(FractionArray fractions);
void reduce(Fraction &f);
int gcd(int x, int y);
//functions to perform fraction arithmetic
Fraction add(Fraction op1, Fraction op2);
Fraction subtract(Fraction op1, Fraction op2);
Fraction multiply(Fraction op1, Fraction op2);
Fraction divide(Fraction op1, Fraction op2);
double getDecimal(Fraction);
//used to convert an integer to a string
//to_string() is defined in the latest version of C++ but is not supported by older compilers
string tostring(int number);
string tostring(Fraction f);
int main(int argc, char *argv[])
{
//create two structures, one for each array
FractionArray fractions1;
FractionArray fractions2;
bool success;
//initialize and output the first array of Fraction structures
success = readDataAndShowFractions("fractiondata1.txt", fractions1);
if (!success)
{
cout << "Fraction array could not be constructed from fractiondata1.txt" << endl;
exit(-1);
}
//initialize and output the second array of Fraction structures
success = readDataAndShowFractions("fractiondata2.txt", fractions2);
if (!success)
{
cout << "Fraction array could not be constructed from fractiondata2.txt" << endl;
exit(-1);
}
//make sure the arrays are of equal size
if (fractions1.nFractions != fractions2.nFractions )
{
cout << "Fraction arrays are not the same size" << endl;
exit(-1);
}
//loop through the Fraction arrays and output the results of
//adding the fractions at each position
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = add(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// subtract
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = subtract(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// multiply
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = multiply(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// divide
for (int i = 0; i < fractions1.nFractions; i++)
{
Fraction result = divide(fractions1.fractions[i],
fractions2.fractions[i]);
cout << tostring(fractions1.fractions[i]) << " + "
<< tostring(fractions2.fractions[i]) << " = "
<< tostring(result) << endl;
}
// decimal value fraction 1
for (int i = 0; i < fractions1.nFractions; i++)
{
cout << tostring(fractions1.fractions[i]) << " = "<<getDecimal(fractions1.fractions[i])<<endl;
}
// decimal value fraction 2
for (int i = 0; i < fractions2.nFractions; i++)
{
cout << tostring(fractions2.fractions[i]) << " = "<<getDecimal(fractions2.fractions[i])<<endl;
}
cin.get();
return 0;
}
//adds the two fractions together, creates a new Fraction from the result and
//returns it
Fraction add(Fraction op1, Fraction op2)
{
int num1;
int num2;
int denom;
denom = op1.denominator * op2.denominator;
num1 = (denom / op1.denominator) * op1.numerator;
num2 = (denom / op2.denominator) * op2.numerator;
Fraction result;
result.numerator = num1 + num2;
result.denominator = denom;
reduce(result);
return result;
}
//subtract the two fractions together, creates a new Fraction from the result and
//returns it
Fraction subtract(Fraction op1, Fraction op2)
{
int num1;
int num2;
int denom;
denom = op1.denominator * op2.denominator;
num1 = (denom / op1.denominator) * op1.numerator;
num2 = (denom / op2.denominator) * op2.numerator;
Fraction result;
result.numerator = num1 - num2;
result.denominator = denom;
reduce(result);
return result;
}
//multiply the two fractions together, creates a new Fraction from the result and
//returns it
Fraction multiply(Fraction op1, Fraction op2)
{
int num;
int denom;
denom = op1.denominator * op2.denominator;
num = op1.numerator * op2.numerator;
Fraction result;
result.numerator = num;
result.denominator = denom;
reduce(result);
return result;
}
//multiply the two fractions together, creates a new Fraction from the result and
//returns it
Fraction divide(Fraction op1, Fraction op2)
{
int num;
int denom;
denom = op1.denominator * op2.numerator;
num = op1.numerator * op2.denominator;
Fraction result;
result.numerator = num;
result.denominator = denom;
reduce(result);
return result;
}
// function to get decimal value
double getDecimal(Fraction f){
return (double)f.numerator/(double)f.denominator;
}
//reduces the given fraction
void reduce(Fraction &f)
{
int factor = gcd(f.numerator, f.denominator);
f.numerator = f.numerator / factor;
f.denominator = f.denominator / factor;
}
//returns the greatest common denominator of two values
int gcd (int x, int y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
Fraction newFraction(int num, int denom)
{
Fraction f;
f.numerator = num;
f.denominator = denom;
return f;
}
/*
Reads fraction data from the given file into the array
defined in the given structure.
Returns true if successful, false if any errors were encountered.
*/
bool readDataAndShowFractions(string filename, FractionArray &fractions)
{
//read the size to make the array and create it
ifstream *infile;
infile = new ifstream(filename, ios::in);
if (!infile->is_open())
return false;
//read the number of values into the struct variable and make
//sure it is not 0
if (*infile >> fractions.nFractions && fractions.nFractions > 0)
{
fractions.fractions = new Fraction[fractions.nFractions];
readDataIntoArray(infile, fractions.fractions, fractions.nFractions);
}
infile->close();
printFractions(fractions);
return true;
}
/* Given a pointer to an open file and an array, reads size data from the file
and populates the array.*/
bool readDataIntoArray(ifstream *infile, Fraction fractions[], int size)
{
int numerator;
int denominator;
for (int i = 0; i < size; i++)
{
*infile >> numerator;
if (infile->eof())
return false;
*infile >> denominator;
if (infile->eof())
return false;
//Add a structure for the new fraction
fractions[i] = newFraction(numerator, denominator);
}
return true;
}
void printFractions(FractionArray fractions)
{
cout << "Fraction data from array" << endl;
for (int i = 0; i < fractions.nFractions; i++)
{
cout << tostring(fractions.fractions[i]) << endl;
}
cout << endl;
}
string tostring(int number)
{
//const int size = sizeof(int) * 4;
//char buf[size + 1];
stringstream ss;
ss << number;
//sprintf_l(buf, "%d", number);
//return string(buf);
return ss.str();
}
string tostring(Fraction f)
{
string num = tostring(f.numerator);
string denom = tostring(f.denominator);
return num + "\\" + denom;
}
|