字符串排序
复制内容到剪贴板
代码:
/* Inputs a list of strings from the keyboard, sorts them, */
/* and then displays them on the screen. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINES 25
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;
/* Read in the lines from the keyboard. */
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]; /* Temporary storage for each line. */
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;
} /* End of get_lines() */
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 25
int get_lines(char *lines[]);
void sort1(char *p[], int n);
void sort2(char *p[], int n);
void print_strings(char *p[], int n);
char *lines[MAXLINES];
int main( void )
{
int number_of_lines, x;
void (*ptr)(char *p[], int n); //定义在这里没有问题的
number_of_lines = get_lines(lines);
if ( number_of_lines < 0 )
{
puts(" Memory allocation error");
exit(-1);
}
printf("Forward 1, Backword 2: ");
scanf("%d", &x);
if(x == 1)
{
ptr = sort1; //原来我这SB竟然把整个函数连同参数部分都写右边了
}
else if(x == 2)
{
ptr = sort2;
}
ptr(lines, number_of_lines); //原来我这SB竟然直接调用没给参数
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 sort1(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 sort2(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 25
int get_lines(char *lines[]);
void func1(void (* ptr_p)(char *p[], int n));
void sort1(char *p[], int n);
void sort2(char *p[], int n);
void print_strings(char *p[], int n);
char *lines[MAXLINES];
int number_of_lines; //子函数要用 所以全局了
int main( void )
{
int x;
void (*ptr)(char *p[], int n);
number_of_lines = get_lines(lines);
if ( number_of_lines < 0 )
{
puts(" Memory allocation error");
exit(-1);
}
printf("Forward 1, Backword 2: ");
scanf("%d", &x);
if(x == 1)
{
ptr = sort1; //原来我这SB竟然把整个函数连同参数部分都写右边了
}
else if(x == 2)
{
ptr = sort2;
}
func1(ptr);
print_strings(lines, number_of_lines);
return 0;
}
void func1(void (* ptr_p)(char *p[], int n))
{
ptr_p(lines, number_of_lines); //在这里卡了很长时间 闹了半天参数作用域要全局 我操 那我还用你干个P
}
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 sort1(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 sort2(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 25
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 ) //如果返回值小于0
{
puts("Memory allocation error"); //报错并退出
exit(-1);
}
puts("Enter 0 for reverse order sort, 1 for alphabetical:" ); //0反序1顺序
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') //输入的条件 gets()的返回值以前学过
{
if ((lines[n] = (char *)malloc(strlen(buffer)+1)) == NULL) //为每行字符传分配空间
return -1; //分配空间出错返回的值
strcpy( lines[n++], buffer ); //成功则把字符串拷贝进空间 然后n++ 继续循环直到退出
}
return n; //反回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++) //n-1次循环
{
for (b = 0; b < n-1; b++) //两层循环才能完成所有的排序 这行是确定从头到尾遍历一次
{
if (compare(p[b], p[b+1]) > 0) //调用前面指向的函数
{
x = p[b]; //用x来做中转
p[b] = p[b+1]; //如果是满足条件就交换
p[b+1] = x;
}
}
}
}
void print_strings(char *p[], int n) //n是接受的行数 p是指针数组
{
int count; //定义循环的次数
for (count = 0; count < n; count++) //因为n行 所以从0到n-1
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)); //其实都是传给它 只不过是谁减谁而已
}