69,382
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#define MAXLEN 1000 //每个输入文本行的最大长度
int getline(char *,int);
char *alloc(int);
void strcpy(char *s,char *t);
/*readlines函数:读取输入行*/
int readlines(char *lineptr[],int maxlines)
{
int len,nlines;
char *p,line[MAXLEN];
nlines = 0;
while((len = getline(line,MAXLEN)) > 0)
if(nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else{
line[len-1] = '\0'; //删除换行符
strcpy(p,line);
lineptr[nlines++] = p;
}
return nlines;
}
int getline(char *s,int lim)
{
int c,i;
for(i=0;(((c=getchar())!=EOF) && (c!='\n'));i++){
if(i<lim-2){
*s = c;
s++;
}
}
if(c=='\n'){
*s=c;
s++;
}
*s='\0';
return i;
}
#include<stdio.h>
void writelines(char *lineptr[],int nlines)
{
while(nlines-- > 0)
printf("%s\n",*lineptr++);
}
main()
{
int nlines;
if((nlines = readlines(lineptr,MAXLINES)) >= 0){
qsort(lineptr,0,nlines-1);
writelines(lineptr,nlines);
return 0;
}else{
printf("error:input too big to sort\n");
return 1;
}
}
#include<stdio.h>
#define ALLOCSIZE 10000
static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;
/*返回指向n个字符的指针*/
char *alloc(int n)
{
if((allocbuf + ALLOCSIZE) - allocp >= n){ //有足够的空闲空间
allocp += n;
return allocp - n; //分配前的指针p
}else //空闲空间不够
return 0;
}
qsort应该没问题吧,这个是快速排序