版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. (10分)栈类类模板题目描述用类模板方式设计一个链栈类stack,其中有两个私有数据成员:Node *head(链首指针,即栈顶元素指针),int num(栈里结点数)以及3个公有成员函数:push(元素入栈)、pop(元素出栈)和stackempty(判断栈是否为空),并建立一个整数栈和一个字符栈。template class stack;注意:为了能够生成结点类型不同的链式栈,结点类的设计也需要用类模板。templateclass Node;输入描述输入整数栈的数据元素和字符栈的数据元素输出描述输出整数栈的数据元素出栈序列和字符栈的数据元素出栈序列输入样例4 8 3 2a d b c
2、输出样例2 3 8 4c b d a我的代码:#includeusing namespace std;template class stacktemplate class NodeT1 a ;Node *next ;public: Node(T1 x):a(x) void show()cout a endl ; friend class stack ;Node *head ;int num ;public:stack()num = 0 ; head = NULL ; void push(T x)Node *p = new Node(x) ;p-next = head ;head = p ;nu
3、m+ ;T pop()Node *p = head ;head = head-next ;T t = p-a ;delete p ;num- ;return t ;bool stackempty()if( head = NULL )return true;elsereturn false;int main()stack a ;stack b ;int x ;char y ;for(int i = 0 ; i x ;a.push(x) ;for( int i = 0 ; i y ;b.push(y);for( int i = 3 ; i = 0 ; i - )a.stackempty() ;co
4、ut a.pop() ;cout = 0 ; i - )b.stackempty();cout b.pop() ;cout endl ;return 0 ;2. (10分)二进制类(1)运算符重载题目描述将一个16位二进制数表示成0和1的字符序列,即用一个字符数组来存放这个二进制数。在这个类中设置两个构造函数,一个是传递整数参数的,另一个是传递字符串参数的。因为用户在创建对象时传递的二进制数,可能是以整数形式给出,也可能是以数字串形式给出,系统应该都能接受。另外有一个类型转换函数int(),用来将类类型向整型转换。两个重载运算符“+”,“-”,用来完成两个二进制数之间的加减运算。class b
5、inary /定义二进制类char bits16; /二进制字模数组public:binary(char *); /字符串参数构造函数binary(int); /整型参数构造函数friend binary operator +(binary,binary); /重载“+”friend binary operator -(binary,binary); /重载“-”operator int(); /类类型转换函数void print();主函数设计如下:int main()binary n1=1011;binary n2=int(n1)+15;binary n3=n1-binary(7);n1.
6、print();n2.print();n3.print();coutint(n2)+5endl;coutn2-binary(5)endl;coutn3+binary(5)endl;coutint(n3)-5endl;return 0;输入描述无输出描述略输入样例无输出样例00000000000010110000000000011010000000000000010031219-1我的代码:方法一:#includeusing namespace std ;class binarypublic:binary( char* ) ;binary( int ) ; friend binary opera
7、tor +( const binary & , const binary & ) ; friend binary operator -( const binary & , const binary & ) ; operator int() ; void print() ;private:char bits16 ; ;binary:binary( char *num )int len1 = strlen( num ) - 1 ;/字符串长度减一为最低位。int len2 = 15 ;while( len1 = 0 & len2 = 0 )bitslen2- = numlen1- ;/逐位赋值wh
8、ile( len2 = 0 )bitslen2- = 0 ;/空高位值0 binary:binary( int num )for( int i = 15 ; i = 0 ; i - )bitsi = ( num % 2 ) + 0 ;/求余数num = 1 ;/向右移一位。相当于整除2 binary operator +( const binary & n1 , const binary & n2 )binary n3 = binary (0000);int sum , num1 = 0 , num2 = 0 ;for( int i = 0 ; i = 15 ; i + )num1 = ( n
9、1.bitsi = 0 ? 0 : 1 ) + 2 * num1 ; for( int i = 0 ; i = 0 ; i - )n3.bitsi = ( sum % 2 ) + 0;sum = 1 ;return n3 ;binary operator -( const binary & n1 , const binary & n2 )binary n3 = binary(0000) ;int cha , num1 = 0 , num2 = 0 ;for( int i = 0 ; i = 15 ; i + )num1 = ( n1.bitsi = 0 ? 0 : 1 ) + 2 * num1
10、 ; for( int i = 0 ; i = 0 ; i - )n3.bitsi = ( cha % 2 ) + 0;cha = 1 ;return n3 ;binary:operator int()int sum = 0 ;for( int i = 0 ; i = 15 ; i + )sum = 2*sum + ( bitsi = 0 ? 0 : 1 ) ;return sum ;void binary:print()char str17 ;strncpy( str , bits , 16 ) ;str16 = 0 ;cout str n ;int main()binary n1 = 10
11、11 ;binary n2 = int( n1 ) + 15 ;binary n3 = n1 - binary(7) ;n1.print();n2.print();n3.print();cout int(n2) + 5 endl ;cout n2 - binary( 5 ) endl ;cout n3 + binary( 5 ) endl ;cout int( n3 ) - 5 endl ;return 0;方法二:#include#include using namespace std ;class binary /定义二进制类 char bits16; /二进制字模数组public:bin
12、ary(char *); /字符串参数构造函数binary(int); /整型参数构造函数friend binary operator +(binary,binary); /重载“+”friend binary operator -(binary,binary); /重载“-”operator int(); /类类型转换函数void print();binary:binary( char *num )int isrc = strlen( num ) - 1 ; /字符串长度-1为最低位int idest = 15 ;while( isrc = 0 & idest = 0 )bitsidest-
13、 = ( numisrc- = 0 ? 0 : 1 ) ; / 逐位赋值while(idest=0)bitsidest- = 0 ; / 空高位值0 binary:binary(int num)for( int i = 15 ; i = 0 ; i - ) bitsi=( num % 2 ) + 0 ; /求余数num = 1; /移位,相当于整除2binary operator +( binary n1 , binary n2 ) unsigned carry = 0 ; unsigned value ; binary res = 0 ; for( int i = 15 ; i = 0 ;
14、i - ) value=( n1.bitsi = 0 ? 0 : 1 ) + ( n2.bitsi = 0 ? 0 : 1 ) + carry; res.bitsi = ( value % 2 = 0 ? 0 : 1 ) ; carry = value 1 ; return res;binary operator -(binary n1, binary n2)unsigned borrow = 0 ;int value ;binary res = 0 ;for(int i=15; i=0; i-)value = ( n1.bitsi = 0 ? 0 : 1 ) - ( n2.bitsi = 0
15、 ? 0 : 1 ) + borrow ; res.bitsi = ( value = -1 | value = 1 ? 1 : 0 ) ; borrow = ( value = -1 | borrow != 0 & ( value = 0 | value = 1 ) ? 1 : 0 ) ;return res;binary:operator int()unsigned value = 0 ;for( int i = 0 ; i = 15 ; i + )value=( value*2 ) + ( bitsi = 0 ? 0 : 1 ) ;return value;void binary:pri
16、nt()char str17;strncpy( str , bits , 16 ) ;str16 = 0 ;cout str n ; int main() binary n1 = 1011 ; binary n2 = int(n1) + 15 ; binary n3 = n1 - binary(7) ; n1.print() ; n2.print() ; n3.print(); cout int( n2 ) + 5 endl ; cout n2 - binary(5) endl ; cout n3 + binary(5) endl ; cout int(n3) - 5 endl ; retur
17、n 0; 3. (10分)二进制类(2)运算符重载题目描述在上一题的基础上,将 + 、 - 运算符定义为binary类的成员函数。并且重载运算符 、 & 、 | ,分别将二进制数按位取反、数按位与及按位或。主函数设计如下:int main()binary n1=1011;binary n2=int(n1)+15;binary n3=n1-binary(7);n1.print();n2.print();n3.print();binary n4=n1&n2;binary n5=n1|n2;binary n6=n1; n4.print();n5.print();n6.print();return 0
18、;输入描述无输出描述略输入样例无输出样例000000000000101100000000000110100000000000000100000000000000101000000000000110111111111111110100我的代码:#includeusing namespace std ;class binarypublic:binary( char* ) ;binary( int ) ;binary operator +( const binary & ) ; binary operator -( const binary & ) ;friend binary operator (
19、 binary &t1 ) ;friend binary operator & ( binary &t1 , binary &t2 ) ;friend binary operator | ( binary &t1 , binary &t2 ) ; operator int() ; void print() ;private:char bits16 ; ;binary:binary( char *num )int len1 = strlen( num ) - 1 ;/字符串长度减一为最低位。int len2 = 15 ;while( len1 = 0 & len2 = 0 )bitslen2-
20、= numlen1- ;/逐位赋值while( len2 = 0 )bitslen2- = 0 ;/空高位值0 binary:binary( int num )for( int i = 15 ; i = 0 ; i - )bitsi = ( num % 2 ) + 0 ;/求余数num = 1 ;/向右移一位。相当于整除2 binary binary:operator +( const binary & n1 )binary n3 = binary (0000);int sum , num = 0 , num1 = 0 ;for( int i = 0 ; i = 15 ; i + )num =
21、 ( bitsi = 0 ? 0 : 1 ) + 2 * num ; for( int i = 0 ; i = 0 ; i - )n3.bitsi = ( sum % 2 ) + 0;sum = 1 ;return n3 ;binary binary:operator -( const binary & n1 )binary n3 = binary(0000) ;int cha , num = 0 , num1 = 0 ;for( int i = 0 ; i = 15 ; i + )num = ( bitsi = 0 ? 0 : 1 ) + 2 * num ; for( int i = 0 ;
22、 i = 0 ; i - )n3.bitsi = ( cha % 2 ) + 0;cha = 1 ;return n3 ;binary operator ( binary &t1 ) for( int i = 0 ; i = 0 ; i- )t3.bitsi = num % 2 + 0 ;num = 1 ;return t3 ;binary operator | ( binary &t1 , binary &t2 ) binary t3 = 0000 ;int num1 = int(t1) , num2 = int(t2) , num ;num = num1 | num2 ;for( int
23、i = 15 ; i = 0 ; i- )t3.bitsi = num % 2 + 0 ;num = 1 ;return t3 ;binary:operator int()int sum = 0 ;for( int i = 0 ; i = 15 ; i + )sum = 2*sum + ( bitsi = 0 ? 0 : 1 ) ;return sum ;void binary:print()char str17 ;strncpy( str , bits , 16 ) ;str16 = 0 ;cout str n ;int main()binary n1 = 1011 ;binary n2 =
24、 int(n1) + 15 ;binary n3 = n1 - binary(7) ; n1.print() ; n2.print() ; n3.print() ; binary n4 = n1 & n2 ; binary n5 = n1 | n2 ; binary n6 = n1 ; n4.print() ; n5.print() ; n6.print() ; return 0 ;4. (10分)图形类继承和派生题目描述定义一个图形类,其中有保护类型的成员数据:高度和宽度,一个公有的构造函数。由该图形类建立两个派生类:矩形类和等腰三角形类。在每个派生类中都包含一个函数Area(),分别用来计
25、算矩形和等腰三角形的面积。提示:class pic;class recg:public pic;class tag:public pic;输入描述输入矩形的长和宽以及等腰三角形的底边长和高。输出描述矩形的面积和等腰三角形的面积输入样例3 53 5输出样例157.5我的代码:#includeusing namespace std ;class picpublic:pic( double w = 0 , double h = 0 );protected:double width , height ;class recg: public picpublic:recg( double w , doub
26、le h );double Area() ;class tag: public picpublic:tag( double w , double h ) ;double Area() ;pic:pic( double w , double h )width = w ;height = h ;tag:tag( double w , double h ): pic( w , h ) width = w ;height = h ;recg:recg( double w , double h ): pic( w , h ) width = w ;height = h ;double recg:Area
27、()double area ;area = width * height ;return area ;double tag:Area()double area ;area = width * height / 2 ;return area ;int main ()int x , y , a , b ;cin x y a b ;recg t1 = recg( x , y ) ;tag t2 = tag( a , b ) ;cout t1.Area() endl t2.Area() ;return 0 ;5. (10分)人员管理信息系统继承与派生题目描述阅读以下程序,并调试运行。#include#
28、includeusing namespace std;class employeeprotected:char *name;/姓名int individualEmpNo;/个人编号int grade;/级别float accumPay;/月薪总额static int employeeNo;/本公司职员编号目前最大值public:employee();/构造函数employee();/析构函数void pay();/汁算月薪函数void promote(int);/升级函数void displayStatus();/显示人员信息;class technician:public employee/
29、兼职技术人员类private:float hourlyRate;/每小时酬金int workHours;/当月工作时数public:technician();/构造函数void pay();/计算月薪函数void displayStatus();/显示人员信息;class salesman: virtual public employee/兼职推销员类protected:float CommRate;/按销售额提取酬金的百分比float sales;/当月销售额public:salesman();/构造函数void pay();/计算月薪函数void displayStatus();/显示人员
30、信息;class manager: virtual public employee/经理类protected:float monthlyPay;/固定月薪数public:manager();/构造函数void pay();/计算月薪函数void displayStatus();/显示人员信息;class salesmanager:public manager,public salesman/销售经理类public:salesmanager();/构造函数void pay();/计算月薪函数void displayStatus();/显示人员信息;int employee:employeeNo=
31、1000;/员工编号基数为1000employee:employee()char namestr50;/输入雇员姓名时首先临时存放在namestr中coutnamestr;name=new charstrlen(namestr)+1;/动态申请用于存放姓名的内存空间strcpy(name,namestr);/将临时存放的姓名复制到nameindividualEmpNo=employeeNo+;/新输入的员工,编号为目前最大编号加1grade=1;/级别初值为1accumPay=0.0;/月薪总额初值为0employee:employee()delete name;/在析构函数中删除为存放姓名动
32、态分配的内存空间void employee:pay()/计算月薪,空函数void employee:promote(int increment)grade+=increment;/升级,提升的级数由increment指定void employee:displayStatus()/显示入员信息,空函数technician:technician()hourlyRate=100;/每小时酬金100元void technician:pay()cout请输入nameworkHours;accumPay=hourlyRate*workHours;/计算月薪,按小时计酬cout兼职技术人员name编号ind
33、ividualEmpNo本月工资accumPayendl;void technician:displayStatus()cout兼职技术人员name编号individualEmpNo级别为grade级,已付本月工资accumPayendl;salesman:salesman()CommRate=0.04;/销售提成比例4void salesman:pay()cout请输入namesales;accumPay=sales*CommRate;/月薪销售提成cout推销员name编号individualEmpNo本月工资accumPayendl;void salesman:displayStatus
34、()cout推销员name编号individualEmpNo级别为grade级,已付本月工资accumPayendl;manager:manager()monthlyPay=8000;/固定月薪80m元void manager:pay()accumPay=monthlyPay;/月薪总额即固定月薪数cout经理name编号individualEmpNo本月工资accumPayendl;void manager:displayStatus()cout经理name编号individualEmpNo级别为grade级,已付本月工资accumPayendl;salesmanager:salesmana
35、ger()monthlyPay=5000;CommRate=0.005;void salesmanager:pay()cout请输入namesales;employee:accumPay=monthlyPay+sales*CommRate;/月薪=固定月薪销售提成cout销售经理name编号individualEmpNo本月工资accumPayendl;void salesmanager:displayStatus()cout销售经理name编号individualEmpNo级别为grade级,已付本月工资accumPayendl;int main()manager m1;technician
36、 t1;salesmanager sm1;salesman s1;mote(3);/经理m1提升3级m1.pay();m1.displayStatus();mote(2);t1.pay();t1.displayStatus();mote(2);sm1.pay();sm1.displayStatus();s1.pay();s1.displayStatus();return 0;输入描述略输出描述略输入样例wanglizhangliu11213输出样例请输入下一个雇员的姓名:请输入下一个雇员的姓名:请输入下一个雇员的姓名:请输入下一个雇员的姓名:经理wang编
37、号1000本月工资8000经理wang编号1000级别为4级,已付本月工资8000请输入li本月的工作时数:兼职技术人员li编号1001本月工资100兼职技术人员li编号1001级别为3级,已付本月工资100请输入zhang所管辖部门本月的销售总额:销售经理zhang编号1002本月工资5000.06销售经理zhang编号1002级别为3级,已付本月工资5000.06请输入liu本月的销售额:推销员liu编号1003本月工资0.52推销员liu编号1003级别为1级,已付本月工资0.52窗体顶端窗体底端6. (10分)构造析构函数继承题目描述阅读下面的程序,请编写一个简单的主函数,使其满足对应
38、的输出要求。#includeusing namespace std;class CBase1int x ;public:CBase1( )x=0 ; cout调用构造函数CBase1( )!n;CBase1( int a)x=1;cout调用构造函数CBase1( int )!n;CBase1( )cout调用析构函数CBase1( )!n;class CBase2int y;public:CBase2( )y=0 ;cout调用构造函数CBase2( )!n;CBase2(int a)y=a ;cout调用构造函数CBase2(int )!n;CBase2()cout调用析造函数CBase2( )!n;class Aint x;public:A () x=0 ; cout调用构造函数A( )!n;A(int a)x=a;cout调用构造函数A(int )!n;A()cout调用析构函数A()!n;class CDerived:public CBase1, virtual public CBase2A a;public:CDerived()cout调用构造函数CDerived( )!n;CDerived(int x,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 广东省深圳市罗湖区2025-2026学年度第一学期学业质量检测七年级地理期末试题(含答案)
- 手术室护士护理与法律
- 2026年哈尔滨市校园安保人员岗位培训应知应会题库(含答案)
- 2026年山西省医疗器械经营质量管理规范培训试题知识竞赛题库(含答案)
- 2026年河北省定点零售药店医保服务规范学习效果测试题(附答案)
- 儿童早期发展健康教育工作方案
- 三亚市天涯区幼儿园防震减灾应急疏散方案
- 防溺水工作实施方案
- 2026年山西省化妆品监督管理条例知识培训考核试题及答案
- 实验室安全考核评分细则
- 韧性视角下社区应急管理能力评价指标体系构建及应用研究
- 成都新都投资集团有限公司2026年招聘市场运营部副部长等岗位(13人)笔试备考题库及答案解析
- 慢性心力衰竭规范化诊疗指南(2026版)
- 2026年全科副主任医师 副高职称面审答辩题库
- 2026秋小学人教版数学三年级上册《三 毫米、分米和千米》易错题专项练习含答案
- 江苏省自考02356数字信号处理高频考点重点
- 2026年甘肃省从优 秀村干部中考试录用乡镇机关公务员(公共知识)测试题及答案
- 2026年东华大学纺纱学考试试题及答案
- ISO 218982024 包装-用于非危险货物的柔性中间散装容器(FIBC)标准立项发展报告
- 2026年基层抗菌药物使用管控培训考试题(含答案)
- 研究生考试《自然辩证法概论》练习题附答案
评论
0/150
提交评论