fork(1) download
  1. //Ryan Shahriyarpour CS1A Chapter 6 Question 19
  2. /*****************************************************************************
  3.  *
  4.  * Compute Stock Profit
  5.  *
  6.  *********************
  7.  *
  8.  * This program calculates the profit or loss from the sale of a stock.
  9.  *
  10.  * *******************
  11.  * Profit is computed using the formula:
  12.  *
  13.  * Profit = ((NS * SP) - SC) - ((NS * PP) + PC)
  14.  * NS = shares, SP = sale price, SC = sale commission,
  15.  * PP = purchase price, and PC = purchase commission
  16.  *
  17.  * INPUT
  18.  * shares - number of shares
  19.  * purchasePrice - purchase price per share
  20.  * purchaseComm - purchase commission paid
  21.  * salePrice - sale price per share
  22.  * saleComm - sale commission paid
  23.  *
  24.  * OUTPUT
  25.  * The amount of the profit or loss from the sale
  26.  *
  27.  ****************************************************************************/
  28.  
  29. #include <iostream>
  30. #include <iomanip>
  31. using namespace std;
  32.  
  33. // Function prototype
  34. double stockProfit(int shares, double purchasePrice, double purchaseComm,
  35. double salePrice, double saleComm);
  36.  
  37. int main()
  38. {
  39. int shares; // Number of shares (NS)
  40. double purchasePrice; // Purchase price per share (PP)
  41. double purchaseComm; // Purchase commission paid (PC)
  42. double salePrice; // Sale price per share (SP)
  43. double saleComm; // Sale commission paid (SC)
  44. double profit; // Profit from the sale
  45.  
  46. cout << "Number of shares: ";
  47. cin >> shares;
  48. cout << "Purchase price per share: $";
  49. cin >> purchasePrice;
  50. cout << "Purchase commission paid: $";
  51. cin >> purchaseComm;
  52. cout << "Sale price per share: $";
  53. cin >> salePrice;
  54. cout << "Sale commission paid: $";
  55. cin >> saleComm;
  56.  
  57. profit = stockProfit(shares, purchasePrice, purchaseComm,
  58. salePrice, saleComm);
  59.  
  60. cout << fixed << setprecision(2);
  61. if (profit >= 0)
  62. cout << "\nThe sale resulted in a PROFIT of $" << profit << endl;
  63. else
  64. cout << "\nThe sale resulted in a LOSS of $" << -profit << endl;
  65.  
  66. return 0;
  67. }
  68.  
  69. // Returns profit (positive) or loss (negative) from stock sale
  70. double stockProfit(int shares, double purchasePrice, double purchaseComm,
  71. double salePrice, double saleComm)
  72. {
  73. double proceeds = (shares * salePrice) - saleComm; // Money received
  74. double cost = (shares * purchasePrice) + purchaseComm; // Money spent
  75. return proceeds - cost;
  76. }
Success #stdin #stdout 0s 5348KB
stdin
123
224
324
400
50
stdout
Number of shares: Purchase price per share: $Purchase commission paid: $Sale price per share: $Sale commission paid: $
The sale resulted in a PROFIT of $21274.00