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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
double setDollarProfitLoss(double[], double[], double[], int);
double setPercentProfitLoss(double[], double[], double[], int);
double findLargestDollarGain(double[],double[], double[], int);
double findLargestDollarLoss(double[],double[], double[], int);
int main()
{
string name[5];
double shares[5], bprice[5], cprice[5];
double largestGain;
double largestLoss;
ifstream inFile;
ofstream outFile;
inFile.open("C:homework1.txt");
outFile.open("C:homework2.txt");
if(!inFile.good())
cout<<"error"<<endl;
else
{ for(int i=0; i<5; i++)
inFile>>name[i]>>shares[i]>>bprice[i]>>cprice[i];
}
for(int h=0; h < 5; h++)
{
setDollarProfitLoss(shares, bprice, cprice, h);
setPercentProfitLoss(shares, bprice, cprice, h);
largestGain = findLargestDollarGain(shares, bprice, cprice, h);
largestLoss = findLargestDollarLoss(shares, bprice, cprice, h);
}
for (int k=0; k<5; k++)
outFile<<name[k]<< setw(12) << fixed << setprecision(2) << shares[k]<< setw(10) << fixed << setprecision(2) << bprice[k]<< setw(10) << fixed << setprecision(2) << cprice[k]<< setw(12) << fixed << setprecision(2) << setDollarProfitLoss(shares, bprice, cprice, k) << setw(10) << fixed << setprecision(2) << setPercentProfitLoss(shares, bprice, cprice, k) << endl;
outFile<<endl;
outFile<<"Largest gain: $"<< fixed << setprecision(2) << largestGain << endl;
outFile<<"Largest loss: $"<< fixed << setprecision(2) << largestLoss << endl;
inFile.close();
outFile.close();
return 0;
}
double setDollarProfitLoss(double shares1[], double bprice1[], double cprice1[], int count)
{
double profitloss;
profitloss = ((cprice1[count] - bprice1[count]) * shares1[count]);
return profitloss;
}
double setPercentProfitLoss(double shares2[], double bprice2[], double cprice2[], int counter)
{
double percentloss;
percentloss= (((cprice2[counter] - bprice2[counter]) * shares2[counter])/ (bprice2[counter] * shares2[counter])) * 100;
return percentloss;
}
double findLargestDollarGain(double shares3[], double bprice3[], double cprice3[], int numcount)
{
double dollarProfitGain[5];
double largestGain_so_far = 0.0;
for(int g=0; g<5; g++)
{
dollarProfitGain[g] = ((cprice3[numcount] - bprice3[numcount]) * shares3[numcount]);
if (largestGain_so_far < dollarProfitGain[g])
largestGain_so_far = dollarProfitGain[g];
}
return largestGain_so_far;
}
double findLargestDollarLoss(double shares4[], double bprice4[], double cprice4[], int ncount)
{
double dollarProfitLoss[5];
double largestLoss_so_far = 0.0;
for(int d=0; d<5; d++)
{
dollarProfitLoss[d] = ((cprice4[ncount] - bprice4[ncount]) * shares4[ncount]);
if(largestLoss_so_far > dollarProfitLoss[d])
largestLoss_so_far = dollarProfitLoss[d];
}
return largestLoss_so_far;
}
| |