ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:19.07KB ,
资源ID:11637254      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11637254.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(程序设计与问题求解.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

程序设计与问题求解.docx

1、程序设计与问题求解桂林电子科技大学试卷2010-2011 学年第 2 学期 A卷课号 课程名称 程序设计与问题求解2 适用班级(专业) 考试时间 120 分钟 座位号 学号 姓名 题号一二三四五六七八成绩满分5020 30得分评卷人一、阅读程序,写出程序运行结果(每题10分,5题共50分)1结构体# include # include using namespace std;struct Worker char name15; / 姓名 int age; / 年龄 float pay; / 工资;void main() Worker Worker1,Worker2; char *t=liout

2、ing; int d=38; float f=493; strcpy(Worker1.name,t); Worker1.age=d; Worker1.pay=f; cout Worker1.name Worker1.age Worker1.payendl; Worker2=Worker1; cout Worker2.name Worker2.age Worker2.payendl; 输出结果是:liouting 38 493liouting 38 4932构造函数与析构函数#include using namespace std;class TAddprivate: int x, y;publ

3、ic: TAdd(int a, int b): x(a), y(b) cout 调用构造函数1. endl; TAdd(TAdd &p) x=p.x; y=p.y; cout 调用构造函数2. endl; TAdd( ) cout 调用析构函数. endl; int add( ) return x+y; ;void main( ) TAdd p1(2, 3); TAdd p2=p1; cout p2.add( ) endl;输出结果是:调用构造函数1.调用构造函数2.5调用析构函数.调用析构函数.3虚函数#include using namespace std;class Apublic: v

4、irtual void f()coutA:f() executingn;class B:public Apublic: void f()coutf(); p = &b; p-f(); a=b; a.f(); 输出结果是:B:f() executingA:f() executingB:f() executingA:f() executing4模板#include using namespace std;template class myclassprivate: Type1 i; Type2 j;public: myclass(Type1 a, Type2 b) i=a; j=b; void s

5、how() couti jendl; ;void main() myclass ob1(1,3); myclass ob2(10,0.23); myclass ob3(A,This is a test); ob1.show(); ob2.show(); ob3.show();输出结果是:1 310 0.23A This is a test5继承#includeusing namespace std;class A int x,y;public: A(int x1=0, int y1=0):x(x1),y(y1) coutA:x yn; A() coutA des!n; ;class B int

6、 i;public: B(int ii) i = ii; coutB con!n; B()coutB des!n;class C:public A,public Bpublic: C (int cx,int cy, int bi):A(cx,cy),B(bi) coutA with B con!n; C () coutA with B des!n;int main() C cm(3,4,5);输出结果是:A:3 4B con!A with B con!A with B des!B des!A des!二、程序填空(每题10分,2题共20分)1. 词频统计:输入一系列英文单词,单词之间用空格隔开

7、,用“xyz”表示结束输入,统计输入过哪些单词以及各单词出现的次数,统计时区分大小写字母,最后按单词的字典顺序输出单词和出现次数的对照表。#include #include using namespace std; / 词条类型struct WordList char word50; int freq;/ 词典排序函数void Sort(WordList list, int count) for(int i=0; ii; j=j-1) if(strcmp(listj-1.word,listj.word)0) WordList tmp; tmp=listj-1; listj-1=listj; l

8、istj=tmp; / 主函数:进行词频统计int main() WordList list5000; int i, num=0; char temp50;cout请输入一系列英语单词,以xyz表示输入结束temp; while( (1) ) for(i=0; i=num) / 若词典中无该词条,添加该词 strcpy(listi.word, temp); (3) (4) (5) / 继续输入单词 Sort(list, num); / 对词典进行排序 / 输出词典 cout词频统计结果如下:endl; for(i=0; inum; i+) coutlisti.wordtlisti.freqte

9、mp;2带头结点链表类的定义如下:#include#includeusing namespace std; template /模板声明class NODE /结点类定义 public: datatype data; /数据域 NODE *next; /指针域;templateclass List /单链表类定义 private: NODE *head; /链表头指针 public: List(); /构造函数创建头结点 int length(); /求表长函数 bool isempty() return head-next=NULL?true:false; /判空链表函数 bool inse

10、rt_data(datatype data,int i); /插入元素函数 List(); /析构函数 ;templatebool List:insert_data(datatype data,int i) /定义插入函数 NODE *current,*previous,*newnode; int j=1; if(ilength()+1)|(i0) /判插入位置的合法性 cout插入位置不正确,不能插入!n; return false; newnode=new NODE; /申请新结点空间 if(newnode=NULL) /判表满否 coutdata=data; newnode-next=N

11、ULL; previous=head; (6) while( (7) ) /寻找第i个元素 previous=current; (8) /指向下一个结点 j+; ; /链入新结点 (9) (10) return true;答案:(6)current=head-next;(7) current!=NULL&jnext;(9) newnode-next=current;(10) previous-next=newnode;三、程序设计(每题15分,2题共30分)1设计一个时间(Time)类,设计多个重载的构造函数,可以设置时间,时间加运算(时间加多少秒),要求重载+来实现时间加运算,按24小时制格

12、式:时:分:秒输出时间。并在主程序中测试所有的操作。(15分)#pragma once/*时间类*/class Time int second ,minute ,hour; int GetSecond(); /计算总秒数 void SetTime(int ss); /根据秒数算出second ,minute ,hour public: Time(); Time(int hh,int mm,int ss); void SetTime(int hh,int mm,int ss); const Time &operator+(int ss); bool operator(Time &); void

13、Print_hms(); /HH:mm:ss ;#include Time.h#include using namespace std;Time:Time() second=0; minute=0; hour=0;Time:Time(int hh,int mm,int ss) SetTime(hh,mm,ss);/计算总秒数int Time:GetSecond() return second+60*minute+3600*hour;void Time:SetTime(int ss) second=ss%60; ss=ss/60; minute=ss%60; ss=ss/60; hour=ss%

14、24;void Time:SetTime(int hh,int mm,int ss) second=(ss=0&ss=0&mm=0&hh24)?hh:0;const Time & Time:operator+(int ss) SetTime(GetSecond()+ss); return *this;bool Time:operator (Time &time) if(GetSecond()-time.GetSecond()0) return true; else return false;void Time:Print_hms() couthour:minute:secondendl;#in

15、clude Time.h#include using namespace std;int main() Time t1,t2; int hour,minute,second; cout请输入时间(时分秒):hourminutesecond; t1.SetTime(hour,minute,second); t1.Print_hms(); t2.SetTime(12,0,0); if(t1t2) coutendl是上午的时间endl; else coutendl是下午的时间endl; return 0; 评分说明:数据成员定义2分,函数定义5分,函数实现5分,主程序测试3分2编写一个雇员和雇主数据

16、输入和显示的程序。雇员数据有编号(no)、姓名(name)和工资(salary),雇主数据有编号(no)、姓名(name)和职位(post)。要求将编号、姓名输入和显示设计成一个类person,并作为雇员数据类employee和雇主数据类employer的基类,并编写主程序进行执行,输出信息时体现运行多态性,并给出执行结果。(15分)答案:#includeusing namespace std;class person int no; char name10; public: virtual void input() coutno; coutname; virtual void output(

17、) coutthe no is noendl; coutthe name is nameendl; ;class employee: public person int salary;public: void input() person:input(); coutsalary; void output() person:output(); coutthe employee salary is salaryendl; ;class employer: public person char post10;public: void input() person:input(); coutpost;

18、 void output() person:output(); coutthe employer post is postinput(); p-output(); p=&er; p-input(); p-output();输出结果:the no is 01the name is wanghongthe employee salary is 1000the no is 1the name is wanghong the employee salary is 1000the no is 02the name is lizhethe emplorer post is bossthe no is 2the name is lizhethe employer post is boss评分说明:头文件2分;person类的两虚函数各2分;employee 类和employer类的公有继承各2分;employee 类和employer类对虚函数的重载各2分;employee 类和employer类对象赋给person类指针各2分;输出结果2分。欢迎您的下载,资料仅供参考!致力为企业和个人提供合同协议,策划案计划书,学习资料等等打造全网一站式需求

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1