

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 46/46面向对象程序设计复习题一、程序填空题1、入到数组a中的数据重新按逆序存放,然后将逆序存放后的数据输出。 #include const int N 10;void main() int aN,*p=a,*q,r;cout”输入10个整数:”;for (int i=0; iaI+;p=a; q=&a9;while( # ) r=*p; p=q; *q=r;p+; # ;cout” 逆序存放结果:”;p= # ;for(i=0; iN ;i+)cout*p+ ;coutendl;答案:pq#q-#a2、以下程序将从data1.dat中读出的内容显示在屏幕上,并将其中的字母写到文件data
2、2.dat中去。#include #include int main() char ch;fstream infile,outfile;infile.open( # , ios:in);if(!infile) cout文件1不能打开!n; return 0; outfile.open( # , ios:out);if(!outfile) cout文件2不能打开!n; return 0; while(infile.get(ch) cout=a&ch=A&ch=Z) # ; coutendl;infile.close();outfile.close();return 1; 答案:” data1.d
3、at”# ” data2.dat”#ouffile.put(ch)3、以下程序用友元函数实现复数加和复数赋制运算,完成程序。#include class complexprivate:double real,image;public:complex( )real=0; image=0; complex(double r , double i )real=r; image=i; # complex operator+(complex &a,const complex &b);complex &operator=(complex &c);void print();complex operator
4、+ (complex &a,const complex &b)return complex( # );complex &complex:operator = (complex &c) # # return *this;void complex:print()coutreal”+”imageendl;void main() complex c1,c2(1.5,4.0),c3(6.5, -2.8);complex c4=c2+c3;c1=c4;c1.print();c4.print(); 答案:friend#a.real+b.real, a.image+b.image#real=c.real;#i
5、mag=c.imag;4、以下程序的功能是输出100以内被4整除且个位数为6的所有整数。请在横线处填写适当内容。#include void main( )int i,k;for(i=0; i (1) # ;i+) k= (2) # +6; if( (3) # ) continue; coutkendl;答案:100#i*10#k%45、以下程序将从键盘输入的20个整数按升序进行排序,并在屏幕上输出排序结果。请在空白处填入合适的内容。#include (4) # ;void main() int a20,i; for(i=0;iai; (5) # ; for(i=0;i20;i+) /输出排序后
6、结果,每行输出5个数 coutai ; if((6) # ) coutendl; void sortdata(int a ) int t; for(int i=0;i19;i+) for(int j=0;j20-i-1;j+) if((7) # ) t=aj; aj=aj+1; aj+1=t; 答案:void sortdata(int a)#sortdata(a)#+i%5=0#ajaj+16、以下程序实现复数加和复数赋制运算,请在空白处填入合适的内容。#include class complexprivate:double real,image;public:complex( )real=0
7、; image=0; complex(double r , double i )real=r; image=i; (8) # complex operator+(complex &a,const complex &b);complex (9) # (complex &c);void print();complex operator + (complex &a,const complex &b)return complex(a.real+b.real, a.image+b.image);complex &complex:operator = (complex &c)real=c.real;ima
8、g=c.imag;return (10) # ;void complex:print()coutreal”+”imageendl;void main() complex c1,c2(1.5,4.0),c3(6.5, -2.8);complex c4=c2+c3;c1=c4;c1.print();c4.print();答案:friend#&operator=#*this7、以下的程序实现求分数序列2/1,3/2,5/3,8/5,13/8,21/13,前10项的和。在横线处填写适当内容。#include#define N 10void main( )int i, m, n, k; (1) # ;m
9、=2, n=1;for( i=1;(2) # ; i+)s=s+1.0*m/n;k=m;m= (3) # ;n= (4) # ;cout”s=”sendl; 答案:double s=0#i=N#m+n#k8、为使下面的程序执行后输出矩形的面积,请在空白处填入合适的内容。#includeclass Point /定义点类 public: Point(double i, double j) x=i; y=j; (8) # double Area() const return 0.0; private: double x,y;class Rectangle:public Point /定义矩形类(点
10、类的公有派生类) public: Rectangle(double i,double j,double k,double l):Point(i,j) w=k ; h=l; (9) # return w*h; private: double w, h;void main() Point *p; Rectangle rec(3.5,1.2,5.2,7.5); (10) # ; cout矩形的面积是:Area()endl;答案:virtual#virtual double Area() const 或 double Area() const#p=&rec9、下面程序用于求解10个整数中的最大值,在横
11、线处填写适当内容。#include # ;void main() int xN;int i,amax; cout输入10个整数:; for(i=0;ixi; # ; for(i=1;iN;i+) if( # ) amax=xi; cout最大值为:amaxendl; 答案:const int N=10#amax=x0#amaxxi10、下面是统计一个英文句子中单词个数、最长单词及其字符数的程序,请在空白处填入合适的内容。#include #include void main() const int SIZE=20; char bufSIZE; char largestSIZE; int cur
12、Len, maxLen=-1; # ; coutInput words:n; while( # ) curLen=strlen(buf); cnt+; if( # ) maxLen=curLen; # ; coutendl; coutcntendl; coutmaxLenendl; coutlargestbuf #curLenmaxLen #strcpy(largest, buf)11、下面是利用运算符重载实现等长一维整型数组相加的程序,请在空白处填入合适的内容。#include #include class array private: int *x; int n; public: arra
13、y (int n1) n=n1;x=new intn; void input() cout请输入n个整数:; for(int i=0;ixi; # if( # ) cout数组长度不等,不能相加!endl; exit(1); else for(int i=0;in;i+) xi+=y.xi; void show() for(int i=0;in;i+) coutxi ; coutendl; ;void main() array x1(3),x2(3); x1.input(); x2.input(); # ; x1.show();答案:void operator+=(array &y)#n!=y
14、.n#x1+=x212、以下的程序实现求分数序列2/1,3/2,5/3,8/5,13/8,21/13,前15项的和。#include#define N 15void main( )int i, m, n, k; # ;m=2, n=1;for( i=1; i=N; i+)s=s+10*m/n;k=m;m= # ;n= # ;cout”s=”sendl; 答案:double s=0 #m+n#k13、输入一串有字母和数字混合的字符串,编程序将字符串中的字母和数字分开,分别存入两个不同的文件。# include # # includevoid main() char ch100; cout请输入一
15、串有字符和数字的字符串:endl; ofstream fout1(shuzitxt); ofstream fout2(zimutxt); if(!fout1) cout文件打开失败! endl; exit(1); if(!fout2) cout文件打开失败! ch; int i=0; do if( # ) fout1chi; if(a=chi&chi=z)|(A=chi&chi=Z) # ; i+; while(chi!=0); fout1close(); fout2close();答案:# include #0=chi&chi=9#fout2chi14、下面的程序输出小于200的素数,而且每
16、行最多输出10个素数。 # include # include class Prime int p;public: Prime(int n)if (n3) p=3;else p=n; # ;void Prime:Run() int k,j,flag,line=0;for(j=2;jp;j+) flag=1;for (k=2;kj;k+)if(j%k=0) # ;if(flag)coutsetw(5)j;line+;if( # )coutendl;coutendl;void main() # ;objRun(); 答案:void Run();#flag=0#line%10=0#Prime obj
17、(200)15、下面程序用于统计一串字符中数字字符占所有字符的百分比,在横线处填写适当内容。#includevoid main() double digits,m; char s80; _#_; couts; for(int i=0; # ;i+) if(si=0&si=9) digits+; # ; cout该字符串中数字字符占所有字符的百分比为m%endl;答案:digits=0#si!=0#m=digits/i*10016、下面是将一个一维数组及各元素和写入文本文件test2.txt的程序,请在空白处填入合适的内容。#include #include #include void main
18、() int a53,s5,i,j; ofstream bout(dat.txt); if( # ) cout打开文件失败,程序终止!endl; exit(1); cout请输入一个5行3列的数组:; for (i=0;i5;i+) for(j=0;jaij; for(i=0;i5;i+) # ; for (j=0;j3;j+) si+=aij; # ; boutendl; bout.close(); 答案:!bout #si=0 #boutsi 17、下面是利用运算符重载实现平面点相加的程序,请在空白处填入合适的内容。#include class Point private: int xco
19、rd,ycord; public: Point (int x=0, int y=0) xcord =x; ycord =y; # Point pp; # ; # ; return pp; void show() cout( xcord , ycord ); ;void main() Point p1(1,2),p2(3,4); # ; /显示p1+p2的结果 答案:Point operator + (Point &p)#pp.xcord =xcord+p.xcord#pp.ycord =ycord+p.ycord#(p1+p2).show()18、下面是实现字符在字符串中出现频率的程序,请在空
20、白处填入合适的内容。#include int nchar(char *s,char c) int n(0); for(int i=0;_#_;i+) if(si=c) n+; _#_ void main() char str80,ch; coutstr; coutch ; coutch 在str 中的出现频率是:_#_ endl ;答案:si #return n; #nchar(str,ch)19、下面是一个判断一个整数是否素数的程序,请在空白处填入合适的内容。#include class integer int number; public: integer(int n)_#_ int ge
21、tnum()return number; bool isprime() for(int i=2;inumber;i+) if(number%i=0) break; if(_#_) return true; else return false; ;void main() integer n1(19); if(_#_) coutn1.getnum()是素数!endl; else coutn1.getnum()不是素数!endl; 答案: number = n; #number=i; #n1.isprime() 20、下面是利用运算符重载实现两个一维等长数组相加(+)的程序,请在空白处填入合适的内容
22、。#includeclass Array int *a; int n; public: Array (int n1) _#_ n=n1; void input() for(int i=0;iai; _#_ void list() for(int i=0;in;i+) coutai ; coutendl; ;Array Array:operator+( Array &a1) Array a2(n); for(int i=0;in;i+) _#_ return _#_; void main() Array a1(5),a2(5); a1.input(); a2.input(); a1.list()
23、; a2.list(); (a1+a2).list();答案:a=new intn1;#Array operator+( Array &a1);#a2.ai=ai+a1.ai;#a221、菲波纳齐数列为:1,1,2,3,5,8,13,;下面是实现计算该数列前20项的程序,请在空白处填入合适的内容。#include long f(int n) /递归函数 long h;if(n2)_#_ else _#_ return h; void main()for(int i=1;i=20;i+) cout_#_ “ ”; coutendl; 答案:h=f(n-1)+f(n-2);#h=1;#f(i)22
24、、下面是将一个一维数组及各元素和写入文本文件test2.txt的程序,请在空白处填入合适的内容。_#_ #include void main() int buf5=1,2,3,4,5,sum=0; _#_ if(!bout) /打开文件失败 coutCannot open the file!endl; exit(1); int i; for(i=0;i5;i+) sum+=bufi; _#_ /将数组元素写入文件 _#_ /将元素的和写入文件 bout.close(); 答案:#include #ofstream bout(test2.txt); #boutbufiendl; #boutsum
25、endl; 23、下面是利用运算符重载实现字符串加等的程序,请在空白处填入合适的内容。#include#includeclass Cstringchar *s;public: Cstring(char *s1=0) s=new charstrlen(s1)+1; strcpy(s,s1); _#_ void list()coutsendl;Cstring Cstring:operator+=(Cstring &s1) char *t=new charstrlen(s)+strlen(s1.s)+1; strcpy(t,s); strcat(t,s1.s); delete s; s=t; _#_
26、 void main() Cstring cstr1(abcd),cstr2(1234); _#_ cstr1.list(); 答案:Cstring operator+=(Cstring &s1);#return *this;#cstr1+=cstr2;24、菲波纳齐数列为:1,1,2,3,5,8,13,;下面是实现计算该数列前10项的程序,请在空白处填入合适的内容。#include long fa(int m) /递归函数 long f; if(m2) _#_ else _#_ return f; void main() for(int i=1;i11;i+) cout_#_ “ ”; co
27、utendl;答案:f=fa(n-1)+fa(n-2); #f=1;#fa(i)25、下面是将一个一维数组及各元素乘积写入文本文件data.txt的程序,请在空白处填入合适的内容。_#_ #include void main() int a10=1,2,3,4,5,6,7,8,9,10,cum=0; _#_ if(!fout) /打开文件失败 coutCannot open the file!endl; exit(1); int i; for(i=0;i10;i+) cum*=ai; _#_ /将数组元素写入文件 _#_ /将元素的乘积写入文件 fout.close(); 答案:#includ
28、e #ofstream bout(test2.txt); #boutbufiendl; #boutsumendl; 26、下面是利用运算符重载实现字符串赋值(=)的程序,请在空白处填入合适的内容。#include#includeclass Cstringchar *s;public: Cstring(char *s1=0) s=new charstrlen(s1)+1; strcpy(s,s1); _#_ void list()coutsendl;Cstring Cstring:operator=(Cstring &s1) delete s;s=new charstrlen(s1.s)+1;
29、strcpy(s,s1.s); _#_ void main() Cstring cstr1(abcd),cstr2(1234); _#_ cstr1.list();答案:Cstring operator+=(Cstring &s1);#return *this;#cstr1+=cstr2;27、下面的程序实现求n个整数中的奇数个数。#includeint codd(int x,int n); void main() int n,*p; do coutn; while(n=0); # cout请输入n个整数:; for(int i=0;ipi; cout奇数的个数是: # endl; delet
30、e p; int codd(int x,int n) int count=0; for(int i=0;in;i+) if( # ) count+; return count; 答案:p=new intn;#codd(p,n) # xi%228、下面程序的功能是将键盘上输入的一串字符存入一个名为data.txt的文件。字符串中不包含空格并以“*”作为输入结束标志。#include#include#includevoid main()char s280,ch;int i=0; cout输入一个字符串:endl; do # ; s2i=ch;i+; while( # ); s2i=0; # ; o
31、utfiles2ch # ch!=* # ofstream outfile(“data.txt”) 29、下面程序从键盘将10名学生的学号和成绩输入到对象数组,按成绩由高到低进行排序后输出排名前30% 的学生。#include #include class Studentpublic: Student(char no,int g) # ; score=g; ; Student() void out(); friend void sort(Student *s,int n);private: char sNo10; int score; # out() cout学号sNo成绩scoreendl;
32、void sort(Student *s,int n) Student temp; int i,j; for(i=0;in;i+) for(j=0;jn-i-1;j+) if(sj.score # ) temp=sj; sj=sj+1; sj+1=temp; void main() const int number=10; Student teststudentnumber; int i,s; char no10; for(i=0;inos; teststudenti=Student(no,s); sort(teststudent, number); for(i=0;inumber*0.3;i
33、+) # .out();答案:strcpy(sNo,no) # void Student: # sj+1.score # teststudenti二、编程题1、编写程序实现如下功能能,从键盘输入一个最长不超过80个字符的字符串,将其中的所有小写字母转换成大写字母,其他字符不变,屏幕输出转换后的字符串。 答案:2、编写一个可以返回一个整数的反序值的函数。例如,对整数7631,函数的返回值为1367。在主函数中调用上述函数,对由键盘输入的任意一个大于10的整数n求出其反序值,屏幕输出n及其反序值。注意:程序中不的能使用全局变量。 答案: 3、从由键盘输入的若干整数中找出其中最大的数并输出。当输入为
34、负数时结束输入。 答案: 4、编写一个函数void delchar(char*s1,char s2),从字符串s1中删去与字符s2相同的字符,并用main ()测试该函数。 答案: 5、将键盘输入的一串字符中的大写字母挑出并输出到屏幕上。说明:字符串中不含空格,长度不超过40个字符。 答案: 6、编写求n个整数之和的函数,函数原型为:int sum(int x,int n);并编写主函数,调用该函数求55矩阵的主对角线元素之和及辅对角线上元素之和,并在屏幕上输出。矩阵数据由键盘输入。 答案: 7、求(即求1!+2!+3!+。+10!)之和。答案: 8、编写求两个整数的最小公倍数的函数,函数原型为:int maxb(int x,inty);并编写主函数,调用该函数求键盘输入的两个整数的最小公倍数,并在屏幕输出。 答案:9、用switch语句编写程序,统计输入的一串字符中每个元音字母(a、e、i、o、u)的出现的次数和字符总数,并输出到屏幕。当输入
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论