fork download
  1.  
  2. //Dione Torrevillas CS1A Chapter 6, P. 374, #19
  3. //
  4. /****************************************************************
  5.   *
  6.   * CALCULATE STOCK PROFIT
  7.   *_______________________________________________________________
  8.   * This program computes the profit or loss from selling
  9.   * shares of stock. It uses a function to calculate the
  10.   * profit based on the purchase price, selling price,
  11.   * and commissions paid.
  12.   *
  13.   * Computation is based on the formula:
  14.   * Profit = (Shares * Selling Price - Selling Commission)
  15.   * - (Shares * Purchase Price + Purchase Commission)
  16.   *_______________________________________________________________
  17.   * INPUT
  18.   * shares : Number of shares of stock
  19.   * purchasePrice : Purchase price per share
  20.   * purchaseComm : Purchase commission paid
  21.   * sellingPrice : Selling price per share
  22.   * sellingComm : Selling commission paid
  23.   *
  24.   * OUTPUT
  25.   * profit : Profit or loss from the stock sale
  26.   *
  27.   ****************************************************************/
  28.  
  29. #include <iostream>
  30. #include <iomanip>
  31. using namespace std;
  32.  
  33. // Function prototype
  34. double stockProfit(double, double, double, double, double);
  35.  
  36. int main()
  37. {
  38. double shares; //INPUT - Number of shares
  39. double purchasePrice; //INPUT - Purchase price per share
  40. double purchaseComm; //INPUT - Purchase commission
  41. double sellingPrice; //INPUT - Selling price per share
  42. double sellingComm; //INPUT - Selling commission
  43. double profit; //OUTPUT - Profit or loss
  44.  
  45. // Get the number of shares
  46. cout << "Enter the number of shares: ";
  47. cin >> shares;
  48.  
  49. // Get the purchase price per share
  50. cout << "Enter the purchase price per share: $";
  51. cin >> purchasePrice;
  52.  
  53. // Get the purchase commission
  54. cout << "Enter the purchase commission: $";
  55. cin >> purchaseComm;
  56.  
  57. // Get the selling price per share
  58. cout << "Enter the selling price per share: $";
  59. cin >> sellingPrice;
  60.  
  61. // Get the selling commission
  62. cout << "Enter the selling commission: $";
  63. cin >> sellingComm;
  64.  
  65. // Call the function to calculate profit
  66. profit = stockProfit(shares, purchasePrice,
  67. purchaseComm, sellingPrice,
  68. sellingComm);
  69.  
  70. // Display the profit or loss
  71. cout << fixed << setprecision(2);
  72.  
  73. if (profit > 0)
  74. {
  75. cout << "The stock profit is: $"
  76. << profit << endl;
  77. }
  78. else if (profit < 0)
  79. {
  80. cout << "The stock loss is: $"
  81. << -profit << endl;
  82. }
  83. else
  84. {
  85. cout << "There is no profit or loss." << endl;
  86. }
  87.  
  88. return 0;
  89. }
  90.  
  91. // Function definition
  92. // Calculates and returns the stock profit or loss
  93. double stockProfit(double shares, double purchasePrice,
  94. double purchaseComm, double sellingPrice,
  95. double sellingComm)
  96. {
  97. double profit; // Profit or loss from stock sale
  98.  
  99. // Calculate the stock profit or loss
  100. profit = ((shares * sellingPrice) - sellingComm)
  101. - ((shares * purchasePrice) + purchaseComm);
  102.  
  103. return profit;
  104. }
  105.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter the number of shares: Enter the purchase price per share: $Enter the purchase commission: $Enter the selling price per share: $Enter the selling commission: $The stock loss is: $0.00