Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MAX_STOCKS 100
- // Definition of a Stock structure
- struct Stock {
- char symbol[10];
- double price;
- int quantity;
- };
- // Definition of a Portfolio structure
- struct Portfolio {
- struct Stock stocks[MAX_STOCKS];
- int stockCount;
- };
- // Function to add a stock to the portfolio
- void addStock(struct Portfolio *portfolio) {
- // Check if the portfolio has space for more stocks
- if (portfolio->stockCount < MAX_STOCKS) {
- struct Stock newStock;
- printf("Enter stock symbol: ");
- scanf("%s", newStock.symbol);
- printf("Enter stock price: ");
- scanf("%lf", &newStock.price);
- printf("Enter quantity: ");
- scanf("%d", &newStock.quantity);
- // Add the new stock to the portfolio and increment stock count
- portfolio->stocks[portfolio->stockCount++] = newStock;
- printf("Stock added successfully.\n");
- } else {
- printf("Portfolio is full. Cannot add more stocks.\n");
- }
- }
- // Function to display all stocks in the portfolio
- void displayPortfolio(struct Portfolio *portfolio) {
- printf("Portfolio:\n");
- for (int i = 0; i < portfolio->stockCount; ++i) {
- printf("%s - Price: %.2f, Quantity: %d\n",
- portfolio->stocks[i].symbol,
- portfolio->stocks[i].price,
- portfolio->stocks[i].quantity);
- }
- }
- // Function to simulate market changes on the stock prices
- void simulateMarketChanges(struct Portfolio *portfolio) {
- srand(time(NULL));
- for (int i = 0; i < portfolio->stockCount; ++i) {
- // Generate a random movement between -0.10 and 0.10
- double movement = (rand() % 21 - 10) / 100.0;
- portfolio->stocks[i].price *= (1 + movement);
- }
- printf("Market changes simulated.\n");
- }
- // Function to calculate the total value of the portfolio
- double calculatePortfolioValue(struct Portfolio *portfolio) {
- double totalValue = 0.0;
- for (int i = 0; i < portfolio->stockCount; ++i) {
- totalValue += portfolio->stocks[i].price * portfolio->stocks[i].quantity;
- }
- return totalValue;
- }
- // Main function to interact with the portfolio
- int main() {
- struct Portfolio myPortfolio = {0};
- int choice;
- // Menu loop
- do {
- printf("\n1. Add Stock\n");
- printf("2. Display Portfolio\n");
- printf("3. Simulate Market Changes\n");
- printf("4. Calculate Portfolio Value\n");
- printf("5. Exit\n");
- printf("Enter your choice: ");
- if (scanf("%d", &choice) != 1) {
- printf("Invalid input. Please enter a number.\n");
- while (getchar() != '\n'); // Clear input buffer
- continue;
- }
- // Switch case to handle the user's choice
- switch (choice) {
- case 1:
- addStock(&myPortfolio);
- break;
- case 2:
- displayPortfolio(&myPortfolio);
- break;
- case 3:
- simulateMarketChanges(&myPortfolio);
- break;
- case 4:
- // Output the total value of the portfolio
- printf("Portfolio Value: %.2f\n", calculatePortfolioValue(&myPortfolio));
- break;
- case 5:
- printf("Exiting...\n");
- break;
- default:
- printf("Invalid choice. Please try again.\n");
- }
- } while (choice != 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment