我创建了一个程序,包含这样的内容
What Do you want to do?
1. Display data
2. search data
3. sort data
4. export data
5. exit
字符串
我已经创建了从选项1
到选项2
的代码,其余的代码仍未构建,因此忽略其余的switch
case代码
这是我的代码:
#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个数据。我混淆了。请帮助我找出我的代码有什么问题。这是the image for more detailed explanation,我想说的。非常感谢。
1条答案
按热度按时间b91juud31#
您的搜索函数不会返回第一个匹配条目,而是返回找到的第一个条目,该条目可能位于一系列匹配条目的中间。您应该以两种方式修改程序:
idx < n
和条件,就将while
循环改为在条目上搜索。无需再次调用`search,范围有限。下面是一个修改后的
searchL
函数:字符串
下面是
main()
函数的修改部分:型
您将大大简化和缩短代码,
qsort
对数组进行排序,只写一个比较函数这里有一个简化的版本供您学习:
型
以下是使用
qsort
的更高级版本:型