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
|
#include <cmath>
using namespace std;
template <class T>
class Rectangle
{
private:
T length, width;
public:
Rectangle()
{
length = 0;
width = 0;
}
Rectangle(T l, T w)
{
length = l;
width = w;
}
void setLength(T l)
{
length = l;
}
void setWidth(T w)
{
width = w;
}
T getLength()
{
return length;
}
T getWidth()
{
return width;
}
bool checkGreatThan() const;
T getPerimeter() const;
T getArea() const;
};
template<class T>
bool Rectangle<T>::checkGreatThan() const
{
if (length > width)
return true;
else
return false;
}
// Perimeter
template<class T>
T Rectangle<T>::getPerimeter() const
{
T perimeter;
perimeter = length + length + width + width;
return perimeter;
}
// Area
template<class T>
T Rectangle<T>::getArea() const
{
T area;
area = length * width;
return area;
}
struct Fraction
{
public:
int numerator, denominator;
Fraction(int n = 0, int d = 0)
{
n = numerator;
d = denominator;
}
};
Fraction operator+(Fraction frac1, Fraction frac2)
{
Fraction result;
result.numerator = (frac1.numerator * frac2.denominator) + (frac2.numerator * frac1.denominator);
result.denominator = (frac1.denominator) * (frac2.denominator);
return result;
}
Fraction operator-(Fraction frac1, Fraction frac2)
{
Fraction result;
result.numerator = (frac1.numerator * frac2.denominator) - (frac2.numerator * frac1.denominator);
result.denominator = frac1.denominator * frac2.denominator;
return result;
}
Fraction operator*(Fraction frac1, Fraction frac2)
{
Fraction result;
result.numerator = frac1.numerator * frac2.numerator;
result.denominator = frac1.denominator * frac2.denominator;
return result;
}
bool operator>(Fraction frac1, Fraction frac2)
{
if ((frac1.numerator / frac1.denominator) > (frac2.numerator / frac2.denominator))
return true;
else
return false;
}
Fraction operator*(Fraction frac1, int number)
{
Fraction result;
result.numerator = frac1.numerator * number;
result.denominator = frac1.denominator;
return result;
}
Fraction operator*(int number, Fraction frac1)
{
Fraction result;
result.numerator = number * frac1.numerator;
result.denominator = number * frac1.denominator;
return result;
}
void PrintFraction(Fraction frac)
{
cout << frac.numerator << "/" << frac.denominator;
}
// RectangleMain.cpp
#include <iostream>
#include "RectangleClass.h"
using namespace std;
int main()
{
Fraction fractionOne, fractionTwo, fracArea, fracPerimeter, fracLength, fracWidth;
Rectangle<float> dRectangle;
Rectangle<Fraction> fRectangle;
float length, width, area, perimeter;
bool check, fracCheck;
cout << "Enter the length of the rectangle: ";
cin >> length;
dRectangle.setLength(length);
cout << "Enter the width of the rectangle: ";
cin >> width;
dRectangle.setWidth(width);
cout << endl;
check = dRectangle.checkGreatThan();
if (check == true)
cout << "The length is longer than the width of the rectangle." << endl;
else
cout << "The length is shorter than the width of the rectangle." << endl;
perimeter = dRectangle.getPerimeter();
cout << "The perimeter of the rectangle is " << perimeter << " units." << endl;
area = dRectangle.getArea();
cout << "The area of the rectangle is " << area << " units squared." << endl;
cout << "----------------------------------------------------------" << endl;
cout << "Enter the numerator of the length: ";
cin >> fractionOne.numerator;
fRectangle.setLength(fractionOne.numerator);
cout << "Enter the denominator of the length: ";
cin >> fractionOne.denominator;
fRectangle.setLength(fractionOne.denominator);
cout << "Enter the numerator of the width: ";
cin >> fractionTwo.numerator;
fRectangle.setWidth(fractionTwo.numerator);
cout << "Enter the denominator of the width: ";
cin >> fractionTwo.denominator;
fRectangle.setWidth(fractionTwo.denominator);
cout << "Length: ";
PrintFraction(fractionOne);
cout << "\nWidth: ";
PrintFraction(fractionTwo);
cout << endl;
fracCheck = operator>(fractionOne, fractionTwo);
if (fracCheck == true)
{
PrintFraction(fractionOne);
cout << " is greater than ";
PrintFraction(fractionTwo);
cout << "." << endl;
}
else
{
PrintFraction(fractionTwo);
cout << " is greater than ";
PrintFraction(fractionOne);
cout << "." << endl;
}
}
|