fork download
  1. // Renee V. Lopez CS1A Chapter 3, P. 147, #21
  2. /*******************************************************************************
  3.  * Calculate Net Profit from Stock Transaction
  4.  * _____________________________________________________________________________
  5.  * This program calculates the net profit from a stock transaction by considering
  6.  * the number of shares bought and sold, the price of the stock at both times,
  7.  * and the commission fees.
  8.  * _____________________________________________________________________________
  9.  * INPUT
  10.  * sharesBought - the number of shares bought
  11.  * stockBuyPrice - the price per share at purchase
  12.  * COM_RATE - the commission rate
  13.  * sharesSold - the number of shares sold
  14.  * stockSellPrice - the price per share at sale
  15.  *
  16.  * OUTPUT
  17.  * buyTotal - the total amount paid for buying shares
  18.  * commissionBuy - the commission paid when buying shares
  19.  * sellTotal - the total amount received from selling shares
  20.  * commissionSell - the commission paid when selling shares
  21.  * netProfit - the final net profit of the transaction (negative if a loss)
  22.  ******************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. // INPUT - Variable declarations
  31. float sharesBought; // the number of shares bought
  32. float stockBuyPrice; // the price per share at purchase
  33. const float COM_RATE = 0.02; // the commission rate
  34. float buyTotal; // the total amount paid for buying shares
  35. float commissionBuy; // the commission fee when buying shares
  36.  
  37. float sharesSold; // the number of shares sold
  38. float stockSellPrice; // the price per share at sale
  39. float sellTotal; // the total amount received from selling shares
  40. float commissionSell; // the commission fee when selling shares
  41.  
  42. // OUTPUT - Variable declaration
  43. float netProfit; // final net profit of the transaction
  44.  
  45. // Calculate the commission fee and total cost for buying shares
  46. sharesBought = 1000;
  47. stockBuyPrice = 32.87;
  48. buyTotal = sharesBought * stockBuyPrice;
  49. commissionBuy = buyTotal * COM_RATE;
  50.  
  51. // Calculate the commission fee and total amount received when selling shares
  52. sharesSold = 1000;
  53. stockSellPrice = 33.92;
  54. sellTotal = sharesSold * stockSellPrice;
  55. commissionSell = sellTotal * COM_RATE;
  56.  
  57. // Calculate the net profit of the transaction
  58. netProfit = sellTotal - buyTotal - (commissionBuy + commissionSell);
  59.  
  60. // Output results to console
  61. cout << "Joe bought " << sharesBought << " shares for $" << fixed << setprecision(2)
  62. << buyTotal << " with a commission fee of $" << commissionBuy << ".\n";
  63.  
  64. cout << "Joe sold " << sharesSold << " shares for $" << fixed << setprecision(2)
  65. << sellTotal << " with a commission fee of $" << commissionSell << ".\n";
  66.  
  67. cout << "Joe made a net profit of $" << fixed << setprecision(2) << netProfit << ".";
  68.  
  69. return 0;
  70. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Joe bought 1000 shares for $32870.00 with a commission fee of $657.40.
Joe sold 1000.00 shares for $33920.00 with a commission fee of $678.40.
Joe made a net profit of $-285.80.