C和指针部分课后习题代码.docx
《C和指针部分课后习题代码.docx》由会员分享,可在线阅读,更多相关《C和指针部分课后习题代码.docx(14页珍藏版)》请在冰豆网上搜索。
C和指针部分课后习题代码
第四单元部分编程题
#include
/*4.14
第1题编写一个程序,读入一个值,计算并打印它的平方根
a[i+1]=(a[i]+(n/a[i]))/2*/
floatMysqrt(floatn)
{
floats;
floatf;
if(n<0)
returnfalse;
}
else
f=1;
do
s=f;
f=(s+n/s)/2;
}while(s!
=f);
returnf;
/*第6题请编写一个函数,他从一个字符串中提取一个子字符串。
*/
intSubstr(char*des,char*src,intstart,intlen)
intcount=0;
while(*src!
='\0')
count++;
*src++;
if(count==start)
inti;
for(i=0;i{des[i]=src[i];if(src[i]=='\0'){des[i]='\0';break;}}des[i]='\0';}}returnstrlen(des);}voidShow(char*str,intlen){for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*第7题编写一个函数,从一个字符串中去掉多余的空格。*/voidDeblank(char*str,intlen){intcount=0;inti;for(i=0;i{if(str[i]==''){count++;}if(count>1&&str[i+1]!=''){for(intk=i;k{str[k-count+2]=str[k+1];}count=0;}if(str[i]==''&&str[i+1]!=''){count=0;}}}intmain(){/*--第1题--*/printf("%f\n",Mysqrt(4));printf("%f\n",Mysqrt(16));/*--第6题--*/charstr1[]="helloworldchina";charstr2[10];printf("%d\n",Substr(str2,str1,5,5));printf("%d\n",Substr(str2,str1,10,10));/*--第7题--*/charstr3[]="helloworldchina";Deblank(str3,strlen(str3));Show(str3,strlen(str3));return0;} 第五单元部分编程题#include#include#include/*请编写一个函数unsignedintreverse_bits(unsignedintvalue)这个函数的返回值是把value的二进制模式从左到右变换一下后的值*/unsignedintreverse_bits(unsignedintvalue){unsignedintrslt=(value&0);unsignedintflag=1;//标志位移次数while(flag!=0){if((value&1)==1){rslt=(rslt<<1)+1;//若为1时rslt加1再向前移1位}else{rslt<<=1;}value>>=1;flag<<=1;}returnrslt;}intmain(){printf("%u\n",reverse_bits(25));printf("%u\n",reverse_bits(1));printf("%u\n",reverse_bits(15));return0;}第七单元部分编程题#include/*第1题厄密多项式*/intHermitePolynomials(intx,intn){inth;if(n<=0){h=1;}elseif(n==1){h=2*x;}elseif(n>=2){h=2*x*HermitePolynomials(x,n-1)-2*(n-1)*HermitePolynomials(x,n-2);}returnh;}/*第2题编写一个叫gcd的函数,它接受两个整形参数,并返回这两个数的最大公约数*/intGcd(inta,intb){inth;intr;inttemp;if(a<0||b<0){return0;}if(a{temp=a;a=b;b=temp;}if(a%b==0){h=b;}if((r=a%b)>0){h=Gcd(b,r);}returnh;}intmain(){printf("%d\n",HermitePolynomials(3,-1));printf("%d\n",HermitePolynomials(3,1));printf("%d\n\n",HermitePolynomials(3,2));printf("%d\n",Gcd(24,12));printf("%d\n",Gcd(24,-12));printf("%d\n\n",Gcd(14,24));return0;} 第九单元部分编程题 #include#include#include#include/*拷贝字符串中前n个字符*/voidMystrncpy(char*desstr,char*stcstr,intn){if(strlen(stcstr)>(unsigned)n){for(inti=0;i{desstr[i]=stcstr[i];}}else{while(*stcstr!='\0'){*desstr++=*stcstr++;}desstr='\0';}}voidShow(char*str,intlen){for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*连接字符串中前n个字符*/char*Mystrncat(char*str1,char*str2,intn){char*p=str1;intj=strlen(str1);if(strlen(str2)>(unsigned)n){for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
des[i]=src[i];
if(src[i]=='\0')
des[i]='\0';
break;
returnstrlen(des);
voidShow(char*str,intlen)
for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*第7题编写一个函数,从一个字符串中去掉多余的空格。*/voidDeblank(char*str,intlen){intcount=0;inti;for(i=0;i{if(str[i]==''){count++;}if(count>1&&str[i+1]!=''){for(intk=i;k{str[k-count+2]=str[k+1];}count=0;}if(str[i]==''&&str[i+1]!=''){count=0;}}}intmain(){/*--第1题--*/printf("%f\n",Mysqrt(4));printf("%f\n",Mysqrt(16));/*--第6题--*/charstr1[]="helloworldchina";charstr2[10];printf("%d\n",Substr(str2,str1,5,5));printf("%d\n",Substr(str2,str1,10,10));/*--第7题--*/charstr3[]="helloworldchina";Deblank(str3,strlen(str3));Show(str3,strlen(str3));return0;} 第五单元部分编程题#include#include#include/*请编写一个函数unsignedintreverse_bits(unsignedintvalue)这个函数的返回值是把value的二进制模式从左到右变换一下后的值*/unsignedintreverse_bits(unsignedintvalue){unsignedintrslt=(value&0);unsignedintflag=1;//标志位移次数while(flag!=0){if((value&1)==1){rslt=(rslt<<1)+1;//若为1时rslt加1再向前移1位}else{rslt<<=1;}value>>=1;flag<<=1;}returnrslt;}intmain(){printf("%u\n",reverse_bits(25));printf("%u\n",reverse_bits(1));printf("%u\n",reverse_bits(15));return0;}第七单元部分编程题#include/*第1题厄密多项式*/intHermitePolynomials(intx,intn){inth;if(n<=0){h=1;}elseif(n==1){h=2*x;}elseif(n>=2){h=2*x*HermitePolynomials(x,n-1)-2*(n-1)*HermitePolynomials(x,n-2);}returnh;}/*第2题编写一个叫gcd的函数,它接受两个整形参数,并返回这两个数的最大公约数*/intGcd(inta,intb){inth;intr;inttemp;if(a<0||b<0){return0;}if(a{temp=a;a=b;b=temp;}if(a%b==0){h=b;}if((r=a%b)>0){h=Gcd(b,r);}returnh;}intmain(){printf("%d\n",HermitePolynomials(3,-1));printf("%d\n",HermitePolynomials(3,1));printf("%d\n\n",HermitePolynomials(3,2));printf("%d\n",Gcd(24,12));printf("%d\n",Gcd(24,-12));printf("%d\n\n",Gcd(14,24));return0;} 第九单元部分编程题 #include#include#include#include/*拷贝字符串中前n个字符*/voidMystrncpy(char*desstr,char*stcstr,intn){if(strlen(stcstr)>(unsigned)n){for(inti=0;i{desstr[i]=stcstr[i];}}else{while(*stcstr!='\0'){*desstr++=*stcstr++;}desstr='\0';}}voidShow(char*str,intlen){for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*连接字符串中前n个字符*/char*Mystrncat(char*str1,char*str2,intn){char*p=str1;intj=strlen(str1);if(strlen(str2)>(unsigned)n){for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
printf("%c",str[i]);
printf("\n");
/*第7题编写一个函数,从一个字符串中去掉多余的空格。
voidDeblank(char*str,intlen)
for(i=0;i{if(str[i]==''){count++;}if(count>1&&str[i+1]!=''){for(intk=i;k{str[k-count+2]=str[k+1];}count=0;}if(str[i]==''&&str[i+1]!=''){count=0;}}}intmain(){/*--第1题--*/printf("%f\n",Mysqrt(4));printf("%f\n",Mysqrt(16));/*--第6题--*/charstr1[]="helloworldchina";charstr2[10];printf("%d\n",Substr(str2,str1,5,5));printf("%d\n",Substr(str2,str1,10,10));/*--第7题--*/charstr3[]="helloworldchina";Deblank(str3,strlen(str3));Show(str3,strlen(str3));return0;} 第五单元部分编程题#include#include#include/*请编写一个函数unsignedintreverse_bits(unsignedintvalue)这个函数的返回值是把value的二进制模式从左到右变换一下后的值*/unsignedintreverse_bits(unsignedintvalue){unsignedintrslt=(value&0);unsignedintflag=1;//标志位移次数while(flag!=0){if((value&1)==1){rslt=(rslt<<1)+1;//若为1时rslt加1再向前移1位}else{rslt<<=1;}value>>=1;flag<<=1;}returnrslt;}intmain(){printf("%u\n",reverse_bits(25));printf("%u\n",reverse_bits(1));printf("%u\n",reverse_bits(15));return0;}第七单元部分编程题#include/*第1题厄密多项式*/intHermitePolynomials(intx,intn){inth;if(n<=0){h=1;}elseif(n==1){h=2*x;}elseif(n>=2){h=2*x*HermitePolynomials(x,n-1)-2*(n-1)*HermitePolynomials(x,n-2);}returnh;}/*第2题编写一个叫gcd的函数,它接受两个整形参数,并返回这两个数的最大公约数*/intGcd(inta,intb){inth;intr;inttemp;if(a<0||b<0){return0;}if(a{temp=a;a=b;b=temp;}if(a%b==0){h=b;}if((r=a%b)>0){h=Gcd(b,r);}returnh;}intmain(){printf("%d\n",HermitePolynomials(3,-1));printf("%d\n",HermitePolynomials(3,1));printf("%d\n\n",HermitePolynomials(3,2));printf("%d\n",Gcd(24,12));printf("%d\n",Gcd(24,-12));printf("%d\n\n",Gcd(14,24));return0;} 第九单元部分编程题 #include#include#include#include/*拷贝字符串中前n个字符*/voidMystrncpy(char*desstr,char*stcstr,intn){if(strlen(stcstr)>(unsigned)n){for(inti=0;i{desstr[i]=stcstr[i];}}else{while(*stcstr!='\0'){*desstr++=*stcstr++;}desstr='\0';}}voidShow(char*str,intlen){for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*连接字符串中前n个字符*/char*Mystrncat(char*str1,char*str2,intn){char*p=str1;intj=strlen(str1);if(strlen(str2)>(unsigned)n){for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
if(str[i]=='')
if(count>1&&str[i+1]!
='')
for(intk=i;k{str[k-count+2]=str[k+1];}count=0;}if(str[i]==''&&str[i+1]!=''){count=0;}}}intmain(){/*--第1题--*/printf("%f\n",Mysqrt(4));printf("%f\n",Mysqrt(16));/*--第6题--*/charstr1[]="helloworldchina";charstr2[10];printf("%d\n",Substr(str2,str1,5,5));printf("%d\n",Substr(str2,str1,10,10));/*--第7题--*/charstr3[]="helloworldchina";Deblank(str3,strlen(str3));Show(str3,strlen(str3));return0;} 第五单元部分编程题#include#include#include/*请编写一个函数unsignedintreverse_bits(unsignedintvalue)这个函数的返回值是把value的二进制模式从左到右变换一下后的值*/unsignedintreverse_bits(unsignedintvalue){unsignedintrslt=(value&0);unsignedintflag=1;//标志位移次数while(flag!=0){if((value&1)==1){rslt=(rslt<<1)+1;//若为1时rslt加1再向前移1位}else{rslt<<=1;}value>>=1;flag<<=1;}returnrslt;}intmain(){printf("%u\n",reverse_bits(25));printf("%u\n",reverse_bits(1));printf("%u\n",reverse_bits(15));return0;}第七单元部分编程题#include/*第1题厄密多项式*/intHermitePolynomials(intx,intn){inth;if(n<=0){h=1;}elseif(n==1){h=2*x;}elseif(n>=2){h=2*x*HermitePolynomials(x,n-1)-2*(n-1)*HermitePolynomials(x,n-2);}returnh;}/*第2题编写一个叫gcd的函数,它接受两个整形参数,并返回这两个数的最大公约数*/intGcd(inta,intb){inth;intr;inttemp;if(a<0||b<0){return0;}if(a{temp=a;a=b;b=temp;}if(a%b==0){h=b;}if((r=a%b)>0){h=Gcd(b,r);}returnh;}intmain(){printf("%d\n",HermitePolynomials(3,-1));printf("%d\n",HermitePolynomials(3,1));printf("%d\n\n",HermitePolynomials(3,2));printf("%d\n",Gcd(24,12));printf("%d\n",Gcd(24,-12));printf("%d\n\n",Gcd(14,24));return0;} 第九单元部分编程题 #include#include#include#include/*拷贝字符串中前n个字符*/voidMystrncpy(char*desstr,char*stcstr,intn){if(strlen(stcstr)>(unsigned)n){for(inti=0;i{desstr[i]=stcstr[i];}}else{while(*stcstr!='\0'){*desstr++=*stcstr++;}desstr='\0';}}voidShow(char*str,intlen){for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*连接字符串中前n个字符*/char*Mystrncat(char*str1,char*str2,intn){char*p=str1;intj=strlen(str1);if(strlen(str2)>(unsigned)n){for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
str[k-count+2]=str[k+1];
count=0;
if(str[i]==''&&str[i+1]!
intmain()
/*--第1题--*/
printf("%f\n",Mysqrt(4));
printf("%f\n",Mysqrt(16));
/*--第6题--*/
charstr1[]="helloworldchina";
charstr2[10];
printf("%d\n",Substr(str2,str1,5,5));
printf("%d\n",Substr(str2,str1,10,10));
/*--第7题--*/
charstr3[]="helloworldchina";
Deblank(str3,strlen(str3));
Show(str3,strlen(str3));
return0;
第五单元部分编程题
/*请编写一个函数unsignedintreverse_bits(unsignedintvalue)
这个函数的返回值是把value的二进制模式从左到右变换一下后的值*/
unsignedintreverse_bits(unsignedintvalue)
unsignedintrslt=(value&0);
unsignedintflag=1;//标志位移次数
while(flag!
=0)
if((value&1)==1)
rslt=(rslt<<1)+1;//若为1时rslt加1再向前移1位
rslt<<=1;
value>>=1;
flag<<=1;
returnrslt;
printf("%u\n",reverse_bits(25));
printf("%u\n",reverse_bits
(1));
printf("%u\n",reverse_bits(15));
第七单元部分编程题
/*第1题厄密多项式*/
intHermitePolynomials(intx,intn)
inth;
if(n<=0)
h=1;
elseif(n==1)
h=2*x;
elseif(n>=2)
h=2*x*HermitePolynomials(x,n-1)-2*(n-1)*HermitePolynomials(x,n-2);
returnh;
/*第2题编写一个叫gcd的函数,它接受两个整形参数,
并返回这两个数的最大公约数*/
intGcd(inta,intb)
intr;
inttemp;
if(a<0||b<0)
if(a
temp=a;
a=b;
b=temp;
if(a%b==0)
h=b;
if((r=a%b)>0)
h=Gcd(b,r);
printf("%d\n",HermitePolynomials(3,-1));
printf("%d\n",HermitePolynomials(3,1));
printf("%d\n\n",HermitePolynomials(3,2));
printf("%d\n",Gcd(24,12));
printf("%d\n",Gcd(24,-12));
printf("%d\n\n",Gcd(14,24));
第九单元部分编程题
/*拷贝字符串中前n个字符*/
voidMystrncpy(char*desstr,char*stcstr,intn)
if(strlen(stcstr)>(unsigned)n)
for(inti=0;i{desstr[i]=stcstr[i];}}else{while(*stcstr!='\0'){*desstr++=*stcstr++;}desstr='\0';}}voidShow(char*str,intlen){for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*连接字符串中前n个字符*/char*Mystrncat(char*str1,char*str2,intn){char*p=str1;intj=strlen(str1);if(strlen(str2)>(unsigned)n){for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
desstr[i]=stcstr[i];
while(*stcstr!
*desstr++=*stcstr++;
desstr='\0';
for(inti=0;i{printf("%c",str[i]);}printf("\n");}/*连接字符串中前n个字符*/char*Mystrncat(char*str1,char*str2,intn){char*p=str1;intj=strlen(str1);if(strlen(str2)>(unsigned)n){for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
/*连接字符串中前n个字符*/
char*Mystrncat(char*str1,char*str2,intn)
char*p=str1;
intj=strlen(str1);
if(strlen(str2)>(unsigned)n)
for(inti=0;i{str1[j]=str2[i];j++;}str1[j]='\0';}else{while(*str1!='\0'){*str1++;}while(*str2!='\0'){*str1++=*str2++;}str1='\0';}returnp;}/*比较字符串中前n个字符的大小*/intMystrncmp(char*str1,char*str2,intn){intcount=0;inttmp;if(strlen(str2)<(unsigned)n){while(count<=(unsigned)n){tmp=*str1-*str2;*str1++;*str2++;count++;}}else{while((tmp=*str1-*str2)==0&&*str1!='\0'){*str1++;*str2++;}}returntmp;}/*将一个数字转化成一个字符型式*/voidMyitoa(char*str,intnum,intradix){inti=0;chartmp;intcount=0;charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表unsignedunum;if(num<0)//将负数先转化成正数{unum=-num;str[i]='-';i++;}else{unum=num;}while(unum!=0){str[i++]=index[unum%radix];//将数字转化成字符unum/=radix;count++;}str[i]='\0';i--;if(str[0]=='-'){tmp=str[0];for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
str1[j]=str2[i];
j++;
str1[j]='\0';
while(*str1!
*str1++;
while(*str2!
*str1++=*str2++;
str1='\0';
returnp;
/*比较字符串中前n个字符的大小*/
intMystrncmp(char*str1,char*str2,intn)
inttmp;
if(strlen(str2)<(unsigned)n)
while(count<=(unsigned)n)
tmp=*str1-*str2;
*str2++;
while((tmp=*str1-*str2)==0&&*str1!
returntmp;
/*将一个数字转化成一个字符型式*/
voidMyitoa(char*str,intnum,intradix)
inti=0;
chartmp;
charindex[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表
unsignedunum;
if(num<0)//将负数先转化成正数
unum=-num;
str[i]='-';
i++;
unum=num;
while(unum!
str[i++]=index[unum%radix];//将数字转化成字符
unum/=radix;
str[i]='\0';
i--;
if(str[0]=='-')
tmp=str[0];
for(intk=0;k{str[k]=str[k+1];//将数组中的每个字符向前移动移动一位}str[count]=tmp;//将负号置于数组末尾}for(intj=0;j{tmp=str[j];str[j]=str[i];str[i]=tmp;}}/*第7题编写一个名叫my_strrchr的函数*/char*my_strrchr(char*str,intch){for(inti=strlen(str)-1;i>=0;i--){if(str[i]==ch){returnstr+i;}}returnfalse;}/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个参数指定ch字符在str字符中第几次出现。*/char*my_strnchr(char*str,intch,intwhich){intcount=0;for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
str[k]=str[k+1];//将数组中的每个字符向前移动移动一位
str[count]=tmp;//将负号置于数组末尾
for(intj=0;j
tmp=str[j];
str[j]=str[i];
str[i]=tmp;
/*第7题编写一个名叫my_strrchr的函数*/
char*my_strrchr(char*str,intch)
for(inti=strlen(str)-1;i>=0;i--)
if(str[i]==ch)
returnstr+i;
/*编写一个名叫my_strnchr的函数,此函数类似于strchr但它的第3个
参数指定ch字符在str字符中第几次出现。
char*my_strnchr(char*str,intch,intwhich)
for(unsignedinti=0;i{if(str[i]==ch){count++;if(which==count){returnstr+i;}}}returnfalse;}/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配第2个参数所包含的字符的数量*/intcount_chars(charconst*str,charconst*chars){intcount=0;while(*str!='\0'){for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
if(which==count)
/*编写一个函数,实现函数在第1个参数中进行查找,并返回匹配
第2个参数所包含的字符的数量*/
intcount_chars(charconst*str,charconst*chars)
while(*str!
for(unsignedinti=0;i{if(*str==chars[i]){count++;}}str++;}returncount;}intmain(){charstr1[50]="helloworld";charstr2[50]={0};Mystrncpy(str2,str1,5);Show(str2,strlen(str2));Mystrncpy(str2,str1,15);Show(str2,strlen(str2));charstr3[]="china";printf("%s\n",Mystrncat(str1,str3,4));charstr4[]="abcdefg";charstr5[]="acbdef";printf("%d\n",Mystrncmp(str4,str5,3));charstr6[32]={0};Myitoa(str6,123,16);Show(str6,32);Myitoa(str6,-123,16);Show(str6,32);Myitoa(str6,456789,20);Show(str6,32);Myitoa(str6,-456789,20);Show(str6,32);printf("%s\n",my_strrchr(str1,'a'));printf("%s\n",my_strrchr(str1,'l'));printf("%s\n",my_strnchr(str1,'l',2));printf("%s\n",my_strnchr(str1,'l',3));printf("%s\n",my_strnchr(str1,'l',4));printf("%d\n",count_chars(str4,str5));return0;}
if(*str==chars[i])
str++;
returncount;
charstr1[50]="helloworld";
charstr2[50]={0};
Mystrncpy(str2,str1,5);
Show(str2,strlen(str2));
Mystrncpy(str2,str1,15);
charstr3[]="china";
printf("%s\n",Mystrncat(str1,str3,4));
charstr4[]="abcdefg";
charstr5[]="acbdef";
printf("%d\n",Mystrncmp(str4,str5,3));
charstr6[32]={0};
Myitoa(str6,123,16);
Show(str6,32);
Myitoa(str6,-123,16);
Myitoa(str6,456789,20);
Myitoa(str6,-456789,20);
printf("%s\n",my_strrchr(str1,'a'));
printf("%s\n",my_strrchr(str1,'l'));
printf("%s\n",my_strnchr(str1,'l',2));
printf("%s\n",my_strnchr(str1,'l',3));
printf("%s\n",my_strnchr(str1,'l',4));
printf("%d\n",count_chars(str4,str5));
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1