请大神看一下这是什么问题(桶排序)#include stdio.h #include malloc.hvoid swap(int *a, int *b){int
请大神看一下这是什么问题(桶排序)
#include <stdio.h>
#include <malloc.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int *array, int low, int high)
{
int i,j,middle = (low + high)/2;
for(i=low,j=high; i<j; ) {
while(array[i] < array[middle]) ++i;
if(i < j && i != middle) {
swap(&array[i], &array[middle]);
middle = i;
}
while(array[j] > array[middle]) --j;
if(i < j && j != middle) {
swap(&array[j], &array[middle]);
middle = j;
}
}
return middle;
}
int quicksort(int *array, int low, int high)
{
int piovt_pos;
if(low < high) {
piovt_pos = partition(array, low, high);
quicksort(array, low, piovt_pos - 1);
quicksort(array, piovt_pos + 1, high);
}
}
struct barrel {
int a[10];
int count;
};
int main()
{
int data[] = {23, 12, 3, 54, 1, 98, 24, 34};
int i,j,min = data[0];
int max = data[0];
for(i=1; i<sizeof(data)/sizeof(int); ++i) {
min = min < data[i] ? min : data[i];
max = max > data[i] ? max : data[i];
}
int num = (max - min + 1)/10 + 1;
barrel *pBarrel =(barrel*)malloc(sizeof(barrel)) ;
for(i=0; i<sizeof(data)/sizeof(int); ++i) {
j = (pBarrel+((data[i]-min+1)/10))->count;
(pBarrel+((data[i]-min+1)/10))->a[j] = data[i];
(pBarrel+((data[i]-min+1)/10))->count++;
}
static int pos = 0;
for(i=0; i<num; ++i) {
quicksort((pBarrel+i)->a, 0, (pBarrel+i)->count-1);
for(j=0; j<(pBarrel+i)->count; ++j) {
data[pos++] = (pBarrel+i)->a[j];
}
}
for(i=0; i<8; ++i) {
printf("%d ", data[i]);
}
return 0;
}
调试结果:1>f:\visual studio\2\2\1.c(94): error C2143: 语法错误 : 缺少“;”(在“类型”的前面)
1>f:\visual studio\2\2\1.c(96): error C2065: “barrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(96): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(96): error C2065: “barrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(96): error C2059: 语法错误:“)”
1>f:\visual studio\2\2\1.c(100): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(100): error C2223: “->count”的左侧必须指向结构/联合
1>f:\visual studio\2\2\1.c(102): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(102): error C2223: “->a”的左侧必须指向结构/联合
1>f:\visual studio\2\2\1.c(104): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(104): error C2223: “->count”的左侧必须指向结构/联合
1>f:\visual studio\2\2\1.c(108): error C2143: 语法错误 : 缺少“;”(在“类型”的前面)
1>f:\visual studio\2\2\1.c(110): error C2065: “num”: 未声明的标识符
1>f:\visual studio\2\2\1.c(112): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(112): error C2223: “->a”的左侧必须指向结构/联合
1>f:\visual studio\2\2\1.c(112): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(112): error C2223: “->count”的左侧必须指向结构/联合
1>f:\visual studio\2\2\1.c(112): error C2198: “quicksort”: 用于调用的参数太少
1>f:\visual studio\2\2\1.c(114): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(114): error C2223: “->count”的左侧必须指向结构/联合
1>f:\visual studio\2\2\1.c(116): error C2065: “pos”: 未声明的标识符
1>f:\visual studio\2\2\1.c(116): error C2065: “pBarrel”: 未声明的标识符
1>f:\visual studio\2\2\1.c(116): error C2223: “->a”的左侧必须指向结构/联合
1>
1>生成失败。
请教这是为什么?多谢
[解决办法]struct barrel
{
int a[10];
int count;
};
改为
typedef struct _barrel
{
int a[10];
int count;
}barrel;
[解决办法]用c++编译,c编译的话,所有barrel要用struct barrel代替、
或者按照mzz_810的方法
------解决方案--------------------
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。