下面是实现一个内部排序的代码,但是出现了问题。
#include<stdio.h>
#include<stdlib.h>
#define LQ(a,b) ((a)<(b)) //定义宏运算
#define Maxsize 20 //顺序表的最大长度
typedef int Infotype //定义其他数据项的类型
typedef int Keytype //定义关键字类型为整型
typedef struct Redtype //记录类型
{
Keytype key; //关键字项
Infotype otherinfo; //其他数据项,具体类型在主程序中定义
};
typedef struct
{
Redtype r[Maxsize+1] //r[0]闲置或用作哨兵单元
int length; //顺序表长度
}Sqlist;
void Merge(Redtype SR[],Redtype TR[],int i,int m,int n)
{
// 将有序的SR[I,M]和SR[M+1,N]归并为有序的TR[I,N]
int j,k,n;
for (j=m+1,k=i;i<=m&&j<=n,++k)
if LQ(SR[i].key,SR[j].key)
TR[k]=SR[i++];
else
TR[k]=SR[j++];
if(i<=m)
for(h=0;h<=m-i;h++)
TR[k+h]=SR[i+h]; //将剩余的SR[I,M]复制到TR
if(j<=n)
for(h=0;h<n-j;h++)
TR[k+h]=SR[j+h]; //将剩余的ST[M+1,N]复制到TR
}
void Msort(Redtype SR[],Redtype TR1[],int s,int t)
{
//将SR[S,T]归并排序为TR1[S,T]
int m;
Redtype TR2[Maxsize+1];
if(s==t)
TR1[s]=SR[s];
else
{
m=(s+t)/2; //将SR[S,T]平分为SR[S,M]和SR[M+1,T]
Msort(SR[],TR2[],s,m); //递归地将SR[S,M]归并为有序的TR[S,M]
Msort(SR[],TR2[],m+1,t); //
Merge(TR2[],TR1[],s,m,t);
}
}
void Mergesort(Sqlist L)
{
Msort(L.r[],L.r[],1,L.length);
}
void print(Sqlist L)
{
int i;
for(i=1;i<L.length;i++)
printf("(%d,%d)",L.r[i].key,L.r[i].otherinfo);
printf("\n");
}
#define N 7
void main()
{
Redtype d[N]={{49,1},{38,2},{65,3},{97,4},{76,5},{13,6},{27,7}};
Sqlist sl;
int i;
for(i=0;i<N;i++)
sl.r[i+1]=d[i];
sl.length=N;
printf("排序前:\n");
print(sl);
Mergesort(sl);
printf("排序后:\n");
print(sl);
}
Compiling...
the practice of the struct.c
F:\实验结果\the practice of the struct.c(6) : error C2054: expected '(' to follow 'Infotype'
F:\实验结果\the practice of the struct.c(8) : error C2085: 'Keytype' : not in formal parameter list
F:\实验结果\the practice of the struct.c(8) : error C2143: syntax error : missing ';' before '<class-head>'
F:\实验结果\the practice of the struct.c(9) : error C2061: syntax error : identifier 'Keytype'
F:\实验结果\the practice of the struct.c(11) : error C2059: syntax error : '}'
F:\实验结果\the practice of the struct.c(11) : error C2018: unknown character '0xa3'
F:\实验结果\the practice of the struct.c(11) : error C2018: unknown character '0xbb'
F:\实验结果\the practice of the struct.c(19) : error C2146: syntax error : missing ')' before identifier 'SR'
F:\实验结果\the practice of the struct.c(19) : error C2061: syntax error : identifier 'SR'
F:\实验结果\the practice of the struct.c(19) : error C2059: syntax error : ';'
F:\实验结果\the practice of the struct.c(19) : error C2059: syntax error : '['
F:\实验结果\the practice of the struct.c(19) : error C2059: syntax error : ')'
F:\实验结果\the practice of the struct.c(36) : error C2146: syntax error : missing ')' before identifier 'SR'
F:\实验结果\the practice of the struct.c(36) : error C2061: syntax error : identifier 'SR'
F:\实验结果\the practice of the struct.c(36) : error C2059: syntax error : ';'
F:\实验结果\the practice of the struct.c(36) : error C2059: syntax error : '['
F:\实验结果\the practice of the struct.c(36) : error C2059: syntax error : ')'
F:\实验结果\the practice of the struct.c(52) : error C2146: syntax error : missing ')' before identifier 'L'
F:\实验结果\the practice of the struct.c(52) : error C2061: syntax error : identifier 'L'
F:\实验结果\the practice of the struct.c(52) : error C2059: syntax error : ';'
F:\实验结果\the practice of the struct.c(52) : error C2059: syntax error : ')'
F:\实验结果\the practice of the struct.c(53) : error C2449: found '{' at file scope (missing function header?)
F:\实验结果\the practice of the struct.c(55) : error C2059: syntax error : '}'
F:\实验结果\the practice of the struct.c(68) : error C2065: 'Redtype' : undeclared identifier
F:\实验结果\the practice of the struct.c(68) : error C2146: syntax error : missing ';' before identifier 'd'
F:\实验结果\the practice of the struct.c(68) : error C2065: 'd' : undeclared identifier
F:\实验结果\the practice of the struct.c(68) : error C2109: subscript requires array or pointer type
F:\实验结果\the practice of the struct.c(68) : error C2059: syntax error : '{'
F:\实验结果\the practice of the struct.c(69) : error C2146: syntax error : missing ';' before identifier 'sl'
F:\实验结果\the practice of the struct.c(69) : error C2065: 'sl' : undeclared identifier
F:\实验结果\the practice of the struct.c(70) : error C2143: syntax error : missing ';' before 'type'
F:\实验结果\the practice of the struct.c(71) : error C2065: 'i' : undeclared identifier
F:\实验结果\the practice of the struct.c(72) : error C2224: left of '.r' must have struct/union type
F:\实验结果\the practice of the struct.c(72) : error C2109: subscript requires array or pointer type
F:\实验结果\the practice of the struct.c(73) : error C2224: left of '.length' must have struct/union type
F:\实验结果\the practice of the struct.c(75) : warning C4013: 'print' undefined; assuming extern returning int
F:\实验结果\the practice of the struct.c(76) : warning C4013: 'Mergesort' undefined; assuming extern returning int
执行 cl.exe 时出错.