tampurus

Ramesh portfolio

Jan 16th, 2024
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define MAX_STOCKS 100
  7.  
  8. // Definition of a Stock structure
  9. struct Stock {
  10.     char symbol[10];
  11.     double price;
  12.     int quantity;
  13. };
  14.  
  15. // Definition of a Portfolio structure
  16. struct Portfolio {
  17.     struct Stock stocks[MAX_STOCKS];
  18.     int stockCount;
  19. };
  20.  
  21. // Function to add a stock to the portfolio
  22. void addStock(struct Portfolio *portfolio) {
  23.     // Check if the portfolio has space for more stocks
  24.     if (portfolio->stockCount < MAX_STOCKS) {
  25.         struct Stock newStock;
  26.  
  27.         printf("Enter stock symbol: ");
  28.         scanf("%s", newStock.symbol);
  29.  
  30.         printf("Enter stock price: ");
  31.         scanf("%lf", &newStock.price);
  32.  
  33.         printf("Enter quantity: ");
  34.         scanf("%d", &newStock.quantity);
  35.  
  36.         // Add the new stock to the portfolio and increment stock count
  37.         portfolio->stocks[portfolio->stockCount++] = newStock;
  38.  
  39.         printf("Stock added successfully.\n");
  40.     } else {
  41.         printf("Portfolio is full. Cannot add more stocks.\n");
  42.     }
  43. }
  44.  
  45. // Function to display all stocks in the portfolio
  46. void displayPortfolio(struct Portfolio *portfolio) {
  47.     printf("Portfolio:\n");
  48.     for (int i = 0; i < portfolio->stockCount; ++i) {
  49.         printf("%s - Price: %.2f, Quantity: %d\n",
  50.                portfolio->stocks[i].symbol,
  51.                portfolio->stocks[i].price,
  52.                portfolio->stocks[i].quantity);
  53.     }
  54. }
  55.  
  56. // Function to simulate market changes on the stock prices
  57. void simulateMarketChanges(struct Portfolio *portfolio) {
  58.     srand(time(NULL));
  59.  
  60.     for (int i = 0; i < portfolio->stockCount; ++i) {
  61.         // Generate a random movement between -0.10 and 0.10
  62.         double movement = (rand() % 21 - 10) / 100.0;
  63.         portfolio->stocks[i].price *= (1 + movement);
  64.     }
  65.  
  66.     printf("Market changes simulated.\n");
  67. }
  68.  
  69. // Function to calculate the total value of the portfolio
  70. double calculatePortfolioValue(struct Portfolio *portfolio) {
  71.     double totalValue = 0.0;
  72.  
  73.     for (int i = 0; i < portfolio->stockCount; ++i) {
  74.         totalValue += portfolio->stocks[i].price * portfolio->stocks[i].quantity;
  75.     }
  76.  
  77.     return totalValue;
  78. }
  79.  
  80. // Main function to interact with the portfolio
  81. int main() {
  82.     struct Portfolio myPortfolio = {0};
  83.     int choice;
  84.  
  85.     // Menu loop
  86.     do {
  87.         printf("\n1. Add Stock\n");
  88.         printf("2. Display Portfolio\n");
  89.         printf("3. Simulate Market Changes\n");
  90.         printf("4. Calculate Portfolio Value\n");
  91.         printf("5. Exit\n");
  92.         printf("Enter your choice: ");
  93.         if (scanf("%d", &choice) != 1) {
  94.             printf("Invalid input. Please enter a number.\n");
  95.             while (getchar() != '\n'); // Clear input buffer
  96.             continue;
  97.         }
  98.  
  99.         // Switch case to handle the user's choice
  100.         switch (choice) {
  101.             case 1:
  102.                 addStock(&myPortfolio);
  103.                 break;
  104.             case 2:
  105.                 displayPortfolio(&myPortfolio);
  106.                 break;
  107.             case 3:
  108.                 simulateMarketChanges(&myPortfolio);
  109.                 break;
  110.             case 4:
  111.                 // Output the total value of the portfolio
  112.                 printf("Portfolio Value: %.2f\n", calculatePortfolioValue(&myPortfolio));
  113.                 break;
  114.             case 5:
  115.                 printf("Exiting...\n");
  116.                 break;
  117.             default:
  118.                 printf("Invalid choice. Please try again.\n");
  119.         }
  120.     } while (choice != 5);
  121.  
  122.     return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment