Code:
#include <stdio.h>
void input(char *stock_name, char *purchase_date, int *num_shares, float *buying_price, float *current_price, float *yearly_fees)
{
printf("Enter stock name: ");
gets(stock_name);
printf("Enter purchase date: ");
gets(purchase_date);
printf("Enter number of shares: ");
scanf("%d", &(*num_shares));
printf("Enter buying price per share: ");
scanf("%f", &(*buying_price));
printf("Enter current price per share: ");
scanf("%f", &(*current_price));
printf("Enter yearly fees: ");
scanf("%f", &(*yearly_fees));
fflush(stdin);
}
float calcProfit(float buying_price, float current_price, float *yearly_fees, float *initial_cost, int *num_shares, float *current_cost, float *profit, float *total)
{
*initial_cost = *num_shares * buying_price;
*current_cost = *num_shares * current_price;
*profit = *current_cost - *initial_cost - *yearly_fees;
*total = *total + *profit;
return *profit;
}
void calcProfit2(float profit, int *pos_profit, int *neg_profit, int *even)
{
if (profit>0)
(*pos_profit)++;
else if (profit<0)
(*neg_profit)++;
else
(*even)++;
}
void print1(char *stock_name, char *purchase_date, float initial_cost, float current_cost)
{
printf("\nStock Name: %s\n", stock_name);
printf("Purchase Date: %s\n", purchase_date);
printf("Initial Cost: $%0.2f\n", initial_cost);
printf("Current Cost: $%0.2f\n", current_cost);
}
void print2(float total, int pos_profit, int neg_profit, int even)
{
printf("Total Profit: $%0.2f\n", total);
printf("Number of stocks with positive profit: %d\n", pos_profit);
printf("Number of stocks with negative profit: %d\n", neg_profit);
printf("Number of stocks broke even: %d\n", even);
}
void main()
{
char stock_name[9], purchase_date[9];
int num_shares, pos_profit=0, neg_profit=0, even=0;
float buying_price, current_price, yearly_fees, initial_cost=0, current_cost=0, profit=0, total=0;
input(stock_name, purchase_date, &num_shares, &buying_price, ¤t_price, &yearly_fees);
calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, ¤t_cost, &profit, &total);
calcProfit2(profit, &pos_profit, &neg_profit, &even);
print1(stock_name, purchase_date, initial_cost, current_cost);
printf("Profit: $%0.2f\n\n", profit);
input(stock_name, purchase_date, &num_shares, &buying_price, ¤t_price, &yearly_fees);
calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, ¤t_cost, &profit, &total);
calcProfit2(profit, &pos_profit, &neg_profit, &even);
print1(stock_name, purchase_date, initial_cost, current_cost, profit);
input(stock_name, purchase_date, &num_shares, &buying_price, ¤t_price, &yearly_fees);
calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, ¤t_cost, &profit, &total);
calcProfit2(profit, &pos_profit, &neg_profit, &even);
print1(stock_name, purchase_date, initial_cost, current_cost, profit);
print2(total, pos_profit, neg_profit, even);
}