-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module2
168 lines (139 loc) · 4.38 KB
/
Module2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Define a structure for linked list node
typedef struct QuoteNode {
char *text;
struct QuoteNode *next;
} QuoteNode;
// ANSI color codes and text formatting
#define RESET_COLOR "\033[0m"
#define BOLD_TEXT "\033[1m"
#define UNDERLINED_TEXT "\033[4m"
// Arrays of quotes for each category
const char* motivational_quotes[] = {
"The only way to do great work is to love what you do. – Steve Jobs",
"Believe you can and you're halfway there. – Theodore Roosevelt",
"Your time is limited, don't waste it living someone else's life. – Steve Jobs"
};
const char* humorous_quotes[] = {
"I'm not arguing, I'm just explaining why I'm right.",
"I can resist everything except temptation. – Oscar Wilde",
"Life is short. Smile while you still have teeth."
};
const char* wisdom_quotes[] = {
"The best way to predict the future is to invent it. – Alan Kay",
"The only true wisdom is in knowing you know nothing. – Socrates",
"Turn your wounds into wisdom. – Oprah Winfrey"
};
// Function to remove a quote by index from the linked list
void removeQuote(QuoteNode **head, int index) {
if (!head || !(*head)) {
printf("No quotes available.\n");
return;
}
QuoteNode *temp = *head;
if (index == 0) {
*head = temp->next;
free(temp->text);
free(temp);
return;
}
for (int i = 0; temp != NULL && i < index - 1; i++) {
temp = temp->next;
}
if (!temp || !temp->next) {
printf("Invalid index.\n");
return;
}
QuoteNode *next = temp->next->next;
free(temp->next->text);
free(temp->next);
temp->next = next;
}
// Function to update a quote by index in the linked list
void updateQuote(QuoteNode *head, int index, const char *newQuote) {
if (!head) {
printf("No quotes available.\n");
return;
}
QuoteNode *temp = head;
for (int i = 0; temp != NULL && i < index; i++) {
temp = temp->next;
}
if (!temp) {
printf("Invalid index.\n");
return;
}
free(temp->text);
temp->text = (char*)malloc((strlen(newQuote) + 1) * sizeof(char));
if (!temp->text) {
printf("Memory allocation failed\n");
exit(1);
}
strcpy(temp->text, newQuote);
}
// Function to display a random quote from a static category
void displayRandomQuoteFromCategory(const char* quotes[], int size) {
if (size == 0) {
printf("No quotes available in this category.\n");
return;
}
int randomIndex = rand() % size;
printf(BOLD_TEXT "Random Quote: %s\n" RESET_COLOR, quotes[randomIndex]);
}
// Function to free allocated memory for linked list
void freeQuotes(QuoteNode *head) {
QuoteNode *temp;
while (head) {
temp = head;
head = head->next;
free(temp->text);
free(temp);
}
}
// Function to display the menu and get user choice
int displayMenu() {
int choice;
printf(BOLD_TEXT "Menu:\n" RESET_COLOR);
printf("6. Display a random motivational quote\n");
printf("7. Display a random humorous quote\n");
printf("8. Display a random wisdom quote\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Consume newline character left in the input buffer
return choice;
}
int main() {
// Initialize the linked list
QuoteNode *quotes = NULL;
// Seed the random number generator
srand(time(NULL));
int choice;
char newQuote[256];
int index;
while (1) {
choice = displayMenu();
switch (choice) {
case 6:
displayRandomQuoteFromCategory(motivational_quotes, sizeof(motivational_quotes) / sizeof(motivational_quotes[0]));
break;
case 7:
displayRandomQuoteFromCategory(humorous_quotes, sizeof(humorous_quotes) / sizeof(humorous_quotes[0]));
break;
case 8:
displayRandomQuoteFromCategory(wisdom_quotes, sizeof(wisdom_quotes) / sizeof(wisdom_quotes[0]));
break;
case 9:
freeQuotes(quotes);
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}