自己写一个字符串比较函数和排序函数不就搞定了。 char toUpperCase(char v) { if (v >= 'a' && v <= 'z') v += 'A' - 'a';
return v; }int strCmp(const char * s1, int len1, const char *s2, int len2) { int i, len; char c1, c2;
len = (len1 < len2) ? len1 : len2;
for (i = 0; i < len; i++) { c1 = toUpperCase(s1[i]); c2 = toUpperCase(s2[i]);
if (c1 != c2) return c1 - c2; }
return len1 - len2; } 排序的方法有多种,自己选择一种去实现就行了。
接11,快速排序的代码:// s1和s2必须长度相同, len是长度 void swap(char * s1, char * s2, int len) { int i; char tmp[len];
//字符串复制 i = 0; while (i < len && (tmp[i] = s1[i]));
i = 0; while (i < len && (s1[i] = s2[i]));
i = 0; while (i < len && (s2[i] = tmp[i])); }//len表示二维数组第2维的长度,start表示第一维的排序开始位置,end表示结束位置 void qsort(char arr[][], int len, int start, int end) { if (start >= end) return;
if (end - start == 1) //只有2个元素,排序后返回 { if (strCmp(arr[start], len, arr[end], len) > 0) swap(arr[start], arr[end], len);
return; } else { int mid = (start + end) / 2; int left = start; int right = end;
//基本排序 while (1) { //扫描左边,找到一个大于中间单词的词 for (; left < mid; left++) { if (strCmp(arr[left], len, arr[mid], len) > 0) break; }
//扫描右边,找到一个小于中间单词的词 for (; right > mid; right--) { if (strCmp(arr[right], len, arr[mid], len) < 0) break; }
if (left < mid && right > mid) // swap left and right { swap(arr[left], arr[right], len); left++; right--; } else if (left < mid) // swap left and mid { swap(arr[left], arr[mid], len); mid = left; right--; } else if (right > mid) // swap right and mid { swap(arr[mid], arr[right], len); mid = right; left++; } else { break; }
就是想用指针,也用不上啊,java 没有指针的概念。
4399
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>#define MAXL 50 //一个单词长度不能超过50字节
#define MAXW 5000 //定义单词个数不超过50000typedef struct
{
char word[MAXL];
int len;
}words;words dictionary[MAXW];
void main()
{
int i,j,k,n,sem,reserve,len,dis_num;
reserve=0;
dis_num=0;
FILE *fp1,*fp2;
char ch,exch[MAXL]; fp1=fopen("china daily","r");
if(fp1==NULL)
{
printf("Can't open file!\n");
exit(0);
} while(!feof(fp1))
{
printf("begin to read!\n");
ch=fgetc(fp1);
for(i=0;i<MAXW;i++)
{
j=reserve;
dictionary[i].len=reserve;
k=0;
while(ch!=' '&&k<50&&ch!='\n')
{
if(ch==EOF||ch==' '||ch=='\n')
{
// printf("EOF?");
break;
}
printf("ch is %c\n",ch);
dictionary[i].word[j++]=ch;
dictionary[i].len++;
k++;
ch=fgetc(fp1);
}
dictionary[i].word[j]='\0';
printf("We have read %d words!\n",i+1);
reserve=0;
while(ch==' '||ch=='\n')
{
ch=fgetc(fp1); //fgetc内部有文件内容指针,每读一次,位置后移一次,将其移到第二个单词
}
if(ch==EOF)
break; }
n=i+1; //记录行值
}
//打印缓存数组
for(i=0;i<n;i++)
{
printf("word%d is: ",i);
for(j=0;j<dictionary[i].len;j++)
{
printf("%c",dictionary[i].word[j]);
if(dictionary[i].word[j]==' ')
printf("()");
}
printf(" the length of word%d is %d.\n",i, dictionary[i].len);
printf("\n");
}
for(i=1;i<n;i++) //插入排序
{
j=i;
sem=0;
while(sem==0)
{
for(k=0;k<MAXL;k++)
{
if(j==0||dictionary[j].word[k]>dictionary[j-1].word[k])
{
sem++;
// printf("should break!");
break;
}
if(dictionary[j].word[k]<dictionary[j-1].word[k])
{
strcpy(exch,dictionary[j].word);
strcpy(dictionary[j].word,dictionary[j-1].word);
strcpy(dictionary[j-1].word,exch);
len=dictionary[j].len;
dictionary[j].len=dictionary[j-1].len;
dictionary[j-1].len=len;
j--;
//printf("%c,%c,%d\n",dictionary[j].word[k],dictionary[j-1].word[k],k);
//printf("Debug: i is %d,j is %d\n",i,j);
break;
}
if((k+1)>=dictionary[j].len||(k+1)>=dictionary[j-1].len)
{
sem++;
break;
}
}
}
//printf("the %d times paixu.\n",i);
}/* for(i=0;i<n;i++)
{
printf("word%d is: ",i);
for(j=0;j<dictionary[i].len;j++)
{
printf("%c",dictionary[i].word[j]);
if(dictionary[i].word[j]==' ')
printf("()");
}
printf(" the length of word%d is %d.\n",i, dictionary[i].len);
printf("\n");
}
*/ fp2=fopen("daily direction","w+");
if(fp2==NULL)
{
printf("Can't creat file!\n");
exit(0);
}
printf("Ok,creat the target file.\n");
for(i=0;i<n;i++)
{
if(i>0&&(dictionary[i].word[0]!=dictionary[i-1].word[0]||(dis_num)%10==0))
{
fputc('\n',fp2); //非同首字母单词不在同行, //每行最多显示10个单词
dis_num=0;
}
for(j=0;j<dictionary[i].len;j++)
fputc(dictionary[i].word[j],fp2);
fputc(' ',fp2);
dis_num++;
}
fclose(fp1);
fclose(fp2);}
从一个文件里读取全部单词,排序,并输出到另外一个文件。
怎么最近看到2个这样的帖子呢。
你参考里面的插入排序部分,有注释的,以前写的。你参考里面的
#include "stdio.h"int wordCompare(char wordOne[],char wordTwo[] ){ //比较两个单词,wordOne大,返回正数,小返回负数,相等返回0
int i=0;
while(wordOne[i]&&wordTwo[i]){
if(wordOne[i]!=wordTwo[i]) return wordOne[i]-wordTwo[i];
i++;
}
return wordOne[i]-wordTwo[i];
}void wordSwap(char wordOne[],char wordTwo[]){ // 交换两个单词的内容。
int i=0,j=0;
char temp;
while(wordOne[i]&&wordTwo[j]){
temp=wordOne[i];
wordOne[i]=wordTwo[j];
wordTwo[j]=temp;
i++;j++;
}
if(wordOne[j]){
while(wordOne[j]){
wordTwo[j]=wordOne[j];
j++;
}
wordOne[i]=0;
wordTwo[j]=0;
return;
}
wordOne[i]=0;
while(wordTwo[i]){
wordOne[i]=wordTwo[i];
i++;
}
wordOne[i]=0;
wordTwo[j]=0;
}main(){
char word[200][20]={"one","two","three","once","onee"};
int i,j,min;
for(i=0;i<5;i++) printf("%s ",word[i]);
printf("\n");
for(i=0;i<5-1;i++){ //选择法排序
min=i;
for(j=i+1;j<5;j++){
if(wordCompare(word[j],word[min])<0) min=j;
}
wordSwap(word[min],word[i]);
}
for(i=0;i<5;i++) printf("%s ",word[i]);
}
char toUpperCase(char v)
{
if (v >= 'a' && v <= 'z')
v += 'A' - 'a';
return v;
}int strCmp(const char * s1, int len1, const char *s2, int len2)
{
int i, len;
char c1, c2;
len = (len1 < len2) ? len1 : len2;
for (i = 0; i < len; i++)
{
c1 = toUpperCase(s1[i]);
c2 = toUpperCase(s2[i]);
if (c1 != c2)
return c1 - c2;
}
return len1 - len2;
}
排序的方法有多种,自己选择一种去实现就行了。
void swap(char * s1, char * s2, int len)
{
int i;
char tmp[len];
//字符串复制
i = 0;
while (i < len && (tmp[i] = s1[i]));
i = 0;
while (i < len && (s1[i] = s2[i]));
i = 0;
while (i < len && (s2[i] = tmp[i]));
}//len表示二维数组第2维的长度,start表示第一维的排序开始位置,end表示结束位置
void qsort(char arr[][], int len, int start, int end)
{
if (start >= end)
return;
if (end - start == 1) //只有2个元素,排序后返回
{
if (strCmp(arr[start], len, arr[end], len) > 0)
swap(arr[start], arr[end], len);
return;
}
else
{
int mid = (start + end) / 2;
int left = start;
int right = end;
//基本排序
while (1)
{
//扫描左边,找到一个大于中间单词的词
for (; left < mid; left++)
{
if (strCmp(arr[left], len, arr[mid], len) > 0)
break;
}
//扫描右边,找到一个小于中间单词的词
for (; right > mid; right--)
{
if (strCmp(arr[right], len, arr[mid], len) < 0)
break;
}
if (left < mid && right > mid) // swap left and right
{
swap(arr[left], arr[right], len);
left++;
right--;
}
else if (left < mid) // swap left and mid
{
swap(arr[left], arr[mid], len);
mid = left;
right--;
}
else if (right > mid) // swap right and mid
{
swap(arr[mid], arr[right], len);
mid = right;
left++;
}
else
{
break;
}
}
//对左半部分排序
qsort(arr, len, start, mid - 1);
//对右半部分排序
qsort(arr, len, mid + 1, end);
}
return;
}
#include <cstdlib>
#include <string>
using namespace std; int comp(const void* a,const void *b) ;
int main()
{
string s[5] = {"mahan","aaa","abbb","abb","aacc"};
int n=5;
qsort(s,n,sizeof(string),comp); //从小到大排序
for(int i=0;i<n;i++)
cout<<s[i]<<endl;
system("pause");
return 0;
}
int comp(const void* a,const void *b)
{
string *str1 = (string*) a;
string *str2 = (string*) b;
return ((*str1).compare(*str2));
}
直接将string 与string 比较久行 在java中string 为了提高调用的效率做了类似常量的处理方式!c/c++比较的是地址,所以直接比较大小是不行的可能不会运行时不一定出错的,但是机器不同处理结果就不一样
所以要用到 compare来判断