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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
class stockType
{
public:
void inputStock (char, double, double, double, double, double, double);
void setStock (char, double, double, double, double, double, double);
void getStock (char&, double&, double&, double&, double&, double&, double&)const;
void printStock() const;
void showPriceDiff();
double calcPercentGain();
double showShares();
friend istream& operator >> (istream &in, const stockType &cStock);
friend ostream& operator<< (ostream &out, const stockType &cStock);
double getTotalAssets();
stockType(char, double, double, double, double, double, double);
stockType();
char ss;
double op, cp, th, tl, pc, sv;
double total_assets;
};
void stockType::inputStock (char symbol, double openingPrice, double closingPrice,
double todayHigh, double todayLow, double prevClose, double volume)
{
ifstream infile("Stocks.txt");
string line;
if( infile.is_open() )
{
while( getline(infile,line) )
{
cin >> symbol;
cin >> openingPrice;
cin >> closingPrice;
cin >> todayHigh;
cin >> todayLow;
cin >> prevClose;
cin >> volume;
}
}
}
void stockType::setStock (char symbol, double openingPrice, double closingPrice,
double todayHigh, double todayLow, double prevClose, double volume)
{
//*
ss = symbol; //*
//*
op = openingPrice; //*
//*
cp = closingPrice; //*
//*
th = todayHigh; //*
//*
tl = todayLow; //*
//*
pc = prevClose; //*
//*
sv = volume; //*
//*
}
void stockType::getStock(char& symbol, double& openingPrice, double& closingPrice,
double& todayHigh, double& todayLow, double& prevClose, double& volume) const
{
symbol = ss; openingPrice = op; closingPrice = cp; todayHigh = th;
todayLow = tl; prevClose = pc; volume = sv;
}
void stockType::printStock() const
{
cout << ss;
cout << op;
cout << cp;
cout << th;
cout << tl;
cout << pc;
cout << sv;
}
void stockType::showPriceDiff()
{ //*
cout << "Today's opening price was " << op << ", and the closing was " << cp << endl; //*
cout << "Today's high price was " << th << ", and the low price was " << tl << endl; //*
cout << "While the previous closing price was " << pc << endl; //*
} //*
double stockType::calcPercentGain() //*
{ //*
return (cp - pc) / pc * 100; //*
}
double stockType::showShares()
{
return sv;
}
istream& operator >> (istream &in, stockType &cStock)
{
// Since operator>> is a friend of the stockType class, we can access StockType members directly.
in >> cStock.ss;
in >> cStock.op;
in >> cStock.cp;
in >> cStock.th;
in >> cStock.tl;
in >> cStock.pc;
in >> cStock.sv;
// cout << cStock.total_assets << endl;
//*& cStock.total_assets += cStock.sv*cStock.pc;
return in;
}
ostream& operator<< (ostream &out, stockType &cStock)
{
cStock.calcPercentGain();
// operator<< is a friend of the StockType class, we can access StockTypes members directly.
cout << setw(8) << left << cStock.ss << right << fixed << setprecision(2) << setw(8) <<
cStock.op << fixed << setprecision(2) << setw(8) <<
cStock.cp << fixed << setprecision(2) << setw(8) <<
cStock.th << fixed << setprecision(2) << setw(8) <<
cStock.tl << fixed << setprecision(2) << setw(8) <<
cStock.pc << fixed << setprecision(2) << setw(12) <<
cStock.sv << fixed << setprecision(2) <<endl;
return out;
}
double stockType::getTotalAssets()
{
return 0.0;
}
stockType::stockType(char symbol, double openingPrice, double closingPrice,
double todayHigh, double todayLow, double prevClose, double volume)
{
if ('A' <= symbol && symbol < 'z')
ss = symbol; else ss = ' ';
if (0 <= openingPrice && openingPrice < 9999)
op = openingPrice;
else op = 0;
if (0 <= closingPrice && closingPrice < 9999)
cp = closingPrice;
else cp = 0;
if (0 <= todayHigh && todayHigh < 9999)
th = todayHigh;
else th = 0;
if (0 <= todayLow && todayLow < 9999)
tl = todayLow;
else tl = 0;
if (0 <= prevClose & prevClose < 9999)
pc = prevClose;
else pc = 0;
if (0 <= volume && volume < 999999)
sv = volume;
else sv = 0;
}
stockType::stockType()
{
ss = 'A';
op = 0;
cp = 0;
th = 0;
tl = 0;
pc = 0;
sv = 0;
};
int main()
{
stockType stock;
stock.printStock();
system ("pause");
return 0;
}
|