RT被要求说出一段代码的功能以及找出其中三个错误说是不懂C语言也能看出来我读了一段时间,感觉出两问题不知道对不对(对C的语法什么都不太熟悉)之前只会写一点jQuery的DOM操作所以来求证一个是32行的strncmp(src_str+i,sub_str,sub_str_len)-直接把src_str和int型i相加在JS里面这样做会把两个拼成一个字符串不知道c里面这样做的结果会如何一个是sort_txt_line里面的for循环有可能第一个元素永远没有和最后一个元素进行比较...功能凭感觉就是把字符串转成数组排序然后输出出来求指正#include #include #include typedefunsignedintBOOL;#defineTRUE1#defineFALSE0#defineMAX_FILE_NAME_LEN255#defineMAX_PRO_TXT_LINES_NUM1024#defineMAX_CHAR_NUM_IN_ONE_LINE1024charsrc_txt_line_arr[MAX_PRO_TXT_LINES_NUM][MAX_CHAR_NUM_IN_ONE_LINE];voidswap(longlong*a,longlong*b){longlongtmp=*a;*a=*b;*b=tmp;}BOOLcontain_str(constchar*src_str,constchar*sub_str){unsignedintsrc_str_len;unsignedintsub_str_len;inti;src_str_len=strlen(src_str);sub_str_len=strlen(sub_str);if(src_str_len{ returnFALSE;}for(i=0;i<=src_str_len-sub_str_len;++i){if(0==strncmp(src_str+i,sub_str,sub_str_len)){returnTRUE;}}returnFALSE;}voidinit_txt_index_arr(longlong*arr,longlongnum){inti;for(i=0;i{ *(arr+i)=i;}}voidsort_txt_line(longlong*txt_index_arr,longlongarr_num){inti;intj;intindex;intindex_next;for(i=0;i{ for(j=0;j{ index=*(txt_index_arr+j);index_next=*(txt_index_arr+j+1);if(strcmp(src_txt_line_arr[index],src_txt_line_arr[index_next])>0){swap(&txt_index_arr[index],&txt_index_arr[index_next]);}}}}voidget_in_txt_line_arr(constchar*file_name,longlong*p_arr_num){FILE*fp;char*line=NULL;size_tlen=0;ssize_tread;longlongstep=0;fp=fopen(file_name,"r");if(NULL==fp){exit(EXIT_FAILURE);}while((read=getline(&line,&len,fp))!=-1){if(TRUE==contain_str(line,"abc")){strcpy(src_txt_line_arr[step++],line);}if(step>=MAX_PRO_TXT_LINES_NUM){printf("sorry.wecanonlyprocess%dlines\n",MAX_PRO_TXT_LINES_NUM);break;}}free(line);*p_arr_num=step;}voidout_put_str_arr(longlong*index_arr,longlongarr_num){longlongi;for(i=0;i{ printf("Line%03lld:%s",i,src_txt_line_arr[*(index_arr+i)]);}}intmain(intargc,char*argv[]){longlongdst_txt_index[MAX_PRO_TXT_LINES_NUM];longlongarr_num=0;if(argc!=2){printf("Usage:xxxfileName\n");exit(0);}get_in_txt_line_arr(argv[1],&arr_num);init_txt_index_arr(dst_txt_index,arr_num);sort_txt_line(dst_txt_index,arr_num);out_put_str_arr(dst_txt_index,arr_num);return0;}
慕盖茨4494581
相关分类