编程与综合练习题-c指针.doc_第1页
编程与综合练习题-c指针.doc_第2页
编程与综合练习题-c指针.doc_第3页
编程与综合练习题-c指针.doc_第4页
编程与综合练习题-c指针.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

17第五章 数组与指针习题二、编程与综合练习题5.3 打印杨辉三角形(10行)。使用二维数组并利用每个系数等于其肩上两系数之和。解:好的算法无特例,二维数组共用11列,第1列全0,方便计算#includeusing namespace std;int main()int a1011=0,1,i,j; /初始化时写好第1行,其余各行全0for(i=1;i10;i+) /为了全部算法无特例,共用11列,第1列全0,方便计算for(j=1;j=i+1;j+)aij=ai-1j-1+ai-1j;for(i=0;i10;i+)for(j=1;j=i+1;j+)coutaijt;coutendl;return 0;5.4 将例5.5改用一维数组,附加行、列参数,实现通用算法。解:用一维数组,附加行、列参数,实现通用算法难度大。#include #include using namespace std;void inverse(int , int ,int,int);/注意数组最高维可缺省,例5.5因初学未省void multi(int , int , int ,int,int,int);void output(int ,int,int);int main() int middle6*3, result6*4;/注意写作6*3等可清楚看出矩阵的行列 int matrix13*6=8,10,12,23,1,3,5,7,9,2,4,6,34,45,56,2,4,6; int matrix23*4=3,2,1,0,-1,-2,9,8,7,6,5,4;output(matrix1,3,6);inverse(matrix1,middle,3,6);output(middle,6,3);output(matrix2,3,4); multi(middle,matrix2,result,6,3,4); output(result,6,4);return 0;void inverse(int matrix1_1,int middle_1,int a,int b) int i,j; for (i=0;ia;i+) for (j=0;jb;j+) middle_1i+j*a=matrix1_1i*b+j; return; void multi(int middle_1,int matrix2_1,int result_1,int a,int b,int c) int i,j,k; for (i=0;ia;i+) for (j=0;jc;j+) result_1i*c+j = 0; for (k=0;kb;k+) result_1i*c+j+=middle_1i*b+k*matrix2_1k*c+j; return;void output(int max_1,int a,int b)for (int i=0;ia;i+) for (int j=0;jb;j+) cout setw(4)max_1i*b+j ; coutn; coutendl; return; 5.5 编写函数int atoi(char s ),将字符串s转化为整型数返回。注意负数处理方法。解:用指针处理字符串非常方便。使用符号位来处理负数。#includeusing namespace std;int atoi(char s)int temp=0,f=1,i=0;while(si!=0&si!=-&(si9) i+;/去除串前部无效字符if(si=-)/读负号f=-1;i+;if(si9) couterror!=0&si=9)/转换数字串temp=temp*10+si-48;i+;return f*temp;int main()char num20;cin.getline(num,19);coutatoi(num)n;return 0;5.6 有如下定义:int ival=60021;int *ip;double *dp;下面哪些赋值非法或可能带来错误,并加以讨论。ival=*ip; ival=ip; *ip=ival; ip=ival; *ip=&ival;ip=&ival; dp=ip; dp=*ip; *dp=*ip;解:ival=*ip; 错,未确定指针ip初值,用随机内存地址中的数据给ival赋值是危险的。但语法对。ival=ip; 错,赋值类型错,指针型不能赋给整型。*ip=ival; 错,未确定指针ip初值,用ival给随机内存赋值是危险的。但语法对。ip=ival; 错,赋值类型错,整型不能赋给指针型。*ip=&ival; 错,赋值类型错,地址(指针型)不能赋给整型。ip=&ival; 对,地址赋给指针型。dp=ip; 错,整型指针不能赋给双精度指针。dp=*ip; 错,赋值类型错,整型不能赋给指针型。*dp=*ip; 对,赋值类型转换5.7 编程定义一个整型、一个双精度型、一个字符型的指针,并赋初值,然后显示各指针所指目标的值与地址,各指针的值与指针本身的地址及各指针所占字节数(长度)。*其中地址用十六进制显示。解:注意:字符指针输出是字符串,必须强制转换为无类型指针#includeusing namespace std;int main()int *ip,ival=100;double *dp,dval=99.9;char *cp,cval=A;ip=&ival;dp=&dval;cp=&cval;cout*ipt&*iptsizeof(*ip)endl;cout*dpt&*dptsizeof(*dp)endl;cout*cpt(void*)&*cptsizeof(*cp)endl;/字符指针输出是字符串,必须强制转换为无类型指针cout*cpt&*cptsizeof(*cp)endl;/输出A开头的字符串coutipt&iptsizeof(ip)endl;coutdpt&dptsizeof(dp)endl;cout(void*)cpt&cptsizeof(cp)endl;return 0;一个典型的运行结果:变量内容首地址长度(字节)cvalA0x0012ff641cp0x0012ff640x0012ff684dval99.90x0012ff6c8dp0x0012ff6c0x0012ff744ival1000x0012ff784ip0x0012ff780x0012ff7c4内存分配解释:速度优化时通常以字(4字节)为单位(开始地址可被4整除)给变量安排内存。cval仅用一个字节,也安排了4个字节。5.8 分别编写下列字符串处理函数 (1)char *strcat1(char *s,const char *ct);将串ct接到串s的后面,形成一个长串。【例6.7】以数组为参数,现用指针为参数。(2)int strlen1(const char * s);求字符串长度的函数,返回串长(不包括串结束符)。(3)char * reverse (char *);反置字符串s,即可将“break”成为“kaerb”。(4)char * strchr( const char *cs,char c);查找字符c在串cs中第一次出现的位置,返回指向该字符的指针,若没有出现则返回NULL。(5)char *strstr (const char *cs1,const char *cs2);返回串cs2作为子串在cs1中第一次出现的位置,若没有出现则返回NULL。解:为了函数的通用性,有些可不要返回值的函数,也保留返回值。反置字符串函数,从串两头的指针同时向中间移动,重合或交错时停止。查找子串,先找子串的第一个字符,再核对子串其他字符。#includeusing namespace std;char* strcat1(char* s,const char* ct)char* st=s;while(*s) s+;/*s作为条件,等效*s!=0while(*s+=*ct+);return st;int strlen1(const char* s)int i=0;while(*s+) i+;return i;char* reverse (char* s)char temp,* temp1=s,* temp2=s;while(*temp2) temp2+;temp2-; /指针移回串尾while(temp2-temp10)/注意此处,从串两头的指针同时向中间移动,重合或交错时停止temp=*temp1;*temp1=*temp2;*temp2=temp;temp1+;temp2-;return s;char* strchr( const char*cs,char c)while(*cs!=c&*cs) cs+;if(*cs=0) cs=NULL; /未找到返回NALLreturn (char*)cs;char *strstr (const char *cs1,const char *cs2)char *temp;char *temp1=(char*)cs2;while(*cs1) /只要主串还有字符未查,则继续while(*cs1!=*cs2&*cs1) cs1+;/找到主串含有子串的第一个字符,或主串查完停止temp=(char*)cs1;temp1=(char*)cs2;if(*cs1) /核对子串其他字符while(*cs1+=*temp1+|*temp1);if(*temp1=0) return temp; /找到子串返回return NULL; /未找到返回NALint main()char a40=李明;char b20=是东南大学学生;char c40=Southeast University;char *cp;coutaendl;coutbendl;strcat1(a,b);cout字符串连接后:endl;coutaendl; /打印字符数组acout字符串长度为:strlen1(a)endl;coutcendl;cp=strchr(c,U);if(cp=NULL) cout未找到endl;else coutcpendl; /找到输出由该字符开始的剩余串cp=strchr(c,A);if(cp=NULL) cout未找到endl;else coutcpendl;coutreverse(c)endl;cp=strstr(a,东南);if(cp!=NULL) coutcpendl; /找到输出由该字符串开始的剩余串else cout未找到endl;cp=strstr(a,西北);if(cp=NULL) cout未找到endl;else coutcpendl;return 0;5.9 使用递归和非递归的两种方法编写函数char *itoa (int n,char *string); 将整数n转换为十进制表示的字符串。(在非递归方法中,可使用reverse()函数。)解:递归方法分析。难度大,可用图解法:每次调用除以10,以去除最后一位,以n=3657为例。由此图可见,由string指向应填入的字符数组的相应位置。由调用的最底层开始,回归时填入,每次回归,指针后移一位,由此得char * itoal(int n,char *string)if(n/10) string=itoal(n/10,string);*string+=n%10+48;/字符,ASCII码return string;考虑,数字串结束符和负数,得完整的函数。char * itoal(int n, char *string)if(n0)*string+=_;n=-n;if(n/10)string=itoal(n/10,string);*string+=n%10+48;*string=0;return string;源代码:#includeusing namespace std;char* reverse (char* s)char temp,* temp1=s,* temp2=s;while(*temp2) temp2+;temp2-;/指针移回串尾while(temp2-temp10)/注意此处,从串两头的指针同时向中间移动,重合或交错时停止temp=*temp1;*temp1=*temp2;*temp2=temp;temp1+;temp2-;return s;char *itoa (int n,char *string)char *temp=string;if(n0)*temp+=-;n=-n;do/注意个位放在前了*temp+=n%10+48;while(n=n/10);/显式的循环*temp=0;if(*string=-) temp=string+1;/有负号仅反转数字部分else temp=string;reverse(temp);/个位放到后面return string;char *itoa1 (int n,char *string)if(nnum;cout输出数字串:itoa(num,st)num;cout输出数字串:itoa0(num,st)endl;return 0;5.10 头文件中定义一个日期时间的结构:struct tm int tm_sec;/秒int tm_min;/分int tm_hour;/时int tm_mday;/日int tm_mon;/月int tm_year;/年,实际放的是与1970年的差,如1990年为20int tm_wday;/星期int tm_yday;/一年中的第几天int tm_isdst;/是否夏时制;函数 time_t time(time_t *tp)是提取当前时间,time_t即长整型,代表从1970年1月1日00:00:00开始计算的秒数(格林尼治时间),放在首地址为tp的单元内。函数 tm *localtime(const time_t *tp) 将tp地址单元中的时间转换为日期时间结构的当地时间;(函数 tm *gmtime(const time_t *tp)转换为日期时间结构的格林尼治时间;)函数 char *asctime(tm *tb)将tb地址单元中的tm结构的日期时间转换为字符串(供显示),它有固有格式,如: Sun Sep 16 01:03:52 1973利用以上资源,重新设计一个日期时间类(DataTime),要求定义对象时取当前时间进行初始化,显示时重取显示时刻的时间并显示出来。解:#include#includeusing namespace std;class datatimetm *timedata;long allsecond;char *tmp;public:datatime()time(&allsecond);timedata=localtime(&allsecond);tmp=asctime(timedata);couttmpendl;void gettime()allsecond=time(NULL);/time有两种用法timedata=localtime(&allsecond);tmp=asctime(timedata);couttmpendl;int main()char ch;datatime dt;cout需要知道现在的日期和时间吗?(Y或N)ch;if(ch=y|Y) dt.gettime();return 0;5.11 完善自定义字符串类mystring,函数包括:构造函数、拷贝构造函数、析构函数,并重载运算符 ,=(分别用mystring和C字符串拷贝),+(strcat),+=,=(strcmp)。解: 此例既是对第4章的复习也是一个提高。拷贝构造函数的应用请参阅4.4.2节末尾两项说明,本例形参使用引用,仅在返回时调用了拷贝构造函数。运算符的重载请参阅4.5节。#includeusing namespace std;const int n=256;class mystringchar strn; /存放字符串的数组容器int maxsize; /最大可用元素数,可防止数组出界,提高健壮性int last; /已用元素最大下标public:mystring()last=0;maxsize=n;str0=0;cout缺省构造函数endl;mystring(char *s)/当C字符串过长,初始化时采用截尾处理last=-1;maxsize=n;dolast+;strlast=slast;while(slast!=0&lastmaxsize-1);strlast =0; /截尾处理时,必须加串结束符cout构造函数endl;mystring(mystring & ms)last=-1;maxsize=n;dolast+;strlast=ms.strlast;while(lastms.last);cout拷贝构造函数endl;mystring()cout析构函数endl;void show()/如需重载,则请参见9.3.3节,暂时未学到,替代方法是用show()函数coutstrlast) last=i; /下标运算符,可添加长度但不查边界return stri;mystring operator=(mystring &);mystring & operator=(char * ms);/这里重载的=是把C风格字符串赋给mystringmystring operator+(mystring &);mystring operator+=(mystring &);bool operator(mystring &);bool operator=(mystring &);mystring mystring:operator=(mystring & ms)last=-1;dolast+;strlast=ms.strlast;while(lastms.last);return *this;mystring & mystring:operator=(char* ms) /这里返回值为引用,不调用拷贝构造函数last=-1;dolast+;strlast=mslast;while(mslast!=0&lastmaxsize-1);strlast =0; /截尾处理时,必须加串结束符return *this;mystring mystring:operator+(mystring & ms)/注意+和+=的不同mystring temp(*this);/+必须在一份拷贝上进行int i=-1;temp.last-;/串的结尾是结束符,连接时要覆盖掉dotemp.last+;i+;temp.strtemp.last=ms.stri;while(ims.last& temp.lastmaxsize-1);temp.strtemp.last =0; /截尾处理时,必须加串结束符return temp;/拷贝的临时变量生命期在调用它的表达式中mystring mystring:operator+=(mystring & ms)/+=在对象自身进行int i=-1;last-;/串的结尾是结束符,连接时要覆盖掉dolast+;i+;strlast=ms.stri;while(ims.last&lastmaxsize-1);strlast =0; /截尾处理时,必须加串结束符return *this;bool mystring:operator(mystring & ms) /重载运算符int i=0,k;dok=stri-ms.stri;i+;while(k=0&ilast&ims.last);if(k0) return true;if(i=last&i!=ms.last) return true;return false;bool mystring:operator=(mystring & ms)int i=0,k;if(last!=ms.last) return false;dok=stri-ms.stri;i+;while(k=0&ilast);if(k!=0) return false;else return true;int main()int i;char *sp1=东南大学,*sp2=交通学院,*sp3=学生,*sp4=教师;mystring ms1(sp1),ms2(sp2),ms3(sp3);/ms1,ms2,ms3是用构造函数生成mystring ms4(ms3),ms5=ms3,ms6; /ms4,ms5用拷贝构造函数生成;ms6用缺省构造函数ms6=sp4; /ms6赋值是返回引用,不用拷贝构造函数ms1.show();ms2.show();ms3.show();ms4.show();ms5.show();ms6.show();ms4=ms1+ms2+ms6;/注意temp和临时变量由拷贝构造函数生成ms4.show();ms1+=ms2+=ms3;ms1.show();if(ms1ms4) ms1.show();cout应排在endl;ms4.show();cout之前endl;else ms1.show();cout应排在endl;ms4.show();cout之后endl;ms6=ms1;/ms6赋值不是返回引用,必须调用拷贝构造函数建立临时对象if(ms1=ms6) cout串ms1与串ms6相同endl; ms1=C+ programming language;i=0;while(ms1i!=0) coutms1i+;/读出coutendl;ms1i+=.;/写入ms1i=0;i=0;ms1.show();return 0;5.12 将习题5.8中的字符串处理函数移植到mystring类中,其中strcat已重载为+运算符,请将其它4个转为成员函数。对比成员函数与独立函数构造上有何不同?解:这四个函数因mystring内部有串长,又要求下标索引,再加上str字符数组可直接使用,构造大不相同。#includeusing namespace std;const int n=256;class mystringchar strn; /存放字符串的数组容器int maxsize; /最大可用元素数,可防止数组出界,提高健壮性int last; /已用元素最大下标public:mystring()last=-1;maxsize=n;str0=0;cout缺省构造函数endl;mystring(char *s)/当C字符串过长,初始化时采用截尾处理last=-1;maxsize=n;dolast+;strlast=slast;while(slast!=0&lastmaxsize-1);strlast =0; /截尾处理时,必须加串结束符cout构造函数endl;mystring(mystring & ms)last=-1;maxsize=n;dolast+;strlast=ms.strlast;while(lastms.last);cout拷贝构造函数endl;mystring()cout析构函数endl;void show()/如需重载,则参见9.3.3节,暂时未学到,替代方法是用show()函数coutstrlast) last=i; /下标运算符,可添加长度但不查边界return stri;mystring & operator=(mystring &);mystring & operator=(char * ms);/这里重载的=是把C风格字符串赋给mystringmystring operator+(mystring &); /这里返回不能用引用mystring & operator+=(mystring &);bool operatori)/注意此处,从串两头同时向中间移动,重合或交错时停止temp=stri; /头尾交换stri=strj;strj=temp;i+;j-;int mystring:strchr(char c)int i;for(i=0;i!=last;i+) if(stri=c) return i;return -1; /未找到返回-1int mystring:strstr(mystring str1)int i=0,k=1;while(stri!=0)/只要主串还有字符未查,则继续while(stri!=str10&stri!=0) i+;/找到主串含有子串的第一个字符,或主串查完停止if(stri!=0)/核对子串其他字符while(stri+k=str1.strk&kstr1.last) k+;/字符串结束符不比较if(k=str1.last) return i;/找到子串返回k=1;i+;return -1; /未找到返回-1mystring & mystring:operator=(mystring & ms)/这里返回值改为引用,不调用拷贝构造函数last=-1;dolast+;strlast=ms.strlast;while(lastms.last);return *this;mystring & mystring:operator=(char* ms) /这里返回值为引用,不调用拷贝构造函数last=-1;dolast+;strlast=mslast;while(mslast!=0&lastmaxsize-1);strlast =0; /截尾处理时,必须加串结束符return *this;mystring mystring:operator+(mystring & ms)/注意+和+=的不同mystring temp(*this);/+必须在一份拷贝上进行int i=-1;temp.last-;/串的结尾是结束符,连接时要覆盖掉dotemp.last+;i+;temp.strtemp.last=ms.stri;while(ims.last& temp.lastmaxsize-1);temp.strtemp.last =0; /截尾处理时,必须加串结束符return temp;/拷贝的临时变量生命期在调用它的表达式中mystring & mystring:operator+=(mystring & ms)/+=在对象自身进行int i=-1;last-;/串的结尾是结束符,连接时要覆盖掉dolast+;i+;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论