usaa24/cv1/program.c

117 lines
2.8 KiB
C
Raw Normal View History

2024-10-02 13:51:04 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2024-10-02 14:24:27 +00:00
#include <ctype.h>
2024-10-02 13:51:04 +00:00
#define LINESIZE 100
2024-10-02 14:24:27 +00:00
#define MAX_PIZZAS 100
2024-10-02 13:51:04 +00:00
2024-10-02 14:24:27 +00:00
// Structure to store pizza menu item
2024-10-02 13:51:04 +00:00
struct pizza {
2024-10-02 14:24:27 +00:00
float price;
2024-10-02 13:51:04 +00:00
char name[LINESIZE];
};
2024-10-02 14:24:27 +00:00
// Function to convert a character to its Hacker Script form
2024-10-02 13:51:04 +00:00
char hacker_script(char c) {
2024-10-02 14:24:27 +00:00
char numbers[] = "0123456789";
char letters[] = "oizeasbtbq";
// Convert to lowercase first
c = tolower(c);
// Check for special characters
for (int i = 0; i < 10; i++) {
if (c == numbers[i]) {
return letters[i];
}
2024-10-02 13:51:04 +00:00
}
2024-10-02 14:24:27 +00:00
return c;
2024-10-02 13:51:04 +00:00
}
2024-10-02 14:24:27 +00:00
// Function to normalize a string by applying hacker script rules
void normalize_string(char *str) {
for (int i = 0; str[i]; i++) {
str[i] = hacker_script(str[i]);
2024-10-02 13:51:04 +00:00
}
}
2024-10-02 14:24:27 +00:00
// Function to read one pizza item from input
int read_pizza(struct pizza *item) {
char line1[LINESIZE], line2[LINESIZE];
// Read name
if (fgets(line1, LINESIZE, stdin) == NULL) {
return 0; // Failed to read
}
// Remove newline character
line1[strcspn(line1, "\n")] = '\0';
2024-10-02 13:51:04 +00:00
2024-10-02 14:24:27 +00:00
// Read price
if (fgets(line2, LINESIZE, stdin) == NULL) {
return 0; // Failed to read
}
float value = strtof(line2, NULL);
if (value == 0.0F) {
return 0; // Invalid price
}
// Copy the read values
strcpy(item->name, line1);
item->price = value;
return 1;
2024-10-02 14:12:41 +00:00
}
2024-10-02 14:16:02 +00:00
2024-10-02 14:24:27 +00:00
// Function to search if a normalized needle exists in a normalized heap
int search_string(const char *heap, const char *needle) {
char norm_heap[LINESIZE], norm_needle[LINESIZE];
// Make copies to normalize
strcpy(norm_heap, heap);
strcpy(norm_needle, needle);
// Normalize both strings
normalize_string(norm_heap);
normalize_string(norm_needle);
// Use strstr to find the normalized needle in the heap
return strstr(norm_heap, norm_needle) != NULL;
2024-10-02 13:51:04 +00:00
}
int main() {
2024-10-02 14:24:27 +00:00
struct pizza menu[MAX_PIZZAS];
char search[LINESIZE];
int count = 0;
// Read search string
printf("Enter the searched ingredient:\n");
fgets(search, LINESIZE, stdin);
search[strcspn(search, "\n")] = '\0'; // Remove newline character
// Read menu
printf("Enter the menu:\n");
while (read_pizza(&menu[count])) {
count++;
if (count >= MAX_PIZZAS) {
break; // Limit reached
}
2024-10-02 13:51:04 +00:00
}
2024-10-02 14:24:27 +00:00
// Display valid dishes containing the searched ingredient
2024-10-02 14:16:02 +00:00
int found = 0;
2024-10-02 14:24:27 +00:00
for (int i = 0; i < count; i++) {
if (search_string(menu[i].name, search)) {
printf("%s\n", menu[i].name);
printf("%.2f\n", menu[i].price);
found++;
2024-10-02 13:51:04 +00:00
}
}
2024-10-02 14:24:27 +00:00
// Display total items read
printf("A total of %d items were loaded.\n", count);
2024-10-02 14:16:02 +00:00
2024-10-02 13:51:04 +00:00
return 0;
}