Update cv1/program.c

This commit is contained in:
Marat Izmailov 2024-10-02 14:08:44 +00:00
parent dd530b2c7a
commit 29986823cf

View File

@ -11,7 +11,7 @@ struct pizza {
}; };
char hacker_script(char c) { char hacker_script(char c) {
if (c >= 'A' && c <= 'Z') return c + 32; // Convert uppercase to lowercase if (c >= 'A' && c <= 'Z') c += 32;
switch (c) { switch (c) {
case 'o': return '0'; case 'o': return '0';
case 'i': return '1'; case 'i': return '1';
@ -19,32 +19,32 @@ char hacker_script(char c) {
case 'e': return '3'; case 'e': return '3';
case 'a': return '4'; case 'a': return '4';
case 's': return '5'; case 's': return '5';
case 'b': return '6'; // Single mapping for 'b' case 'b': return '6';
case 't': return '7'; case 't': return '7';
case '8': return '8'; // Added mapping for '8' case '8': return '8';
case 'q': return '9'; case 'q': return '9';
default: return c; // Return unchanged for other characters default: return c;
} }
} }
void normalize_string(const char* input, char* output) { void normalize_string(const char* input, char* output) {
for (int i = 0; input[i] != '\0'; i++) { int i = 0;
while (input[i] != '\0' && input[i] != '\n') {
output[i] = hacker_script(input[i]); output[i] = hacker_script(input[i]);
i++;
} }
output[strlen(input)] = '\0'; // Null-terminate output[i] = '\0';
} }
int read_pizza(struct pizza* item) { int read_pizza(struct pizza* item) {
char line[LINESIZE]; char line[LINESIZE];
if (fgets(line, sizeof(line), stdin) == NULL) return 0; // End of input if (fgets(line, sizeof(line), stdin) == NULL) return 0;
normalize_string(line, item->name); // Normalize the name normalize_string(line, item->name);
if (fgets(line, sizeof(line), stdin) == NULL) return 0;
item->prize = strtof(line, NULL);
if (fgets(line, sizeof(line), stdin) == NULL) return 0; // Read price return (item->prize > 0) ? 1 : 0;
item->prize = strtof(line, NULL); // Convert to float
return (item->prize > 0) ? 1 : 0; // Return success or failure
}
int find_string(const char* heap, const char* needle) { int find_string(const char* heap, const char* needle) {
char normalized_heap[LINESIZE]; char normalized_heap[LINESIZE];
@ -59,18 +59,21 @@ int main() {
struct pizza menu[MENU_SIZE]; struct pizza menu[MENU_SIZE];
int item_count = 0; int item_count = 0;
char search_term[LINESIZE]; // Renamed to avoid conflict char search_term[LINESIZE];
printf("Zadaj hladanu surovinu:\n"); printf("Zadaj hladanu surovinu:\n");
fgets(search_term, sizeof(search_term), stdin); fgets(search_term, sizeof(search_term), stdin);
search_term[strcspn(search_term, "\n")] = '\0';
printf("Zadaj jedalny listok:\n"); printf("Zadaj jedalny listok:\n");
while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) { while (item_count < MENU_SIZE && read_pizza(&menu[item_count])) {
item_count++; item_count++;
} }
// Print matching items
for (int i = 0; i < item_count; i++) { for (int i = 0; i < item_count; i++) {
if (find_string(menu[i].name, search_term)) { // Use the new function name if (find_string(menu[i].name, search_term)) {
printf("%s\n%.2f\n", menu[i].name, menu[i].prize); printf("%s\n%.2f\n", menu[i].name, menu[i].prize);
} }
} }