Guest User

Untitled

a guest
May 27th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. //Stocks with while
  2. //Jonny Gerold
  3. //cs36
  4. //11-18-08
  5.  
  6. #include<stdio.h>
  7.  
  8. void enter_info(char name[15], int *num_shares, float *buy_price, float *cur_price, float *yrly_fees) {
  9. printf("Please enter the stock name: ");
  10. gets(name);
  11. printf("How many stocks of %s did you buy: ", name);
  12. scanf("%d", num_shares);
  13. printf("Please enter your purchase price, current price, and yearly fees of %s: ", name);
  14. scanf("%f%f%f", buy_price, cur_price, yrly_fees);
  15. }
  16.  
  17. void calc(int num_shares, float *ini_cost, float *cur_cost, float *profit, float buy_price, float cur_price, float yrly_fees) {
  18. *ini_cost = num_shares * buy_price;
  19. *cur_cost = num_shares * cur_price;
  20. *profit = *cur_cost - *ini_cost - yrly_fees;
  21. }
  22.  
  23. int print(float ini_cost, float cur_cost, float profit) {
  24. printf("Initial cost is %0.2f\n", ini_cost);
  25. printf("Current cost is %0.2f\n", cur_cost);
  26.  
  27. if (profit > 0) {
  28. printf("Profit is %0.2f\n", profit);
  29. return 1;
  30. } else
  31. if (profit < 0) {
  32. printf("You lost %0.2f\n", profit);
  33. return -1;
  34. }
  35. else {
  36. printf("You broke even");
  37. return 0;
  38. }
  39. }
  40.  
  41. void print_t(float p, float p1, float p2, float p3, float p4) {
  42. float total;
  43. total = p + p1 + p2 + p3 + p4;
  44. printf("You had a total profit of %0.2f\n", total);
  45. }
  46.  
  47. int main() {
  48. //variables for adding total profit/loss/break even at the end
  49. int even=0, loss=0, total_profit=0, tmp=0;
  50.  
  51. char name[15];
  52. int num_shares;
  53. float ini_cost, cur_cost, profit, buy_price, cur_price, yrly_fees;
  54.  
  55. enter_info(name, &num_shares, &buy_price, &cur_price, &yrly_fees);
  56.  
  57. while (name != -999) {
  58.  
  59. calc(num_shares, &ini_cost, &cur_cost, &profit, buy_price, cur_price, yrly_fees);
  60. tmp = print(ini_cost, cur_cost, profit);
  61.  
  62. //prints number of break even/ negative/ and positive
  63. if (tmp == 0) {
  64. even++;
  65. }
  66. if (tmp < 0) {
  67. loss++;
  68. }
  69. if (tmp > 0) {
  70. total_profit++;
  71. }
  72.  
  73. enter_info(name, &num_shares, &buy_price, &cur_price, &yrly_fees);
  74.  
  75. __fpurge(stdin);
  76. }
  77.  
  78. }
Add Comment
Please, Sign In to add comment