fork download
  1. //Micah Krosby CS1A Ch. 3, P. 147-148, # 21
  2. /*******************************************************************************
  3.  *
  4.  * CALCULATE PROFIT ON STOCK TRADES
  5.  * _____________________________________________________________________________
  6.  *
  7.  * This program calculates the profit or loss on shares bought at one rate, sold
  8.  * at another, with a buying commission rate and a selling commission rate.
  9.  *
  10.  * FORMULAE-
  11.  * Amount Paid = Number of Shares * Price per Share
  12.  * Buy Commission = Amount Paid * Commission Rate for Buy
  13.  * Amount Recieved = Number of Shares * Price per Share
  14.  * Sell Commission = Amount Received * Commission Rate for Sale
  15.  * Profit = Amount Received - Amount Paid - Buy Commission - Sell Commission
  16.  * _____________________________________________________________________________
  17.  *
  18.  * INPUTS-
  19.  * numShares : Number of shares
  20.  * pricePerShare : Price per share bought
  21.  * commissionRateBuy : Commission rate when buying
  22.  * commissionRateSell : Commission rate when selling
  23.  * profitPerSale : Price per share sold
  24.  *
  25.  * OUTPUTS-
  26.  * amountPaid : Amount paid for shares
  27.  * commissionBuy : Commission price for buying shares
  28.  * amountRecieved : Amount received for share sale
  29.  * commissionSale : Commission price for selling shares
  30.  * profit : Total net profit for trade
  31.  * profitWord : Keyword to identify whether trade was a profit or loss
  32.  *
  33. *******************************************************************************/
  34.  
  35. #include <iostream>
  36. #include <iomanip>
  37. #include <cmath>
  38. using namespace std;
  39.  
  40. int main() {
  41. unsigned short int numShares; //INPUT- Number of shares
  42. float pricePerShare; //INPUT- Price per share
  43. float commissionRateBuy; //INPUT- Commission rate when buying
  44. float commissionRateSale; //INPUT- Commission rate when selling
  45. float profitPerSale; //INPUT- Profit per share sold
  46. float amountPaid; //OUTPUT- Amount paid for shares
  47. float commissionBuy; //OUTPUT- Commission for buying shares
  48. float amountReceived; //OUTPUT- Amount recieved for share sale
  49. float commissionSale; //OUTPUT- Commission for selling shares
  50. float profit; //OUTPUT- Total profit for trade
  51. string profitWord; //OUTPUT- Profit or loss keyword
  52.  
  53. //Initialize inputs
  54. numShares = 1000;
  55. pricePerShare = 32.87;
  56. commissionRateBuy = 0.02;
  57. commissionRateSale = 0.02;
  58. profitPerSale = 33.92;
  59.  
  60. //Calculate outputs
  61. amountPaid = numShares * pricePerShare;
  62. commissionBuy = amountPaid * commissionRateBuy;
  63. amountReceived = numShares * profitPerSale;
  64. commissionSale = amountReceived * commissionRateSale;
  65. profit = amountReceived - amountPaid - commissionBuy - commissionSale;
  66.  
  67. //Positive or negative profit
  68. profitWord = "Total ";
  69. if (profit >= 0) {
  70. profitWord += "Profit";
  71. }
  72. else {
  73. profitWord += "Loss";
  74. }
  75.  
  76. //Output
  77. cout << setw(30) << left << "Amount Paid" << "$" << setw(20) << right
  78. << fixed << setprecision(2) << amountPaid << endl;
  79. cout << setw(30) << left << "Commission when Bought" << "$" << setw(20)
  80. << right << fixed << setprecision(2) << commissionBuy << endl;
  81. cout << setw(30) << left << "Amount Received on Sale" << "$" << setw(20)
  82. << right << fixed << setprecision(2) << amountReceived << endl;
  83. cout << setw(30) << left << "Commission when Sold" << "$" << setw(20)
  84. << right << fixed << setprecision(2) << commissionSale << endl;
  85. cout << setw(30) << left << profitWord << "$" << setw(20) << right
  86. << fixed << setprecision(2) << abs(profit) << endl;
  87. return 0;
  88. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Amount Paid                   $            32870.00
Commission when Bought        $              657.40
Amount Received on Sale       $            33920.00
Commission when Sold          $              678.40
Total Loss                    $              285.80