Stock
stock0.c
stock0.c
/* File: stock0.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of one stock in a portfolio */
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
/* Set data values */
shares = 57
sell_whole = 11;
sell_numer = 3;
sell_denomin = 8;
/* Print data value */
/* Calculate portfolio value */
portfolio_value = shares * sell_whole + sell_numer / sell_denomin;
/* Print result */
stock1
stock1
stock1.c
stock1.c
/* File: stock1.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of one stock in a portfolio */
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
float portfolio_value; /* the value of the portfolio */
/* Set data values */
shares = 57;
sell_whole = 11;
sell_numer = 3;
sell_denomin = 8;
/* Print data value */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_numer,sell_denomin);
/* Calculate portfolio value */
portfolio_value = shares * sell_whole + sell_numer / sell_denomin;
/* Print result */
prinf("\nThe value of your portfolio is $%f\n",portfolio_value);
}
stock1.debug.c
stock1.debug.c
/* File: stock1.debug.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of one stock in a portfolio */
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
float portfolio_value; /* the value of the portfolio */
float price;
/* Set data values */
shares = 57;
sell_whole = 11;
sell_numer = 3;
sell_denomin = 8;
/* Print data value */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
/* Calculate portfolio value */
portfolio_value = shares * ( sell_whole + sell_numer / sell_denomin);
price = (sell_whole + sell_numer / sell_denomin);
printf("debug: price = %f\n",price);
portfolio_value = shares * price;
/* Print result */
printf("\nThe value of your portfolio is $%f\n",portfolio_value);
}
stock1.fixed.c
stock1.fixed.c
/* File: stock1.fixed.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of one stock in a portfolio */
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
float portfolio_value; /* the value of the portfolio */
float price;
/* Set data values */
shares = 57;
sell_whole = 11;
sell_numer = 3;
sell_denomin = 8;
/* Print data value */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
/* Calculate portfolio value */
portfolio_value = shares * (sell_whole +
(float) sell_numer / (float) sell_denomin);
/*
price = (sell_whole + sell_numer / sell_denomin);
printf("debug: price = %f\n",price);
portfolio_value = shares * price;
*/
/* Print result */
printf("\nThe value of your portfolio is $%f\n",portfolio_value);
}
stock2
stock2
stock2.c
stock2.c
/* File: stock2.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of one stock in a portfolio reading
the data at run time */
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
float portfolio_value; /* the value of the portfolio */
/* Get the number of shares values */
printf("Enter number of shares: ");
scanf("%d", &shares);
/* Get the selling price */
printf("Enter stock selling price as whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
/* Calculate portfolio value */
portfolio_value = shares * (sell_whole +
(float) sell_numer / (float) sell_denomin);
/* Print result */
printf("\nThe value of your portfolio is $%f\n",portfolio_value);
}
stock2.dat
stock2.dat
57 11 3 8
stock3
stock3
stock3.c
stock3.c
/* File: stock3.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of one stock in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio */
#include <stdio.h>
float stock_price(int shares, int whole, int num, int denom);
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
int main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost; /* the cost of the portfolio */
float portfolio_value; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
printf("Enter number of shares: ");
scanf("%d", &shares);
/* Get the selling price */
printf("Enter stock selling price as whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* Calculate portfolio cost */
portfolio_cost = stock_price(shares,buy_whole,
buy_numer,buy_denomin);
/* Calculate portfolio value */
portfolio_value = stock_price(shares,sell_whole,
sell_numer,sell_denomin);
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/*
printf("debug:enter stock_price:shares=%d whole=%d num=%d denom=%d\n",
shares, whole, num, denom);
*/
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/*
printf("debug: exit stock_price: %6.3f\n", value);
*/
/* Return the value */
return value;
}
stock4
stock4
stock4. count.c
stock4. count.c
/* File: stock4.count.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Counter used for number of stocks */
float stock_price(int, int, int, int);
main()
{ /* Variable Declarations */
int num_stocks; /* number of stocks in portfolio */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost; /* the cost of the portfolio */
float portfolio_value; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Initialize cost and value */
portfolio_cost = 0.0;
portfolio_value = 0.0;
/* Get the number of stocks */
printf("Enter the number of stocks to process: ");
scanf("%d", &num_stocks);
/* While there are more stocks to process */
while( num_stocks > 0 )
{ /* Get the number of shares values */
printf("Enter number of shares: ");
scanf("%d", &shares);
/* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,sell_denomin);
/* Update stock counter */
num_stocks = num_stocks - 1;
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
stock4.eof.c
stock4.eof.c
/* File: stock4.eof.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Data is read until end of file */
#include <stdio.h>
float stock_price(int, int, int, int);
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
int flag; /* result of scanf flag */
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
flag = scanf("%d", &shares);
/* While there are more stocks to process */
while( flag != EOF )
{ /* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,sell_denomin);
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
flag = scanf("%d", &shares);
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
stock4.eof2.c
stock4.eof2.c
/* File: stock4.eof2.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Data is read until end of file */
#include <stdio.h>
float stock_price(int, int, int, int);
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
/* While there are more stocks to process */
while( scanf("%d", &shares) != EOF )
{ /* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,sell_denomin);
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
stock4.spv.c
stock4.spv.c
/* File: stock4.spv.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. End of data occurs when shares entered is 0 */
float stock_price(int, int, int, int);
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
printf("Enter number of shares(0 to quit): ");
scanf("%d", &shares);
/* While there are more stocks to process */
while( shares != 0 )
{ /* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,buy_denomin);
/* Accumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,sell_denomin);
/* Get the number of shares values */
printf("Enter number of shares(0 to quit): ");
scanf("%d", &shares);
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
stock4._count.dat
stock4._count.dat
2
57
11 3 8
9 1 4
6
94 1 4
50 1 2
stock4_eof2.dat
stock4_eof2.dat
57
11 3 8
9 1 4
6
94 1 4
50 1 2
stock4_spv.c
stock4_spv.c
57
11 3 8
9 1 4
6
94 1 4
50 1 2
0
stock5
stock5
stock5.c
stock5.c
/* File: stock5.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Data is read until end of file.
The program has been improved to test for valid data entry. */
#include <stdio.h>
#define FALSE 0
#define TRUE 1
float stock_price(int, int, int, int);
int data_ok(int, int, int);
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
/* While there are more stocks to process */
while( scanf("%d", &shares) != EOF )
{ /* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* if the data is ok */
if( data_ok(sell_whole,sell_numer,sell_denomin))
{ /* Accumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,
buy_denomin);
/* Accumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,
sell_denomin);
}
/* otherwise print an error message and ignore the stock */
else printf("\aINVALID stock data - stock ignored\n");
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
int data_ok(int whole, int num, int denom)
/* Given: the stock price values
Returns: TRUE (1) if a valid stock price, FALSE(0) otherwise
*/
{
if(denom == 0)
return FALSE; //return 0
return TRUE; //return 1
}
stock5.more.c
stock5.more.c
/* File: stock5.more.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Data is read until end of file.
The program has been improved to test for valid data entry. */
#include <stdio.h>
#define FALSE 0
#define TRUE 1
float stock_price(int, int, int, int);
int data_ok(int, int, int);
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Initialize loop condition */
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
/* While there are more stocks to process */
while( scanf("%d", &shares) != EOF )
{ /* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* if the data is ok */
if( data_ok(sell_whole,sell_numer,sell_denomin) &&
data_ok(buy_whole,buy_numer,buy_denomin))
{ /* Calculate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,
buy_denomin);
/* Calculate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,
sell_denomin);
}
/* otherwise print an error message and ignore the stock */
else printf("\aINVALID stock data - stock ignored\n");
/* Update loop condition */
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
int data_ok(int whole, int num, int denom)
/* Given: the stock price values
Returns: TRUE if a valid stock price, FALSE otherwise
*/
{
/* if any of the price data items are negative, the data is bad */
if((whole < 0) || (num < 0) || (denom < 0)) return FALSE;
/* if the numerator is greater than the denominator, the data is bad */
if( num >= denom ) return FALSE;
/* if the denominator is not 2,4, or 8, the data is bad */
if((denom != 2) && (denom != 4) && (denom != 8)) return FALSE;
/* otherwise the data is ok */
return TRUE;
}
stock6.c
stock6.c
/* File: stock6.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Data is read until end of file.
The program has been improved to test for valid data entry. */
#include <stdio.h>
#include "stock_utils.h"
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
/* While there are more stocks to process */
while( scanf("%d", &shares) != EOF )
{ /* Get the selling price */
printf("Enter stock selling price as"
" whole numerator denominator: ");
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
printf("Enter stock purchase price as"
" whole numerator denominator: ");
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* if the data is ok */
if( data_ok(sell_whole,sell_numer,sell_denomin) &&
data_ok(buy_whole,buy_numer,buy_denomin))
{ /* Accumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,
buy_denomin);
/* Accumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,
sell_denomin);
}
/* otherwise print an error message and ignore the stock */
else printf("\aINVALID stock data - stock ignored\n");
/* Get the number of shares values */
printf("Enter number of shares(EOF to quit): ");
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
stock7.c
stock7.c
/* File: stock7.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Program to compute the value of multiple stocks in a portfolio reading
the data at run time. The program computes our net profit/loss
on this portfolio. Data is read until end of file.
The program has been improved to test for valid data entry. */
#include <stdio.h>
#include "stock_utils.h"
#define INTERACT
main()
{ /* Variable Declarations */
int shares; /* number of shares owned */
int sell_whole; /* selling price information */
int sell_numer, sell_denomin;
int buy_whole; /* buying price information */
int buy_numer, buy_denomin;
float portfolio_cost = 0.0; /* the cost of the portfolio */
float portfolio_value = 0.0; /* the value of the portfolio */
float profit; /* the profit or loss */
/* Get the number of shares values */
#ifdef INTERACT
printf("Enter number of shares(EOF to quit): ");
#endif
/* While there are more stocks to process */
while( scanf("%d", &shares) != EOF )
{ /* Get the selling price */
#ifdef INTERACT
printf("Enter stock selling price as"
" whole numerator denominator: ");
#endif
scanf("%d %d %d", &sell_whole, &sell_numer, &sell_denomin);
/* Get the purchase price */
#ifdef INTERACT
printf("Enter stock purchase price as"
" whole numerator denominator: ");
#endif
scanf("%d %d %d", &buy_whole, &buy_numer, &buy_denomin);
/* Print data values */
printf("You have %d shares of stock\n",shares);
printf("selling at $%d %d/%d\n",
sell_whole,sell_numer,sell_denomin);
printf("which you bought at $%d %d/%d\n",
buy_whole,buy_numer,buy_denomin);
/* if the data is ok */
if( data_ok(sell_whole,sell_numer,sell_denomin) &&
data_ok(buy_whole,buy_numer,buy_denomin))
{ /* Alccumulate portfolio cost */
portfolio_cost = portfolio_cost +
stock_price(shares,buy_whole,buy_numer,
buy_denomin);
/* Alccumulate portfolio value */
portfolio_value = portfolio_value +
stock_price(shares,sell_whole,sell_numer,
sell_denomin);
#ifdef DEBUG
printf("debug: portfolio_cost=%f portfolio_value=%f\n",
portfolio_cost,portfolio_value);
#endif
}
/* otherwise print an error message and ignore the stock */
else printf("\aINVALID stock data - stock ignored\n");
/* Get the number of shares values */
#ifdef INTERACT
printf("Enter number of shares(EOF to quit): ");
#endif
}
/* Calculate profit */
profit = portfolio_value - portfolio_cost;
/* Print result */
printf("\nYou bought stock for $%f.",portfolio_cost);
printf("The value of your portfolio is $%f\n",portfolio_value);
printf("Your net profit is $%f\n",profit);
}
stock_utils.c
stock_utils.c
/* File: stock_utils.c
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* This file contains stock portfolio utility functions for computing
an extended stock price and checking price validity */
#include "stock_utils.h"
#include "tfdef.h"
float stock_price(int shares, int whole, int num, int denom)
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
{ float value;
/* Compute the stock price */
value = shares * (whole + (float) num / (float) denom);
/* Return the value */
return value;
}
int data_ok(int whole, int num, int denom)
/* Given: the stock price values
Returns: TRUE if a valid stock price, FALSE otherwise
*/
{
/* if any of the price data items are negative, the data is bad */
if((whole < 0) || (num < 0) || (denom < 0)) return FALSE;
/* if the numerator is greater than the denominator, the data is bad */
if( num >= denom ) return FALSE;
/* if the denominator is not 2,4, or 8, the data is bad */
if((denom != 2) && (denom != 4) && (denom != 8)) return FALSE;
/* otherwise the data is ok */
return TRUE;
}
stock_utils.h
stock_utils.h
/* File: stock_utils.h
By: Tep Dobry
login: tep
Date:
Mod :
*/
/* Header file declaring stock utility functions */
float stock_price(int shares, int whole, int num, int denom);
/* Given: the number of shares, and stock price in whole, numerator
and denominator.
Return: the cost/value of this amount of stock.
*/
int data_ok(int whole, int num, int denom);
/* Given: the stock price values
Returns: TRUE if a valid stock price, FALSE otherwise
*/