C面向对象程序设计习题解答与上机指导(第二版)源程序.doc

合集下载

C语言程序设计实验指导书参考答案_第二版

C语言程序设计实验指导书参考答案_第二版
数的整除由键盘输入个整数逐个判断它们能否被27整除能的输出yes不能的输出no注意输出时一个判断结果占一行5个数的判断共占5第一组自测数据键盘输入271757732554正确输出yesyesyes第二组自测数据键盘输入271757732554正确输出yesyesyes提示整除即除得余数为0参考程序
C 语言程序设计 上机实验指导与习题(第二版) 参考答案(仅供教师内部参考)来自华南农业大学目 录
上机实验 ................................................................................................................................................................... 1 实验 1 C 语言程序初步 .................................................................................................................................... 1 一、实验目的............................................................................................................................................ 1 二、实验内容.................................................................................................................................

C++面向对象程序设计习题解答与上机指导(第二版)源程序.doc

C++面向对象程序设计习题解答与上机指导(第二版)源程序.doc

C++面向对象程序设计习题解答与上机指导(第2版)习题参考答案源代码使用源程序的几点注意事项(1) 由于源程序在复制、编辑、解压缩等过程中可能引起部分符号(主要是标点符号,如分号、冒号、逗号、引号)的字体、半全角等发生变化,在编译时可能被检出语法错误,只要使用“替换”功能,纠正后即能顺利运行。

(2) 有的C++系统(如Visual C++6.0)没有完全实现C++标准,它所提供的不带后缀的.h 的头文件不支持友元运算符重载函数,在Visual C++6.0中编译会出错,这时可采用带后缀的.h头文件。

将程序中的#include<iostream>using namespace std;修改成#include<iostream.h>即可顺利运行。

第2章 C++基础【2.2】下面是一个C程序,改写它,使它采用C++风格的I/O语句。

#include<stdio.h>int main(){ int a,b,d,min;printf("Enter two numbers:");scanf("%d%d",&a,&b);min=a>b? b:a;for (d=2; d<min; d++)if (((a%d)==0)&&((b%d)==0)) break;if (d==min){ printf("No common denominators\n");return 0;}printf("The lowest common denominator is %d\n",d);return 0;}【解】#include<iostream>using namespace std;int main(){ int a,b,d,min;cout<<"Enter two numbers:";cin>>a;cin>>b;min=a>b? b:a;for (d=2; d<min; d++)if (((a%d)==0)&&((b%d)==0)) break;if (d==min){ cout<<"No common denominators\n";return 0;}cout<<"The lowest common denominator is "<<endl<<d;return 0;}【2.24】写出下列程序的运行结果。

C面向对象程序设计课后习题答案第版谭浩强

C面向对象程序设计课后习题答案第版谭浩强

C面向对象程序设计课后习题答案第版谭浩强文件排版存档编号:[UYTR-OUPT28-KBNTL98-UYNN208]第六章课后习题答案(第二版谭浩强)1://xt6-1/cpp#include <iostream> //如用VC++应改为∶#include <iosttram.h>using namespace std; //如用VC++应取消此行#include "cylinder.h"#include "point.cpp"#include "circle.cpp"#include "cylinder.cpp"int main(){Cylinder cy1(3.5,6.4,5.2,10);cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<", r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<", volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnew cylinder:\n"<<cy1;Point &pRef=cy1;cout<<"\npRef as a point:"<<pRef;Circle &cRef=cy1;cout<<"\ncRef as a Circle:"<<cRef;return 0;}3:解法一#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){} ~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radius;};int main(){Point *p=new Circle(2.5,1.8,4.5);delete p;return 0;}3:解法二#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(int a,int b,int r):Point(a,b),radius(r){}~Circle(){cout<<"executing Circle destructor"<<endl;}private:float radius;};int main(){Point *p=new Circle(2.5,1.8,4.5);Circle *pt=new Circle(2.5,1.8,4.5);delete pt;return 0;}3:解法三#include <iostream>using namespace std;class Point{public:Point(float a,float b):x(a),y(b){}virtual ~Point(){cout<<"executing Point destructor"<<endl;} private:float x;float y;};class Circle:public Point{public:Circle(float a,float b,float r):Point(a,b),radius(r){}virtual ~Circle(){cout<<"executing Circle destructor"<<endl;} private:float radius;};void main(){Point *p=new Circle(2.5,1.8,4.5);delete p;}4:#include <iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle类class Circle:public Shape{public:Circle(double r):radius(r){} //结构函数virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数protected:double radius; //半径};//定义Rectangle类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return width*height;} //定义虚函数protected:double width,height; //宽与高};class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area() const {return 0.5*width*height;} //定义虚函数protected:double width,height; //宽与高};//输出面积的函数void printArea(const Shape &s){cout<<s.area()<<endl;} //输出s的面积int main(){Circle circle(12.6); //建立Circle类对象circlecout<<"area of circle =";printArea(circle); //输出circle的面积Rectangle rectangle(4.5,8.4); //建立Rectangle类对象rectanglecout<<"area of rectangle ="; printArea(rectangle); //输出rectangle的面积Triangle triangle(4.5,8.4); //建立Triangle类对象cout<<"area of triangle =";printArea(triangle); //输出triangle的面积return 0;}5:#include <iostream>using namespace std;//定义抽象基类Shapeclass Shape{public:virtual double area() const =0; //纯虚函数};//定义Circle(圆形)类class Circle:public Shape{public:Circle(double r):radius(r){} //结构函数virtual double area() const {return 3.14159*radius*radius;}; //定义虚函数protected:double radius; //半径};//定义Square(正方形)类class Square:public Shape{public:Square(double s):side(s){} //结构函数virtual double area() const {return side*side;} //定义虚函数protected:double side;};//定义Rectangle(矩形)类class Rectangle:public Shape{public:Rectangle(double w,double h):width(w),height(h){}//结构函数virtual double area() const {return width*height;}//定义虚函数protected:double width,height;//宽与高};//定义Trapezoid(梯形)类class Trapezoid:public Shape{public:Trapezoid(double t,double b,doubleh):top(t),bottom(t),height(h){} //结构函数virtual double area() const {return 0.5*(top+bottom)*height;} //定义虚函数protected:double top,bottom,height; //上底、下底与高};//定义Triangle(三角形)类class Triangle:public Shape{public:Triangle(double w,double h):width(w),height(h){} //结构函数virtual double area()const {return 0.5*width*height;} //定义虚函数protected:double width,height; //宽与高};int main(){Circle circle(12.6); //建立Circle类对象circleSquare square(3.5); //建立Square类对象squareRectangle rectangle(4.5,8.4); //建立Rectangle类对象rectangleTrapezoid trapezoid(2.0,4.5,3.2); //建立Trapezoid类对象trapezoidTriangle triangle(4.5,8.4); //建立Triangle类对象Shape *pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};//定义基类指针数组pt,使它每一个元素指向一个派生类对象double areas=0.0; //areas为总面积for(int i=0;i<5;i++){areas=areas+pt[i]->area();}cout<<"totol of all areas="<<areas<<endl; //输出总面积return 0;}。

C语言程序设计习题与上机指南 第二版 课后答案(牛志成 著) 清华大学

C语言程序设计习题与上机指南 第二版  课后答案(牛志成 著) 清华大学

C语言程序设计习题与上机指南第二版课后答案(牛志成著) 清华大学C语言程序设计习题与上机指南(牛志成编)答案。

第一章 C语言概述参考答案一、1.C 2.B3.C4.D5.D6.B7.A8.A9.B10.A二、1、函数 2、位 3、分号 4、main 5、{ } 6、换行 7、注释 8、函数首部、函数体9、main 10、编辑、编译、连接、运行三、1.错2.错3.对4.对5.对6.对7.错 8.对四、1、#include <stdio.h>/*包含头文件,为了使用printf和scanf语句,注意句末无分号*/ void main(){printf(“* * * * * * * * * * * \n”); /*\n的作用相当于回车*/printf(“ I am a student.\n”);printf(“* * * * * * * * * * * \n”);}2、#include <stdio.h>void main(){ int a,b;printf(“Please input a,b:\n”);sca nf(“%d,%d”,&a,&b); /*注意输入两个数之间用逗号分隔*/if(a>b) printf(“%d”,a);else printf(“%d”,b);}3、#include <stdio.h>void main(){int a,b,c,max;printf(“Please input a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(a<b) max=b; /*max用于保存a,b中比较大的数*/ if(max<c) max=c; /*将a,b中较大的数再和c比较* / printf(“The largest number is %d”,max);}第二章数据与运算参考答案一、1.B 2.D3.D4.A5.C6.B7.C8.B9.A10.A11.D12.A13.B14.A 15.C二、1、8 2、28 3、5 4、0 5、double 6、8 ,107、12 8、D 9、52 10、25,21,37 11、4812、20 200.000000 14、9,11,9,10 15、11.50 13、aa口bb口口口cc口口口口口口abcA口N三、1.错 2.对3.对4.错5.错6.对7.错8.错9.错10.对四、1、1)第1步:(int)(x+y)=(int)(7.2)=7第2步:a%3*7%2/4=7%3*7%2/4=1*7%2/4=1/4=0第3步:x+0=x=2.5(计算机显示为2.500000)1、2)第1步:(float)(a+b)/2=(float)(2+3)/2=(float)(5)/2=5.0/2=2.5 第2步:(int)x%(int)y=(int)(3.5)%(int)(2.5)=3%2=1第3步:2.5+1=3.5(计算机显示为3.500000)2、1)(a=a+a=12+12=24)2、2)(a=a-2=12-2=10)2、3)(a=a*(2+3)=12*5=60)2、4) (a=a/(a+a)=12/24=0,注意a是整型)2、5) 已知n的值等于5 a=0 2、6)(从右向左解开:a=a*a=144;a=a-a=0;a=a+a=0) 五、编程题1、参考答案:#include <stdio.h>void main(){char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’;c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf(“password is %c%c%c%c%c\n”,c1,c2,c3,c4,c5);}运行结果:password is Glmre也可以用循环和数组: #include <stdio.h>void main(){char c[]={“China”};printf(“password is “); for(int i=0;i<=4,i++)printf(“%c”,c[i]+=4);printf(“\n”);}2、参考答案:#include <stdio.h>void main(){char c1=97,c2=98;printf(“%d, %d\n”,c1,c2);printf(“%c,%c\n”,c1,c2); }3、参考答案:#include <stdio.h>#include <math.h>void main(){ double a, b, c, total, average, square, squareroot; printf("请输入三个双精度实数:");scanf("%lf%lf%lf", &a, &b, &c);total = a + b + c;average = total/3.0;square = a*a + b*b + c*c;squareroot = sqrt(square);printf("三个数的和=%-15.3f,三个数的平均值=%-15.3f\n", total, average);printf("三个数的平方和x=%.3f,x的平方根=%.3f\n", square, squareroot);}运行结果:请输入三个双精度实数:1.23 4.56 7.89三个数的和=13.680 ,三个数的平均值=4.560三个数的平方和x=84.559,x的平方根=9.196 4、参考答案:#include <stdio.h>#include <math.h>void main(){ float a, b, c, k, area;printf("请输入三角形三条边的边长:");scanf("%f%f%f", &a, &b, &c); /*由键盘输入三角形三边边长*/if ((a+b <= c) || (a+c <= b) || (b+c <= a)){ printf("您输入的三条边无法构成三角形。

c面向对象程序设计(第二版)

c面向对象程序设计(第二版)

c面向对象程序设计(第二版)面向对象程序设计是一种以对象为基本单位的编程范式,它强调数据和方法的封装、继承和多态性。

这种设计方法使得软件更加模块化、易于扩展和维护。

《面向对象程序设计(第二版)》这本书深入探讨了面向对象编程的基本概念、原理和实践,适合初学者和有经验的程序员学习。

第一章:面向对象编程基础面向对象编程(OOP)的核心概念包括类、对象、封装、继承和多态。

类是现实世界中某些具有共同特征的事物的抽象,而对象则是类的实例。

封装是将数据和操作这些数据的方法组合在一起,隐藏内部细节,只提供必要的接口。

继承允许新类从现有类中继承属性和方法,而多态性则允许对象以多种形式表现。

第二章:类与对象类是创建对象的蓝图。

定义类时,需要指定其属性(数据成员)和方法(成员函数)。

对象是类的实例,每个对象都拥有自己的状态和行为。

书中通过实例展示了如何定义类和创建对象,以及如何通过构造函数和析构函数管理对象的生命周期。

第三章:封装封装是OOP中最重要的概念之一。

它涉及到隐藏对象的内部状态,只通过公共接口与外界交互。

封装可以提高代码的安全性和可维护性。

书中详细讨论了访问修饰符(public、private、protected)的用法,以及如何使用它们来控制类成员的访问权限。

第四章:继承继承是面向对象编程中的另一个关键概念,它允许创建新的类来扩展或修改现有类的行为。

通过继承,可以避免代码重复,提高代码的复用性。

书中介绍了单继承和多继承的概念,以及如何使用继承来实现代码的层次结构。

第五章:多态性多态性允许对象以多种形式表现。

在OOP中,多态性主要通过虚函数和抽象类来实现。

虚函数允许子类重写父类的方法,而抽象类则定义了一组接口,但具体实现由子类完成。

书中讨论了多态性的实现机制,以及如何在实际编程中应用多态性。

第六章:接口与抽象类接口定义了一组方法,但不提供实现。

抽象类是包含至少一个纯虚函数的类。

它们都用于定义对象的契约,确保对象实现特定的行为。

C 程序设计教程第二版习题答案

C  程序设计教程第二版习题答案

C 程序设计教程第二版习题答案C 程序设计教程第二版习题答案C 程序设计是一门广泛应用于计算机科学和工程领域的编程语言,它具有高效、灵活和可移植等优点。

对于初学者来说,掌握C 程序设计的基本知识是非常重要的。

而《C 程序设计教程》是一本经典的教材,为学习者提供了一系列的习题来巩固所学知识。

本文将为大家提供《C 程序设计教程第二版》习题的答案,帮助大家更好地理解和掌握C 程序设计。

第一章:C 程序设计入门1.1 习题答案:1. 编写一个C程序,输出"Hello, World!"。

```c#include <stdio.h>int main() {printf("Hello, World!");return 0;}```1.2 习题答案:1. 编写一个C程序,输入两个整数,然后输出它们的和。

```c#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两个整数的和为:%d", sum);return 0;}```第二章:C 程序设计基本要素2.1 习题答案:1. 编写一个C程序,输入一个字符,然后输出它的ASCII码。

```c#include <stdio.h>int main() {char ch;printf("请输入一个字符:");scanf("%c", &ch);printf("该字符的ASCII码为:%d", ch);return 0;}```2.2 习题答案:1. 编写一个C程序,输入一个整数,然后输出它的绝对值。

c程序设计(第二版)课后习题答案

c程序设计(第二版)课后习题答案
2
C 语言程序设计(第二版) 课后习题参考答案
putchar(c2);//将变量 c2 的值输出 printf("\n"); printf("%c%c\n",c1,c2);//用 printf 输出 c1、c2 的值 printf("%d,%d\n",c1,c2);//输出 c1,c2 的 ASCII 码 } 第四章 【习题 4.5】 /*有三个整数 a,b,c,由键盘输入,输出其中最大的数,请编程序。*/ /*变量:三个整数 a、b、c,中间变量 t,最大值 max*/ #include<stdio.h> void main() { int a,b,c,t,max; printf("please input a,b,c:\n"); scanf("%d,%d,%d",&a,&b,&c); t=a>b?a:b;//比较 a 与 b 的大小,将大者赋给中间变量 t max=t>c?t:c;//比较 t 与 c 的大小,将大者赋给最大值 max printf("the max is:\n"); printf("%d\n",max); } 【习题 4.6】 /*给出一百分制成绩,要求输出成绩等级'A'、'B'、'C'、'D'、'E'。90 分以上为'A',80~89 分为 'B',70~79 分为'C',60~69 分为'D',60 分以下为'E'。*/ #include<stdio.h> void main() { int score; printf("please input the score:(0-100)\n"); scanf("%d",&score); if(score>=90&&score<=100) printf("A");//如果成绩大于 90 分,输出 A else if(score>=80&&score<=89) printf("B");//如果成绩在 80~89 之间,输出 B else if(score>=70&&score<=79) printf("C");//如果成绩在 70~79 之间,输出 C else if(score>=60&&score<=69) printf("D");//如果成绩在 60~69 之间,输出 D else printf("E");//成绩小于 60 分时,输出 E printf("\n"); } 【习题 4.7】 /*给一个不多于 5 位的正整数,要求:(1)求出它是几位数;(2)分别输出每一个数字;(3)按 逆顺序输出各位数字,例如原数为 321,应输出 123.*/ /*变量:正整数 x、万位数 a、千位位数 b、百位数 c、十位数 d、个位数 e*/ #include <stdio.h>

C语言程序设计习题解答与上机指导(第二版)

C语言程序设计习题解答与上机指导(第二版)

《“十三五”普通高等教育本科规划教材 C语言程序设计习题解答与上机指导(第二版)》作为C语言程序设 计省级精品课程配套教材,不仅可作为普通高校本、专科学生学习C语言的参考书,也可以作为相关工程技术人员 的自学与参考用书。本书是**以从业多年来以洪荒之力墙裂推荐的C语言教材,首先是图书编写团队非常严谨、 从全书结构设置到程序排版,均做到事无巨细、极尽所能;其次是全书内容由浅入深、安排合理,案例深入浅出, 对知识点的理解很有帮助;*后是本书与主教材配套而出,且配有相应的立体化学习、教学资源,确实是一套《C 语言程序设计》教学好书。
图书目录
序 前言 第一部分学习指导与习题解答 第 1章 C语言程序设计概述 1.1内容提要 1.2常见错误 1.3习题解答 第 2章数据及运算 2.1内容提要 2.2常见错误 2.3习题解答
谢谢观看
作者简介
李新华,安徽大学电子信息与计算机学院副教授.安徽大学青年骨干教师,其负责的C语言程序设计课程被评被 安徽省重点课程,编写的教材很有特色.
内容简介
《“十三五”普通高等教育本科规划教材 C语言程序设计习题解答与上机指导(第二版)》为李新华、梁栋 等编著的普通高等教育“十二五”规划教材《C语言程序设计(第二版)》配套使用的参考书。内容包括:⑴学习 指导与习题解答:内容提要强化各章的知识点、常用算法和编程方法;习题解析对程序设计的难点进行了分析, 所附参考程序全部上机调试通过。⑵上机指南:详细介绍了Visual C++ 6.0集成环境下的编辑、编译、调试和运 行C程序的开发方法。⑶上机实验指导,安排了同步的上机实践环节,每个实验都包括相应章节的关键知识、算法 和编程训练。⑷期中、期末两套模拟试卷和参考答案,可有效测试读者对C程序设计的掌握程度。⑸课程设计示范 引导读者基于图形、图像的良好程序设计规范。|
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++面向对象程序设计习题解答与上机指导(第2版)习题参考答案源代码使用源程序的几点注意事项(1) 由于源程序在复制、编辑、解压缩等过程中可能引起部分符号(主要是标点符号,如分号、冒号、逗号、引号)的字体、半全角等发生变化,在编译时可能被检出语法错误,只要使用“替换”功能,纠正后即能顺利运行。

(2) 有的C++系统(如Visual C++6.0)没有完全实现C++标准,它所提供的不带后缀的.h 的头文件不支持友元运算符重载函数,在Visual C++6.0中编译会出错,这时可采用带后缀的.h头文件。

将程序中的#include<iostream>using namespace std;修改成#include<iostream.h>即可顺利运行。

第2章 C++基础【2.2】下面是一个C程序,改写它,使它采用C++风格的I/O语句。

#include<stdio.h>int main(){ int a,b,d,min;printf("Enter two numbers:");scanf("%d%d",&a,&b);min=a>b? b:a;for (d=2; d<min; d++)if (((a%d)==0)&&((b%d)==0)) break;if (d==min){ printf("No common denominators\n");return 0;}printf("The lowest common denominator is %d\n",d);return 0;}【解】#include<iostream>using namespace std;int main(){ int a,b,d,min;cout<<"Enter two numbers:";cin>>a;cin>>b;min=a>b? b:a;for (d=2; d<min; d++)if (((a%d)==0)&&((b%d)==0)) break;if (d==min){ cout<<"No common denominators\n";return 0;}cout<<"The lowest common denominator is "<<endl<<d;return 0;}【2.24】写出下列程序的运行结果。

#include<iostream>using namespace std;int i=15;int main(){ int i;i=100;::i=i+1;cout<<::i<<endl;return 0;}运行结果:101Please any key to continue。

【2.25】写出下列程序的运行结果。

#include<iostream>using namespace std;void f(int &m,int n){ int temp;temp=m;m=n;n=temp;}int main(){ int a=5,b=10;f(a,b);cout<<a<<" "<<b<<endl;return 0;}结果:10 10Please any key to continue。

【2.26】分析下面程序的输出结果。

#include<iostream>using namespace std;int &f(int &i){ i+=10;return i;}int main(){ int k=0;int &m=f(k);cout<<k<<endl;m=20;cout<<k<<endl;return 0;}运行结果:1020Please any key to continue。

【2.27】编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci数列的前20项并存储到动态分配的空间中。

【解】实现本题功能的程序如下:#include<iostream>using namespace std;int main(){ int *p=new int[20]; //动态分配20个整型内存空间*p=1;*(p+1)=1; //对前面2个内存空间赋值1cout<<*p<<"\t"<<*(p+1)<<"\t";p=p+2; //p指向第3个内存空间for (int i=3;i<=20;i++){ *p=*(p-1)+*(p-2);cout<<*p<<"\t";if (i%5==0) cout<<endl;p++; //p指向下一个内存空间;}return 0;}【2.28】编写一个C++风格的程序,建立一个被称为sroot的函数,返回其参数的二次方根。

重载函数sroot三次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt)。

【解】实现本题功能的程序如下:#include<iostream>#include<cmath>using namespace std;double sroot(int i){ return sqrt(i);double sroot(long l){ return sqrt(l);}double sroot(double d){ return sqrt(d);}int main(){ int i=12;long l=1234;double d=12.34;cout<<"i的二次方根是:"<<sroot(i)<<endl;cout<<"l的二次方根是:"<<sroot(l)<<endl;cout<<"d的二次方根是:"<<sroot(d)<<endl;return 0;}【2.29】编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、5分的硬币,有多少种换法?【解】实现本题功能的程序如下:#include<iostream>using namespace std;int main(){ int i,j,sum=0;;for(i=0;i<=20;i++)for (j=0;j<=50;j++)if (100-5*i-2*j>=0){ sum++;cout<<100-5*i-2*j<<"\t"<<j<<"\t"<<i<<endl;}cout<<"sum is "<<sum<<endl;return 0;}【2.30】编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序输出。

要求使用变量的引用。

【解】实现本题功能的程序如下:#include<iostream>using namespace std;int main(){ void change(int &,int &);int a,b;cin>>a>>b;if(a>b)change(a,b);cout<<a<<" "<<b<<endl;return 0;void change(int &a1,int &b1){ int temp;temp=a1;a1=b1;b1=temp;}【2.31】编写C++风格的程序,用二分法求解f(x)=0的根。

【解】实现本题功能的程序如下:#include<iostream>#include <cmath>using namespace std;inline float f(float x){ return 2*x*x*x-4*x*x+3*x-6;}int main(){ float left,right,middle,ym,yl,yr;cout<<"please two number:"<<endl; //接收输入,确定第一组数据区域 cin>>left>>right;yl=f(left);yr=f(right);do{ middle=(right+left)/2;ym=f(middle);if (yr*ym>0){ right=middle;yr=ym;}else{ left=middle;yl=ym;}} while (fabs(ym)>=1e-6);cout<<"\nRoot is :"<<middle;return 0;}第3章类和对象(一)【3.18】写出下面程序的运行结果。

#include<iostream>using namespace std;class test{ public:test() ;~test(){ };private:int i;};test::test(){ i = 25;for (int ctr=0; ctr<10; ctr++){ cout<<"Counting at "<<ctr<<"\n";}}test anObject;int main(){ return 0;}【3.19】写出下面程序的运行结果。

#include<iostream>using namespace std;class Test{private:int val;public:Test(){ cout<<"default."<<endl;}Test(int n){ val=n;cout<<"Con."<<endl;}Test(const Test& t){ val=t.val;cout<<"Copy con."<<endl;}};int main(){ Test t1(6);Test t2=t1;Test t3;t3=t1;return 0;}【3.20】指出下列程序中的错误,并说明为什么。

相关文档
最新文档