程序设计与问题求解Word下载.docx

上传人:b****6 文档编号:19295095 上传时间:2023-01-05 格式:DOCX 页数:16 大小:19.67KB
下载 相关 举报
程序设计与问题求解Word下载.docx_第1页
第1页 / 共16页
程序设计与问题求解Word下载.docx_第2页
第2页 / 共16页
程序设计与问题求解Word下载.docx_第3页
第3页 / 共16页
程序设计与问题求解Word下载.docx_第4页
第4页 / 共16页
程序设计与问题求解Word下载.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

程序设计与问题求解Word下载.docx

《程序设计与问题求解Word下载.docx》由会员分享,可在线阅读,更多相关《程序设计与问题求解Word下载.docx(16页珍藏版)》请在冰豆网上搜索。

程序设计与问题求解Word下载.docx

cout<

<

Worker1.name<

"

"

Worker1.age<

Worker1.pay<

endl;

Worker2=Worker1;

Worker2.name<

Worker2.age<

Worker2.pay<

}

输出结果是:

liouting 

38 

493

2.构造函数与析构函数

#include<

classTAdd

{

private:

intx,y;

public:

TAdd(inta,intb):

x(a),y(b)

{

cout<

调用构造函数1."

<

endl;

}

TAdd(TAdd&

p)

x=p.x;

y=p.y;

调用构造函数2."

~TAdd()

调用析构函数."

intadd(){returnx+y;

voidmain()

TAddp1(2,3);

TAddp2=p1;

p2.add()<

调用构造函数1.

调用构造函数2.

5

调用析构函数.

3.虚函数

classA

virtualvoidf(){cout<

A:

:

f()executing\n"

classB:

publicA

voidf(){cout<

B:

voidmain()

Aa;

Bb;

b.f();

A*p=&

a;

p->

f();

p=&

b;

a=b;

a.f();

f()executing

 

4.模板

template<

classType1,classType2>

classmyclass

Type1i;

Type2j;

myclass(Type1a,Type2b)

i=a;

j=b;

voidshow()

cout<

i<

j<

myclass<

int,int>

ob1(1,3);

int,double>

ob2(10,0.23);

char,char*>

ob3('

A'

"

Thisisatest"

);

ob1.show();

ob2.show();

ob3.show();

13

100.23

AThisisatest

5.继承

#include<

classA

{

intx,y;

A(intx1=0,inty1=0):

x(x1),y(y1)

x<

'

'

y<

\n'

~A(){

Ades!

\n"

classB

inti;

B(intii)

i=ii;

cout<

Bcon!

~B(){cout<

Bdes!

classC:

publicA,publicB

C(intcx,intcy,intbi):

A(cx,cy),B(bi)

{

AwithBcon!

~C(){cout<

AwithBdes!

intmain()

Ccm(3,4,5);

34

二、程序填空(每题10分,2题共20分)

1.词频统计:

输入一系列英文单词,单词之间用空格隔开,用“xyz”表示结束输入,统计输入过哪些单词以及各单词出现的次数,统计时区分大小写字母,最后按单词的字典顺序输出单词和出现次数的对照表。

cstring>

//词条类型

structWordList

charword[50];

intfreq;

//词典排序函数

voidSort(WordListlist[],intcount)

for(inti=0;

i<

count;

i=i+1)

for(intj=count-1;

j>

i;

j=j-1)

if(strcmp(list[j-1].word,list[j].word)>

0)

{

WordListtmp;

tmp=list[j-1];

list[j-1]=list[j];

list[j]=tmp;

}

//主函数:

进行词频统计

WordListlist[5000];

inti,num=0;

chartemp[50];

cout<

请输入一系列英语单词,以xyz表示输入结束"

cin>

>

temp;

while(

(1))

for(i=0;

num;

i++)//扫描当前词典

{

if(strcmp(list[i].word,temp)==0)//若词典中存在该词条,词频加1

(2)

break;

}

if(i>

=num)//若词典中无该词条,添加该词

strcpy(list[i].word,temp);

(3)

(4)

(5)//继续输入单词

Sort(list,num);

//对词典进行排序

//输出词典

词频统计结果如下:

for(i=0;

i++)

list[i].word<

\t"

list[i].freq<

return0;

答案:

(1)strcmp(temp,"

xyz"

)!

=0

(2)list[i].freq++;

(3)list[i].freq=1;

(4)num++;

(5)cin>

2.带头结点链表类的定义如下:

cstdio>

template<

classdatatype>

//模板声明

classNODE//结点类定义

{public:

datatypedata;

//数据域

NODE<

datatype>

*next;

//指针域

classList//单链表类定义

{private:

NODE<

*head;

//链表头指针

public:

List();

//构造函数创建头结点

intlength();

//求表长函数

boolisempty(){returnhead->

next==NULL?

true:

false;

}//判空链表函数

boolinsert_data(datatypedata,inti);

//插入元素函数

……………

~List();

//析构函数

boolList<

insert_data(datatypedata,inti)//定义插入函数

*current,*previous,*newnode;

intj=1;

if((i>

length()+1)||(i<

0))//判插入位置的合法性

{cout<

插入位置不正确,不能插入!

returnfalse;

newnode=newNODE<

//申请新结点空间

if(newnode==NULL)//判表满否

内存无空闲空间,不能插入!

newnode->

data=data;

next=NULL;

previous=head;

(6)

while((7))//寻找第i个元素

{previous=current;

(8)//指向下一个结点

j++;

};

//链入新结点

(9)

(10)

returntrue;

(6)current=head->

next;

(7)current!

=NULL&

&

i

(8)current=current->

(9)newnode->

next=current;

(10)previous->

next=newnode;

三、程序设计(每题15分,2题共30分)

1.设计一个时间(Time)类,设计多个重载的构造函数,可以设置时间,时间加运算(时间加多少秒),要求重载+来实现时间加运算,按24小时制格式:

时:

分:

秒输出时间。

并在主程序中测试所有的操作。

(15分)

#pragmaonce

/*时间类*/

classTime

intsecond,minute,hour;

intGetSecond();

//计算总秒数

voidSetTime(intss);

//根据秒数算出second,minute,hour

Time();

Time(inthh,intmm,intss);

voidSetTime(inthh,intmm,intss);

constTime&

operator+(intss);

booloperator<

(Time&

voidPrint_hms();

//HH:

mm:

ss

#include"

Time.h"

Time:

Time()

second=0;

minute=0;

hour=0;

Time(inthh,intmm,intss)

SetTime(hh,mm,ss);

//计算总秒数

intTime:

GetSecond()

returnsecond+60*minute+3600*hour;

voidTime:

SetTime(intss)

second=ss%60;

ss=ss/60;

minute=ss%60;

hour=ss%24;

SetTime(inthh,intmm,intss)

second=(ss>

=0&

ss<

60)?

ss:

0;

minute=(mm>

mm<

hour=(hh>

hh<

24)?

hh:

constTime&

Time:

operator+(intss)

SetTime(GetSecond()+ss);

return*this;

boolTime:

operator<

time)

if(GetSecond()-time.GetSecond()<

else

returnfalse;

Print_hms()

hour<

minute<

second<

Timet1,t2;

inthour,minute,second;

请输入时间(时分秒):

hour>

minute>

second;

t1.SetTime(hour,minute,second);

t1.Print_hms();

t2.SetTime(12,0,0);

if(t1<

t2)

endl<

是上午的时间"

是下午的时间"

评分说明:

数据成员定义2分,函数定义5分,函数实现5分,主程序测试3分

2.编写一个雇员和雇主数据输入和显示的程序。

雇员数据有编号(no)、姓名(name)和工资(salary),雇主数据有编号(no)、姓名(name)和职位(post)。

要求将编号、姓名输入和显示设计成一个类person,并作为雇员数据类employee和雇主数据类employer的基类,并编写主程序进行执行,输出信息时体现运行多态性,并给出执行结果。

classperson

intno;

charname[10];

virtualvoidinput()

thenois"

no;

thenameis"

name;

virtualvoidoutput()

no<

name<

classemployee:

publicperson

intsalary;

voidinput()

person:

input();

theemployeesalaryis"

salary;

voidoutput()

output();

salary<

classemployer:

charpost[10];

theemployerpostis"

post;

post<

employeeee;

employerer;

person*p=&

ee;

p->

p=&

er;

输出结果:

thenois01

thenameiswanghong

theemployeesalaryis1000

thenois1

thenameiswanghong

thenois02

thenameislizhe

theemplorerpostisboss

thenois2

theemployerpostisboss

头文件2分;

person类的两虚函数各2分;

employee类和employer类的公有继承各2分;

employee类和employer类对虚函数的重载各2分;

employee类和employer类对象赋给person类指针各2分;

输出结果2分。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

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

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