一个包含指针数组的排序和使用函数指针来控制排序方式

/* 该程序从键盘读取多行输入,在读取时为每一行分配内存空间,并使用char指针数组来跟踪这些内容。当用户输入一个空行,输入结束,程序按字母顺序对这些字符串进行排序,并将它们显示在屏幕上。*/

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

 

#define MAXLINES 5

 

int get_lines(char *lines[]);

void sort(char *p[], int n);

void print_strings(char *p[], int n);

 

char *lines[MAXLINES];

 

int main( void )

{  

int number_of_lines;

     number_of_lines = get_lines(lines);

 

   if ( number_of_lines < 0 )

   {   puts(" Memory allocation error");   exit(-1);   }

 

    sort(lines, number_of_lines);

    print_strings(lines, number_of_lines);

    return 0;

}

 

int get_lines(char *lines[])

{ 

 int n = 0;

   char buffer[80]; 

   puts("Enter one line at time; enter a blank when done.");

   while ((n < MAXLINES) && (gets(buffer) != 0) &&(buffer[0] != '/0'))

   {     

if ( (lines[n] = (char *)malloc(strlen(buffer)+1) ) == NULL) return -1;

       strcpy( lines[n++], buffer );  

}

   return n;   

}

 

void sort(char *p[], int n)

{  

int a, b;     char *x;

for (a = 1; a < n; a++)

{

for (b = 0; b < n-1; b++)

{

if (strcmp(p[b], p[b+1]) > 0)

{x = p[b]; p[b] = p[b+1]; p[b+1] = x;}

 }

}

}

 

void print_strings(char *p[], int n)

{  

int count;

    for (count = 0; count < n; count++)

       printf("%s/n", p[count]);       

}

 

*******************************************************************************

/*使用函数指针来控制排序方式*/

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#define MAXLINES 10

int get_lines(char *lines[]);

void sort(char *p[], int n, int sort_type);

void print_strings(char *p[], int n);

int alpha(char *p1, char *p2);

int reverse(char *p1, char *p2);

 

char *lines[MAXLINES];

 

int main( void )

{

   int number_of_lines, sort_type;

   number_of_lines = get_lines(lines);

 

   if ( number_of_lines < 0 ) { puts("Memory allocation error"); exit(-1);}

   puts("Enter 0 for reverse order sort, 1 for alphabetical:" );

   scanf("%d", &sort_type);

 

   sort(lines, number_of_lines, sort_type);

   print_strings(lines, number_of_lines);

   return 0;

}

 

int get_lines(char *lines[])

{   int n = 0;    char buffer[80];

    puts("Enter one line at time; enter a blank when done.");

    while (n < MAXLINES && gets(buffer) != 0 && buffer[0] != '/0')

    {   if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL)        return -1;

        strcpy( lines[n++], buffer );

    }

    return n;

}

void sort(char *p[], int n, int sort_type)

{    int a, b;    char *x;

     int (*compare)(char *s1, char *s2);

   compare = (sort_type) ? reverse : alpha;

    for (a = 1; a < n; a++)

    {    for (b = 0; b < n-1; b++)

        {    if (compare(p[b], p[b+1]) > 0)

            {   x = p[b];  p[b] = p[b+1];  p[b+1] = x;    }

        }

    }

}

 

void print_strings(char *p[], int n)

{    int count;

    for (count = 0; count < n; count++)

        printf("%s/n", p[count]);

}

int alpha(char *p1, char *p2){    return(strcmp(p2, p1));   }

int reverse(char *p1, char *p2){  return(strcmp(p1, p2));   }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值