· 4 years ago · Mar 20, 2021, 04:06 PM
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <time.h>
5
6#define FLAG_BUFFER 128
7#define MAX_SYM_LEN 4
8
9typedef struct Stonks {
10 int shares;
11 char symbol[MAX_SYM_LEN + 1];
12 struct Stonks *next;
13} Stonk;
14
15typedef struct Portfolios {
16 int money;
17 Stonk *head;
18} Portfolio;
19
20int view_portfolio(Portfolio *p) {
21 if (!p) {
22 return 1;
23 }
24 printf("\nPortfolio as of ");
25 fflush(stdout);
26 system("date"); // TODO: implement this in C
27 fflush(stdout);
28
29 printf("\n\n");
30 Stonk *head = p->head;
31 if (!head) {
32 printf("You don't own any stonks!\n");
33 }
34 while (head) {
35 printf("%d shares of %s\n", head->shares, head->symbol);
36 head = head->next;
37 }
38 return 0;
39}
40
41Stonk *pick_symbol_with_AI(int shares) {
42 if (shares < 1) {
43 return NULL;
44 }
45 Stonk *stonk = malloc(sizeof(Stonk));
46 stonk->shares = shares;
47
48 int AI_symbol_len = (rand() % MAX_SYM_LEN) + 1;
49 for (int i = 0; i <= MAX_SYM_LEN; i++) {
50 if (i < AI_symbol_len) {
51 stonk->symbol[i] = 'A' + (rand() % 26);
52 } else {
53 stonk->symbol[i] = '\0';
54 }
55 }
56
57 stonk->next = NULL;
58
59 return stonk;
60}
61
62int buy_stonks(Portfolio *p) {
63 if (!p) {
64 return 1;
65 }
66 char api_buf[FLAG_BUFFER];
67 FILE *f = fopen("api","r");
68 if (!f) {
69 printf("Flag file not found. Contact an admin.\n");
70 exit(1);
71 }
72 fgets(api_buf, FLAG_BUFFER, f);
73
74 int money = p->money;
75 int shares = 0;
76 Stonk *temp = NULL;
77 printf("Using patented AI algorithms to buy stonks\n");
78 while (money > 0) {
79 shares = (rand() % money) + 1;
80 temp = pick_symbol_with_AI(shares);
81 temp->next = p->head;
82 p->head = temp;
83 money -= shares;
84 }
85 printf("Stonks chosen\n");
86
87 // TODO: Figure out how to read token from file, for now just ask
88
89 char *user_buf = malloc(300 + 1);
90 printf("What is your API token?\n");
91 scanf("%300s", user_buf);
92 printf("Buying stonks with token:\n");
93 printf(user_buf);
94
95 // TODO: Actually use key to interact with API
96
97 view_portfolio(p);
98
99 return 0;
100}
101
102Portfolio *initialize_portfolio() {
103 Portfolio *p = malloc(sizeof(Portfolio));
104 p->money = (rand() % 2018) + 1;
105 p->head = NULL;
106 return p;
107}
108
109void free_portfolio(Portfolio *p) {
110 Stonk *current = p->head;
111 Stonk *next = NULL;
112 while (current) {
113 next = current->next;
114 free(current);
115 current = next;
116 }
117 free(p);
118}
119
120int main(int argc, char *argv[])
121{
122 setbuf(stdout, NULL);
123 srand(time(NULL));
124 Portfolio *p = initialize_portfolio();
125 if (!p) {
126 printf("Memory failure\n");
127 exit(1);
128 }
129
130 int resp = 0;
131
132 printf("Welcome back to the trading app!\n\n");
133 printf("What would you like to do?\n");
134 printf("1) Buy some stonks!\n");
135 printf("2) View my portfolio\n");
136 scanf("%d", &resp);
137
138 if (resp == 1) {
139 buy_stonks(p);
140 } else if (resp == 2) {
141 view_portfolio(p);
142 }
143
144 free_portfolio(p);
145 printf("Goodbye!\n");
146
147 exit(0);
148}
149