#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <malloc.h> #include <sys/stat.h> #include <sys/types.h> #include <errno.h> #include <fcntl.h> /* xiaolai 2011-5-09 I pay 8 hours to write ,so bad .... you need to be carry up !!! */ char **malloc2d( int r,int c){ int i; char ** str = malloc(r * sizeof(char *)); for(i=0;i<r;i++){ str[i] = malloc(c * sizeof(char)); } return str; } //Rotation void rotation(int length,char **str,char * line){ int i ,j; // puts("rotation"); for(i = 0;i<length;i++){ str[0][i] = line[i]; // printf("%c",str[0][i]); } // printf("---0----/n"); for(i = 1;i<length;i++){ for(j = 0;j<length-1;j++){ str[i][j] = str[i-1][j+1]; // printf("%c",str[i][j]); } str[i][length-1] = str[i-1][0]; // printf("%c/n", str[i][length-1]); } } void sort(int length,char **str){ int i,j; char temp[length+1]; for(i=0;i<length;i++) for (j=i+1;j<length;j++) { if(strcmp(str[i],str[j]) > 0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } } char find_key(int length,char **str){ char key = str[0][length - 1]; //printf("key is /n%c/n",key); return key; } int key_line_display(int length,char **str,char * line) { int i,j; int key_line; // puts("sort:"); for(i = 0 ;i< length;i++){ if(str[i] == NULL ) { puts("wrong /n"); } // else { puts(str[i]);} if(strcmp(str[i], line) == 0){key_line = i; } } return key_line; } void first_last(int length,char *temp_f,char * temp_l,char **str){ int i; for( i = 0;i<length;i++) { temp_f[i] = str[i][0]; temp_l[i] = str[i][length - 1]; } } void output(int length,int key_line,char * temp_l,char * temp_f,char key,char * last ,FILE * out){ int i,j; int cout = length -1; int a[length],b[length]; int number = 0 ; while(cout > -1){ // printf("keyline is %d,%c/n",key_line,key); last[cout] = key; for(i = 0 ;i< key_line + 1; i++) { if(temp_l[i] == key ){ number ++; } } int _temp_number = 0; for(i = 0 ;i< length;i++) { if(temp_f[i] == key ){ a[i] = 1; _temp_number++; if(_temp_number == number)break; } } key = temp_l[i]; key_line = i; number = _temp_number = 0; cout--; } //puts("/nThe result is :"); for(i = 0;i< length;i++){ fprintf(out, "%c",last[i]);} } static char * opt_input = NULL; static char * opt_output= NULL; int main(int argc ,char * argv[]) { int opt; char opts[]="i:o:"; opterr = 0; while((opt = getopt(argc,argv,opts)) != -1) switch (opt){ case 'i':opt_input = optarg; printf("the input file is :%s/n",opt_input); break; case 'o':opt_output = optarg; printf("the output is %s/n",opt_output); break; } FILE *p,*out; if((p= fopen(opt_input,"r")) == NULL){ fprintf(stderr,"Fail to open input/n"); exit(EXIT_FAILURE); } if((out = fopen (opt_output,"w")) == NULL){ fprintf(stderr,"Fail to open input/n"); exit(EXIT_FAILURE); } char line[2048]; while(fgets(line,sizeof(line),p)){ int i,j; int length = strlen(line); // calloc menory char ** str = malloc2d( length, length); //in it str rotation(length,str,line); char key = find_key(length,str); sort(length,str); int key_line = key_line_display(length,str,line); // aliagn char *temp_f = (char *)calloc((length+1),sizeof(char)); char *temp_l = (char *)calloc((length+1),sizeof(char)); first_last(length,temp_f,temp_l,str); //puts("line_l and f:"); // puts(temp_l); // puts(temp_f); char * last = (char *)calloc((length+1),sizeof(char)); output(length,key_line,temp_l,temp_f,key,last,out); fprintf(out,"%s",last); free(str); str=NULL; free(temp_f);temp_f = NULL; free(temp_l);temp_l = NULL; free(last);last = NULL; } return 0; }