Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Stocks with while
- //Jonny Gerold
- //cs36
- //11-18-08
- #include<stdio.h>
- void enter_info(char name[15], int *num_shares, float *buy_price, float *cur_price, float *yrly_fees) {
- printf("Please enter the stock name: ");
- gets(name);
- printf("How many stocks of %s did you buy: ", name);
- scanf("%d", num_shares);
- printf("Please enter your purchase price, current price, and yearly fees of %s: ", name);
- scanf("%f%f%f", buy_price, cur_price, yrly_fees);
- }
- void calc(int num_shares, float *ini_cost, float *cur_cost, float *profit, float buy_price, float cur_price, float yrly_fees) {
- *ini_cost = num_shares * buy_price;
- *cur_cost = num_shares * cur_price;
- *profit = *cur_cost - *ini_cost - yrly_fees;
- }
- int print(float ini_cost, float cur_cost, float profit) {
- printf("Initial cost is %0.2f\n", ini_cost);
- printf("Current cost is %0.2f\n", cur_cost);
- if (profit > 0) {
- printf("Profit is %0.2f\n", profit);
- return 1;
- } else
- if (profit < 0) {
- printf("You lost %0.2f\n", profit);
- return -1;
- }
- else {
- printf("You broke even");
- return 0;
- }
- }
- void print_t(float p, float p1, float p2, float p3, float p4) {
- float total;
- total = p + p1 + p2 + p3 + p4;
- printf("You had a total profit of %0.2f\n", total);
- }
- int main() {
- //variables for adding total profit/loss/break even at the end
- int even=0, loss=0, total_profit=0, tmp=0;
- char name[15];
- int num_shares;
- float ini_cost, cur_cost, profit, buy_price, cur_price, yrly_fees;
- enter_info(name, &num_shares, &buy_price, &cur_price, &yrly_fees);
- while (name != -999) {
- calc(num_shares, &ini_cost, &cur_cost, &profit, buy_price, cur_price, yrly_fees);
- tmp = print(ini_cost, cur_cost, profit);
- //prints number of break even/ negative/ and positive
- if (tmp == 0) {
- even++;
- }
- if (tmp < 0) {
- loss++;
- }
- if (tmp > 0) {
- total_profit++;
- }
- enter_info(name, &num_shares, &buy_price, &cur_price, &yrly_fees);
- __fpurge(stdin);
- }
- }
Add Comment
Please, Sign In to add comment