首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

将字符串按字典排序的程序。解决思路

2012-05-11 
将字符串按字典排序的程序。[codeC/C++][/code]#include stdafx.h#include stdio.h#include string.h

将字符串按字典排序的程序。
[code=C/C++][/code]
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
char *a[4] = {"bacd","bad","bacde","abc"};
int com_char(char *a,char *b, int m)//比较两个字符串
{
//int i, j;
int len;
len = (strlen(a) > strlen(b) ? strlen(b) : strlen(a));//两个字符串中长度较小的一个
if(m < len)
{
if(a[m] > b[m])
return 1;
else
if(a[m] < b[m])
return 0;
else
return com_char(a,b,m + 1);
}
//else
//break;
}
void dsort(char a[4][10])//冒泡排序 //如何传递形参
{
char temp[20];
int i, j, m = 0;
for(i = 1; i < 4; i ++)
{
for(j = 0; j <4 - i; j ++)
{
if(com_char(a[i],a[j], m))
{
*temp = *a[i];
*a[i] = *a[j];
*a[j] = *temp;
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
dsort(a[4]);//怎么写实参
for(i = 0; i < 4; i ++)
printf("%s\t",a[i]);
return 0;
}
帮我看看,就是传递二维数组的时候函数的参数应该怎么写?

[解决办法]
函数原型:
void qsort(void *base,size_t nelem,size_t width,int (*Comp)(const void *,const void *));

对字符串进行排序 

struct In 

int data; 
char str[100]; 
}s[100]; 

//按照结构体中字符串str的字典顺序排序 

int cmp ( const void *a , const void *b ) 

return strcmp( (*(In *)a)->str , (*(In *)b)->str ); 


qsort(s,100,sizeof(s[0]),cmp); 


[解决办法]
另外关于qsort的一个使用的例子
http://blog.csdn.net/kuzuozhou/article/details/7285131
[解决办法]
泛型函数。。。

热点排行