- 论坛徽章:
- 0
|
一个招聘试题,想找初程工作的来试试能力。
下面是我写的代码。因代码不规范(尤其变量命名方面,没有遵守匈牙利命名规则)被涮。受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=&
- /*检查参数个数*/
- if(argc!=3){
- fprintf(stderr,"You must input two parameter");
- exit(1);
- }
- item_src=argv[1];
- item_code=argv[2];
- /*下面代码检字符串中字符个数是否大于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];
- /*下面代码检查第二个参数是否是三位数字*/
- 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);
- /*调用日志函数*/
- 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;){
- /*
- *
- *下面查找域码的首地址,若此域码域长或域内容不合条件,则跳出进行下次查找
- *
- */
- 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指向域长的第一个字符*/
- /* 检查域长是否为三位数字*/
- 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函数中的length*/
- /*把域长后域长位字符复制到字符数组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; /*域内容不符合条件,跳出进行下次查找*/
- }
- *(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);
- /*把日期转换为文件名*/
- sprintf(filename,"%04d",tm_ptr->;tm_year+1900);
- strcat(filename,"");/*linux中必须此代码,否则执行.exe产生错误*/
- 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);
- }
复制代码 |
|