//Micah Krosby CS1A Ch. 3, P. 147-148, # 21
/*******************************************************************************
*
* CALCULATE PROFIT ON STOCK TRADES
* _____________________________________________________________________________
*
* This program calculates the profit or loss on shares bought at one rate, sold
* at another, with a buying commission rate and a selling commission rate.
*
* FORMULAE-
* Amount Paid = Number of Shares * Price per Share
* Buy Commission = Amount Paid * Commission Rate for Buy
* Amount Recieved = Number of Shares * Price per Share
* Sell Commission = Amount Received * Commission Rate for Sale
* Profit = Amount Received - Amount Paid - Buy Commission - Sell Commission
* _____________________________________________________________________________
*
* INPUTS-
* numShares : Number of shares
* pricePerShare : Price per share bought
* commissionRateBuy : Commission rate when buying
* commissionRateSell : Commission rate when selling
* profitPerSale : Price per share sold
*
* OUTPUTS-
* amountPaid : Amount paid for shares
* commissionBuy : Commission price for buying shares
* amountRecieved : Amount received for share sale
* commissionSale : Commission price for selling shares
* profit : Total net profit for trade
* profitWord : Keyword to identify whether trade was a profit or loss
*
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
unsigned short int numShares; //INPUT- Number of shares
float pricePerShare; //INPUT- Price per share
float commissionRateBuy; //INPUT- Commission rate when buying
float commissionRateSale; //INPUT- Commission rate when selling
float profitPerSale; //INPUT- Profit per share sold
float amountPaid; //OUTPUT- Amount paid for shares
float commissionBuy; //OUTPUT- Commission for buying shares
float amountReceived; //OUTPUT- Amount recieved for share sale
float commissionSale; //OUTPUT- Commission for selling shares
float profit; //OUTPUT- Total profit for trade
string profitWord; //OUTPUT- Profit or loss keyword
//Initialize inputs
numShares = 1000;
pricePerShare = 32.87;
commissionRateBuy = 0.02;
commissionRateSale = 0.02;
profitPerSale = 33.92;
//Calculate outputs
amountPaid = numShares * pricePerShare;
commissionBuy = amountPaid * commissionRateBuy;
amountReceived = numShares * profitPerSale;
commissionSale = amountReceived * commissionRateSale;
profit = amountReceived - amountPaid - commissionBuy - commissionSale;
//Positive or negative profit
profitWord = "Total ";
if (profit >= 0) {
profitWord += "Profit";
}
else {
profitWord += "Loss";
}
//Output
cout << setw(30) << left << "Amount Paid" << "$" << setw(20) << right
<< fixed << setprecision(2) << amountPaid << endl;
cout << setw(30) << left << "Commission when Bought" << "$" << setw(20)
<< right << fixed << setprecision(2) << commissionBuy << endl;
cout << setw(30) << left << "Amount Received on Sale" << "$" << setw(20)
<< right << fixed << setprecision(2) << amountReceived << endl;
cout << setw(30) << left << "Commission when Sold" << "$" << setw(20)
<< right << fixed << setprecision(2) << commissionSale << endl;
cout << setw(30) << left << profitWord << "$" << setw(20) << right
<< fixed << setprecision(2) << abs(profit) << endl;
return 0;
}