您好,欢迎访问三七文档
100、有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中.#includestdio.h#includestdlib.hintmain(intargc,char*argv[]){FILE*fp;inti,j,k,num,NUM;charc[50],t,ch;if((fp=fopen(A,r))==NULL)/*canbereplacedbyopen*intfd=open(A,O_RDONLY|O_CREAT);*/{printf(fileAcannotbeopened\n);exit(0);}printf(\nAcontentsare:\n);for(i=0;(ch=fgetc(fp))!=EOF;i++)/*一个字符一个字符读*/{c[i]=ch;putchar(c[i]);}num=i+1;fclose(fp);if((fp=fopen(B,r))==NULL){printf(fileBcannotbeopened\n);exit(0);}printf(\nBcontentsare:\n);for(i=0;(ch=fgetc(fp))!=EOF;i++){c[num+i]=ch;putchar(c[num+i]);}fclose(fp);NUM=num+i+1;for(k=0;kNUM-1;k++)/*冒泡排序*/{for(j=0;jNUM-k-1;j++){if(c[j]c[j+1]){t=c[j];c[j]=c[j+1];c[j+1]=t;}}}printf(\nCfileis:\n);fp=fopen(C,w);for(i=0;iNUM;i++){putc(c[i],fp);/*将字符一个个写入文件中*/putchar(c[i]);/*一个个输出字符*/}fclose(fp);return1;}86.有一浮点型数组A,用C语言写一函数实现对浮点数组A进行降序排序,并输出结果,要求要以数组A作为函数的入口.(建议用冒泡排序法)#includestdio.h#includestdlib.hvoidBubbleSort(intarr[],intn){inti,j;intexchange=1;//交换标志,提高算法效率;inttemp;for(i=0;in-1;i++){exchange=0;//本趟排序开始前,交换标志应为假for(j=0;jn-i-1;j++){if(arr[j+1]arr[j]){temp=arr[j+1];arr[j+1]=arr[j];arr[j]=temp;exchange=1;//发生了交换,故将交换标志置为真}}if(!exchange)//本趟排序未发生交换,提前终止算法return;}}intmain(intargc,char*argv[]){intarr[5]={1,4,2,6,5};inti;BubbleSort(arr,5);printf(aftersort,arris:\n);for(i=0;i5;i++){printf(%3d,arr[i]);}return1;}77.写出二分查找的代码:Intbinary_search(int*arr,intkey,intsize){Intmid;Intlow=0;Inthigh=size-1;While(low=high){Mid=(low+high)/2;If(arr[mid]key)High=mid-1;ElseIf(arr[mid]key)Low=mid+1;ElseReturnmid;}Return-1;}补充1:用帅选法查找100之内的质数#includeiostreamusingnamespacestd;#defineN100intmain(){/*0~100共101个数*/intsieve[N+1];inti;//step1:初始化(sieve[i]=0表示不在筛中,即不是质数;1表示在筛中)sieve[0]=sieve[1]=0;for(inti=2;i=N;i++){sieve[i]=1;}//step2:偶数(2的倍数)肯定不是质数,所以应该先筛除for(i=2;i=N/2;i++){sieve[i*2]=0;}intp=2;//第一个质数是2//step3:从sieve中删去P的倍数while(p*p=N){p=p+1;//选下一个pwhile(sieve[p]==0){p++;}intt=p*p;ints=2*p;/*质数与质数之和包含合数,但质数于合数之和必为质数,提高算法效率*/while(t=N){sieve[t]=0;//删除t=t+s;}}//step4:输出结果for(i=2;i=N;i++){if(sieve[i]!=0){couti,;}}return1;}《《《《链表操作考察》》》》87、实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数。//删除操作StatusListDelete_DuL(DuLinkList&L,inti,ElemType&e){if(!(p=GetElemP_DuL(L,i)))returnERROR;//容错判断;e=p-data;p-prior-next=p-next;p-next-prior=p-pror;free(p);p=NULL;//勿忘,否则内存泄露returnOK;}//插入操作StatusListInsert_DuL(DuLinkList&L,inti,ElemType&e){if(!(p=GetElemP_DuL(L,i)))returnERROR;if(!(s=(DuLinkList)malloc(sizeof(DuLNode))))returnERROR;/*assert((s=(DuLinkList)malloc(sizeof(DuLNode)))!=NULL)*/s-data=e;s-prior=p;p-next-prior=s;p-next=s;s-next=p-next-next;returnOK;}88、把一个链表反向。//链表头插法;intre_Link(LinklistH){Linklistp=H-next,q;H-next=NULL;while(p!=NULL){q=p;p=p-next;q-next=H-next;H-next=q;}return0;}《《《《strcpy和memcpy》》》》76.已知strcpy函数的原型是char*strcpy(char*strDest,constchar*strSrc);其中strDest是目的字符串,strSrc是源字符串。(1)不调用C++/C的字符串库函数,请编写函数strcpy。char*stringcpy(char*Des,constchar*Src){assert((Des!=NULL)&&(Src!=NULL));char*address=Des;while((*Des++=*Src++)!='\0');returnaddress;}断言assert是一个宏,该宏在<assert>中,当使用assert时候,给他个参数,即一个判读为真的表达式。预处理器产生测试该断言的代码,如果断言不为真,则发出一个错误信息告诉断言是什么以及它失败一会,程序会终止。我们一般可以用在判断某件操作是否成功上。详见《高质量c&c++编程,林锐,6.5章》(2)strcpy能把strSrc的内容复制到strDest,为什么还要char*类型的返回值?为了实现链式表达式:intlen=strlen(stringcpy(Des,hello));内存复制:void*memcpy(void*pvTo,constvoid*pvFrom,size_tsize){assert((pvTo!=NULL)&&(pvFrom!=NULL));byte*pbTo=pvTo;byte*pbFrom=pbFrom;while(size--0){*pbTo++=*pbFrom++;}returnpvTo;}注意:内存拷贝时要避免内存空间重叠的问题,(即pvfrom与pvto所指向的内存不能重叠)为了防止内存空间重叠,若是目标地址高于源地址,从后往前复制;若是源地址高于目标地址,从前往后复制;《《《《查找字符串中的子串》》》》84、请编写一个C函数,该函数在一个字符串中找到可能的最长的子字符串,该字符串是由同一字符组成的。#includestdio.h#includestdlib.h#includestring.hintChildString(char*p){char*q=p;intstringlen=0,i=0,j=1,len=0,maxlen=1;//stringlen=strlen(p);while(*q!='\0')//不能用strlen,求得长stringlen{stringlen++;q++;}while(istringlen){if(*(p+i)==*(p+j)&&jstringlen){len++;//统计子串长度i++;j++;}else{if(len=maxlen)//统计最大子串长度{maxlen=len+1;len=0;}elselen=0;i++;j++;}}returnmaxlen;}intmain(intargc,char*argv[]){chararr[11];intlen;printf(pleaseinputchararr(10):\n);scanf(%s,arr);len=ChildString(arr);printf(thelenofchildarris:%d\n,len);return1;}99.计算字符串中子串出现的次数方法1;intmain(intargc,char*argv[]){charstr1[20],str2[20],*p1,*p2;intsum=0;printf(pleaseinputtwostrings\n);scanf(%s%s,str1,str2);p1=str1;p2=str2;while(*p1!='\0'){if(*p1==*p2){while((*p1++==*p2++)&&*p2!='\0');/*不断比较字符串1与2,至字符串2到达‘\0’*/}elsep1++;/*如果,字符串2一次匹配已结束,或者此刻*p1与*p2不等;*/if(*p2=='\0')/*如果是字符串2结束,则成功找到一次,sum++*/sum++;p2=str2;/*p2始终指向str2;*/}printf(%d,sum);return1;}方法2:#includestdio.h#includestdlib.h#includestring.h//判断两字符串是否相等,相等返回1,不等返回0intJudge(char*movePt,char*tempPt)#if1{intret=0;while(!(*movePt-*tempPt)&&*tempPt){movePt++;tempPt++;}if(*tempPt=='\0'){ret=1;}returnret;}#endif#if0{inti;for(i=0;istrlen(tempPt);i++,movePt++){if(*movePt!=tempPt[i])return0;return1;}}#endif//计算子串出现的次数,str为原字符串,sub为子串intStrCount(char*str,char*sub){intcount=0;char*move=str;if(strlen(str)strlen(sub)){return0;}else{while(strlen(move)=strlen(sub)){printf(%s\n,move);if(Judge(move,sub)){count++;printf(cou
本文标题:经典c语言笔试题
链接地址:https://www.777doc.com/doc-2135707 .html