/*main.cpp*/ #include #include \"fraction.h\" #include #include #include using n
ID: 3689079 • Letter: #
Question
/*main.cpp*/
#include
#include "fraction.h"
#include
#include
#include
using namespace std;
using namespace cs_fraction;
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(ifstream& in);
string boolString(bool convertMe);
int main()
{
BasicTest();
RelationTest();
BinaryMathTest();
MathAssignTest();
}
void BasicTest()
{
cout << " ----- Testing basic fraction creation & printing ";
cout << "(fractions should be in reduced form, and as mixed numbers.) ";
const fraction fr[] = { fraction(4, 8), fraction(-15,21),
fraction(10), fraction(12, -3),
fraction(), fraction(28, 6), fraction(0, 12) };
for (int i = 0; i < 7; i++) {
cout << "fraction [" << i << "] = " << fr[i] << endl;
}
cout << " ----- Now reading fractions from file ";
ifstream in("fraction.txt");
while (!eof(in)) {
fraction f;
if (in.peek() == '#') {
in.ignore(128, ' '); //skip this line, it's a comment
}
else {
in >> f;
cout << "Read fraction = " << f << endl;
}
}
}
bool eof(ifstream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
string boolString(bool convertMe) {
if (convertMe) {
return "true";
}
else {
return "false";
}
}
void RelationTest()
{
cout << " ----- Testing relational operators between fractions ";
const fraction fr[] = { fraction(3, 6), fraction(1,2), fraction(-15,30),
fraction(1,10), fraction(0,1), fraction(0,2) };
for (int i = 0; i < 5; i++) {
cout << "Comparing " << fr[i] << " to " << fr[i + 1] << endl;
cout << " Is left < right? " << boolString(fr[i] < fr[i + 1]) << endl;
cout << " Is left <= right? " << boolString(fr[i] <= fr[i + 1]) << endl;
cout << " Is left > right? " << boolString(fr[i] > fr[i + 1]) << endl;
cout << " Is left >= right? " << boolString(fr[i] >= fr[i + 1]) << endl;
cout << " Does left == right? " << boolString(fr[i] == fr[i + 1]) << endl;
cout << " Does left != right ? " << boolString(fr[i] != fr[i + 1]) << endl;
}
cout << " ----- Testing relations between fractions and integers ";
fraction f(-3, 6);
int num = 2;
cout << "Comparing " << f << " to " << num << endl;
cout << " Is left < right? " << boolString(f < num) << endl;
cout << " Is left <= right? " << boolString(f <= num) << endl;
cout << " Is left > right? " << boolString(f > num) << endl;
cout << " Is left >= right? " << boolString(f >= num) << endl;
cout << " Does left == right? " << boolString(f == num) << endl;
cout << " Does left != right ? " << boolString(f != num) << endl;
fraction g(1, 4);
num = -3;
cout << "Comparing " << num << " to " << g << endl;
cout << " Is left < right? " << boolString(num < g) << endl;
cout << " Is left <= right? " << boolString(num <= g) << endl;
cout << " Is left > right? " << boolString(num > g) << endl;
cout << " Is left >= right? " << boolString(num >= g) << endl;
cout << " Does left == right? " << boolString(num == g) << endl;
cout << " Does left != right ? " << boolString(num != g) << endl;
}
void BinaryMathTest()
{
cout << " ----- Testing binary arithmetic between fractions ";
const fraction fr[] = { fraction(1, 6), fraction(1,3),
fraction(-2,3), fraction(5), fraction(-4,3) };
for (int i = 0; i < 4; i++) {
cout << fr[i] << " + " << fr[i + 1] << " = " << fr[i] + fr[i + 1] << endl;
cout << fr[i] << " - " << fr[i + 1] << " = " << fr[i] - fr[i + 1] << endl;
cout << fr[i] << " * " << fr[i + 1] << " = " << fr[i] * fr[i + 1] << endl;
cout << fr[i] << " / " << fr[i + 1] << " = " << fr[i] / fr[i + 1] << endl;
}
cout << " ----- Testing arithmetic between fractions and integers ";
fraction f(-1, 2);
int num = 4;
cout << f << " + " << num << " = " << f + num << endl;
cout << f << " - " << num << " = " << f - num << endl;
cout << f << " * " << num << " = " << f * num << endl;
cout << f << " / " << num << " = " << f / num << endl;
fraction g(-1, 2);
num = 3;
cout << num << " + " << g << " = " << num + g << endl;
cout << num << " - " << g << " = " << num - g << endl;
cout << num << " * " << g << " = " << num * g << endl;
cout << num << " / " << g << " = " << num / g << endl;
}
void MathAssignTest()
{
cout << " ----- Testing shorthand arithmetic assignment on fractions ";
fraction fr[] = { fraction(1, 6), fraction(4),
fraction(-1,2), fraction(5) };
for (int i = 0; i < 3; i++) {
cout << fr[i] << " += " << fr[i + 1] << " = ";
cout << (fr[i] += fr[i + 1]) << endl;
cout << fr[i] << " -= " << fr[i + 1] << " = ";
cout << (fr[i] -= fr[i + 1]) << endl;
cout << fr[i] << " *= " << fr[i + 1] << " = ";
cout << (fr[i] *= fr[i + 1]) << endl;
cout << fr[i] << " /= " << fr[i + 1] << " = ";
cout << (fr[i] /= fr[i + 1]) << endl;
}
cout << " ----- Testing shorthand arithmetic assignment using integers ";
fraction f(-1, 3);
int num = 3;
cout << f << " += " << num << " = ";
cout << (f += num) << endl;
cout << f << " -= " << num << " = ";
cout << (f -= num) << endl;
cout << f << " *= " << num << " = ";
cout << (f *= num) << endl;
cout << f << " /= " << num << " = ";
cout << (f /= num) << endl;
cout << " ----- Testing increment/decrement prefix and postfix ";
fraction g(-1, 3);
cout << "Now g = " << g << endl;
cout << "g++ = " << g++ << endl;
cout << "Now g = " << g << endl;
cout << "++g = " << ++g << endl;
cout << "Now g = " << g << endl;
cout << "g-- = " << g-- << endl;
cout << "Now g = " << g << endl;
cout << "--g = " << --g << endl;
cout << "Now g = " << g << endl;
}
/*fraction.h*/
#ifndef A12
#define A12
#include
#include
namespace cs_fraction
{
class fraction
{
private:
int numerator;
int denominator;
void simplify();
public:
fraction(int inNumerator = 0, int inDenominator = 1); // Initilization constructor
fraction operator++();
fraction operator++(int);
fraction operator--();
fraction operator--(int);
fraction operator+=(const fraction& right);
fraction operator-=(const fraction& right);
fraction operator*=(const fraction& right);
fraction operator/=(const fraction& right);
friend fraction operator+(const fraction& left,
const fraction& right);
friend fraction operator-(const fraction& left,
const fraction& right);
friend fraction operator*(const fraction& left,
const fraction& right);
friend fraction operator/(const fraction& left,
const fraction& right);
friend bool operator<(const fraction &left,
const fraction &right);
friend bool operator<=(const fraction &left,
const fraction &right);
friend bool operator>(const fraction &left,
const fraction &right);
friend bool operator>=(const fraction &left,
const fraction &right);
friend bool operator==(const fraction &left,
const fraction &right);
friend bool operator!=(const fraction &left,
const fraction &right);
friend std::ostream& operator<<(std::ostream& out,
const fraction& right);
friend std::istream& operator >> (std::istream& in,
fraction& right);
};
}
#endif
/*fraction.cpp*/
#include
#include
#include
#include
#include "fraction.h"
using namespace std;
namespace cs_fraction {
fraction::fraction(int inNumerator, int inDenominator)
{
assert(inDenominator != 0);
numerator = inNumerator;
denominator = inDenominator;
simplify();
}
// Simplify returns a simplified fraction object.
void fraction::simplify()
{
int end;
int gcd_value = 1;
if (denominator < 0) {
numerator *= -1;
denominator *= -1;
}
if (numerator < denominator)
{
end = abs(numerator);
}
else
{
end = abs(denominator);
}
for (int i = 1; i <= end; i++)
{
if (numerator % i == 0 && denominator % i == 0)
gcd_value = i;
}
numerator = numerator / gcd_value;
denominator = denominator / gcd_value;
}
fraction fraction::operator++() // ++g
{
numerator += denominator;
return *this;
}
fraction fraction::operator++(int) // g++
{
fraction temp(numerator, denominator);
numerator += denominator;
return temp;
}
fraction fraction::operator--() // --g
{
numerator -= denominator;
return *this;
}
fraction fraction::operator--(int) // g--
{
fraction temp(numerator, denominator);
numerator -= denominator;
return temp;
}
fraction fraction::operator+=(const fraction& right)
{
*this = *this + right;
return *this;
};
fraction fraction::operator-=(const fraction& right)
{
*this = *this - right;
return *this;
}
fraction fraction::operator*=(const fraction& right)
{
*this = *this * right;
return *this;
}
fraction fraction::operator/=(const fraction& right)
{
*this = *this / right;
return *this;
}
fraction operator+(const fraction& left,
const fraction& right)
{
fraction tempFraction;
tempFraction.numerator = left.numerator * right.denominator + left.denominator * right.numerator;
tempFraction.denominator = left.denominator * right.denominator;
tempFraction.simplify();
return tempFraction;
}
fraction operator-(const fraction& left,
const fraction& right)
{
fraction tempFraction;
tempFraction.numerator = left.numerator * right.denominator - left.denominator * right.numerator;
tempFraction.denominator = left.denominator * right.denominator;
tempFraction.simplify();
return tempFraction;
}
fraction operator*(const fraction& left,
const fraction& right)
{
fraction tempFraction;
tempFraction.numerator = left.numerator * right.numerator,
tempFraction.denominator = left.denominator * right.denominator;
tempFraction.simplify();
return tempFraction;
}
fraction operator/(const fraction& left,
const fraction& right)
{
fraction tempFraction;
tempFraction.numerator = left.numerator * right.denominator;
tempFraction.denominator = left.denominator * right.numerator;
tempFraction.simplify();
return tempFraction;
}
bool operator<(const fraction& left,
const fraction& right)
{
return left.numerator * right.denominator < right.numerator * left.denominator;
}
bool operator<=(const fraction& left,
const fraction& right)
{
return left.numerator * right.denominator <= right.numerator * left.denominator;
}
bool operator>(const fraction& left,
const fraction& right)
{
return left.numerator * right.denominator > right.numerator * left.denominator;
}
bool operator>=(const fraction& left,
const fraction& right)
{
return left.numerator * right.denominator >= right.numerator * left.denominator;
}
bool operator==(const fraction& left,
const fraction&right)
{
return left.numerator * right.denominator == right.numerator * left.denominator;
}
bool operator!=(const fraction& left,
const fraction&right)
{
return left.numerator * right.denominator != right.numerator * left.denominator;
}
// Function converts improper and regualar fraction objects to mixed(if needed) fractions and outputs in an easy to read format.
ostream& operator<<(ostream& out, const fraction& right)
{
if (right.numerator == 0)
{
out << 0;
}
else if (right.denominator == 1 || right.denominator == -1)
{
out << right.numerator * right.denominator;
}
else if (right.numerator >= right.denominator)
{
out << right.numerator / right.denominator << "+" << right.numerator - (right.numerator / right.denominator) * right.denominator << "/" << right.denominator;
}
else {
out << right.numerator << "/" << right.denominator;
}
return out;
}
// Function reads insertion stream and converts mixed fractions to improper fractions to store as fraction objects.
istream& operator >> (std::istream& in, fraction& right)
{
int temp;
char dummy;
int whole = 0;
int numerator = 0;
int denominator;
in >> temp;
if (in.peek() == '+') {
// 3+1/4
in >> whole >> dummy >> numerator >> dummy >> denominator;
}
else if (in.peek() == '/') {
// 10/4
in >> numerator >> dummy >> denominator;
}
else {
in >> numerator;
}
if (whole != 0) {
right.numerator = whole * denominator + numerator;
}
else {
right.numerator = numerator;
}
right.denominator = denominator;
return in;
}
}
EXPECTED OUTPUT:
CLIENT PROGRAM
Explanation / Answer
main.cpp
//main.cpp
#include <iostream>
#include "fraction.h"
#include <fstream>
#include <cassert>
using namespace std;
void BasicTest();
void RelationTest();
void BinaryMathTest();
void MathAssignTest();
bool eof(ifstream& in);
int main()
{
BasicTest();
RelationTest();
BinaryMathTest();
MathAssignTest();
system("pause");
}
void BasicTest()
{
cout << " ----- Testing basic fraction creation & printing ";
cout << "(fractions should be in reduced form, and as mixed numbers.) ";
const fraction fr[] = {fraction(4, 8), fraction(-15,21),
fraction(10), fraction(12, -3),
fraction(), fraction(28, 6), fraction(0, 12)};
for (int i = 0; i < 7; i++){
cout << "fraction [" << i <<"] = " << fr[i] << endl;
}
cout << " ----- Now reading fractions from file ";
ifstream in("fraction.txt");
assert(in);
while (!eof(in)) {
fraction f;
if (in.peek() == '#') {
in.ignore(128, ' '); //skip this line, it's a comment
} else {
in >> f;
cout << "Read fraction = " << f << endl;
}
}
}
bool eof(ifstream& in)
{
char ch;
in >> ch;
in.putback(ch);
return !in;
}
void RelationTest()
{
cout << " ----- Testing relational operators between fractions ";
const fraction fr[] = {fraction(3, 6), fraction(1,2), fraction(-15,30),
fraction(1,10), fraction(0,1), fraction(0,2)};
for (int i = 0; i < 5; i++) {
cout << "Comparing " << fr[i] << " to " << fr[i+1] << endl;
cout << " Is left < right? " << (fr[i] < fr[i+1]) << endl;
cout << " Is left <= right? " << (fr[i] <= fr[i+1]) << endl;
cout << " Is left > right? " << (fr[i] > fr[i+1]) << endl;
cout << " Is left >= right? " << (fr[i] >= fr[i+1]) << endl;
cout << " Does left == right? " << (fr[i] == fr[i+1]) << endl;
cout << " Does left != right ? " << (fr[i] != fr[i+1]) << endl;
}
cout << " ----- Testing relations between fractions and integers ";
fraction f(-3,6);
int num = 2;
cout << "Comparing " << f << " to " << num << endl;
cout << " Is left < right? " << (f < num) << endl;
cout << " Is left <= right? " << (f <= num) << endl;
cout << " Is left > right? " << (f > num) << endl;
cout << " Is left >= right? " << (f >= num) << endl;
cout << " Does left == right? " << (f == num) << endl;
cout << " Does left != right ? " << (f != num) << endl;
fraction g(1,4);
num = -3;
cout << "Comparing " << num << " to " << g << endl;
cout << " Is left < right? " << (num < g) << endl;
cout << " Is left <= right? " << (num <= g) << endl;
cout << " Is left > right? " << (num > g) << endl;
cout << " Is left >= right? " << (num >= g) << endl;
cout << " Does left == right? " << (num == g) << endl;
cout << " Does left != right ? " << (num != g) << endl;
}
void BinaryMathTest()
{
cout << " ----- Testing binary arithmetic between fractions ";
const fraction fr[] = {fraction(1, 6), fraction(1,3),
fraction(-2,3), fraction(5), fraction(-4,3)};
for (int i = 0; i < 4; i++) {
cout << fr[i] << " + " << fr[i+1] << " = " << fr[i] + fr[i+1] << endl;
cout << fr[i] << " - " << fr[i+1] << " = " << fr[i] - fr[i+1] << endl;
cout << fr[i] << " * " << fr[i+1] << " = " << fr[i] * fr[i+1] << endl;
cout << fr[i] << " / " << fr[i+1] << " = " << fr[i] / fr[i+1] << endl;
}
cout << " ----- Testing arithmetic between fractions and integers ";
fraction f(-1, 2);
int num = 4;
cout << f << " + " << num << " = " << f + num << endl;
cout << f << " - " << num << " = " << f - num << endl;
cout << f << " * " << num << " = " << f * num << endl;
cout << f << " / " << num << " = " << f / num << endl;
fraction g(-1, 2);
num = 3;
cout << num << " + " << g << " = " << num + g << endl;
cout << num << " - " << g << " = " << num - g << endl;
cout << num << " * " << g << " = " << num * g << endl;
cout << num << " / " << g << " = " << num / g << endl;
}
void MathAssignTest()
{
cout << " ----- Testing shorthand arithmetic assignment on fractions ";
fraction fr[] = {fraction(1, 6), fraction(4),
fraction(-1,2), fraction(5)};
for (int i = 0; i < 3; i++) {
cout << fr[i] << " += " << fr[i+1] << " = ";
cout << (fr[i] += fr[i+1]) << endl;
cout << fr[i] << " -= " << fr[i+1] << " = ";
cout << (fr[i] -= fr[i+1]) << endl;
cout << fr[i] << " *= " << fr[i+1] << " = ";
cout << (fr[i] *= fr[i+1]) << endl;
cout << fr[i] << " /= " << fr[i+1] << " = ";
cout << (fr[i] /= fr[i+1]) << endl;
}
cout << " ----- Testing shorthand arithmetic assignment using integers ";
fraction f(-1, 3);
int num = 3;
cout << f << " += " << num << " = ";
cout << (f += num) << endl;
cout << f << " -= " << num << " = ";
cout << (f -= num) << endl;
cout << f << " *= " << num << " = ";
cout << (f *= num) << endl;
cout << f << " /= " << num << " = ";
cout << (f /= num) << endl;
cout << " ----- Testing increment/decrement prefix and postfix ";
fraction g(-1, 3);
cout << "Now g = " << g << endl;
cout << "g++ = " << g++ << endl;
cout << "Now g = " << g << endl;
cout << "++g = " << ++g << endl;
cout << "Now g = " << g << endl;
cout << "g-- = " << g-- << endl;
cout << "Now g = " << g << endl;
cout << "--g = " << --g << endl;
cout << "Now g = " << g << endl;
}
fraction.cpp
#include <iostream>
#include "fraction.h"
using namespace std;
fraction::fraction(int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
reduce();
}
ostream& operator<<(ostream& out, const fraction& right)
{
if (right.denominator == 1){
out << right.numerator;
} else if (abs(right.numerator) < right.denominator){
out << right.numerator << "/" << right.denominator;
} else {
out << right.numerator/right.denominator << "+" <<
(abs(right.numerator % right.denominator)) << "/" <<
right.denominator;
}
return out;
}
istream& operator>>(istream& in, fraction &fractionRead)
{
int wholeNumber;
int posOrNeg = 1;
if (in.peek() == '-'){
posOrNeg = -1;
in.ignore();
}
in >> fractionRead.numerator;
if (in.peek() == '+'){
wholeNumber = fractionRead.numerator;
in.ignore();
in >> fractionRead.numerator;
}
if (in.peek() == '/'){
in.ignore();
in >> fractionRead.denominator;
} else {
fractionRead.denominator = 1;
}
fractionRead.numerator = posOrNeg * (wholeNumber * fractionRead.denominator + fractionRead.numerator);
fractionRead.reduce();
return in;
}
void fraction::reduce()
{
int gcf;
if (denominator < 0){
denominator = denominator * -1;
numerator = numerator * -1;
}
if (numerator == 0){
denominator = 1;
}
gcf = abs(numerator);
while ( gcf > 0 && ((numerator % gcf !=0) || (denominator % gcf !=0))) {
gcf--;
}
if (gcf > 0){
numerator /= gcf;
denominator /= gcf;
}
}
fraction operator+(const fraction &left, const fraction &right)
{
fraction sum;
sum.numerator = ((left.numerator * right.denominator) + (right.numerator * left.denominator));
sum.denominator = (left.denominator * right.denominator);
sum.reduce();
return sum;
}
fraction operator-(const fraction &left, const fraction &right)
{
fraction difference;
difference.numerator = ((left.numerator * right.denominator) - (right.numerator * left.denominator));
difference.denominator = (left.denominator * right.denominator);
difference.reduce();
return difference;
}
fraction operator*(const fraction &left, const fraction &right)
{
fraction product;
product.numerator = (left.numerator * right.numerator);
product.denominator = (left.denominator * right.denominator);
product.reduce();
return product;
}
fraction operator/(const fraction &left, const fraction &right)
{
fraction quotient;
quotient.numerator = (left.numerator * right.denominator);
quotient.denominator = (left.denominator * right.numerator);
quotient.reduce();
return quotient;
}
fraction fraction::operator+=(const fraction &right)
{
*this = *this + right;
return *this;
}
fraction fraction::operator-=(const fraction &right)
{
*this = *this - right;
return *this;
}
fraction fraction::operator*=(const fraction &right)
{
*this = *this * right;
return *this;
}
fraction fraction::operator/=(const fraction &right)
{
*this = *this / right;
return *this;
}
fraction fraction::operator++()
{
numerator = numerator + denominator;
return *this;
}
fraction fraction::operator++(int)
{
fraction temp(numerator, denominator);
numerator = numerator + denominator;
return temp;
}
fraction fraction::operator--()
{
numerator = numerator - denominator;
return *this;
}
fraction fraction::operator--(int)
{
fraction temp(numerator, denominator);
numerator = numerator - denominator;
return temp;
}
bool operator<(const fraction &left, const fraction &right)
{
return (left.numerator * right.denominator) < (right.numerator * left.denominator);
}
bool operator>(const fraction &left, const fraction &right)
{
return operator<(right, left);
}
bool operator==(const fraction &left, const fraction &right)
{
return (left.numerator * right.denominator) == (right.numerator * left.denominator);
}
bool operator!=(const fraction &left, const fraction &right)
{
return !operator==(left, right);
}
bool operator<=(const fraction &left, const fraction &right)
{
return !operator>(left, right);
}
bool operator>=(const fraction &left, const fraction &right)
{
return !operator<(left, right);
}
fraction.h
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class fraction {
public:
fraction(int inNumerator=0, int inDenominator=1);
friend ostream& operator<<(ostream& out, const fraction &right);
friend istream& operator>>(istream& in, fraction &fractionRead);
friend fraction operator+(const fraction &left, const fraction &right);
friend fraction operator-(const fraction &left, const fraction &right);
friend fraction operator*(const fraction &left, const fraction &right);
friend fraction operator/(const fraction &left, const fraction &right);
fraction operator+=(const fraction &right);
fraction operator-=(const fraction &right);
fraction operator*=(const fraction &right);
fraction operator/=(const fraction &right);
fraction operator++();
fraction operator++(int);
fraction operator--();
fraction operator--(int);
friend bool operator<(const fraction &left, const fraction &right);
friend bool operator>(const fraction &left, const fraction &right);
friend bool operator==(const fraction &left, const fraction &right);
friend bool operator!=(const fraction &left, const fraction &right);
friend bool operator<=(const fraction &left, const fraction &right);
friend bool operator>=(const fraction &left, const fraction &right);
private:
int numerator;
int denominator;
void reduce();
};
#endif
fraction.txt
1/3
3/6
3072/4096
-4/5
12/2
5
-8
21/15
-50/3
1+1/4
1+5/5
-4+3/12
-10+10/12
output
----- Testing basic fraction creation & printing
fraction [0] = 4/8
fraction [1] = -15/21
fraction [2] = 10/1
fraction [3] = 12/-3
fraction [4] = 0/1
fraction [5] = 28/6
fraction [6] = 0/12
----- Testing relational operators between fractions
Comparing 3/6 to 1/2
Is left < right? false
Is left <= right? true
Is left > right? false
Is left >= right? true
Does left == right? true
Does left != right ? false
Comparing 1/2 to -15/30
Is left < right? false
Is left <= right? false
Is left > right? true
Is left >= right? true
Does left == right? false
Does left != right ? true
Comparing -15/30 to 1/10
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing 1/10 to 0/1
Is left < right? false
Is left <= right? false
Is left > right? true
Is left >= right? true
Does left == right? false
Does left != right ? true
Comparing 0/1 to 0/2
Is left < right? false
Is left <= right? true
Is left > right? false
Is left >= right? true
Does left == right? true
Does left != right ? false
----- Testing relations between fractions and integers
Comparing -3/6 to 2
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
Comparing -3 to 1/4
Is left < right? true
Is left <= right? true
Is left > right? false
Is left >= right? false
Does left == right? false
Does left != right ? true
----- Testing binary arithmetic between fractions
1/6 + 1/3 = 9/18
1/6 - 1/3 = -3/18
1/6 * 1/3 = 1/18
1/6 / 1/3 = 3/6
1/3 + -2/3 = -3/9
1/3 - -2/3 = 9/9
1/3 * -2/3 = -2/9
1/3 / -2/3 = 3/-6
-2/3 + 5/1 = 13/3
-2/3 - 5/1 = -17/3
-2/3 * 5/1 = -10/3
-2/3 / 5/1 = -2/15
5/1 + -4/3 = 11/3
5/1 - -4/3 = 19/3
5/1 * -4/3 = -20/3
5/1 / -4/3 = 15/-4
----- Testing arithmetic between fractions and integers
-1/2 + 4 = 7/2
-1/2 - 4 = -9/2
-1/2 * 4 = -4/2
-1/2 / 4 = -1/8
3 + -1/2 = 5/2
3 - -1/2 = 7/2
3 * -1/2 = -3/2
3 / -1/2 = 6/-1
----- Testing shorthand arithmetic assignment on fractions
1/6 += 4/1 = 25/6
25/6 -= 4/1 = 1/6
1/6 *= 4/1 = 4/6
4/6 /= 4/1 = 4/24
4/1 += -1/2 = 7/2
7/2 -= -1/2 = 16/4
16/4 *= -1/2 = -16/8
-16/8 /= -1/2 = -32/-8
-1/2 += 5/1 = 9/2
9/2 -= 5/1 = -1/2
-1/2 *= 5/1 = -5/2
-5/2 /= 5/1 = -5/10
----- Testing shorthand arithmetic assignment using integers
-1/3 += 3 = 8/3
8/3 -= 3 = -1/3
-1/3 *= 3 = -3/3
-3/3 /= 3 = -3/9
----- Testing increment/decrement prefix and postfix
Now g = -1/3
g++ = -1/3
Now g = 2/3
++g = 5/3
Now g = 5/3
g-- = 5/3
Now g = 2/3
--g = -1/3
Now g = -1/3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.