#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
struct data{
char Location1[100];
char Location2[100];
char price[10];
char rooms[10];
char bathrooms[10];
char carparks[10];
char type[100];
char furnish[100];
};
struct data aol[4000];
int printedlines;
void fileread() {
FILE *fp = fopen("AOLDATA.csv","r");
if(fp == NULL){
printf("Error! File not found.");
exit(0);
}
while(fscanf(fp, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^\n]", aol[printedlines].Location1, aol[printedlines].Location2,
aol[printedlines].price, aol[printedlines].rooms, aol[printedlines].bathrooms, aol[printedlines].carparks,
aol[printedlines].type, aol[printedlines].furnish) != EOF) {
printedlines++;
fgetc(fp);
}
fclose(fp);
}
void searchData(char *keyword) {
int found = 0;
for(int i = 0; i < printedlines; i++) {
if(strstr(aol[i].Location1, keyword) || strstr(aol[i].Location2, keyword) ||
strstr(aol[i].price, keyword) || strstr(aol[i].rooms, keyword) ||
strstr(aol[i].bathrooms, keyword) || strstr(aol[i].carparks, keyword) ||
strstr(aol[i].type, keyword) || strstr(aol[i].furnish, keyword)) {
printf("%-30s%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n", aol[i].Location1, aol[i].Location2, aol[i].price, aol[i].rooms,
aol[i].bathrooms, aol[i].carparks, aol[i].type, aol[i].furnish);
found++;
}
}
if(!found) {
printf("Data not found!");
}
}
void swap(struct data *a, struct data *b) {
struct data temp = *a;
*a = *b;
*b = temp;
}
int partition(struct data *aol, int low, int high) {
int price;
int pivot = aol[high]->price;
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (aol[j]->price < pivot) {
i++;
swap(&aol[i], &aol[j]);
}
}
swap(&aol[i + 1], &aol[high]);
return i + 1;
}
void quickSort(struct data *aol, int low, int high) {
if (low < high) {
int pivotIndex = partition(aol, low, high);
quickSort(aol, low, pivotIndex - 1);
quickSort(aol, pivotIndex + 1, high);
}
}
void printArray(struct data *aol, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", aol[i].price);
}
printf("\n");
}
int main(){
int option;
char line[100];
int n = sizeof(aol) / sizeof(aol[0]);
fileread();
printf("What do you want to do?\n1.Display data\n2.Search Data\n3.Sort Data\n4.Export Data\n5.Exit\n");
printf("Your choice: ");
scanf("%d", &option);
if(option == 3){
char column1[20];
char sortmethod[10];
int x;
printf("Choose column: ");
scanf("%s", column1); getchar();
printf("Sort ascending or descending? ");
scanf("%s", &x); //getchar();
if(x == 7){
quickSort(aol, 0, n - 1);
printArray(aol, n);
}
}
return 0;
}
Loc1 Loc2 Price Rooms Bathrooms Carparks Type Furnish
NY US 10 2 1 4 Bad Expensive
LN UK 20 3 1 5 Good Expensive
JK IN 20 3 1 2 Good Cheap
尝试进行快速排序,但在函数分区中它说 -> 的基操作数具有非指针类型“数据”。我该如何解决? 我尝试使用快速排序升序或降序对 .csv 文件中的列价格进行排序,但我收到此错误,我只是不知道它意味着什么。谁能帮我解决这个问题吗?
您收到错误是因为
->
运算符的左侧(即一个实例中的 aol[high]
和另一个实例中的 aol[j]
)不是指向结构类型的指针,而是结构类型。这意味着您需要使用 .
运算符来访问成员,即 aol[high].price
或 aol[j].price
。
当你解决这个问题时,你就会遇到另一个问题。
price
成员是一个 char
数组,但您尝试将其分配给 int
变量并将其与 int
进行比较。您首先需要使用类似 atoi
的方法将字符串转换为整数。