1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
/*
Dustin Nieffenegger, COMS 229
Page 374 Problem 19
Stock Profit
The Profit from the sale of a stock can be calculated as follows:
Profit = ((NS * SP)-SC)-((NS * PP) + PC)
where NS is the number of shares, SP is the sale price per share, SC is the sale commis-
sion paid, PP is the purchase price per share, and PC is the purchase commission paid.
If the calculation yields a positive value, then the sale of the stock resulted in a profit.
If the calculation yields a negative number, then the sale resulted in a loss.
Write a function that accepts as arguments the number of shares, the purchase price
per share, the purchase commission paid, the sale price per share, and the sale com-
mission paid. The function should return the profit (or loss) from the sale of stock.
Demonstrate the function in a program that asks the user to enter the necessary data
and displays the amount of the profit or loss.
*/
#include <iostream>
using namespace std;
int NS; // Number of shares
double SP; // Selling Price
double SC; // Sales commission paid
double PP; // Purchase Price
double PC; // Purchase commission paid
// Function prototypes
double profit();
void warning();
int main()
{
cout << "You are selling stock.\n";
// Input Number of shares
cout << "How many shares are you selling?\n";
cin >> NS;
while (NS < 1){
cout << "You must sell at least one share.\n";
cout << "How many shares are you selling?\n";
cin >> NS; }
// Input Sales Price
cout << "How much are you selling the stock for?\n";
cin >> SP;
while (SP <= 0.0){
warning();
cout << "How much are you selling the stock for?\n";
cin >> SP; }
// Input Sales commission
cout << "How much must you pay the broker?\n";
cin >> SC;
while (SC <= 0.0){
warning();
cout << "How much must you pay the broker?\n";
cin >> SC; }
// Input Purchase price
cout << "How much did you initially buy the share for?\n";
cin >> PP;
while (PP <= 0.0){
warning();
cout << "How much did you initially buy the share for?\n";
cin >> PP; }
// Input Purchase commission
cout << "How much did you initially pay the broker?\n";
cin >> PC;
while (PC <= 0.0){
warning();
cout << "How much did you initially pay the broker?\n";
cin >> PC; }
cout << "Total profit is " << profit(NS,SP,SC,PP,PC);
return 0;
}
double profit(int NS, double SP, double SC, double PP, double PC)
{
double result;
result = (((double)NS * SP)-SC)-(((double)NS * PP) + PC);
return result;
}
void warning()
{
cout << "Amount must be greater than 0.00 USD.\n";
}
|