- 論壇徽章:
- 0
|
一個招聘試題,想找初程工作的來試試能力。
下面是我寫的代碼。因代碼不規(guī)范(尤其變量命名方面,沒有遵守匈牙利命名規(guī)則)被涮。受linux影響啊。
- /*只輸出字符串中第一個符合條件的域,第二個不能輸出。*/
- #include<stdio.h>;
- #include<string.h>;
- #include<ctype.h>;
- #include<stdlib.h>;
- #include<time.h>;
- main(int argc,char *argv[])
- {
- int iGetItem(char *,char *,char *,int *);
- void log(char *,char *);
- char *item_src;
- char *item_code;
- char item_value[1000];
- int *item_len;
- int i,temp,length;
- item_len=&
- /*檢查參數(shù)個數(shù)*/
- if(argc!=3){
- fprintf(stderr,"You must input two parameter");
- exit(1);
- }
- item_src=argv[1];
- item_code=argv[2];
- /*下面代碼檢字符串中字符個數(shù)是否大于6,小于6則沒有完整的域*/
- for(i=0;i<6;i++){
- if((*item_src)=='\0'){
- fprintf(stderr,"the string must more than 6 character");
- exit(1);
- }
- item_src++;
- }
- item_src=argv[1];
- /*下面代碼檢查第二個參數(shù)是否是三位數(shù)字*/
- for(i=0;i<3;i++){
- if(isdigit(*(item_code+i))==0){
- fprintf(stderr,"the argv[2] must be three numbers");
- exit(1);
- }
- }
- item_code=argv[2];
- memset(item_value,'\0',sizeof(item_value));
- iGetItem(item_src,item_code,item_value,item_len);
- printf("Item[%s],value=[%s],length=%d",item_code,item_value,*item_len);
- /*調(diào)用日志函數(shù)*/
- log(argv[1],argv[2]);
- exit(0);
- }
- int iGetItem(char *szSrc,char *szItemCode,char *szItemValue,int *piItemlen)
- {
- char *item_point_2;
- char *item_point_end;
- char *item_point_oth;
- char *item_code_2;
- char *item_value_2;
- int i;
- int length_2;
- item_value_2=szItemValue;
- item_code_2=szItemCode;
- item_point_2=szSrc;
- item_point_end=szSrc+strlen(szSrc)-6;
- /*item_point_2不可以指向字符串的最后5個字符,沒有完整的域*/
- for(;item_point_2<item_point_end;){
- /*
- *
- *下面查找域碼的首地址,若此域碼域長或域內(nèi)容不合條件,則跳出進行下次查找
- *
- */
- item_point_oth=strstr(item_point_2,item_code_2);
- if(item_point_oth==NULL||item_point_oth>;item_point_end){
- printf("No such item\n");
- exit(0);
- }
- item_point_2=item_point_oth;
- item_point_oth+=3; /*使item_point_oth指向域長的第一個字符*/
- /* 檢查域長是否為三位數(shù)字*/
- for(i=0;i<3;i++){
- if(isdigit(*(item_point_oth+i))==0)
- goto loop; /*域長不符合條件,跳出進行下次查找*/
- }
- /*求域長*/
- length_2=((int)(*(item_point_oth))-48)*100+((int)(*(item_point_oth+1))-48)*10+((int)(*(item_point_oth+2))-48);
- *piItemlen=length_2; /*賦值給main函數(shù)中的length*/
- /*把域長后域長位字符復制到字符數(shù)組item_value_2 */
- item_point_oth+=3;
- for(i=0;i<length_2;i++){
- if(*(item_point_oth)=='\0'){
- memset(szItemValue,'\0',i);
- item_value_2=szItemValue;
- goto loop; /*域內(nèi)容不符合條件,跳出進行下次查找*/
- }
- *(item_value_2++)=*(item_point_oth++);
- }
- return(1);
- loop: item_point_2++;
- }
- }
- void log(char *point_a,char *point_b)
- {
- struct tm *tm_ptr;
- time_t the_time;
- char filename[12];
- char temp[2];
- FILE *fp;
- char *point_1;
- char *point_2;
- (void)time(&the_time);
- tm_ptr=localtime(&the_time);
- /*把日期轉(zhuǎn)換為文件名*/
- sprintf(filename,"%04d",tm_ptr->;tm_year+1900);
- strcat(filename,"");/*linux中必須此代碼,否則執(zhí)行.exe產(chǎn)生錯誤*/
- sprintf(temp,"%02d",tm_ptr->;tm_mon+1);
- strcat(filename,temp);
- sprintf(temp,"%02d",tm_ptr->;tm_mday);
- strcat(filename,temp);
- strcat(filename,".log");
- if((fp=fopen(filename,"a+"))==NULL){
- fprintf(stderr,"Cannot open %s",filename);
- exit(1);
- }
- point_1=point_a;
- point_2=point_b;
- for(;*point_1!='\0';point_1++)
- fprintf(fp," %d ",*point_1);
- fprintf(fp," %s\n",point_a);
- for(;*point_2!='\0';point_2++)
- fprintf(fp," %d",*point_2);
- fprintf(fp," %s\n",point_b);
- fclose(fp);
- }
復制代碼 |
|