C++课程实习及答案全解.docx

上传人:b****0 文档编号:9590167 上传时间:2023-05-20 格式:DOCX 页数:100 大小:36.92KB
下载 相关 举报
C++课程实习及答案全解.docx_第1页
第1页 / 共100页
C++课程实习及答案全解.docx_第2页
第2页 / 共100页
C++课程实习及答案全解.docx_第3页
第3页 / 共100页
C++课程实习及答案全解.docx_第4页
第4页 / 共100页
C++课程实习及答案全解.docx_第5页
第5页 / 共100页
C++课程实习及答案全解.docx_第6页
第6页 / 共100页
C++课程实习及答案全解.docx_第7页
第7页 / 共100页
C++课程实习及答案全解.docx_第8页
第8页 / 共100页
C++课程实习及答案全解.docx_第9页
第9页 / 共100页
C++课程实习及答案全解.docx_第10页
第10页 / 共100页
C++课程实习及答案全解.docx_第11页
第11页 / 共100页
C++课程实习及答案全解.docx_第12页
第12页 / 共100页
C++课程实习及答案全解.docx_第13页
第13页 / 共100页
C++课程实习及答案全解.docx_第14页
第14页 / 共100页
C++课程实习及答案全解.docx_第15页
第15页 / 共100页
C++课程实习及答案全解.docx_第16页
第16页 / 共100页
C++课程实习及答案全解.docx_第17页
第17页 / 共100页
C++课程实习及答案全解.docx_第18页
第18页 / 共100页
C++课程实习及答案全解.docx_第19页
第19页 / 共100页
C++课程实习及答案全解.docx_第20页
第20页 / 共100页
亲,该文档总共100页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C++课程实习及答案全解.docx

《C++课程实习及答案全解.docx》由会员分享,可在线阅读,更多相关《C++课程实习及答案全解.docx(100页珍藏版)》请在冰点文库上搜索。

C++课程实习及答案全解.docx

C++课程实习及答案全解

实验题目(共4题,第1题)

标题:

1、字符串输入输出

时限:

1000ms

存限制:

10000K

总时限:

3000ms

描述:

编写一个简单的控制台应用程序,先输入,如“John”,再输出问候语,如“Hello,John!

”。

输入:

John

输出:

Hello,John!

输入样例:

John

输出样例:

Hello,John!

提示:

1、使用string类定义字符串对象,需包含头文件

2、使用cin和提取符>>从键盘输入数据,使用cout和插入符<<输出结果到屏幕,需包含头文件

3、注意使用名称空间std。

来源:

#include

#include

#include

usingnamespacestd;

intmain()

{

chars[10];

gets(s);

cout<<"Hello,"<

"<

return0;

}

示例代码----------------------------------------------

#include

#include

usingnamespacestd;

intmain()

{

stringszName;

cin>>szName;

cout<<"Hello,"<

"<

return0;

}

-----------------------------------------------------

实验题目(共4题,第2题)

标题:

2、求3个数的平均值

时限:

1000ms

存限制:

10000K

总时限:

3000ms

描述:

从键盘上输入3个浮点数,求这3个数的平均值。

输入:

3个浮点数

输出:

3个数的平均值

输入样例:

1.51.61.3

输出样例:

1.46667

提示:

1、用usingnamespacestd;明确名字空间

2、用cin对象,采用>>运算符输入数据

3、用cout对象,采用<<运算符输出数据

来源:

#include

usingnamespacestd;

intmain()

{

floata,b,c,aver=0;

cin>>a;

cin>>b;

cin>>c;

aver=(a+b+c)/3.0;

cout<

return0;

}

示例代码-----------------------------------

#include

usingnamespacestd;

intmain()

{

floatx1,x2,x3;

cin>>x1>>x2>>x3;

cout<<(x1+x2+x3)/3<

return0;

}

---------------------------------------------

实验题目(共4题,第3题)

标题:

3、求鞍点

时限:

1000ms

存限制:

10000K

总时限:

3000ms

描述:

输入一个二维矩阵,找出其中所有鞍点。

如果矩阵有鞍点,则输出鞍点的信息:

行号、列号、值;

如果没有鞍点,则输出“Notfound!

”。

所谓“鞍点”,是指满足以下条件的矩阵中的一个数:

在它所在的行上最小、所在列上最大。

该题中假设矩阵中任意两个数互不相等。

输入:

输入数据有多行:

第一行是矩阵的行数m和列数n

从第二行起共包含m行,每行包含n个数,为矩阵的一行数据

输出:

如果矩阵有鞍点,输出鞍点的信息,包括:

所在行、所在列、值

如果没有鞍点,输出Notfound!

输入样例:

34

11235647

12456690

16773418

输出样例:

2016

提示:

1、要求用动态存分配来完成,可用new和delete实现;

2、屏幕输出只有2016(加回车换行),不能有其它信息。

来源:

#include

usingnamespacestd;

voidan(inth,intl)

{

intma,mi,a=0,b=0;

int**p;

p=newint*[h];

for(inti=0;i

{

p[i]=newint[l];

}

for(inti=0;i

{

for(intj=0;j

{

cin>>p[i][j];

}

}

intflag=0;

for(inti=0;i

{

mi=p[i][0];

a=i,b=0;

for(intj=0;j

{

if(p[i][j]

{

mi=p[i][j];

a=i,b=j;

}

}

for(intt=i+1;t

{

ma=mi;

if(ma

}

if(ma==mi)

{

flag=1,cout<

}

}

if(flag==0)

cout<<"Notfound!

"<

delete[]p;

}

intmain()

{

inth,l;

cin>>h>>l;

an(h,l);

return0;

}

示例代码-----------------------------------------------------------------------------------------------------------

#include

usingnamespacestd;

intmain()

{

int**mat;

int*matRow,*matCol;

intnMaxRow=0,nMaxCol=0;

boolbFind=false;

intnTargetRow=0,nTargetCol=0,nSaddlePoint=0;

inti,j;

//cout<<"Pleaseinputthenumberofrowsandthenumberofcolumns:

"<

cin>>nMaxRow>>nMaxCol;

//Allocatememoriesforthetwodimensionalmatrices

mat=newint*[nMaxRow];

for(i=0;i

mat[i]=newint[nMaxCol];

matRow=newint[nMaxRow];//Bufferstosavetheminimumelementineachrow

matCol=newint[nMaxCol];//Bufferstosavethemaximumelementineachcolumn

//Inputtheelements

//cout<<"Pleaseinputtheelements:

"<

for(i=0;i

for(j=0;j

cin>>mat[i][j];

//Findtheminimumelementineachrow

for(i=0;i

{

intnMin=mat[i][0];

for(j=1;j

{

if(mat[i][j]

}

matRow[i]=nMin;

}

//Findthemaximumelementineachcolumn

for(j=0;j

{

intnMax=mat[0][j];

for(i=1;i

{

if(mat[i][j]>nMax)nMax=mat[i][j];

}

matCol[j]=nMax;

}

//Findthesaddlepoint

for(i=0;i

bFind;i++)

{

for(j=0;j

{

if(mat[i][j]==matRow[i]&&mat[i][j]==matCol[j])

{

nTargetRow=i;

nTargetCol=j;

nSaddlePoint=mat[i][j];

bFind=true;

break;

}

}

}

//Outputthesearchedrowandcolumnandthecorrespondingsaddlepoint

if(!

bFind)

cout<<"Notfound!

"<

else

cout<

//Releasememories

delete[]matCol;

delete[]matRow;

for(i=0;i

delete[]mat[i];

delete[]mat;

return0;

}

---------------------------------------------------------------------------------------------------------------

实验题目(共4题,第4题)

标题:

4、链表操作

时限:

1000ms

存限制:

10000K

总时限:

3000ms

描述:

建立一个链表,每个节点包括学生的学号、、性别、年龄。

先输入5个学生的数据,再输入一个年龄,如果链表中有年龄等于此年龄的记录,则删除所有年龄等于此年龄的记录,否则在链表的最后增加一个新节点,学号为180为"aaa",性别为"male"。

输入:

创建链表时输入5个职工的职工号和工资,学号为大于100且小于200的整数,为长度小于20的字符串,性别为长度小于10的字符串,年龄为大于等于0且小于200的整数。

输出:

按顺序输出链表中的所有数据,每个数据占一行。

输入样例:

101zhangsanmale30

103lisifemale18

105wangwumale25

107maliumale28

109niuqifemale22

28

输出样例:

101

zhangsan

male

30

103

lisi

female

18

105

wangwu

male

25

109

niuqi

female

22

提示:

要求用动态存分配实现,注意new和delete的使用。

来源:

#include

usingnamespacestd;

classnode

{

private:

int_num,_age;

string_name,_sexy;

public:

node(intnum,intage,stringname,stringsexy)

{

_num=num;

_age=age;

_name=name;

_sexy=sexy;

}

voiddisplay(void)

{

cout<<_num<

cout<<_name<

cout<<_sexy<

cout<<_age<

}

intsame(intage)

{

if(age==_age)return1;

return0;

}

};

classLinkedList

{

public:

node*Node;

LinkedList*next;

LinkedList(intnum,intage,stringname,stringsexy)

{

Node=newnode(num,age,name,sexy);

next=NULL;

}

};

intmain(void)

{

intnum,age;

stringname,sexy;

cin>>num>>name>>sexy>>age;

LinkedListhead(num,age,name,sexy);

LinkedList*p=&head;

for(inti=1;i<=4;i++)

{

cin>>num>>name>>sexy>>age;

p->next=newLinkedList(num,age,name,sexy);

p=p->next;

}

cin>>age;

LinkedList*pp=&head;

intflag=0;

while(pp!

=NULL)

{

if(pp->Node->same(age))flag++;

elsepp->Node->display();

pp=pp->next;

}

if(!

flag)

{

noden(180,age,"aaa","male");

n.display();

}

return0;

}

示例代码-------------------------------------------------------------------------------------------------------

#include

#include

usingnamespacestd;

constintMAX_STR_LEN=32;

structStuNode

{

intID;

charname[MAX_STR_LEN];

chargender[MAX_STR_LEN];

intage;

structStuNode*next;

};

typedefstructStuNode*StuLink;

voidCreateHeadNode(StuLink&pHead,StuLink&pTail)

{

pHead=newStuNode;

if(pHead==NULL)return;

pHead->next=NULL;

pTail=pHead;

}

voidAddStudent(StuLink&pTail,StuNodedata)

{

StuLinkp;

p=newStuNode;

if(p==NULL)return;

p->ID=data.ID;

strcpy(p->name,data.name);

strcpy(p->gender,data.gender);

p->age=data.age;

p->next=NULL;

//Makethecurrentnodepointerpointtothelastaddeddata

pTail->next=p;

pTail=p;

}

boolRemoveStudent(StuLink&pHead,StuLink&pTail,intage)

{

boolbFind=false;

StuLinkpCurr,pPre;

pPre=pHead;

//Skiptothesecondnodebecausethefirstnodeistheheadnodewithoutdata

pCurr=pHead->next;

while(pCurr!

=NULL)

{

if(pCurr->age==age)

{

pPre->next=pCurr->next;

deletepCurr;

//Recoverthecurrentpointertofindthenextnodethathasthesamekey

pCurr=pPre->next;

bFind=true;

}

else

{

//Movetothenextnode

pPre=pCurr;

pCurr=pCurr->next;

}

}

//Recoverthetailpointerincasethelastnodeisdeleted

if(bFind&&pPre->next==NULL)pTail=pPre;

returnbFind;

}

voidOutputAllStudentsInfo(constStuLinkpHead)

{

StuLinkp;

//Skiptothesecondnodebecausethefirstnodeistheheadnodewithoutdata

p=pHead->next;

if(p==NULL)return;

while(p!

=NULL)

{

cout<ID<

cout<name<

cout<gender<

cout<age<

p=p->next;

}

}

voidInputStudentInfo(StuNode&Emp)

{

cin>>Emp.ID>>Emp.name>>Emp.gender>>Emp.age;

while(Emp.ID>=200||Emp.ID<100||

strlen(Emp.name)>=20||

strlen(Emp.gender)>=10||

Emp.age<0||Emp.age>=200)

{

cin>>Emp.ID>>Emp.name>>Emp.gender>>Emp.age;

}

}

intmain()

{

StuLinkEmpInstHead,EmpInstTail;

StuNodeEmpTemp;

inti,age;

//Createaheadnodeforthelinkedlist

CreateHeadNode(EmpInstHead,EmpInstTail);

for(i=0;i<5;i++)

{

InputStudentInfo(EmpTemp);

AddStudent(EmpInstTail,EmpTemp);

}

cin>>age;

//Removeaheadnodefromthelinkedlist

if(!

RemoveStudent(EmpInstHead,EmpInstTail,age))

{

EmpTemp.age=age;

EmpTemp.ID=180;strcpy(EmpTemp.name,"aaa");strcpy(EmpTemp.gender,"male");EmpTemp.next=NULL;

AddStudent(EmpInstTail,EmpTemp);

}

OutputAllStudentsInfo(EmpInstHead);

return0;

}

-------------------------------------------------------------------------------------------------------------------------

实验题目(共4题,第1题)

标题:

1.函数重载

时限:

1000ms

存限制:

10000K

总时限:

3000ms

描述:

设计一菜单程序,利用函数重载实现员工月工资的计算,计算方法如下:

(1)管理人员的月工资=月薪-缺勤天数×月薪÷22;

(2)销售人员的月工资=底薪+销售金额×提成比例;

(3)计件工人的月工资=产品件数×每件报酬;

(4)计时工人的月工资=工作小时×小时报酬;

输入:

职工类别及相关信息。

职工类别:

1表示管理人员;2表示销售人员;3表示计件工人;4表示计时工人;其余字符表示退出。

相关信息:

若为管理人员,则输入月薪和缺勤天数;若为销售人员,则输入底薪、销售金额和提成比例;若为计件工人,则输入产品件数和每件报酬;若为计时工人,则输入工作小时和小时报酬。

输出:

员工月工资。

输入样例:

1〈--职工类别

5000.01〈--月薪和缺勤天数

输出样例:

4772.73

提示:

1.计算管理人员、销售人员、计件工人、计时工人的月工资的函数原型可以分别设计如下:

doublegetEarning(doublesalary,intabsenceDays);

doublegetEarning(doublebaseSalary,doublesalesSum,doublerate);

doublegetEarning(intworkPieces,doublewagePerPiece);

doublegetEarning(doublehours,doublewagePerHour);

2.菜单程序设计如下:

intmain()

{

...

cout<<"Pleaseselect..."<

cout<<"1:

Manager."<

cout<<"2:

SalesMan."<

cout<<"3:

PiecesWorker."<

cout<<"4:

Hour-Worker."<

cout

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

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2