为什么我的代码(从 csv 文件排序和搜索)没有显示数据的所有结果?

问题描述 投票:0回答:1

我创建一个包含类似内容的程序

What Do you want to do? 
1. Display data
2. search data
3. sort data
4. export data
5. exit

我已经创建了从选项

1
到选项
2
的代码,其余的代码还没有构建,所以忽略其余的
switch
案例代码

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    char location[100];
    char city[100];
    int price;
    int room;
    int bathroom;
    int carpark;
    char type[100];
    char furnish[100];
} data;

//Quick Sort by location//
void swap(data *a, data *b)
{
    data temp = *a;
    *a = *b;
    *b = temp;
}

int partitionL(data arr[], int low, int high)
{
    char pivot[1000];
    strcpy(pivot, arr[high].location);
    int i = low - 1;

    for (int j = low; j < high; j++)
    {
        if (strcmp(arr[j].location, pivot) < 0)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortL(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionL(arr, low, high);
        sortL(arr, low, pi - 1);
        sortL(arr, pi + 1, high);
    }
}

/// end of quick sort by location

//Quick Sort by city//

int partitionC(data arr[], int low, int high)
{
    char pivot[1000];
    strcpy(pivot, arr[high].city);
    int i = low - 1;

    for (int j = low; j < high; j++)
    {
        if (strcmp(arr[j].city, pivot) < 0)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortC(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionC(arr, low, high);
        sortC(arr, low, pi - 1);
        sortC(arr, pi + 1, high);
    }
}

/// end of quick sort by city

//Quick Sort by type//

int partitionT(data arr[], int low, int high)
{
    char pivot[1000];
    strcpy(pivot, arr[high].type);
    int i = low - 1;

    for (int j = low; j < high; j++)
    {
        if (strcmp(arr[j].type, pivot) < 0)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortT(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionT(arr, low, high);
        sortT(arr, low, pi - 1);
        sortT(arr, pi + 1, high);
    }
}

/// end of quick sort by type

//Quick Sort by furnish//

int partitionF(data arr[], int low, int high)
{
    char pivot[1000];
    strcpy(pivot, arr[high].furnish);
    int i = low - 1;

    for (int j = low; j < high; j++)
    {
        if (strcmp(arr[j].furnish, pivot) < 0)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortF(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionF(arr, low, high);
        sortF(arr, low, pi - 1);
        sortF(arr, pi + 1, high);
    }
}

/// end of quick sort by furnish

//Quick Sort by price//

int partitionP(data arr[], int low, int high)
{
    int pivot= arr[high].price;
    int i = low - 1;
  
    for (int j = low; j < high; j++)
    {
        if (arr[j].price < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortP(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionP(arr, low, high);
        sortP(arr, low, pi - 1);
        sortP(arr, pi + 1, high);
    }
}

/// end of quick sort price

//Quick Sort by room//

int partitionR(data arr[], int low, int high)
{
    int pivot= arr[high].room;
    int i = low - 1;
  
    for (int j = low; j < high; j++)
    {
        if (arr[j].room < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortR(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionR(arr, low, high);
        sortR(arr, low, pi - 1);
        sortR(arr, pi + 1, high);
    }
}

/// end of quick sort room

//Quick Sort by bathroom//

int partitionB(data arr[], int low, int high)
{
    int pivot = arr[high].bathroom;
    int i = low - 1;
   
    for (int j = low; j < high; j++)
    {
        if (arr[j].bathroom < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortB(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionB(arr, low, high);
        sortB(arr, low, pi - 1);
        sortB(arr, pi + 1, high);
    }
}

/// end of quick sort bathroom

//Quick Sort by carpark//

int partitionK(data arr[], int low, int high)
{
    int pivot = arr[high].carpark;
    int i = low - 1;
   
    for (int j = low; j < high; j++)
    {
        if (arr[j].carpark < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}

void sortK(data arr[], int low, int high)
{
    if (low < high)
    {
        int pi = partitionK(arr, low, high);
        sortK(arr, low, pi - 1);
        sortK(arr, pi + 1, high);
    }
}

/// end of quick sort carpark

// Search location
int searchL(data array[], char target[], int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while(low <= high) {
        
        int middle = low + (high - low) / 2;
        char value[100];
        strcpy(value, array[middle].location);
        
        if (strcmp(value, target) < 0)
            low = middle + 1;
        else if (strcmp(value, target) > 0)
            high = middle - 1;
        else
            return middle; //target found
    }
    
    return -1;
}
//end of search location

//Search city
int searchC(data array[], char target[], int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        char value[100];
        strcpy(value, array[middle].city);
        
        if (strcmp(value, target) < 0)
           low = middle + 1;
        else if (strcmp(value, target) > 0)
            high = middle - 1;
        else
           return middle; //target found
    }
    
    return -1;
}
// end of search city

//Search type
int searchT(data array[], char target[], int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        char value[100];
        strcpy(value, array[middle].type);
        
        if (strcmp(value, target) < 0)
           low = middle + 1;
        else if (strcmp(value, target) > 0)
            high = middle - 1;
        else
            return middle; //target found
    }
    
    return -1;
}
// end of search type

//Search furnish
int searchF(data array[], char target[], int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        char value[100];
        strcpy(value, array[middle].furnish);
        
        if (strcmp(value, target) < 0)
            low = middle + 1;
        else if (strcmp(value, target) > 0)
            high = middle - 1;
        else
            return middle; //target found
    }
    
    return -1;
}
// end of search furnish

//search price
int searchP(data array[], int target, int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        int value = array[middle].price;
        
        if (value < target)
            low = middle + 1;
        else if (value > target)
            high = middle - 1;
        else
            return middle; 
    }
    
    return -1;
}
// end of search price

//search room
int searchR(data array[], int target, int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        int value = array[middle].room;
        
        if (value < target)
            low = middle + 1;
        else if (value > target)
            high = middle - 1;
        else
            return middle; 
    }
    
    return -1;
}
// end of search room

//search Bathroom
int searchB(data array[], int target, int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        int value = array[middle].bathroom;
        
        if (value < target)
            low = middle + 1;
        else if (value > target)
            high = middle - 1;
        else
            return middle; 
    }
    
    return -1;
}
// end of search Bathroom

//search carpark
int searchK(data array[], int target, int start, int array_length) {
    
    int low = start;
    int high = array_length - 1;
    
    while (low <= high) {
        
        int middle = low + (high - low) / 2;
        int value = array[middle].carpark;
        
        if (value < target)
            low = middle + 1;
        else if (value > target)
            high = middle - 1;
        else
            return middle; 
    }
    
    return -1;
}
// end of search carpark


int main()
{
    data house[4000];

    FILE *file = fopen("data.csv", "r");
    if(file == NULL){
        printf("File is missing!\n");
        return 1;
    }
    char buffer[1000];
    fgets(buffer, 1000, file);
    char header[1000]; strcpy(header, buffer);

    int n = 0;
    while (fgets(buffer, 1000, file))
    {
        char *token = strtok(buffer, ",");
        strcpy(house[n].location, token);

        token = strtok(NULL, ",");
        strcpy(house[n].city, token);

        token = strtok(NULL, ",");
        house[n].price = atoi(token);

        token = strtok(NULL, ",");
        house[n].room = atoi(token);

        token = strtok(NULL, ",");
        house[n].bathroom = atoi(token);

        token = strtok(NULL, ",");
        house[n].carpark = atoi(token);

        token = strtok(NULL, ",");
        strcpy(house[n].type, token);

        token = strtok(NULL, ",\n");
        strcpy(house[n].furnish, token);

        n++;
    }

    fclose(file);

//  //for checking the output and file processed
//      printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
//      for (int i = 0; i < n; i++)
//      {
//             printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[i].location, house[i].city, house[i].price, house[i].room, house[i].bathroom, house[i].carpark, house[i].type, house[i].furnish);
//      }

    int input = 0;

    do {
        printf("What do you want to do?\n");
        printf("1.  Display data\n");
        printf("2.  Search Data\n");
        printf("3.  Sort Data\n");
        printf("4.  Export Data\n");
        printf("5.  Exit\n");
        printf("Your choice: "); 
        scanf("%d", &input);
    
        switch (input)
        {
          case 1:
            printf("Number of rows: "); 
            int rows;
            scanf("%d", &rows);
            printf("\n");
            printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
            for (int i = 0; i < rows; i++) {
                printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[i].location, house[i].city, house[i].price, house[i].room, house[i].bathroom, house[i].carpark, house[i].type, house[i].furnish);
            }
            printf("\n");
            break;
        
          case 2:
            printf("Choose column: ");
            char column[100];
            scanf("%s", column); 
            printf("What data do you want to find? ");
            char search[100];
            scanf("%s", search);
            
            if (strcmp(column, "Location") == 0) {
                sortL(house, 0, n-1);
                int idx = -1;
                if (searchL(house, search, idx, n ) != -1)
                {   
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while((idx = searchL(house, search, idx, n)) != -1){
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            } 
            else if (strcmp(column, "City") == 0) {
                sortC(house, 0, n-1);
                int idx = 0;
                if (searchC(house, search, idx, n ) != -1)
                {
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchC(house, search, idx, n)) != -1){
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            }
            else if (strcmp(column, "Price") == 0) {
                sortP(house, 0, n-1);
                int target = atoi(search);
                int idx = 0;
                if (searchP(house, target, idx, n) != -1) {
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchP(house, target, idx, n)) != -1){
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                }
            }
            else if (strcmp(column, "Rooms") == 0) {
                sortR(house, 0, n-1);
                int target = atoi(search);
                int idx = 0;
                if (searchR(house, target, idx, n) != -1) {
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchR(house, target, idx, n)) != -1){
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            }
            else if (strcmp(column, "Bathroom") == 0) {
                sortB(house, 0, n-1);
                int target = atoi(search);
                int idx = 0;
                if (searchB(house, target, idx, n) != -1) {
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchB(house, target, idx, n)) != -1){
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            }
            else if (strcmp(column, "Carpark") == 0) {
                sortK(house, 0, n-1);
                int target = atoi(search);
                int idx = 0;
                if (searchK(house, target, idx, n) != -1) {
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchK(house, target, idx, n)) != -1) {
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            }
            else if (strcmp(column, "Type") == 0) {
                sortT(house, 0, n-1);
                int idx = 0;
                if (searchT(house, search, idx, n ) != -1)
                {   
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchT(house, search, idx, n)) != -1) {
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            }
            else if (strcmp(column, "Furnish") == 0) {
                sortF(house, 0, n-1);
                int idx = 0;
                if (searchF(house, search, idx, n ) != -1)
                {   
                    printf("Data Found. Detail of data:\n");
                    printf("%-25s %-15s %-10s %-5s %-10s %-8s %-10s %-10s\n", "Location", "City", "Price", "Rooms", "Bathroom", "Carpark", "Type", "Furnish");
                    while ((idx = searchF(house, search, idx, n)) != -1) {
                        printf("%-25s %-15s %-10d %-5d %-10d %-8d %-10s %-10s\n", house[idx].location, house[idx].city, house[idx].price, house[idx].room, house[idx].bathroom, house[idx].carpark, house[idx].type, house[idx].furnish);
                        idx++; 
                    }
                    printf("\n");
                } else {
                    printf("Data Not Found!\n");
                }
            }
            break;
    
          case 3: 
    
          default:
            break;
        }
    } while (input == 1 || input == 2 || input == 3 || input == 4);
    
    return 0;
}

这是 data.csv

显示数据选项工作正常,所以忽略它即可。 搜索数据选项无法正常工作。好的,例如,我尝试搜索城市:

Kuala-Lumpur
我的程序只显示 12 条数据,来自数千个实际数据。其他列(如位置、价格、浴室等)也是如此。它只显示最多 12 条数据。我很困惑。请帮我找出我的代码有什么问题。 这是更详细解释的图片,我想说的。 非常感谢。

c csv search quicksort binary-search
1个回答
0
投票

您的搜索函数不会返回第一个匹配条目,而是返回找到的第一个匹配条目,该条目可能位于一系列匹配条目的中间。您应该通过两种方式修改程序:

使搜索功能返回第一个条目
  • 更改
  • while
  • 循环以迭代条目,只要
    idx < n
    且满足条件即可。无需再调用`限定范围搜索。
    
        
© www.soinside.com 2019 - 2024. All rights reserved.