《C++》--分类--练习题

上传人:人*** 文档编号:576836682 上传时间:2024-08-20 格式:PDF 页数:46 大小:3.87MB
返回 下载 相关 举报
《C++》--分类--练习题_第1页
第1页 / 共46页
《C++》--分类--练习题_第2页
第2页 / 共46页
《C++》--分类--练习题_第3页
第3页 / 共46页
《C++》--分类--练习题_第4页
第4页 / 共46页
《C++》--分类--练习题_第5页
第5页 / 共46页
点击查看更多>>
资源描述

《《C++》--分类--练习题》由会员分享,可在线阅读,更多相关《《C++》--分类--练习题(46页珍藏版)》请在金锄头文库上搜索。

1、C +程序设计分类练习题C+程序设计课程组编写目 录第一部分简答题与论述题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 .1 简答题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. 2 简述题. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1第二部分分析程序运行结果或给出程序功能. .

2、. . . . . . . . . . . . . . 32. 1 结构化程序设计. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32. 2 数组与指针. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52. 3 函数. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92. 4 类与对象基本知识. . .

3、. . . . . . . . . . . . . . . . . . . . . . . . . 122. 5 类继承. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182. 6 类多态. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222. 7 I/O与文件操作. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26第

4、三部分程序设计部分. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273. 1 类的定义( 只给出类结构的声明). . . . . . . . . . . . . . 273. 2 类操作的实现. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283. 3 继承与派生. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313. 4 重载. . . . . .

5、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343. 5 文件操作. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37第四部分综合应用系统的分析、设计. . . . . . . . . . . . . . . . . .39第五部分常见的通用算法与设计思想. . . . . . . . . . . . . . . . . . . . 41第一部简答题与论述题一、简答题( 一) 定义功能说明1、分别说明下列定

6、义的含义:int pl;int p210;int *p3;int *p410char *p5;( 二) 类的基本性质特点char *p610int *p7;int p8();int (*p9)();intp!0ll010;1、 面向对象程序设计方法的基本特征主要包括哪四项?2、构造函数及析构函数具有哪些功能?它们分别在何时执行?( 三) 多态性的定义1、运算符重载有哪两种方式,分别给出定义运算符重载的格式?( 四) 继承与派生的定义1、什么是继承与派生?2、继承的三种继承方式是什么?3、由基类而生成的派生类需要经历哪3 个步骤?第二部分分析程序运行结果或给出程序功能( 一) 结构化程序设计练习

7、题目1 .给出程序的运行结果:#include void main()(int i,sum=0;i二l;dosum=sum+i;i=i+l; while(i=10);cout Hsum = sum endl;)2 .给出程序的运行结果:#include void main()(int m;m=9;cout nplease input a number:n;cin m ;int i;fbr(i=2; im; i+)if(m%i=0)break;if(m=i)cout m n is primeAn;elsecout m vv isnt primeAn;)3 .给出程序的运行结果:#include

8、void main()int a=3, b=2, c=l;int d,logic;d=a+;co u t a= an,d=n d en d l;d=+b;coutnb = b n,d=dendl;b*=a+c;c o u t a , b , c en d l;b=2;logic=ab;coutlogic=logicendl;logic=(ca)ll(cb);coutHlogic=Hlogicendl;)4 .给出程序的运行结果:#include void main()int i=l,sum=0;while (i20)break;)couti;)5 .给出程序的运行结果:#include #in

9、clude void main()char string80|;strcpy( string, Hello world from );strcat( string, strcpy );strcat( string, nand );strcat( string, strcat!u );coutstringendl;)6 .给出程序的运行结果:#include #include #include #include char a10=,hellon;char b10=world”;void main()(int c;c=strcmp(a,b);if(c= =0)coutna is equal to

10、bendl;elseif(c0)coutna is greater than bHendl;elsecouta is less than bendl;)7 . 给出程序的运行结果:#include void main()(int a=-l,b=4,k;k=(a+=0)& !(b-=0);c o u t k a b en d l;)8 . 给出程序的运行结果:#includeintm33= 1,2,3 );int n33= 1,2,3 );void main() coutm 1 0+n0 0 ,n,m 0 l+n 1 0,n,;( 二) 数组与指针部分练习题目1 .给出程序的运行结果:#incl

11、ude #incl udevoid main()(char ch=how are you;cout size of array: sizeof(ch) endl;cout size of string: strlen(how are you1) endl;( 提示:各单词之间有一个空格)2 .给出程序的运行结果:#include void main() int* iPtr;int iCount=18;iPtr=&iCount;cout *iPtr endl;3 .给出程序的运行结果:#include void main() int iArray4;int sum=0;int* iPtr=iAr

12、ray;for(int i=0;i4;i+)iArrayi=i*2;for(int index=0; index 4; index-H-)sum+= *iPtr;iPtr+;)cout nsum is sum endl;4 .给出程序的运行结果:#include void mystrcpy(char* dest, const char* source)(while(*dest+ = *source+);void main()(char a20=How are you!;char b20;mystrcpy(b,a);cout b endl;5 .给出程序的运行结果:#include void m

13、ain()char buffer! lOABC ”;char* pc;pc=nhellon;cout p c endl;pc+;cout p c endl;cout *pc endl;pc=buffer;cout pc;)6 . 给出程序的运行结果:#include #include void main()char buffer l10=nhellon;charbuffer210=hellon;if(strcmp(bufferl,buffer2)=0)cout ,equaln,;elsecout not equalnn;)7 . 给出程序的运行结果:#include void main()(i

14、nt *v,b;v=&b;b= 100;*v+=b;coutb;)8 . 给出程序的运行结果:#includevoid callbyval(int a,int b,int c)a=3;b=2;c=l;void callbypointer(int* a,int* b,int* c)*a=3;*b=2;*c=l;void callbyreference(int& a,int& b,int& c)a=l;b=2;c=3;void main()int a=l,b=2,c=3;int& al=a;int& bl=a;int& cl=a;callbyval(a,b,c);co u t a b c en d

15、 l;callbypointer(&a,&b,&c);co u t a b c en d l;callbyreference(al ,b1 ,cl);co u t a b c en d l;)9 .给出程序的运行结果:#includevoid ast(int x,int y,int *cp,int *dp)|*cp=x*y;*dp=x%y;)void main() int a,b,c,d;a=2; b=3;ast(a,b,&c,&d);coutcdendl;10 .给出程序的运行结果:#includeint f(char *s) char *p=s;while(*p) p+;return p

16、- s;)void main(void) coutf(nABCDEFn); 11 .给出程序的运行结果:#includevoid main() int a10=10,20,30,40,50,60),*pl=&a0,*p2=&a5;coutM*p 1=*p I ,*p2=,*p2;pl+; p2-;cout,n*pl=, * p l ,*p2=|,*p2;pl+=2;coutvv”n*p l=n * p 1 ”,*(p2+1 )=vv*(p2+1);coutnnp2与p l之间整型变量的个数=vp2-pl;( 三) 函数部分练习题目1 .给出程序的运行结果:#include void func(

17、);int n=l;void main()(static int a;int b= -10;cout a : a n b:n b n n:“ n endl;b+=4;func();cout na:n a b:n b u n:“ n endl;n+=10;func();)void func()static int a=2;int b=5;a+=2;n+=12;b+=5;cout na: a n b:“ b n: n endl;)2 .给出程序的运行结果:#include int sum(int , int);void main()(static int ia5=2,3,6,8,10);int s

18、umOfArray;sumOfArray=sum(ia, 5);coutnsum of array: nsumOfArray endl;int sum(int array, int len)int iSum=O;for(int i=0; Men; i+)iSum+=arrayi;return iSum;)3 . 给出程序的运行结果:#include int maximum(int4,int,int);void main()(int sg34= 68,77,73,86),87,96,78,89,90,70,81,86;coutthe max grade is: Mmaximum(sg,3,4)

19、endl;)int maximum(int grade4,int pupils,int tests)(int max=0;for(int i=0; ipupils; i+)for(int j=0; jmax)max=gradeij;return max;)4 . 给出程序的运行结果:#include void swap(int,int);void main()(int a=3, b=8;cout na= a n, b=M b endl;swap(a,b);cout nafter swapping.An1 1;cout Ha= a , b= b endl;)void swap(int x,int

20、 y)(int temp=x;x=y;y=temp;5 .给出程序的运行结果:#include void swap(int &x,int &y);void main()(int x=5, y=6;cout Mbefore swap, x: x ,y: y endl;sw 叩(x,y);cout nafter swap, x:“ x n ,y:n y endl;)void swap(int &rx,int &ry)(int temp=rx; rx=ry; ry=temp;)6 .给出程序的运行结果:#include double& fn(const double& pd)static doubl

21、e ad=32;ad+=pd;cout Mfn being called.the value is: p d endl;return ad;)void main()(double a=345.6;double& pa=fn(a);cout p a endl;a=55.5;pa = fn(a);cout p a endl;)7 .给出程序的运行结果:#includevoid print(double a)(cout+a;)void print(int a, int b)coutba;void main()print(1.2);coutprint(3,4);)8 .给出程序的运行结果:#inclu

22、declass Sample(int n;public:Sample(int i)n=i;friend int add(Sample &sl,Sample &s2);int add(Sample &sl,Sample &s2)(return sl.n+s2.n;)void main()(Sample s 1(10),s2(20);coutadd(sl,s2)( 四) 类与对象基本知识部分的练习题目1 .给出程序的运行结果:#include class Studentpublic:Student()(cout Hconstructing student.Xn1;);class Teacherpu

23、blic:Teacher()cout constructing teacher.nH;);class TutorPairpublic:TutorPair()(cout constructing tutorpairAn;)protected:Student student;Teacher teacher;);void main()TutorPair tp;cout back in mainAn;)2 . 给出程序的运行结果:#include class Studentpublic:Student()(cout constructing student.nu;)Student。(cout *des

24、tructing studentAn;);class Teacherpublic:Teacher()(cout constructing teacherAn*1;)Teacher。(cout ndestructing teacherAn;);class TutorPairpublic:TutorPair()(cout Hconstructing tutorpairAn;)-TutorPair()(cout Hdestructing tutorpair.n”;)protected:Student student;Teacher teacher;);void main()TutorPair tp;

25、cout Mback in mainAn;)3 .给出程序的运行结果:#include class Tdatepublic:Tdate(int m=4,int d=15,int y=1995)month=m; day=d; year=y;cout month 7M day 7n year endl;)protected:int month;int day;int year;);void main()Tdate aday;Tdate bday(2);Tdate cday(3,12);Tdate dday( 1,2,1998);)4 .给出程序的运行结果:# include class Apubl

26、ic:A(intj):age(j),num(j+1)(cout age: age ,num: num endl;)protected:int num;int age;);void main()(Asa(15);)5 .给出程序的运行结果:#include class Myclasspublic:Myclass(int a,int b,int c);void PrintNumber();void Printsum();private:int ARC;static int Sum;);int Myclass:Sum=O;Myclass:Myclass(int a,int bjnt c)A=a;B=

27、b;C=c;Sum+=A+B+C;)void Myclass:PrintNumber() cout,Number=, A ,;, B H;, C en d l; void Myclass:PrintSum() cout MS um=S umendl; void main()Myclass M(3,7,l0),N( 14,9,1);M.PrintNumber();N.PrintNumber();M.PrintSum();N.PrintSum();)6 . 给出程序的运行结果:#include class Myclass(public:Myclass();void GetSum(int a);in

28、t Sum;private:int A;);Myclass:Myclass()(Sum=10;)void Myclass:GetSum(int a)A=a;Sum+=A;)void PrintSum(Myclass& A)cout,Sum=,A.Sum endl;)void main()(Myclass M,N;M.GetSum(3);N.GetSum(7);PrintSum(M);7 . 给出程序的运行结果:#include #include class Pointpublic:Point(double xx,double yy) x=xx;y=yy;void Getxy();friend

29、double Distance(Point & a,Point & b);private:double x,y;);void Point:Getxy()(cout,( x n, y ,),endl;double Distance(Point &a,Point &b)(double dx=a.x-b.x;double dy=a.y-b.y;return sqrt(dx*dx+dy*dy);)void main()Point pl(3.0,4.0),p2(6.0,8.0);pl.Getxy();p2.Getxy();double d=Distance(p 1 ,p2);coutDi stance

30、is, d en d l;8 .给出程序的运行结果:#include class A(public:A(int x,int y) X=x; Y=y; int a() return X; int b() return Y; int c();int d();private:int X,Y;);inline int A:c()return a( )+b();)inline int A:d()return c();void main ()Am(3,5);int i=m.d ();coutiendl;)9 .给出程序的运行结果:#includeclass B(int i;public:B(int x)i

31、=x;friend class A;);class A(int i;public:int set(B&);int get()return i;A(int x)i=x;);int A:set(B &b)(return i=b.i;)void main()Aa(l);B b(2);couta.get()H,;a.set(b);couta.get();)( 五) 继承部分练习题目1 .给出程序的运行结果:#includeclass Basepublic:virtual void fn()(cout HIn Base classn;);class SubClass :public Basepublic

32、:virtual void fn()cout HIn SubClassnn;);void test(Base& b)(b.fn();void main()(Base be;SubClass sc;cout MCalling test(bc)nu;test(bc);cout nCalling test(sc)nn;test(sc);)2 .给出程序的运行结果:#incl udeclass Basepublic:void fn()(cout HIn Base classnM;);class SubClass :public Basepublic:void fn()(cout In SubClass

33、nn;);void test(Base& b)b.fn();)void main()(Base be;SubClass sc;cout nCalling test(bc)nH;test(bc);cout Calling test(sc)nn;test(sc);)3 .给出程序的运行结果:#include class Animal public:Animal()void eat() cout ,eat.nH; );class Giraffe :private Animalpublic:Giraffe()void StretchNeck() cout stretch neckAn; void ta

34、ke() eat(); ;void Func(Giraffe & an)an.take();)void main()Giraffe gir;gir.StretchNeck();Func(gir);)4 .给出程序的运行结果:#includeclass Bl int x;public:Bl(int a) x=a; coutvv”基类 Bl 的构造函数! n; Bl() coutv”基类B l的析构函数! n; class B2 int y;public:B2(int a) y=a; coutv”基类 B2 的构造函数! n”; B2() coutvv”基类B 2的析构函数! n”; );clas

35、s D:public Bl,public B2 int z;Bl bl,b2;public:D(int a,int b):B 1(a),B2(20),b 1(200),b2(a+b) z=b; cout”派生类D 的构造函数! n; D() coutvv”派生类D 的析构函数! n; !);void main(void) Dd( 100,200); 5 .给出程序的运行结果:#includeclass Aprotected:int x;public:A() x=1000; void print() cout,x=, x t; );class B:public Aint y;public:B()

36、 y=2000; void print() coutny= n y n ; );void main(void) A a,*pa;Bb;a.print(); b.print();pa=&a; pa-print();pa=&b; pa-print();( 六) 多态部分练习题目1 .给出程序的运行结果:#include class CD atepublic:CDate();CDate(int d);CDate(int m,int d);CDate(int y,int m,int d);protected:int year;int month;int day;);CDate:CDate()year=

37、1999; month= 11; day=24;coutyear7,m onth7,dayendl;)CDate:CDate(int d)(year=1999; month=l 1; day=d;coutyear,/,m onth7dayendl;)CDate:CDate(int m,int d)(year=1999; month=m; day=d;coutyear7nm onth7dayendl;)CDate:CDate(int y, int m, int d)(year=y; month=m; day=d;coutyear,/,m onth7,dayendl;)void main()(CD

38、ate aday;CDate bdate(lO);CDate cdate(8,8);CDate ddate(1998,l,l);2 . 给出程序的运行结果:#include class Apublic:virtual -A( ) coutA:A() CalledAn,; );class B : public Apublic:B(int i) buf = new chari; virtual -B ()(delete buf;cout,B:-B() Called.nM;)private:char * buf;);void fun(A *a)(delete a;)void main()(A *a

39、= new B(15);fun(a);)3 . 给出程序的运行结果:#include class Undergraduate(public:void Display( )coutnCall BaseClassnendl;coutnUnergraduate LiMing*endl;);class Master:public Undergraduatepublic:void Display( )coutnCall MasterclassHendl;cout*Master WangWeinendl;);class Doctor:public Master(public:void Display( )

40、(cout*Call DoctorClassnendl;coutT,Doctor ZhangHua*Display ();pointer=&s2;pointer-Display();)4 .给出程序的运行结果:# i n c l u d e c l a s s p o i n t(p u b l i c :p o i n t ( d o u b l e I , d o u b l e j ) x = I ; y = j ; v i r t u a l d o u b l e A r e a ( ) r e t u r n 0 . 0 ; p r i v a t e :d o u b l e x

41、 , y ;) ;c l a s s R e c t a n g l e : p u b l i c p o i n t(p u b l i c :R e c t a n g l e ( d o u b l e I , d o u b l e j , d o u b l e k , d o u b l e 1 ) ;virtual double Area()(return w*h;)private:double w, h;) ;Rectangle:Rectangle(double I, double j, double k, double 1):point(I, j)(w=k;h=l;void

42、 fun (point &s) couts. Area ();)void main ()(Rectangle rec (3. 0, 5. 2,15. 0, 25. 0);fun(rec);5 .给出程序的运行结果:#incl udeclass Aint x;public:A(int a=0) x=a; coutHcall A(int=0)n;);class Bint y;public:B(int a=0) y=a; coutMcall B(int=0)nM;);class C:public B,virtual public Aint z;public:C(int a=0,int b=0):B(

43、b+20),A(b) z=a; coutncall C(int=0,int=0)nn; );void main(void) Cc( 100,200); ( 七 )I /O与文件操作部分练习题目1 . 给出该程序的功能:#include #include #include void main() fstream outfile,infile;infile.open(nf2.datn,ios:in);if( Jinfile) coutf2.dat cant open. nendl;abort(); outfile.open(f4.dat,ios:out);if(!outfile) coutnf4.

44、dat cant open. Hendl;abort(); )char ch;while(infile.get(ch)outfile.put(ch);infile.close();outfile.close();2 .给出程序的运行结果:/ include void main () ofstream ostrm;ostrm. open (/zf 1. dat ) ;ostrm 120endl;ostrni310. 85endl;ostrm. closeO ;ifstream istrm (, zf 1. dat ) ;int n;double d;istrm nd;coutn, z, ,den

45、dl;istrm. closeO ;第三部分程序设计部分一、类的定义( 只给出类结构的声明)( 注意:对于成员函数,如果有参数,必须注明参数的含义)1定义一个日期类,其中日期由年月日组成,该类支持以下操作( 1 ) 判断是否是闰年( 2 ) 显示日期( 3 )获取年月日的值2定义一个平面上的圆类( 圆心和半径) ,该类支持以下操作:( 1 ) 获得圆心和半径( 2 ) 分别计算圆的周长、面积( 3 )设置圆心和半径3定义一个平面上矩形类( 矩形的两个顶点) ,该类支持以下操作( 1 ) 计算矩形的周长和面积( 2 ) 读取、设置矩形的信息4定义一个学生类,该类封装以下信息和操作( 1 ) 学生

46、的姓名、学号、班级、籍贯( 2 ) 显示学生的信息( 3 )获取学生的各项信息5定义直线类,该类封装以下操作计算直线的斜率6定义课程类,该类封装以下信息(1 ) 课程号,课程名,课程学分,开课学期(2) 读取、设置各项信息7定义三角形类,该类支持以下操作(1 ) 读取、设置三角形的基本信息(2) 计算三角形的周长8定义一个类,用于描述一个一元二次方程,并封装以下操作:( 1 ) 求解一元二次方程的根(2) 输出一元二次方程9 . 银行卡用于储蓄和消费。每张银行卡有唯一的卡号、密码,每张银行卡可以查询用户的余额。设计一个银行卡类,该类支持以下操作:(1 ) 余额查询,密码修改,存款,取款等1 0

47、 教务处在排课过程中,需要根据学生的数量,是否支持多媒体等信息安排教室。设置一个教室类,该类支持以下操作:( 1 ) 设置、修改、查询教室的容量(2 ) 查询是否支持多媒体二、 类操作的实现1 日期类结构如下:class dateint year, month, day, weekday;public:date operate +(int x);)实现date operate+(intx)函数,要求:根据天数(x ) 计算出新的日期。2 日期类结构如下:class date(int year, month, day, weekday;public:friend date operate +(d

48、ate a, int x);)实现date operate+(date a, int x)函数,要求:根据天数(x)计算出新的日期。3日期类结构如下:class date(int year, month, day, weekday;public:date(形参) ;实现构造函数的定义。要求:构造带默认值的构造函数,默认值为1900.1.1,1(星期一) 。4 已知类A 如下:class A(int a;char * str;public:A();A(const & a);)实现类中声明的两个函数:要求在构造函数中对数据成员进行初始化,在拷贝构造函数中实现用已知的对象构造一个新对象的操作。5已知

49、类A 如下:class Aint a| 100;plulic:void max ( 参数)o o o o m a x函数求出一维数组中最大值及最大值所在的位置。(最大值不唯一)6 已 知 polyx)的定义如下1n=0polyn(x)=x n=l(2*n-l)*x*polyn.i(x)-(n-l)*polyn.2(x)/n试编写函数求解polyn(x)的值7 有如下的定义struct studentchar *name;char studentid10;int vc, English, math;int total;)class allstudent(student a| 100;studen

50、t average;public:allstudent();void calcute();)实现calcute函数, 该函数计算每个学生的总成绩及各科的平均成绩. 说明: 在 allstudent类的构造函数中,对数据成员a100中数组分量的姓名,学号,三科成绩赋值。个人的总成绩需要在calcute函数中计算。平均成绩保留在average数据成员中。8 回文的定义如下:如果一个字符串左右对称则成为回文,如 abcba1abba都是回文假设有如下的字符串类:class string(char *str;public:bool isre verse();实现iseverse。 函数,该函数用来判断

51、字符串是否是回文9 字符串类定义如下:class string(char *str;int strleng; 记录字符串的长度public:bool operate = (string s);实现上述函数,判断量的字符串是否相等(相等的定义:长度相同,对应位置上的符号相同) 。1 0 结构体、类定义如下struct student(char *name;char studentid10;int vc, English, math;int total;class allstudentstudent *a;int number; 学生的人数public:allstudent ();-allstud

52、ent();) ;实现构造函数和析购函数。要求:构造函数中,根 据 number进行数组大小的确定,并在构造函数中给每个数组分量赋值。三、 继承与派生1 根据如下的类层次结构,给出类的定义:其中,类 A 中封装了数据成员a (整型) 和成员函数show () (输 出 a 的值) ,类 A 1和A 2 继承该数据成员,井分别派生出al ( A l) 和 a2 (A 2 )两个整型的数据成员,A l、A2的 show()函数输出其所有数据成员的值。A3继承了 A l、A 2的所有数据成员,并没有派生出新的数据成员,其 show ( ) 函数输出数据成员的所有值。根据以上的描述,完成类的定义2 已

53、知point类定义如下:class PointPoint( double a,double b) x=a; y=b;Protect:double,x,y ;) ;以point类作为基类, 构造派生类circle,该类含有计算圆面积的成员函数, 并由circle派生 出 cylinder类,该类含有圆柱体体积的函数。( 要求:给出类的声明并实现成员函数的定义)3 要开发一个教学信息管理系统, 该系统对教师和学生的信息进行管理: 在该系统中分别对学生、教师的以下信息进行记录:学生:姓名,性别,出生日期,身份证号,学号,入学时间,所在院教师:姓名、性别,出生日期,身份证号,职工编号,参加工作时间,所

54、在院,职称,学历进修教师:同时拥有学生和教师的所有信息。根据上述描述,依据类的继承机制,采用简洁的方式,完成类的定义( 只进行类的数据成员的定义) 。4 家俱城有个专门经营桌子的商户。 桌子有圆桌和方桌之分, 每种桌子的颜色、 高度、 面积,容纳的人数个不相同。根据上述信息,依据类的继承机制,完成相关类的定义( 进行类的数据成员和成员函数的声明,并实现面积的计算) 。要求:基类和派生类都要支持带参的构造函数。5 编写一个程序设计- 一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weighto小车类car是它的私有派生类,其中包含载人数passengejload。卡车类tr

55、uck是 vehicle的私有派生类,其斗包含载人数passengerjoad和载重量payload,每个类都有相关数据的输出方法。6 任何的企业都是由员工组成,员工分为普通员工,领导,工程师等,有些领导同时又是工程 师 (称谓主任工程师) 。在企业人员信息管理系统中,要记录所有员工的姓名、年龄等基本的数据,对领导,还要记录领导的职务和所在的部门等信息,对工程师,要记录职称和专业等信息。对主任工程师要记录所有的信息。根据以上的信息,采用类的继承机制,完成相关类的定义。7 根据下图的描述,采用类的继承机制,进行类的声明。8 假设某商店有如下几种货品:衬衣、帽子、立柜。每一种货物都有与其关联的说明

56、信息。衬衣:单价、产地、库存量、布料;帽子:单价、产地、库存量、布料、样 式 ( 平顶或尖顶);立柜:单价、产地、库存量、木料、颜色。对这些商品的操作有:商品的进库( 增加库存量),商品的出库( 减少库存量),该类货品总价格的计算。要求自行设计数据结构, 用类的继承与派生关系将匕述的各种货品表示出来, 并使用类的构造函数来初始化每一类对象的初始数据( 定义带参的构造函数)。9 已知shape类定义如下:class shape(protected:int sidevaluel,sidevalue2,high;public:void setvalue(int a,int y=0,int h=0)(

57、sidevaluel=x; si de val ue2=y ;hi gh=h;)virtual void calculatearea()=O;)以该类为基类, 定义circle类和rectangle类, 这两个类都有函数calculatearea计算几何面积。1 0 日期由年、月、日组成,时间由时、分、秒。采用类的继承机制,定义一个日期类、时间类和日期时间类,该类中除了包含年月日,时分秒等信息之外,还包含星期的信息。要求类的构造函数带有参数,并设置缺省值( 1900.1.1,0:0:0,星 期 1)四、 重载1 设计一个长方形类Triangle,包含长方形边长的私有数据成员,另有一个重载运算符

58、以实现求两个三角形对象的面积之和2 设计一个学生类student,包括姓名和三门课程成绩, 利用重载运算符” + “ 将所有学生的各科成绩单独相加放在一个学生对象中. ( 即在该学生对象中分别记录三门课的总成绩) . 请根据要求给出重载函数的定义( 重载为友员函数)#include#include#includeclass student(char name10;int degl,deg2,deg3;static int total; 总人数public:student()student(char na| ,int dl,int d2,int d3)strcpy(name,na);degl=d

59、l;deg2=d2;deg3=d3;)friend student operator+(student si,student s2);void disp()(coutsetw( 10)namesetw(5)deglsetw(5)deg2setw(5)deg3endl;)friend void avg(student &s,int n)(coutvvsetw( 10) vv”平均分” ;coutsetw(5)s.degl/nsetw(5)s.deg2/nsetw(5)s.deg3/nendl;) ;3 设计一个学生类student,包括姓名和三门课程成绩, 利用重载运算符” + “ 将所有学生的

60、各科成绩单独相加放在一个学生对象中. ( 即在该学生对象中分别记录三门课的总成绩) 。请根据要求给出重载函数的定义( 重载为成员函数) 。#include#include#includeclass studentchar name10;int degl,deg2,deg3;static int total;public:student()student(char na|,int dl,int d2,int d3)(strcpy(name,na);degl=dl ;deg2=d2;deg3=d3;student operator+(student s i);void disp()(coutset

61、w( 10)nam esetw(5)deglsetw(5)deg2setw(5)deg3endl;)friend void avg(student &s,int n)(coutsetw( 0)vv ” 平均分” ;coutsetw(5)s.degl/nsetw(5)s.deg2/nsetw(5)s.deg3/nendl;) ;4 . 定义日期类( 年、月、日) ,并对“ + ”运算符重载为该类的友员函数,支持日期的加法运算,即当前日期+ 天数得到新的日期信息5 . 定义日期类( 年、月、日) ,并对“ + ”运算符重载为该类的成员函数,支持日期的加法运算,即当前日期+ 天数得到新的日期信息6

62、. 定义人民币类,并对+ 、* 运算符重载,支持利息的计算和人民币的加法运算。7 已知一个字符串类:class string(char *str;)对+ 运算符进行重载为友元函数,以支持字符串的连接运算,即”12+“23”=“1223”:( 说明,利用字符串连接函数strcat(char *sl,char *s2)实现)8 已知一个字符串类:class stringchar *str;对+ 运算符进行重载为成员函数,以支持字符串的连接运算,即“12“+“23”=“ 1223”:9 已知定义日期类( 年,月、日) ,并重载 运算符,用于输出日期信息。( 分别重载成员函数和友员函数) 。1 0 对

63、 如下的student类class student(private:char * name;int age;int vc, english, math;public:重载插入和提取运算符,实现student类型信息的输入和输出。五、 文件操作1 .已知student类的定义如下:class student(private:char *name;int age;int visual C+, english, math;public: ( 说明:该类中没有定义构造函数)要求:设计一个程序,利用该类定义一个静态数组,并依次录入各位同学的相关信息,并将这些信息写入到文本文件中。2 .已知student

64、类的定义如下:class studentprivate:char *name;int age;int vc, english, math;public:) ( 说明:该类中没有定义构造函数)要求:设计一个程序,利用该类定义一个静态数组,并利用存储在student.txt文件中的学生的相关信息对数组分量进行赋值。3 .设计程序,对文本文件result.txt进行复制( copy.txt)。4 . 已 知 两 个 文 本 文 件 :resultl.txt和 result.txt,将 两 个 文 件 合 并 成 一 个 文 件result.txt.(resultl .txt: 12345,resul

65、t2.txt:abc,result.txt: 12345abc)5 . 将乘法表存储到文件中。如下:1*1 = 12*1=2 2*2=43*1=3 3*2=6 3*3=9O O O O O O O O6 .统计文件中字符的个数( 不包括空格、制表符、回车符等) 。7 .将文件中的大写字符转化为小写字符,并将转化后的结果保存到新文件中。( 提示:利用 string.h 头文件中定义的字符转换函数( isuppe (char ) ,islower(char) , tolower(char c ),toupper(char c),)8 .统计文件中文本的行数。第四部分 综合应用系统的分析、设计1 .

66、建立一个结点包括职工的编号、 年龄和性别的单向链表, 分别定义函数完成以下功能:1 )遍历该链表输出全部职工信息;2 )分别统计出男女性职工的人数;3 )在链表尾部插入新职工结点;4 )删除指定编号的职工结点;5 )删除年龄在6 0岁以上的男性职工或5 5岁以上的女性职工结点,保存在另一个链表中。6 )主函数建立简单菜单选择,测试你的程序。2 .定义个S t ud e n t类,在该类定义中包括:一个数据成员s c o r e ( 分数)及两个静态数据成员t o t a l (总分)和学生人数c o un t ;成员函数S 8 r e t o t a l c o un t ( f l o a

67、t s )用于设置分数、求总分和累计学生人数;静态成员函数s um ( )用于返回总分;静态成员函数a v e r a g e。 用于求平均值。3 . 在m a i n函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。1 )建立单向链表,每个结点包括:学 号( i n t ) ,姓 名( c h a r n a m e ) ,性 别( c h a r )。2 )将一个新的结点插入到链表中某一个结点之后。3 )查找某一个结点将其从链表中删除。4 )输出处理过的链表。在主函数中分别调用创建、插入、删除和输出函数。4 .用链表实现通讯录管理系统1 )输入模块:输入新的通讯录条目,包

68、括:姓名,性别,电话,E - m a i l ,通讯地址,邮编,QQ号等。2 )删除模块:删除指定姓名的条目3 )查询模块:输入姓名可以查询出该人的其他信息。4)修改模块:修改某一条记录5 )存储模块: 将输入的信息存储到硬盘文件; 并能够读入存储在该文件中的信息。5 . 某商店经销一种货物( G o o d s ) ,货物成箱购进, 成箱卖出, 购进和卖出时均以重量为单位, 各箱的重量不一样, 因此, 商店需要记录下目前库存的货物的总重量, 试根据以上说明及下述程序的行为定义并实现G o o d s类用以模拟货物购进和卖出情况。6 . 王婆卖瓜过程如下:1 ) 每卖一个瓜要记录该瓜重量2 )

69、 记录所卖出的瓜的总重量和所卖出瓜的总个数3 ) 允许退瓜。请用面向对象方法设计并实现上述王婆卖瓜过程。7 . 请用链表实现如下功能:1 )设置学生的成绩信息:学生学号、姓名、所选课程名称、平时成绩、期末成绩、平时成绩所占百分比2 )计算学生总评成绩:总评成绩= 平时成绩* 平时成绩所占百分比+ 期末成绩*( 1 - 平时成绩所占百分比)3 )显示学生的姓名、所选课程名称以及平时成绩、期末成绩和总评成绩信息4)利用文件存储学生成绩信息,并在程序运行开始时读入链表中;5 )编写菜单将以上功能组成一个应用系统8 . 考勤信息记录了学生的缺课情况,它包括:缺课日期、第几节课、课程名称、学生姓名、缺课

70、类型( 迟到、早退、请假及旷课) 。试设计一考勤管理系统,使之能提供以下功能:1 )录入学生的缺课记录;2 )修改某个学生的缺课记录;3 )查询某个学生的缺课情况;4)统计某段时间内,旷课学生姓名及旷课次数,按旷课次数由多到少排序;5 ) 统计某段时间内, 有学生旷课的课程及旷课人次, 按旷课人次由多到少排序;6 )系统以菜单方式工作。第五部分常见的通用算法与设计思想注意:应将这些常用算法,针对不同的应用结构( 例如:数组结构、链表结构、类结构中) 给出不同的实现,下面所给出的算法思想主要是针对数组结构,类似可以给出链表结构的相应的算法。希望同学们认真研究这些算法,并将其应用到各实际系统中。一

71、、计数、求和、求阶乘等简单算法此类问题都要使用循环,要注意根据问题确定循环变量的初值、终值或结束条件,更要注意用来表示计数、和 、阶乘的变量的初值。例:用随机函数产生1 0 0个 0 , 9 9 范围内的随机整数,统计个位上的数字分别为1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0的数的个数并打印出来。算法思想:本题使用数组来处理,用 数 组a UO O 存 放 产生的确1 0 0个随机整数,数组x 1 0 来存放个位上的数字分别为1 , 2, 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0的数的个数。即个位是1的个数存放在x l 中,个位是2的个数存放

72、在x 2 中. . . . . 个位是0的个数存放在x 0 o二、求两个整数的最大公约数、最小公倍数分析:求最大公约数的算法思想:( 最小公倍数= 两个整数之积/ 最大公约数)( 1 )对于已知两数m, n ,使 得m n;( 2 ) m除 以n得余数r ;( 3 )若r = 0 ,则n为求得的最大公约数,算法结束;否则执行( 4 ) ;( 4 ) m- n, n - r ,再重复执行( 2 )。三、判断素数只能被1 或本身整除的数称为素数基本思想:把 m 作为被除数,将 2 I N T ( M 的平方根)作为除数,如果都除不尽,m 就是素数,否则就不是。四、验证哥德巴赫猜想( 任意一个大于等

73、于6的偶数都可以分解为两个素数之和)基本思想:n 为大于等于6的任一偶数,可分解为nl 和 n2 两个数,分别检查 nl 和 n 2 是否为素数,如都是,则为一组解。如 nl 不是素数,就不必再检查n2 是否素数。先 从 nl = 3 开始,检 验 n l 和 n2 ( n2 = N - nl )是否素数。然 后 使 nl + 2 再检验 nl 、n2 是否素数, 直 到 nl = n/ 2 为止。五、排序问题1 .选 择 法 排 序 ( 升序)基本思想:1 )对 有 n 个数的序列( 存放在数组a n 中) ,从中选出最小的数,与 第 1个数交换位置;2 )除 第 1个数外,其 余 n- l

74、 个数中选最小的数,与第2个数交换位置;3 )依次类推,选择了 n- l 次后,这个数列已按升序排列2 .冒泡法排序( 升序)基本思想: ( 将相邻两个数比较,小的调到前头)1 )有 n 个 数 ( 存放在数组a n 中) ,第一趟将每相邻两个数比较,小的调到前头,经 n- l 次两两相邻比较后,最大的数已“ 沉底” ,放在最后一个位置,小数上升“ 浮起” ;2 )第二趟对余 下 的 n- l 个 数 ( 最大的数已“ 沉底” )按上法比较,经 n- 2次两两相邻比较后得次大的数;3 )依次类推,n 个数共进行n- l 趟比较,在第j 趟中要进行n- j 次两两比较。3 . 合并法排序:( 将

75、两个有序数组A、B 合并成另一个有序的数组C , 升序)基本思想:1 )先在A 、B 数组中各取第一个元素进行比较, 将小的元素放入C 数组;2 ) 取小的元素所在数组的下一个元素与另一数组中上次比较后较大的元素比较,重复上述比较过程,直到某个数组被先排完;3 ) 将另一个数组剩余元素抄入C 数组,合并排序完成。六、查找问题1 . 顺序查找法( 在一列数中查找某数x)一列数放在数组a(l)-a(n)中, 待查找的关键值为key,把 key与a 数组中的元素从头到尾一一进行比较查找,若相同,查找成功,若找不到,则查找失败。基本思想:一列数放在数组alanj中,待查找的数放在x 中,把 x 与 a

76、数组中的元素从头到尾一一进行比较查找。用变量p 表示a 数组元素下标,p 初值为1 , 使 x 与ap比较,如果x 不等于ap ,则使p=p+l,不断重复这个过程;一旦x 等于ap则退出循环;另外,如果p 大于数组长度,循环也应该停止.2 . 折半查找法( 只能对有序数列进行查找)基本思想:设n 个有序数( 从小到大) 存放在数组a(l)- -a(n)中,要查找的数 为 x o 用变量bot、top、m id分别表示查找数据范围的底部( 数组下界) 、顶部( 数组的上界) 和中间, mid=(top+bot)/2,折半查找的算法如下:(1) x=a(mid),则已找到退出循环,否则进行下面的判

77、断;(2) xa(mid), x 必定落在mid+1和top的范围之内,即bot=mid+l;( 4 ) 在确定了新的查找范围后,重复进行以上比较,直到找到或者bot=top0将上面的算法可写成函数, 若找到则返回该数所在的下标值, 没找到则返回-1。七、插入法把一个数插到有序数列中,插入后数列仍然有序基本思想:n个有序数( 从小到大) 存放在数组a I a n 中,要插入的数Xo首先确定x插在数组中的位置P .八、矩阵( 二维数组) 运算(1)矩阵的加、减运算C ( i , j ) = a( i , j ) + b ( i , j ) 加法C ( i , j ) = a( i , j ) -

78、 b ( i , j ) 减法(2)矩阵相乘( 矩阵A有M*L个元素,矩阵B有L*N个元素,则矩阵C=A*B有M*N个元素) 。矩阵 C 中任一元素( i = l , 2 , . . . , m ; j = l , 2 , , n )(3)矩阵传置(4)求二维数组中最小元素及其所在的行和列九、迭代法算法思想:对于一个问题的求解x,可由给定的一个初值xO,根据某一迭代公式得到一个新的值xl,这个新值xl比初值xO更接近要求的值x;再以新值作为初值,即:xlf x O ,重新按原来的方法求x l ,重复这一过和直到l x l - x O I ( 某一给定的精度) 。此时可将xl作为问题的解。例:用

79、迭代法求e值。十、数制转换将个十进制整数m转换成一r ( 2 1 6 )进制字符串。方法:将m不断除r取余数,直到商为零,以反序得到结果。转换函数,参数i d e e为十进制数,i b as e为要转换成数的基( 如二进制的基是2,八进制的基是8等) ,函数输出结果是字符串。十一、字符串的一般处理1 .简单加密和解密加密的思想是:将每个字母C 加 ( 或减)一 序 数 K, 即用它后的第K个字母代替,变换式公式:c =c + k例如序数 k 为 5 , 这时当加序数后的字母超过 Z 或 z 则 c =Ch a r ( c + k - 2 6 )例如:Y o u a r e g o o d -

80、*Dtz f w j I t t i解密为加密的逆过程将每个字母C 减 ( 或加)一序数K , EP c =c - k ,例如序数k为 5 , 这时 z - u , Y T 当加序数后的字母小于 A 或 a 则 c =Ch a r ( c - k + 2 6 )2 .统计文本单词的个数算法思路:( 1 )从 文 本 ( 字符串)的左边开始,取出一个字符;设逻辑量WT表示所取字符是否是单词内的字符,初值设为Fa ls e( 2 )若所取字符不是“ 空格” ,“ 逗号” ,“ 分号, 或“ 感叹号 等单词的分隔符,再 判 断 WT是 否 为 T r ue ,若 WT不 为 T r ue 则表是新单

81、词的开始,让单词数N w = N w + l , 让 W T =T r ue ;( 3) 若所取字符是“ 空格” , “ 逗号” , “ 分号” 或“ 感叹号” 等单词的分隔符,则表示字符不是单词内字符,让 W T =Fa ls e ;( 4) 再依次取下一个字符,重 得 ( 2 ) ( 3) 直到文本结束。十二、穷举法穷 举 法 ( 又称“ 枚举法” )基本思想是: 一一列举各种可能的情况, 并判断哪一种可能是符合要求的解,这是一种“ 在没有其它办法的情况的方法” ,是一种最“ 笨” 的方法,然而对一些无法用解析法求解的问题往往能奏效,通常采用循环来处理穷举问题。例:将一张面值为1 0 0 元的人民币等值换成1 0 0 张 5 元、1 元 和 0 . 5元的零钞,要求每种零钞不少于1 张,问有哪几种组合?

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号