본문 바로가기
카테고리 없음

temp

by KWONE 2024. 12. 10.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX_NAME 50

typedef struct {
    char name[MAX_NAME];
    char type[10];
    double x, y;
    double score;
} Restaurant;

// 함수 프로토타입
Restaurant* addRestaurant(Restaurant* restaurants, int* count);
int* recommendRestaurants(Restaurant* restaurants, int count, const char* type, double x, double y, double minScore);
void printRecommendedRestaurants(Restaurant* restaurants, int* matches, int count);
void searchRestaurants(Restaurant* restaurants, int count, const char* query);
double calculateDistance(double x1, double y1, double x2, double y2);

int main() {
    Restaurant* restaurants = NULL;
    int count = 0, choice;
    char query[100];

    while (1) {
        printf("\n--- Restaurant Management ---\n");
        printf("1. Add restaurant information\n");
        printf("2. Recommend restaurants\n");
        printf("3. Search for restaurants\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        if (choice == 1) {
            restaurants = addRestaurant(restaurants, &count);
        } else if (choice == 2) {
            char type[10];
            double x, y, minScore;
            printf("Enter food type (Korean, Chinese, Japanese, Western): ");
            scanf("%s", type);
            printf("Enter your location (x y): ");
            scanf("%lf %lf", &x, &y);
            printf("Enter minimum score: ");
            scanf("%lf", &minScore);

            int* matches = recommendRestaurants(restaurants, count, type, x, y, minScore);
            printRecommendedRestaurants(restaurants, matches, count);
            free(matches);
        } else if (choice == 3) {
            printf("Enter search query (type, name, or location): ");
            scanf(" %[^\n]s", query);
            searchRestaurants(restaurants, count, query);
        } else if (choice == 4) {
            printf("Exiting program. Memory cleared.\n");
            free(restaurants);
            break;
        } else {
            printf("Invalid choice. Try again.\n");
        }
    }

    return 0;
}

// 식당 정보 추가
Restaurant* addRestaurant(Restaurant* restaurants, int* count) {
    restaurants = realloc(restaurants, (*count + 1) * sizeof(Restaurant));
    if (!restaurants) {
        perror("Memory allocation failed");
        exit(1);
    }

    printf("--- Add Restaurant ---\n");
    printf("Enter restaurant name: ");
    scanf("%s", restaurants[*count].name);
    printf("Enter food type (Korean, Chinese, Japanese, Western): ");
    scanf("%s", restaurants[*count].type);
    printf("Enter location (x y): ");
    scanf("%lf %lf", &restaurants[*count].x, &restaurants[*count].y);
    printf("Enter score (0.0 ~ 5.0): ");
    scanf("%lf", &restaurants[*count].score);

    (*count)++;
    printf("Restaurant added successfully!\n");
    return restaurants;
}

// 추천 식당 검색
int* recommendRestaurants(Restaurant* restaurants, int count, const char* type, double x, double y, double minScore) {
    int* matches = calloc(count, sizeof(int));
    if (!matches) {
        perror("Memory allocation failed");
        exit(1);
    }

    for (int i = 0; i < count; i++) {
        double distance = calculateDistance(restaurants[i].x, restaurants[i].y, x, y);
        if (strcmp(restaurants[i].type, type) == 0 && distance <= 500 && restaurants[i].score >= minScore) {
            matches[i] = 1;
        }
    }
    return matches;
}

// 추천 식당 출력
void printRecommendedRestaurants(Restaurant* restaurants, int* matches, int count) {
    int found = 0;
    printf("--- Recommended Restaurants ---\n");
    for (int i = 0; i < count; i++) {
        if (matches[i]) {
            printf("Name: %s, Type: %s, Location: (%.2f, %.2f), Score: %.1f\n",
                   restaurants[i].name, restaurants[i].type, restaurants[i].x, restaurants[i].y, restaurants[i].score);
            found = 1;
        }
    }
    if (!found) {
        printf("No result\n");
    }
}

// 식당 검색
void searchRestaurants(Restaurant* restaurants, int count, const char* query) {
    int found = 0;
    printf("--- Search Results ---\n");
    if (sscanf(query, "%lf %lf %lf", &restaurants[0].x, &restaurants[0].y, &restaurants[0].score) == 3) {
        double x, y, range;
        sscanf(query, "%lf %lf %lf", &x, &y, &range);
        for (int i = 0; i < count; i++) {
            if (calculateDistance(restaurants[i].x, restaurants[i].y, x, y) <= range) {
                printf("Name: %s, Type: %s, Location: (%.2f, %.2f), Score: %.1f\n",
                       restaurants[i].name, restaurants[i].type, restaurants[i].x, restaurants[i].y, restaurants[i].score);
                found = 1;
            }
        }
    } else if (strcmp(query, "Korean") == 0 || strcmp(query, "Chinese") == 0 ||
               strcmp(query, "Japanese") == 0 || strcmp(query, "Western") == 0) {
        for (int i = 0; i < count; i++) {
            if (strcmp(restaurants[i].type, query) == 0) {
                printf("Name: %s, Type: %s, Location: (%.2f, %.2f), Score: %.1f\n",
                       restaurants[i].name, restaurants[i].type, restaurants[i].x, restaurants[i].y, restaurants[i].score);
                found = 1;
            }
        }
    } else {
        for (int i = 0; i < count; i++) {
            if (strstr(restaurants[i].name, query)) {
                printf("Name: %s, Type: %s, Location: (%.2f, %.2f), Score: %.1f\n",
                       restaurants[i].name, restaurants[i].type, restaurants[i].x, restaurants[i].y, restaurants[i].score);
                found = 1;
            }
        }
    }

    if (!found) {
        printf("No result\n");
    }
}

// 두 점 사이 거리 계산
double calculateDistance(double x1, double y1, double x2, double y2) {
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}