程序设计与问题求解.docx

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

程序设计与问题求解.docx

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

程序设计与问题求解.docx

程序设计与问题求解

桂林电子科技大学试卷

2010-2011学年第2学期A卷

课号课程名称程序设计与问题求解2适用班级(专业)

考试时间120分钟座位号学号姓名

题号

成绩

满分

50

20

30

得分

评卷人

一、阅读程序,写出程序运行结果(每题10分,5题共50分)

1.结构体

#include

#include

usingnamespacestd;

structWorker{

charname[15];//姓名

intage;//年龄

floatpay;//工资

};

voidmain(){

WorkerWorker1,Worker2;

char*t="liouting";

intd=38;

floatf=493;

strcpy(Worker1.name,t);

Worker1.age=d;

Worker1.pay=f;

cout<

Worker2=Worker1;

cout<

}

输出结果是:

liouting 38 493

liouting 38 493

2.构造函数与析构函数

#include

usingnamespacestd;

classTAdd

{

private:

intx,y;

public:

TAdd(inta,intb):

x(a),y(b)

{

cout<<"调用构造函数1."<

}

TAdd(TAdd&p)

{

x=p.x;

y=p.y;

cout<<"调用构造函数2."<

}

~TAdd()

{

cout<<"调用析构函数."<

}

intadd(){returnx+y;}

};

voidmain()

{

TAddp1(2,3);

TAddp2=p1;

cout<

}

输出结果是:

调用构造函数1.

调用构造函数2.

5

调用析构函数.

调用析构函数.

3.虚函数

#include

usingnamespacestd;

classA

{

public:

virtualvoidf(){cout<<"A:

:

f()executing\n";}

};

classB:

publicA

{

public:

voidf(){cout<<"B:

:

f()executing\n";}

};

voidmain()

{

Aa;

Bb;

b.f();

A*p=&a;

p->f();

p=&b;

p->f();

a=b;

a.f();

}

输出结果是:

B:

:

f()executing

A:

:

f()executing

B:

:

f()executing

A:

:

f()executing

 

4.模板

#include

usingnamespacestd;

template

classmyclass

{

private:

Type1i;

Type2j;

public:

myclass(Type1a,Type2b)

{

i=a;j=b;

}

voidshow()

{

cout<

}

};

voidmain()

{

myclassob1(1,3);

myclassob2(10,0.23);

myclassob3('A',"Thisisatest");

ob1.show();

ob2.show();

ob3.show();

}

输出结果是:

13

100.23

AThisisatest

5.继承

#include

usingnamespacestd;

classA

{

intx,y;

public:

A(intx1=0,inty1=0):

x(x1),y(y1)

{

cout<<"A:

"<

}

~A(){

cout<<"Ades!

\n";

}

};

classB

{

inti;

public:

B(intii)

{

i=ii;

cout<<"Bcon!

\n";

}

~B(){cout<<"Bdes!

\n";}

};

classC:

publicA,publicB

{

public:

C(intcx,intcy,intbi):

A(cx,cy),B(bi)

{

cout<<"AwithBcon!

\n";

}

~C(){cout<<"AwithBdes!

\n";}

};

intmain()

{

Ccm(3,4,5);

}

输出结果是:

A:

34

Bcon!

AwithBcon!

AwithBdes!

Bdes!

Ades!

 

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

1.词频统计:

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

#include

#include

usingnamespacestd;

//词条类型

structWordList

{

charword[50];

intfreq;

};

//词典排序函数

voidSort(WordListlist[],intcount)

{

for(inti=0;i

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;

}

}

//主函数:

进行词频统计

intmain()

{

WordListlist[5000];

inti,num=0;

chartemp[50];

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

cin>>temp;

while(

(1))

{

for(i=0;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);//对词典进行排序

//输出词典

cout<<"词频统计结果如下:

"<

for(i=0;i

cout<

return0;

}

答案:

(1)strcmp(temp,"xyz")!

=0

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

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

(4)num++;

(5)cin>>temp;

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

#include

#include

usingnamespacestd;

template//模板声明

classNODE//结点类定义

{public:

datatypedata;//数据域

NODE*next;//指针域

};

template

classList//单链表类定义

{private:

NODE*head;//链表头指针

public:

List();//构造函数创建头结点

intlength();//求表长函数

boolisempty(){returnhead->next==NULL?

true:

false;}//判空链表函数

boolinsert_data(datatypedata,inti);//插入元素函数

……………

~List();//析构函数

};

template

boolList:

:

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

{

NODE*current,*previous,*newnode;

intj=1;

if((i>length()+1)||(i<0))//判插入位置的合法性

{cout<<"插入位置不正确,不能插入!

\n";

returnfalse;

}

newnode=newNODE;//申请新结点空间

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

{cout<<"内存无空闲空间,不能插入!

\n";

returnfalse;

}

newnode->data=data;

newnode->next=NULL;

previous=head;

(6)

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

{previous=current;

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

j++;

};

//链入新结点

(9)

(10)

returntrue;

}

答案:

(6)current=head->next;

(7)current!

=NULL&&j

(8)current=current->next;

(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

public:

Time();

Time(inthh,intmm,intss);

voidSetTime(inthh,intmm,intss);

constTime&operator+(intss);

booloperator<(Time&);

voidPrint_hms();//HH:

mm:

ss

};

#include"Time.h"

#include

usingnamespacestd;

Time:

:

Time()

{

second=0;

minute=0;

hour=0;

}

Time:

:

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;

ss=ss/60;

hour=ss%24;

}

voidTime:

:

SetTime(inthh,intmm,intss)

{

second=(ss>=0&&ss<60)?

ss:

0;

minute=(mm>=0&&mm<60)?

mm:

0;

hour=(hh>=0&&hh<24)?

hh:

0;

}

constTime&Time:

:

operator+(intss)

{

SetTime(GetSecond()+ss);

return*this;

}

boolTime:

:

operator<(Time&time)

{

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

returntrue;

else

returnfalse;

}

voidTime:

:

Print_hms()

{

cout<

"<

"<

}

#include"Time.h"

#include

usingnamespacestd;

intmain()

{

Timet1,t2;

inthour,minute,second;

cout<<"请输入时间(时分秒):

"<

cin>>hour>>minute>>second;

t1.SetTime(hour,minute,second);

t1.Print_hms();

t2.SetTime(12,0,0);

if(t1

cout<

else

cout<

return0;

}

评分说明:

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

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

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

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

(15分)

答案:

#include

usingnamespacestd;

classperson

{

intno;

charname[10];

public:

virtualvoidinput()

{

cout<<"thenois";

cin>>no;

cout<<"thenameis";

cin>>name;

}

virtualvoidoutput()

{

cout<<"thenois"<

cout<<"thenameis"<

}

};

classemployee:

publicperson

{

intsalary;

public:

voidinput()

{

person:

:

input();

cout<<"theemployeesalaryis";

cin>>salary;

}

voidoutput()

{

person:

:

output();

cout<<"theemployeesalaryis"<

}

};

classemployer:

publicperson

{

charpost[10];

public:

voidinput()

{

person:

:

input();

cout<<"theemployerpostis";

cin>>post;

}

voidoutput()

{

person:

:

output();

cout<<"theemployerpostis"<

}

};

voidmain()

{

employeeee;

employerer;

person*p=ⅇ

p->input();

p->output();

p=&er;

p->input();

p->output();

}

输出结果:

thenois01

thenameiswanghong

theemployeesalaryis1000

thenois1

thenameiswanghong

theemployeesalaryis1000

thenois02

thenameislizhe

theemplorerpostisboss

thenois2

thenameislizhe

theemployerpostisboss

评分说明:

头文件2分;person类的两虚函数各2分;employee类和employer类的公有继承各2分;employee类和employer类对虚函数的重载各2分;employee类和employer类对象赋给person类指针各2分;输出结果2分。

 

 

欢迎您的下载,

资料仅供参考!

 

致力为企业和个人提供合同协议,策划案计划书,学习资料等等

打造全网一站式需求

 

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

当前位置:首页 > 求职职场 > 简历

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

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