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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include "lab2.h"
using namespace std;
DollarAmount::DollarAmount(double num1, double num2)
{
dollar = num1;
cents = num2;
}
istream& operator >> (istream& ins, DollarAmount& arg)
{
double num1 = 0.00;
double num2 = 0.00;
//int num1 = 0;
//int num2 = 0;
char ch;
string s;
double num3;
bool wrongInput = false;
//ins >> num1 >> ch >> num2;
ins >> num3;
s = to_string(num3);
arg.number = s;
int index = s.find(".");
num1 = atof(s.substr(0, index).c_str());
num2 = atof(s.substr(index, 3).c_str());
do
{
if (wrongInput == true)
{
cout << "Enter the expenditure record (e.g., $1.95, coffee, enter -1 to end):$";
ins >> num1 >> ch >> num2;
}
else if (cin.fail() || num1 < 0 || num1 > 9999 || num2 > 99) //in case the above fails, e.g., ten dollor five cents...
{
cout << "Wrong input types. Try again:\n";
cin.clear(); //clear the error flags in cin
cin.ignore(2048, '\n'); //ignore everthing in the input buffer, up to 2048 char,
//up to the newline char =< ignore the rest of the line
wrongInput = true;
}
else
wrongInput = false;
} while (wrongInput == true || num1 < 0 || num1 > 9999 || num2 > 99); // if input false ask for input again
arg.dollar = num1;
arg.cents = num2;
arg.maxsequence = arg.getmaxsequence() + 1;
return ins;
}
ostream& operator << (ostream& out, const DollarAmount& arg)
{
out << arg.getDollar() + arg.getCent();
return out;
}
DollarAmount operator + (const DollarAmount& arg1, const DollarAmount& arg2)
{
DollarAmount temp;
double sum = 0.00;
double x = 0.00;
double y = 0.00;
string z;
temp.dollar = 0.00;
temp.cents = 0.00;
x = arg1.dollar + arg1.cents;
y = arg2.dollar + arg2.cents;
sum = x + y;
cout << "This is the sum: " << sum << endl;
z = to_string(sum);
int index = z.find(".");
temp.dollar = atof(z.substr(0, index).c_str());
temp.cents = atof(z.substr(index + 1, 2).c_str());
cout << "This is the dollar: " << temp.getDollar() << endl;
cout << "This is the cents: " << temp.getCent() << endl;
return temp;
}
bool operator > (const DollarAmount& arg1, const DollarAmount& arg2)
{
DollarAmount temp;
double x, y;
x = arg1.dollar + arg1.cents;
y = arg2.dollar + arg2.cents;
if (x > y)
return true;
else
return false;
}
void DollarAmount::sortobjects(DollarAmount a[], int length)
{
int i;
int last = length - 1; //point to the index of last element in unsorted part of the array
for (int pass = 0; pass < length; pass++)
{
//a[0]...a[last] is not sorted
//repeated bubble the largest element in this range to the end ...
for (i = 0; i <= last - 1; i++)
{
if (a[i] > a[i + 1])
{
//swap
DollarAmount tmp;
tmp = a[i + 1];
a[i + 1] = a[i];
a[i] = tmp;
}
}
//at this point, the largest element in a[0...last] is stored in a[last]
//unsorted part is now a[0...last-1]
last = last - 1;
}
}
void DollarAmount::median(DollarAmount a[], int length)
{
DollarAmount temp;
int mid;
double average = 0.00;
double x = 0.00;
double y = 0.00;
if (length % 2 == 0)
{
mid = length / 2;
int test = mid - 1;
x = a[mid].dollar + a[mid].cents;
y = a[test].dollar + a[test].cents;
average = (x + y) / 2.00;
temp.avg = average;
cout << temp.getavg();
}
else
{
mid = length / 2;
x = a[mid].dollar + a[mid].cents;
average = x;
temp.avg = average;
cout << temp.getavg();
}
}
|