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

上传人:ca****in 文档编号:100340573 上传时间:2022-06-02 格式:DOC 页数:71 大小:315KB
收藏 版权申诉 举报 下载
C++课程实习及答案全解_第1页
第1页 / 共71页
C++课程实习及答案全解_第2页
第2页 / 共71页
C++课程实习及答案全解_第3页
第3页 / 共71页
资源描述:

《C++课程实习及答案全解》由会员分享,可在线阅读,更多相关《C++课程实习及答案全解(71页珍藏版)》请在装配图网上搜索。

1、实验题目(共4题, 第1题)标题: 1、字符串输入输出 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 编写一个简单的控制台应用程序,先输入姓名,如“John”,再输出问候语,如“Hello, John!”。 输入: John 输出: Hello, John! 输入样例: John 输出样例: Hello,John! 提示: 1、使用string类定义字符串对象,需包含头文件;2、使用cin和提取符从键盘输入数据,使用cout和插入符输出结果到屏幕,需包含头文件;3、注意使用名称空间std。 来源:#include #include #include u

2、sing namespace std;int main() char s10; gets(s); cout Hello, s ! endl; return 0; 示例代码-#include #include using namespace std;int main() string szName; cin szName; cout Hello, szName ! 运算符输入数据3、用cout对象,采用运算符输出数据 来源:#include using namespace std;int main() float a,b,c,aver=0; cin a; cin b; cin c; aver=(

3、a+b+c)/3.0; cout aver endl; return 0; 示例代码-#include using namespace std;int main() float x1, x2, x3; cinx1x2x3; cout(x1+x2+x3)/3endl; return 0;-实验题目(共4题, 第3题)标题: 3、求鞍点 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 输入一个二维矩阵,找出其中所有鞍点。如果矩阵有鞍点,则输出鞍点的信息:行号、列号、值;如果没有鞍点,则输出“Not found!”。所谓“鞍点”,是指满足以下条件的矩阵中的一个

4、数:在它所在的行上最小、所在列上最大。 该题中假设矩阵中任意两个数互不相等。 输入: 输入数据有多行:第一行是矩阵的行数m和列数n 从第二行起共包含m行,每行包含n个数,为矩阵的一行数据 输出: 如果矩阵有鞍点,输出鞍点的信息,包括:所在行、所在列、值 如果没有鞍点,输出Not found! 输入样例: 3 411 23 56 4712 45 66 9016 77 34 18 输出样例: 2016 提示: 1、要求用动态内存分配来完成,可用new和delete实现;2、屏幕输出只有2 0 16(加回车换行),不能有其它信息。 来源:#include using namespace std;vo

5、id an(int h,int l) int ma,mi,a=0,b=0; int *p; p=new int* h; for(int i=0; ih; i+) pi=new intl; for(int i=0; ih; i+) for(int j=0; jpij; int flag=0; for(int i=0; ih&flag=0; i+) mi=pi0; a=i,b=0; for(int j=0; jl; j+) if(pijmi) mi=pij; a=i,b=j; for(int t=i+1; th; t+) ma=mi; if(maptb)ma=ptb; if(ma=mi) flag

6、=1,couta b maendl; if(flag=0) coutNot found!hl; an(h,l); return 0;示例代码-#include using namespace std;int main() int *mat; int *matRow, *matCol; int nMaxRow=0, nMaxCol=0; bool bFind=false; int nTargetRow=0, nTargetCol=0, nSaddlePoint=0; int i, j; /cout Please input the number of rows and the number of

7、 columns: nMaxRow nMaxCol; /Allocate memories for the two dimensional matrices mat = new int *nMaxRow; for (i=0; inMaxRow; i+) mati = new intnMaxCol; matRow = new intnMaxRow; /Buffers to save the minimum element in each row matCol = new intnMaxCol; /Buffers to save the maximum element in each column

8、 /Input the elements /cout Please input the elements: endl; for (i=0; inMaxRow; i+) for (j=0; j matij; /Find the minimum element in each row for (i=0; inMaxRow; i+) int nMin=mati0; for (j=1; jnMaxCol; j+) if(matijnMin) nMin = matij; matRowi = nMin; /Find the maximum element in each column for (j=0;

9、jnMaxCol; j+) int nMax=mat0j; for (i=1; inMax) nMax = matij; matColj = nMax; /Find the saddle point for (i=0; inMaxRow & !bFind; i+) for (j=0; jnMaxCol; j+) if(matij=matRowi & matij=matColj) nTargetRow = i; nTargetCol = j; nSaddlePoint = matij; bFind = true; break; /Output the searched row and colum

10、n and the corresponding saddle point if(!bFind) cout Not found! endl; else cout nTargetRow nTargetCol nSaddlePoint endl; /Release memories delete matCol; delete matRow; for (i=0; inMaxRow; i+) delete mati; delete mat; return 0; -实验题目(共4题, 第4题)标题: 4、链表操作 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 建立

11、一个链表,每个节点包括学生的学号、姓名、性别、年龄。先输入5个学生的数据,再输入一个年龄,如果链表中有年龄等于此年龄的记录,则删除所有年龄等于此年龄的记录,否则在链表的最后增加一个新节点,学号为180姓名为aaa,性别为male。 输入: 创建链表时输入5个职工的职工号和工资,学号为大于100且小于200的整数,姓名为长度小于20的字符串,性别为长度小于10的字符串,年龄为大于等于0且小于200的整数。 输出: 按顺序输出链表中的所有数据,每个数据占一行。 输入样例: 101 zhangsan male 30103 lisi female 18105 wangwu male 25107 mal

12、iu male 28109 niuqi female 2228 输出样例: 101zhangsanmale30103lisifemale18105wangwumale25109niuqifemale22 提示: 要求用动态内存分配实现,注意new和delete的使用。 来源:#include using namespace std;class nodeprivate: int _num,_age; string _name,_sexy;public: node(int num,int age,string name,string sexy) _num=num; _age=age; _name=

13、name; _sexy=sexy; void display(void) cout_numendl; cout_nameendl; cout_sexyendl; cout_agenumnamesexyage; LinkedList head(num,age,name,sexy); LinkedList *p=&head; for(int i=1; inumnamesexyage; p-next=new LinkedList(num,age,name,sexy); p=p-next; cinage; LinkedList *pp=&head; int flag=0; while(pp!=NULL

14、) if(pp-Node-same(age)flag+; else pp-Node-display(); pp=pp-next; if(!flag) node n(180,age,aaa,male); n.display(); return 0;示例代码-#include #include using namespace std;const int MAX_STR_LEN = 32;struct StuNode int ID; char nameMAX_STR_LEN; char genderMAX_STR_LEN; int age; struct StuNode *next;typedef

15、struct StuNode *StuLink; void CreateHeadNode(StuLink &pHead, StuLink &pTail) pHead = new StuNode; if (pHead = NULL) return; pHead-next = NULL; pTail = pHead; void AddStudent(StuLink &pTail, StuNode data) StuLink p; p = new StuNode; if (p = NULL) return; p-ID = data.ID; strcpy(p-name, data.name); str

16、cpy(p-gender, data.gender); p-age = data.age; p-next = NULL; /Make the current node pointer point to the last added data pTail-next = p; pTail = p; bool RemoveStudent(StuLink &pHead, StuLink &pTail, int age) bool bFind=false; StuLink pCurr, pPre; pPre = pHead; /Skip to the second node because the fi

17、rst node is the head node without data pCurr = pHead-next; while (pCurr != NULL) if (pCurr-age = age) pPre-next = pCurr-next; delete pCurr; /Recover the current pointer to find the next node that has the same key pCurr = pPre-next; bFind=true; else /Move to the next node pPre = pCurr; pCurr = pCurr-

18、next; /Recover the tail pointer in case the last node is deleted if(bFind & pPre-next=NULL) pTail = pPre; return bFind; void OutputAllStudentsInfo(const StuLink pHead) StuLink p; /Skip to the second node because the first node is the head node without data p = pHead-next; if (p = NULL) return; while

19、 (p != NULL) cout ID endl; cout name endl; cout gender endl; cout age next; void InputStudentInfo(StuNode &Emp) cin Emp.ID Emp.name Emp.gender Emp.age; while ( Emp.ID=200 | Emp.ID=20 | strlen(Emp.gender)=10 | Emp.age=200) cin Emp.ID Emp.name Emp.gender Emp.age; int main() StuLink EmpInstHead, EmpIns

20、tTail; StuNode EmpTemp; int i, age; /Create a head node for the linked list CreateHeadNode(EmpInstHead, EmpInstTail); for (i=0; i age; /Remove a head node from the linked list if(!RemoveStudent(EmpInstHead, EmpInstTail, age) EmpTemp.age = age; EmpTemp.ID=180; strcpy(EmpTemp.name, aaa); strcpy(EmpTem

21、p.gender, male); EmpTemp.next=NULL; AddStudent(EmpInstTail, EmpTemp); OutputAllStudentsInfo(EmpInstHead); return 0;- 实验题目(共4题, 第1题)标题: 1. 函数重载 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 设计一菜单程序,利用函数重载实现员工月工资的计算,计算方法如下: (1)管理人员的月工资 月薪 缺勤天数 月薪 22; (2)销售人员的月工资 底薪 + 销售金额 提成比例; (3)计件工人的月工资 产品件数 每件报酬; (4

22、)计时工人的月工资 工作小时 小时报酬; 输入: 职工类别及相关信息。职工类别:1表示管理人员;2表示销售人员;3表示计件工人;4表示计时工人;其余字符表示退出。相关信息:若为管理人员,则输入月薪和缺勤天数;若为销售人员,则输入底薪、销售金额和提成比例;若为计件工人,则输入产品件数和每件报酬;若为计时工人,则输入工作小时和小时报酬。 输出: 员工月工资。 输入样例: 1 职工类别5000.0 1月薪和缺勤天数 输出样例: 4772.73 提示: 1. 计算管理人员、销售人员、计件工人、计时工人的月工资的函数原型可以分别设计如下: double getEarning(double salary,

23、 int absenceDays); double getEarning(double baseSalary, double salesSum, double rate); double getEarning(int workPieces, double wagePerPiece); double getEarning(double hours, double wagePerHour);2. 菜单程序设计如下: int main() . cout Please select. endl; cout 1: Manager. endl; cout 2: Sales Man. endl; cout

24、3: Pieces Worker. endl; cout 4: Hour-Worker. endl; cout Others: Quit sel; switch(sel) case 1: cin .; cout .; cout .; cout .; cout getEarning(.); break; default: break; return 0; #include using namespace std;double getEarning(double salary,int absenceDays) return salary-absenceDays*salary/22;double g

25、etEarning(double baseSlary,double salesSum,double rate) return baseSlary+salesSum*rate;double getEarning(int workPieces,double wagePerPiece) return workPieces*wagePerPiece;double getEarning(double hours,double wagePerHour) return hours*wagePerHour;int main() double salary,baseSalary,salesSum,rate,ho

26、urs,wagePerHour,wagePerPiece; int absenceDays,workPieces,sel; cout Please select. endl; cout 1: Manager. endl; cout 2: Sales Man. endl; cout 3: Pieces Worker. endl; cout 4: Hour-Worker. endl; cout Others: Quit sel; switch(sel) case 1: cin salaryabsenceDays; cout baseSalarysalesSumrate; cout workPiec

27、eswagePerPiece; cout hourswagePerHour; cout getEarning(hours,wagePerHour); break; default: break; return 0;示例代码-#includeusing namespace std;double getEarning(double salary ,int absenceDays) return (salary -salary*absenceDays/22);/ admindouble getEarning(double baseSalary ,double salesSum,double rate

28、) return (baseSalary + salesSum*rate);double getEarning(int workPieces,double wagePerPiece) return (workPieces*wagePerPiece);double getEarning(double hours ,double wagePerHour) return (hours*wagePerHour);int main() int kind = 0 ; cout Please select. endl; cout 1: Manager. endl; cout 2: Sales Man. en

29、dl; cout 3: Pieces Worker. endl; cout 4: Hour-Worker. endl; cout Others: Quit kind ; switch(kind) case 1: double salary ; int abDays; cinsalaryabDays; coutbasesalesSumrate; coutworkPieceswagePerPiece; couthourswagePerHour; coutgetEarning(hours,wagePerHour); break; default: break; / coutgetEarning(10

30、0.0,11);/ coutgetEarning(100.0,10.0,0.1);/ coutgetEarning(100,0.2);/ coutgetEarning(200,0.3); return 0 ;-来源: 实验题目(共4题, 第2题)标题: 2. 引用传递 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 设计一个函数,将两个浮点数传入,然后通过引用把其和、差、积传出。函数原型如下:void Math(float a,float b,float &sum,float &sub,float &pro); 输入: 输入两个浮点数 输出: 输出两个浮

31、点数的和、差、积。 输入样例: 6.5 2.3 输出样例: 8.8 4.2 14.95 提示: 来源:#include #include #include using namespace std;void Math(float a,float b) float sum,sub,pro; sum=a+b; sub=a-b; pro=a*b; cout sum sub pro a b ; Math(a,b); return 0; 示例代码-#includeusing namespace std;void Math(float a , float b ,float& sum,float &sub,f

32、loat&pro) sum = a+ b ; sub = a-b ; pro = a*b ;int main()float sum = 0 ;float sub = 0 ;float pro = 0 ;float a = 0 ;float b = 0 ; cinab; Math(a,b,sum,sub,pro); coutsum sub proendl;return 0 ; -实验题目(共4题, 第3题)标题: 3. 函数模板 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 设计一个函数模板,实现两个同类型数据的交换。 将设计好的函数模板分别实例化为两个

33、整型数交换、两个字符交换的模板函数,调用这些函数并输出运行结果。 输入: 分别输入两个整型数和两个字符 输出: 分别输出两个整型数和两个字符交换的结果 输入样例: 5 9 输出样例: 9 5 提示: 来源:#include using namespace std;template Type huan(Type a,Type b) Type c; c=a; a=b; b=c; cout a b a b ; huan(a,b); char c,d; cin c d; huan(c,d); return 0;示例代码-#includeusing namespace std;templateT cg(

34、T &a ,T& b)T c ;c = a ;a = b ;b = c ;int main() int a = 0 ; int b = 0 ; char c = 0 ; char d = 0 ; cinabcd; cg(a,b); cg(c,d); couta bendlc dendl; return 0 ;-实验题目(共4题, 第4题)标题: 4. 默认形参值 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 设计一个求空间两点距离的函数,要求第2个点的默认值为坐标原点。 输入: 两个点的坐标。 输出: 输出第一个点与原点之间的距离及输入的两个点之间的距

35、离。 输入样例: 1 1 15 5 5 输出样例: 1.732056.9282 提示: 函数原型可设计如下:float distance(float x1,float y1,float z1, float x2=0,float y2=0,float z2=0); 来源: #include #include #include #include using namespace std;float distanceyuan(float x1,float y1,float z1, float x2=0,float y2=0,float z2=0) float ju1; ju1=sqrt(x1*x1+y

36、1*y1+z1*z1); return ju1;float distanceliang(float x1,float y1,float z1, float x2,float y2,float z2) float ju2,a,b,c; a=x1-x2;b=y1-y2;c=z1-z2; ju2=sqrt(a*a+b*b+c*c); return ju2;int main() float x,y,z,w,m,n,ju1,ju2; cin x y z; cin w m n; ju1=distanceyuan(x,y,z); ju2=distanceliang(x,y,z,w,m,n); cout ju

37、1 endl ju2 endl; return 0;示例代码-#include#includeusing namespace std;float distance(float x1,float y1,float z1,float x2=0,float y2=0,float z2=0) return sqrt(pow(x1-x2,2)+pow(y1-y2,2)+pow(z1-z2,2);int main() float x1 = 0 ; float x2 = 0 ; float y1 = 0 ; float y2 = 0 ; float z1 = 0 ; float z2 = 0 ; cinx1

38、y1z1x2y2z2; coutdistance(x1,y1,z1)endl; coutdistance(x1,y1,z1,x2,y2,z2)endl; return 0 ;-实验题目(共2题, 第1题)标题: 类的定义_静态常量 时 限: 1000 ms 内存限制: 10000 K 总时限: 3000 ms 描述: 设计并实现一个圆柱类Cylinder,要求:(1)设计两个double型的私有数据成员:高度height和底圆半径radius。(2)设计一个double型的公有静态常量数据成员pi(其值初始化为3.1415926),以及一个获取该常量的公有静态成员函数GetPI。(3)在构造函

39、数的初始化列表中初始化高度height和底圆半径radius。(4)设计用于获取半径的成员函数GetRadius、获取高度的成员函数GetHeight、设置半径的成员函数SetRadius、设置高度的成员函数SetHeight。(5)设计用于计算圆柱体积的成员函数Volume()、计算圆柱表面积的成员函数Area。 在main函数中:根据输入的高度和半径构造圆柱对象,并输出圆柱的基本信息、体积和表面积。 输入: 圆柱的高度和底圆半径 输出: 圆周率pi、圆柱高度、底圆半径、圆柱的体积和表面积 输入样例: 2.0 1.0 输出样例: pi=3.14159, height=2, radius=1:

40、volume=6.28319, area=18.8496 提示: 来源: #include using namespace std;class Cylinderprivate: double height,radius;public: static const double Pi=3.1415926; Cylinder(double a=0,double b=0) height=a; radius=b; double GetPI() return Pi; void GetRadius() cin radius; void GetHeight() cin height; double SetRa

41、dius() return radius; double SetHeight() return height; double Volume() return Pi*radius*radius*height; double Area() return 2*Pi*radius*height+2*Pi*radius*radius; ;int main() Cylinder y; y.GetHeight(); y.GetRadius(); cout pi= y.GetPI() , ; cout height= y.SetHeight() , ; cout radius= y.SetRadius() : ; cout volume= y.Volume() , ; cout area= y.Area() endl; return 0;*

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!