自考C++程序
课后答案
第一章 #include < iostream > 一、选择
#include < algorithm > //用于数组排列的头文件 1.B; (typedef ,typeid ,typename,都为保留字); using namespace std; 2.C; (标识符,应该以字母或,下划线开头); void main()
3.C; (标识符中有的特殊符号,只能有下划线); {
二、填空题 float *p;
1. cin,cout p = new float[15]; 2. new,delete cout<<"输入15个float类型的值:" << endl; 3. int a(55); for(int i = 0;i < 15 ; i++) 三、改错题 {
1.没有定义变量num; cin>>*(p+i); 2.不能给变量x,声明指向常量的指针const int *p=&x; 如 }
果吧x定义为常量const,*p不能当作“左值”。 for(i = 0;i < 15;i++) 3.p为常量指针,不能吧p作为“左值”,p=&y,错误。 {
四、编程题 cout<<*(p+i)<<","; 1. 分别用字符和ASCII码形式输出整数值65和66. }
#include < iostream > sort(p,p+15); using namespace std; cout<<"\n最小的是:"<<*(p)<< endl; void main() delete p;
{
char a='A',b='B'; }
int ascii_1=53,ascii_2=54;//ASCII码中的,5和6 4.声明如下数组:
cout<<"字符输出:"<<(int)a<<","<<(int)b<< endl; int a[] = {1 ,2 ,3, 4, 5, 6, 7, 8};
cout<<"ASCII码输出:先查找4的位置,讲数组a复制给数组b,然后将数组a"<<(char)ascii_2<<(char)ascii_1<<","; 的
反转,再查找4的位置,最后分别输出数组a和b
cout<<(char)ascii_2<<(char)ascii_2<< endl; 的内容。
} #include < iostream> 2.编写一个int型变量分配100个整形空间的程序。 #include < algorithm> #include < iostream > #include < functional> using namespace std; using namespace std; void main() void main()
{ {
int *p; int a[]={1,2,3,4,5,6,7,8},b[8];
p = new int[100]; cout<<"数组a中„4?的位置是:"<< find(a,a+8,4)<<
for(int i = 0;i < 100;i++) endl;//查找4的位置
{ copy(a,a+8,b);//将数组a复制给数组b
*(p+i)=i; reverse_copy(b,b+8,a);//把数组b,逆向复制给a,
} 完成a的逆转
for(i = 0;i < 100;i++) cout<<"数组a反转后,„4?的位置是:"<<
{ find(a,a+8,4)<< endl;//在查找4的位置
cout<<*(p+i)<<",";
} cout<<"数字a的内容:"<< endl;
delete p; for(int i=0;i<8;i++) } cout<< a[i]<<" ,"; 3.编写完整的程序,它读入15个float值,用指针把它们 cout<<"\n数组b中的内容:"<< endl; 存放在一个存储快里,然后输出这些值和以及最小值。 for(i=0;i<8;i++)
cout<< b[i]<<" ,"; string str1("C++"),str2("程序设计");
string str3; } str3 = str1+str2;//连接方式1 第二章 cout<< str3<< endl; 一、单项选择
1.D; 2.D; //使用char数组定义字符串,完成连接 二、作图题 char c1[] = {"c++"},c2[] = {"program"};
1( 已知一个学生类具有性别和年龄两个属性,男学生张 char c3[20]; 明的年龄为12岁,女学生李红的年龄为11岁。给出这 int i=0,k=0; 个学生类的类图和它们的对象图。 for(i=0;i<20;i++)//初始化c3 (类)Student (对象)张明 (对象)李红 c3[i]='\0'; string sex; sex(男); sex(女); i=0;
int age; age(12); age(11); while(c1[i]!='\0') 方法… 方法… 方法… {
c3[k]=c1[i]; 2( 一个圆具有圆心坐标和半径两个属性,并且能够给出 i++;
圆面积,请画出这个圆类的类图。 k++;
(类) Circularity (类)Point }
Point p; float x; i=0;
float radii; float y; while(c2[i]!='\0')
float getX(); {
float getAcreage(); float getY(); c3[k]=c2[i]; 3( 画出一个班级类的类图,为它设计必要的属性以
示 i++;
这个类的特征。 k++;
(类) PubClass }
string no;//编号 cout<< c3<< endl; int num;//人数 }
… 2.已知一个string的对象str的内容为“We are here~”,4( 画出一种电话卡的类图,为它设计必要的属性。 使用多种方法输出“h”。
(类) Card #include < iostream > long no;//编号 #include < functional > float balance;//余额 #include < algorithm > 5( 为上题的电话卡设计必要的成员函数,以便提供基本#include < string > 服务。 using namespace std; (类) Card void main()
long no;//编号 {
float balance;//余额 string str1("We are here!"); float getBalance();//显示余额 cout<< str1[7]<< endl;//通过数组 三、编程题 string str2=str1.substr(7,1);//通过得到子字符串 1.使用多种方法编写将两个字符串连接在一起的程序。 cout<< str2<< endl; #include < iostream > char *p=find(str1.begin(),str1.end(),'h');//通过find函#include < string > 数
using namespace std; if(p)
void main() cout<<*p<< endl; { }
//使用string类定义字符串,完成字符串连接 第三章
一、填空题 }
1.函数原型声明; void equation_3 (int a, int b, int c)
2.inline {
3.传值,传引用 double temp, real1, real2, image1, image2;
4.函数func返回引用 temp = - (b*b - 4 * a * c); 5.int *fun(char,int); real1 = -b / (2 * a *1.0); 二、单项选择题 real2 = real1; 1.A; 2.C; 3.D; image1 = sqrt(temp); 三、改错题 image2 = - image1; 1.y = x * x - T; 错误,T是类型,不是变量,不能参加运 cout<<"两个虚根"<< endl; 算; cout<<"x1 = "<< real1<<" + "<< image1<<"j"<<
2.y没有类型。 endl;
T max(T x, T y) cout<<"x2 = "<< real2<<" + "<< image2<<"j"<<
{ endl;
return (x>y) ? (x) : (y) ;
} }
3.函数change 的参数定义成了常量,只能使用参数,而void main()
无权修改他。 {
void change (string & s) int a, b, c;
{ double temp;
s = s + "pig!"; cout<<"输入a,b,c的值"<< endl;
} cin>>a>>b>>c; 四、编程题 cout<<"方程为:"<< a<<"x*x+"<< b<<"x+"<< c<<" =
1.编写一个求方程ax2 + bx + c = 0的根 的程序,用3个0"<< endl;
函数分别求当b2-4ac大于零、等于零、和小于零时的方 temp = b*b - 4 * a * c; 程的根。要求从主函数输入a,b,c的值并输出结果。 if(temp > 0)
#include < iostream.h > equation_1 (a, b, c); #include < math.h > if(temp == 0) void equation_1 (int a, int b, int c) equation_2 (a, b, c); { if(temp < 0)
double x1, x2, temp; equation_3 (a, b, c);
temp = b*b - 4 * a * c;
x1 = (-b + sqrt(temp) ) / (2 * a * 1.0); }
x2 = (-b - sqrt(temp) ) / (2 * a * 1.0); 2.定义函数up(ch),如字符变量ch是小写字母就转换成
cout<<"两个不相等的实根"<< endl; 大写字母并通过up返回,否则字符ch不改变。要求在
cout<<"x1 = "<< x1<<", x2 = "<< x2<< endl; 短小而完全的程序中显示这个程序是怎样被调用的。 } #include < iostream > void equation_2 (int a, int b, int c) using namespace std; { char up (char c)
double x1, x2, temp; {
temp = b*b - 4 * a * c;
x1 = (-b + sqrt(temp) ) / (2 * a * 1.0); if(c >= 97 && c <= 123)
x2 = x1; return (c - 32) ;
cout<<"两个相等的实根"<< endl; else
cout<<"x1 = "<< x1<<", x2 = "<< x2<< endl; return c;
}
void main() cout<< endl; { }
int i; }
char c[15] = void main()
{'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'}; {
for(i = 0 ; i < 15 ; i++) print_triangle('a',10);
cout<< up(c[i])<<", "; }
cout<< endl; 5.编写一个ieqiu字符串长度的函数,strlen(),再用strlen} ()函数编写一个函数revers(s)的倒序递归程序,使3.编写主程序条用带实数r和整数n两个参数的函数并输字符串s逆序。
出r的n次幂。 #include < iostream > #include < iostream.h > #include < string > #include < math.h > using namespace std; double power(double a, int b) int strlen(char *str) { {
int i; int len = 0;
double result = 1.0; while(str[len] != '\0')
for(i=0;i< b;i++) {
result = result * a; len++;
return result; }
} return len;
void main() }
{ void revers(char *b)
double r; {
int n; char c;
cout<<"r = "; int j, len;
cin>>r; len=strlen(b);
cout<<"n = "; j=len/2-1;
cin>>n; while(j>=0)
cout<< r<<"的"<< n<<"次幂是:"<< power(r,n)<< {
endl; c=*(b+j); } *(b+j)=*(b+len-j-1); 4.编写有字符型参数C和整形参数N的函数,让他们显 *(b+len-j-1)=c; 示出由字符C组成的三角形。其方式为第1行有1个字 j--; 符C,第2行有2个字符C ,等等。 }
#include < iostream > b[len]='\0';
using namespace std; }
void print_triangle(char c, int n) void main()
{ {
int i, j; char str[]={"1234567890"};
for(i=0; i< n; i++) cout<< str<<"----的长度:"<< strlen(str)<< endl;
{ cout<< str<< endl;//倒序前
for(j=0; j<=i; j++) revers(str);//
{ cout<< str<< endl;//倒序后
cout<< c; }
} 6.用函数模板实现3个数值中按最小值到最大值排序的
程序。 }
#include < iostream > 8.重载上题中的函数模板,使他能够进行两个数组的求using namespace std; 和。
template #include < iostream > void sort(T a, T b, T c) using namespace std; { template
T array[3],temp; T sum (T a[], int n)
int i,j; {
int i;
array[0] = a; T s=0;
array[1] = b; for(i=0;i< n;i++)
array[2] = c; s = s + a[i];
for(i=0;i<3;i++) return s;
{ }
for(j=0;j<2;j++) template //重载上面的模板
if(array[j]>array[j+1]) T sum (T a[], int n, T b[], int m)
{ {
temp = array[j]; return sum(a,n)+sum(b,m);
array[j] = array[j+1]; }
array[j+1] = temp; void main ()
} {
} int a[5]={1,2,3,4,5};
cout<< array[0]<< array[1]<< array[2]<< endl; int b[10]={1,2,3,4,5,6,7,8,9,10};
} int s1 = sum(a, 5); void main() int s2 = sum(b, 10); { int s3= sum(a, 5, b, 10);
sort(5,1,9); cout<< s1<< endl; } cout<< s2<< endl; 7.利用函数模板设计一个求数组元素中和的函数,并检验 cout<< s3<< endl; 之。 }
#include < iostream > 第四章
using namespace std; 一、填空题
template 1.数据成员、成员函数;
T sum (T a[],int n) 2.类、重载、1;
{ 3.fun:fun(fun &)、fun:fun(const fun &);
int i; 二、单项选择题
T s=0; 1.C。2.C。3.没又答案,应该是A::~A(void)、或A::~A()。
for(i=0;i< n;i++) 4.B。 5.C。 6.C。 7.D
s = s + a[i]; 三、改错题
return s; 1.return m;---错误,没又定义变量m; } 2.A.init(24,56);---错误,应该先定义A对象:Point A; void main () 四、完成程序题
{ 1.#include < iostream >
int a[5]={1,2,3,4,5}; using namespace std;
int s = sum(a,5); class base
cout<< s<< endl; {
private : //私有数据成员 }
int a, b; void main()
public : {
void init(int x, int y)//公有函数 Point a(10,89);
{ Point b(a);
a = x; a.print();
b = y; b.print();
} }
void print() 五、程序分析题
{ 1.没有结果,因为没有main函数
cout<<"2 * "<< a<<" - "<< b<<" = 如果加main函数
"<<(2*a-b)<< endl; void main()
} {
}; base b(10, 20); void main() }
{ 输出:
base a; 初始化...10,20
a.init(68,55); Destory...10,20
a.print(); 2.
} 输出:
2. 55
#include 六、编程题
using namespace std; 1.设计一个点类Point,再设计一个矩形类,矩形类使用class Point Point类的两个坐标点作为矩形的对角顶点。并可以输出{ 4个坐标值和面积。使用测试程序验证程序。
private : #include
int m, n; using namespace std;
public : class Point //点类
Point(int, int);//整型变量,为参数的构造函{
数 private:
Point(Point&);//复制构造函数的原型 int x, y;//私有成员变量,坐标
print() public :
{ Point()//无参数的构造方法,对xy初始化
cout<<"m = "<< m<<", n = "<< n<< {
endl; x = 0;
} y = 0; }; }
Point::Point(int a, int b) Point(int a, int b)//又参数的构造方法,对xy{ 赋值
m = a; {
n = b; x = a; } y = b; Point::Point(Point & t)//复制构造函数的定义 }
{ void setXY(int a, int b)//设置坐标的函数
m = t.m; {
n = t.n; x = a;
y = b; cout<<"C:("<< point3.getX() <<","<<
} point3.getY() <<")"<< endl;
int getX()//得到x的方法 cout<<"D:("<< point4.getX() <<","<<
{ point4.getY() <<")"<< endl;
return x; }
} int getArea()//计算面积的函数
int getY()//得到有的函数 {
{ int height, width, area;
return y; height = point1.getY() - point3.getY();
} width = point1.getX() - point2.getX();
}; area = height * width; class Rectangle //矩形类 if(area > 0)
{ return area;
private: else
Point point1, point2, point3, point4;//私有 return -area; 成员变量,4个点的对象
public : }
Rectangle();//类Point的无参构造函数已经};
对每个对象做初始化啦,这里不用对每个点多初始化了 void main()
Rectangle(Point one, Point two)//用点对象{
做初始化的,构造函数,1和4为对角顶点 Point p1(-15, 56), p2(89, -10);//定义两个点
{ Rectangle r1(p1, p2);//用两个点做参数,声明一个矩
point1 = one; 形对象r1
point4 = two; Rectangle r2(1, 5, 5, 1);//用两队左边,声明一个矩形
init(); 对象r2
}
Rectangle(int x1, int y1, int x2, int y2)//用 cout<<"矩形r1的4个定点坐标:"<< endl; 两对坐标做初始化,构造函数,1和4为对角顶点 r1.printPoint();
{ cout<<"矩形r1的面积:"<< r1.getArea() << endl;
point1.setXY(x1, y1);
point4.setXY(x2, y2); cout<<"\n矩形r2的4个定点坐标:"<< endl;
init(); r2.printPoint();
} cout<<"矩形r2的面积:"<< r2.getArea() << endl;
void init()//给另外两个点做初始化的函数 }
{ 2.使用内联函数设计一个类,用来表示直角坐标系中的任
point2.setXY(point4.getX(), 意一条直线并输出它的属性。
point1.getY() );
point3.setXY(point1.getX(), #include < iostream.h > point4.getY() ); #include < math.h >
} class Line
void printPoint()//打印四个点的函数 {
{ private:
cout<<"A:("<< point1.getX() <<","<< int x1, y1, x2, y2; point1.getY() <<")"<< endl; public :
cout<<"B:("<< point2.getX() <<","<< Line();
point2.getY() <<")"<< endl; Line(int =0, int =0, int =0, int=0 );
void printPoint(); #include < iostream >
double getLength(); using namespace std;
class test
}; {
inline Line::Line(int a, int b, int c, int d) int x;
{ public :
x1 = a; test(int a)
y1 = b; {
x2 = c; x = a;
y2 = d; }
} int GetX() inline void Line::printPoint() {
{ return x;
cout<<"A:"<< x1 <<", "<< y1 << endl; }
cout<<"B:"<< x2 <<", "<< y2 << endl; };
} void main()
inline double Line::getLength() {
{ int i;//填空一,声明变量i
double length; test *p, a[2][3] = {{1, 2, 3}, {4, 5, 6}};
length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ); for( p=&a[0][0], i=0; i<=6; i++, p++)//填空2,初始化
return length; p,i
} {
void main() if((p-a[0])%3 == 0) { cout<< endl;
Line line(10,80,-10,12); cout<< p->GetX() <<" ";
line.printPoint(); }
cout<< line.getLength() << endl; }
} 五、编程题
第五章 1.声明复数的类,complex,使用友元函数add实现复数一、填空题 加法。
1.常成员函数; #include < iostream > 2.常量成员函数; using namespace std; 3.const class Complex 二、单项选择题 {
1.B; 2.A; 3.C; 4.A; private:
三、改错题 double real, image; 1.static int getn(){return number;}错误 public :
静态成员函数,只允许访问静态成员变量,number不是 Complex(){} 静态成员变量 Complex(double a,double b) 2.void main() {
{ real = a;
test *two[2] = {new test(4, 5), test(6 ,8)}; image = b;
for( i=0; i<2; i++) }
delete two[i]; void setRI(double a, double b)
} {
四、完成程序题 real = a;
image = b; int i, n, temp=0;
} cout<<"输入数组大小:";
double getReal() cin>>n;
{ double *array = new double[n]; //用指针,动态申请
return real; 数组大小
} cout<<"给每个数组元素赋值:"<< endl;
double getImage() for(i=0; i < n; i++)
{ {
return image; cout<<"array["<< i <<"] = ";
} cin>>temp;
void print() *(array+i) = temp;//给数组元素赋值
{ }
if(image>0) cout<<"动态数组个元素的值如下:"<< endl;
cout<<"复数:"<< real <<" + "<< for(i=0; i < n; i++) image <<"i"<< endl; {
if(image<0) cout<<"array["<< i <<"] = "<< array[i] << endl;//
cout<<"复数:"<< real <<" - "<< 打印数组元素
image <<"i"<< endl; }
} delete [] array;//释放内存
friend Complex add(Complex ,Complex);//}
声明友元函数 4.定义一个Dog类,它用静态数据成员Dogs
Dog}; 的个体数目,静态成员函数GetDogs用来存取Dogs。设Complex add(Complex c1, Complex c2)//定义友元函数 计并测试这个类。
{ #include < iostream >
Complex c3; using namespace std;
c3.real = c1.real + c2.real;//访问Complex类中的私class Dog
有成员 {
c3.image = c1.image + c2.image; private:
return c3; static int dogs;//静态数据成员,记录Dog} 的个体数目
void main() public :
{ Dog(){}
Complex c1(19, 0.864), c2, c3; void setDogs(int a)
c2.setRI(90,125.012); {
c3 = add(c1, c2); dogs = a;
cout<<"复数一:";c1.print(); }
cout<<"复数二:";c2.print(); static int getDogs()
cout<<"相加后:";c3.print(); {
} return dogs; 2.例子5.8,114页例子不错; }
3.编写一个程序,该程序建立一个动态数组,为动态数组};
的元素赋值,显示动态数组的值并删除动态数组。 int Dog :: dogs = 25;//初始化静态数据成员 #include < iostream > void main()
using namespace std; {
void main() cout<<"未定义Dog类对象之前:x = "<< { Dog::getDogs() << endl;; //x在产生对象之前即存在,输
出25 {
Dog a, b; return area;
cout<<"a中x:"<< a.getDogs() << endl; }
cout<<"b中x:"<< b.getDogs() << endl; };
a.setDogs(360); class Column : public Circular//从圆类派生圆柱类
cout<<"给对象a中的x设置值后:"<< endl; {
cout<<"a中x:"<< a.getDogs() << endl; protected:
cout<<"b中x:"<< b.getDogs() << endl; double h;
} double cubage; 第六章 public :
一、填空题 Column(double a, double b) : Circular(a)
1.单一继承; 2.private protected public {
二、单项选择 h = b;
1.D;2.A;3.C;4.D; cubage = getArea() * h; 三、改错题 }
1.类derived和base中均没变量b,derived的构造函数 double getCubage()//返回圆柱体积函数 中的m(b)错误; {
2.Derived类中重载show()方法 return cubage;
void Show() }
{ };
Base1::Show();Base2::Show(); void main()
} {
四、编程题 Circular circular(45); 1.设计一个基类,从基类派生圆柱,设计成员函数输出它 Column column(12, 10); 们的面积和体积; cout<<"圆的面积:"<< circular.getArea() << endl;
#include < iostream > cout<<"圆柱的体积:"<< column.getCubage() <<
using namespace std; endl;
class Basic//基类 }
{ 3.定义一个线段类作为矩形的基类,基类有起点和终点坐
protected: 标,有输出左边和长度以及线段和x轴的夹角的成员函
double r; 数。矩线段对象的两个坐标作为自己一条边的位置,它具
public : 有另外一条边,能输出矩形的4个顶点坐标。给出类的定
Basic(){ r = 0; } 义并用程序验证它们的功能。
Basic(double a):r(a){} #include < iostream > }; #include < cmath > class Circular : public Basic//从基类派生圆类 using namespace std; { class Point//点类
protected: {
double area; protected:
public : double x, y;
Circular(double a) public :
{ Point(){}
r = a; Point(double a, double b)
area = area = 3.1415926 * r * r; {
} x = a; y = b;
double getArea()//返回圆面积 }
double getX() }
{return x;} };
double getY() class Rectangle : public Line
{return y;} {
}; protected:
class Line Line *line;
{ public:
protected: Rectangle(double a, double b, double c,
Point p1, p2;//Point对象做成员 double d, double e, double f, double g, double
double length, angle; h):Line(a,b,c,d)
public: {
Line(double a, double b, double c, double line = new Line(e,f,g,h); d):p1(a, b), p2(c, d)//用两对坐标初始化线段 }
{ Rectangle(Point a, Point b, Point c, Point
init(); d) : Line(a, b)//4个点对象,初始化
} {
Line(Point a, Point b)//用两个点的对象初 line = new Line(c, d); 始化线段 }
{ void printPoint()
p1 = a; p2 = b; {
init(); cout<<"矩形4个顶点:\n";
} printXY();
void init()//计算线段长度,以及和x轴的夹 line->printXY(); 角的度数 }
{ };
double x1 = p1.getX(), y1 = p1.getY(); void main()
double x2 = p2.getX(), y2 = p2.getY(); {
length = sqrt((x1-x2)*(x1-x2) + Point p1(0, 0), p2(4, 3), p3(12, 89), p4(10, -50);
(y1-y2)*(y1-y2));
angle = atan( (y2-y1) / (x2-x1) ); Line l1(0,0,4,3);
angle = angle *180/3.141592653; l1.printXY();
} l1.printLength();
void printXY() l1.printAngle();
{
cout<<"("<< p1.getX() <<","<< Line l2(p1, p2);
p1.getY() <<"); ("<< p2.getX() <<","<< p2.getY() l2.printXY();
<<")"<< endl; l2.printLength();
} l2.printAngle();
void printLength()
{ Rectangle r1(12,45,89,10,10,23,56,1);
cout<<"线段长度:"<< length << endl; r1.printPoint();
}
void printAngle() Rectangle r2(p1, p2, p3, p4);
{ r2.printPoint();
cout<<"与x轴的夹角:"<< angle }
<<"?"<< endl;
c.printArea(); 4.基类是使用极坐标的点类,从它派生一个圆类,圆类用}
点类的左边作圆心,圆周通过极坐标原点,圆类有输出圆5.设计一个线段基类,当创建五参数对象时,才要求用户心直、圆半径和面积的成员函数。完成类的设计并验证之。 输入长度。同样,其派生的直角三角形类也是在产生对象#include < iostream > 时要求输入两个直角边的长度。直角三角形在派生矩形#include < cmath > 类,矩形类的参数也由键盘输入。设计这些类并测试他们using namespace std; 的功能。
class Point//点类
{ #include < iostream >
protected: #include < cmath >
int x, y; using namespace std;
public : class Line//线段基类
Point(){} {
}; protected:
double sizeA; class Circular : public Point//圆类,继承点类 public :
{ Line()
protected: {
double r, area; cout<<"输入线段的长度:"<< endl;
public : cin>>sizeA;
Circular(int a, int b) }
{ Line(double a)
x = a; {
y = b; sizeA = a;
r = sqrt( x * x + y * y ); }
area = 3.1415926 * r * r; double getLength()
} {
void printPoint() return sizeA;
{ }
cout<<"圆形直角坐标:("<< x <<", "<< };
y <<")"<< endl; class Triangle : public Line//三角形类
} {
void printRadius() protected:
{ double sizeB, sizeC;
cout<<"圆的半径:"<< r << endl; public :
} Triangle()
void printArea() {
{ cout<<"输入线段长度:"<< endl;
cout<<"圆的面积:"<< area << endl; cin>>sizeB;
} sizeC = sqrt( sizeB * sizeB + sizeA *
}; sizeA );
void main() }
{ void printSize()
Circular c(10,25); {
c.printPoint(); cout<<"直角三角形,三条边分别为:
c.printRadius(); ";
cout<<"A: "<< sizeA << ", b: "<< sizeB 2.类Point的构造方法中的参数类型是int,所以在Line<< ", C: "<< sizeC << endl; 构造方法中的a,b应该是int型;
} 四、编程题
}; 1.使用类模板演示复制兼容性规则。 class Rectangle : public Triangle//矩形类 #include < iostream> { using namespace std;
protected: template
double sizeD; class Point
public : {
Rectangle() protected:
{ T x, y;
sizeC = sizeA; public :
sizeD = sizeB; Point(T a, T b)
} {
void printSize() x = a;
{ y = b;
cout<<"矩形,四条边分别为:"; }
cout<<"A: "<< sizeA << ", b: "<< sizeB void show() << ", C: "<< sizeC << ", D: "<< sizeD << endl; {
} cout<<"x = "<< x <<", y = "<< y <<
}; endl;
void main() }
{ };
/* Line *l = new Line(); template
cout<<"线段长度为:"<< l->getLength() << endl; class Rectangle : public Point */ {
/*Triangle *t = new Triangle(); private:
t->printSize();*/ T h, w;
public :
Rectangle *r = new Rectangle(); Rectangle(T a, T b, T c, T d) : Point(a, b)
r->printSize(); {
} h = c;
第七章 w = d;
一、单项选择 }
1.A; 2.A; 3.B; 4.D; 5.D void show() 二、填空题 {
1.rbegin(), insert(iterator it, const T& ); cout<<"x = "<< x <<", y = "<< y <<"; h
2.size(), 2; = "<< h <<", w = "<< w << endl;
3.typedef vector< 数据类型 >::reverse_iterator }
reverse_iterator };
三、改错题 void main()
1.第六行的返回类型 {
T getx() Point a(3, 4);
{ Rectangle b(5.1, 6.2, 7.3, 8.4);
return x; a.show();
} b.show();
}
Point & ra = b;//子类对象 初始化父类的引用 Line_1(const Line_1 & );//复制构造函数
ra.show(); void show()
{
Point * p = &b;//子类对象的地址,赋给指向父类的 cout<<"("<< x <<", "<< y <<"); ("<< x1
指针 <<", "<< y1 <<")"<< endl;
p->show(); }
};
Rectangle * pb = &b;//子类指针pb template Line_1 :: Line_1(const Line_1 & t) :
pb->show(); Point(t.x, t.y)
{
a = b; //派生类对象的属性值,更新 x1 = t.x1;
父类对象的属性值 y1 = t.y1;
a.show(); }
} template class Line_2 //包含point类模板,的线段类 //134页,例6.3 赋值兼容规则的例子 {
2.设计一个点的类模板,分别使用继承、包含的方法设计 protected:
线段类模板,要求演示构造函数和复制构造函数的设计方 Point p1, p2; 法,并用主程序验证之。 public :
#include < iostream> Line_2(T a, T b, T c, T d) using namespace std; {
template class Point p1.x = a;
{ p1.y = b;
public : p2.x = c;
T x, y; p2.y = d;
Point(T a=0, T b=0) }
{ Line_2(const Line_2 &);//复制构造函数
x = a; void show()
y = b; {
} cout<<"("<< p1.x <<", "<< p1.y <<");
void show() ("<< p2.x <<", "<< p2.y <<")"<< endl;
{ }
cout<<"x = "<< x <<", y = "<< y << };
endl; template Line_2 :: Line_2(const Line_2 & t)
} {
}; p1 = t.p1;
template class Line_1 : public Point // 继承Point类模 p2 = t.p2;
板, 的线段类模板 }
{ void main()
protected: {
T x1, y1; Line_1 L1(1,2,3,4);
public : cout<<"L1 : ";L1.show();
Line_1(T a, T b, T c, T d) : Point(a, b)
{ Line_1 L2(L1); //用现有的对象,初始化新对象
x1 = c; cout<<"L2 : ";L2.show();
y1 = d;
Line_2 J1(5,6,7,8); {
cout<<"J1 : ";J1.show(); int i;
int j;
Line_2 J2(J1); public:
cout<<"J2 : ";J2.show(); base(int I, int J) : i(I),j(J)
{
} display();
3.已知有一个整型数组a,其内容为1 3 5 7 9 2 4 6 8 10. }
先对数组进行升序排列,再使用它产生向量b,然后再在 int getI() const 向量的尾部追加11,并按降序排列输出向量的内容和 {
capacity()的内容。 return i;
#include < iostream > }
#include < vector > int getJ() const #include < algorithm > {
using namespace std; return j;
void main() }
{ void display() const
int a[] = {1,3,5,7,9,2,4,6,8,10}; {
sort(a, a+10);//先对数组进行升序排序 cout<<"i = "<< i <<'\t'<<"j = "<< j << endl;
copy(a, a+10, ostream_iterator(cout," ")); }
cout<< endl;
vector pa(a, a+10); //再声明向量 };
class derived : public base
pa.push_back(11);//向量尾部追加11 {
reverse_copy(pa.begin(), pa.end(), int k;
ostream_iterator(cout," "));//按降序输出向量的内容 public:
derived(int I, int J, int K) : base(I, J), k(K)
cout<<"\ncapacity : "<< pa.capacity() << endl;//输出 {
capacity()的内容 display();
} }
第八章 void display() const 一、单项选择题 {
1.A; 2.B; cout<<"i = "<< getI() <<'\t'<<"j = "<< getJ()
二、分析程序题 <<'\t'<<"k = "<< k << endl; 1. cout<<"i + k = "<< (getI()+k) <<'\t'<<"j + k
2. = "<< (getJ()+k) << endl; 三、查错题 }
print函数的参数应该是引用 };
void print(base & p)
{ void main()
p.show(); {
} base b3(8, 9); 四、完成程序题 derived d1(10, 20, 5); #include < iostream > }
using namespace std; 第九章
class base 一、单项选择
1.B; 2.A; 3.C; 4.B 5.D 6.B public :
二、填空题 Student(){}
1.输出数据按输出域右边对齐输出 Student(string n, float s) 2.cin.ignore(3) {
3.ofstream fout("Text.txt"); name = n;
三、分析程序题 score = s;
1. }
2. string getName()
四、完成程序题 {
1.#include < iostream > return name;
#include < iomanip > }
using namespace std; float getScore()
void main() {
{ return score;
cout.precision(6); }
cout<< scientific << showpos; };
cout<< -25.89F <<" "; void main()
cout<< 25.89f << endl; {
} Student s1("liming", 98); 2. Student s2("sdfh", 90); class Fun Student s3("vn.fy", 80); { Student s4("cnbtrt", 70);
friend ostream & operator<<(ostream & os, Fun f) Student s5("ryuety", 48);
{ cout.width(15); cout<< left <<"姓名"<< right <<"
os.setf(ios::left); 分数"<< endl;
return os;; cout.width(15); cout<< left << s1.getName() <<
} right << s1.getScore() << endl; }; cout.width(15); cout<< left << s2.getName() <<
right << s2.getScore() << endl; void main() cout.width(15); cout<< left << s3.getName() <<
{ right << s3.getScore() << endl;
Fun fun; cout.width(15); cout<< left << s4.getName() <<
cout<< setfill('*') << setw(10) <<12345<<" "; right << s4.getScore() << endl;
cout<< fun << setw(10) << 54321 << endl; cout.width(15); cout<< left << s5.getName() <<
} right << s5.getScore() << endl; 五、编程题 }
1.利用流格式控制,进行成绩和名字的输出,要求名字左2.编写一个产生文本文件的程序。
对齐,分数右对齐。 #include < iostream> #include < iostream > #include < fstream > #include < string > using namespace std; using namespace std; void main()
class Student {
{ char *p = {"C++程序设计"};
private : ofstream myFile("Worl9_5_2.txt");
string name; myFile<< p;
float score; }
3.编写一个程序,要求输入三角形的3条边,然后判断是 double a, b, c; 否合理,如果不合理,给出信息并要求重新输入;如果合 while(1)
理,计算其面积并将结果存入文件中。 {
//我调试这个程序的时候,发现必须关掉卡巴斯基才可以, cout<<"三角形,边A:"; 不知道为什么 cin>>a;
#include < iostream >
#include < fstream > if(a == -1)//结束符为-1
#include < cmath > {
#include < vector > ofstream writeFile; #include < iomanip > char fileName[20]; #include < string > cout<<"输入要保存到的文件名:"; using namespace std; cin>>fileName; class Triangle cout<<"保存到文件:"<< fileName << endl; { writeFile.open(fileName);
double sizeA, sizeB, sizeC, area; if(writeFile.fail())
public: {
Triangle(){} cout<<"没有正确建立文件~"<< endl;
void setArea() return;
{ }
double p = (sizeA + sizeB + sizeC) *0.5; for(int i=0; i< v.size(); i++)
area = sqrt( p * (p - sizeA) * (p - sizeB) * (p writeFile<< v[i].sizeA <<" "<< - sizeC) ); v[i].sizeB <<" "<< v[i].sizeC <<" "<< v[i].area << endl;
} writeFile.close();
void setSizeA(double a) cout<<"一共写入"<< v.size() <<"个三角形
{ 信息"<< endl;
sizeA = a; return;
} }
void setSizeB(double b) cout<<"三角形,边B:";
{ cin>> b;
sizeB = b; cout<<"三角形,边C:";
} cin>> c;
void setSizeC(double c) if( a>0 && b>0 && c>0 && a+b>c && a+c>b &&
{ b+c>a )
sizeC = c; {
}
void set(vector &); t.setSizeA(a); }; t.setSizeB(b); //*************************************** t.setSizeC(c); //* 成员函数:set t.setArea();
//* 参 数 :向量对象的引用 v.push_back(t); //* 返回值 :无 }
//* 功能 :为向量赋值并将向量存入文件 else
//*************************************** cout<<"不能组成三角形,重新输入"<< void Triangle :: set(vector & v ) endl;
{ }
Triangle t; }
void main() //* 返回值 :无
{ //* 功能 :为向量赋值并将向量存入文件
vector tri; //***************************************
Triangle triangle; void Triangle :: set(vector & v )
triangle.set(tri); {
} Triangle t;
4.改写上题的程序,使程序反复计算,直到输入结束符号 double a, b, c;
为止。要求在停止计算后,询问要保存的文件名,然后讲 while(1)
结果一次写入制定文件中。 {
//需要关掉卡巴斯基 cout<<"三角形,边A:";
#include < iostream > cin>>a;
#include < fstream >
#include < cmath > if(a == -1)//结束符为-1
#include < vector> {
#include < iomanip > ofstream writeFile; #include < string > char fileName[20]; using namespace std; cout<<"输入要保存到的文件名:";
cin>> fileName; class Triangle cout<<"保存到文件:"<< fileName << endl; { writeFile.open(fileName);
double sizeA, sizeB, sizeC, area; if(writeFile.fail())
public: {
Triangle(){} cout<<"没有正确建立文件~"<< endl;
void setArea() return;
{ }
double p = (sizeA + sizeB + sizeC) *0.5; for(int i=0; i< v.size(); i++)
area = sqrt( p * (p - sizeA) * (p - sizeB) * (p writeFile<< v[i].sizeA <<" "<< - sizeC) ); v[i].sizeB <<" "<< v[i].sizeC <<" "<< v[i].area << endl;
} writeFile.close();
void setSizeA(double a) cout<<"一共写入"<< v.size()<<"个三角形
{ 信息"<< endl;
sizeA = a; return;
} }
void setSizeB(double b) cout<<"三角形,边B:";
{ cin>>b;
sizeB = b; cout<<"三角形,边C:";
} cin>>c;
void setSizeC(double c) if( a>0 && b>0 && c>0 && a+b>c && a+c>b &&
{ b+c>a )
sizeC = c; {
}
void set(vector &); t.setSizeA(a);
}; t.setSizeB(b);
//*************************************** t.setSizeC(c);
//* 成员函数:set t.setArea();
//* 参 数 :向量对象的引用 v.push_back(t);
} 输入的字符串以“$”结束。
else //需要关掉卡巴斯基
cout<<"不能组成三角形,重新输入"<< #include < iostream > endl; #include < fstream >
} using namespace std; } void main()
void main() {
{ char a[100];
vector tri; ofstream writeFile("text.txt");
Triangle triangle; int i;
triangle.set(tri); while(1)
} {
5.从文件TEST中读出字符并写入TEST1里,要求均附 cin>>a;
加错误检查。 if(a[0] == '$') #include return;
#include i = 0;
using namespace std; while(a[i] != '\0') void main() {
{ if( a[i]>=65 && a[i]<=90 )
ifstream txt1("TEST.txt"); a[i]=a[i] + 32;
ofstream txt2("TEST1.txt"); i++;
char c; }
if(!txt1) writeFile<< a<<" ";
{ }
cout<<"文件打不开!"<< endl; }
return; 第十章
} 一、单项选择题
if(!txt2) 1.D; 2.A; 3.B; 4.D
{ 二、填空题
cout<<"没有正确建立文件!"<< endl; 1.过程抽象和数据抽象
return; 2.对象
} 3.问题域、系统边界、系统责任
while(1) 4.我觉得应该是,类的成员有(数据成员)和(成员函数)
{ 两打类。
txt1>>c; 5.
if(txt1.eof()) 四、编程题
{ 1.取消设计实例中的Cow属性,练习使用模板实现包含
txt1.close; 的设计方法。
return; #include < iostream >
} #include < cmath >
cout<< c;//打印字符 using namespace std;
txt2<< c;//写文件TEST1.txt中 //********************
} //* 声明Point类
} //******************** 6.从键盘输入一个字符串,将其中的大写字母全部转换成template
小写字母,然后存入到文件名为“text”的磁盘文件中保存。class Point
{ //*************************
T x, y; //*成员函数:Line::Dispaly
public: //*参数 :无
Point(T a= 0, T b= 0) : x(a), y(b){} //*功能 :打印线段每个点的坐标
Point(Point & a) template
{ void Line :: Display()
x = a.x; y = a.y; {
} a.Display();
void Display(); b.Display();
T Distance(Point &); }
T getX(){return x;} //*************************
T getY(){return y;} //*成员函数:Line::Distance }; //*参数 :无
//*************** //*返回值 :线段长度
//*成员函数:Point :: Display() template
//*功能 :打印点坐标 T Line :: Distance() template {
void Point :: Display() T x = a.getX() - b.getX(); { T y = a.getY() - b.getY();
cout<< x <<","<< y << endl; return sqrt( x * x + y * y ); } }
//************************** void main()
//*成员函数:Point :: Distance {
//*参数 :Point对象的引用 Point a;
//*返回值 :两点间距离 Point b(7.8, 9.8); //*返回类型:T Point c(34.5, 67.8); template a = c;
T Point :: Distance(Point & a) a.Display();
{ b.Display();
return sqrt( (x - a.x)*(x - a.x) + (y - a.y) * (y - a.y) ); cout<<"两点之距离:"<< a.Distance(b) << endl;
}
//********************** Line s(a, b); //* 声明Line Line s1(s);
//********************** cout<<"线段属性如下:"<< endl; template s1.Display(); class Line cout<<"线段长度:"<< s1.Distance()<< endl;
{ }
Point a, b; 2.取消设计实例中的Cow属性,练习使用模板实现继承
public: 的设计方法。
Line(Point & a1, Point & a2) : a(a1), b(a2){} #include < iostream >
Line(Line & s) : a(s.a), b(s.b){} #include < cmath >
void Display(); using namespace std;
T Distance(); //*********************
T getArea(); //*声明Point类
}; //********************
template
class Point
{ //***************************
protected: //*成员函数:Line::Distance()
T x, y; //*返回值 :线段长度
public: template
Point(T a = 0, T b = 0): x(a), y(b){} T Line::Distance()
Point(Point &a) {
{ T x = x2 - x;
x = a.x; y = a.y; T y = y2 - y;
} return sqrt(x*x + y*y);
virtual void Display() }
{
cout<<"X = "<< x <<", Y = "<< y << endl; //***************************
} //*成员函数:Line::Display()
T Distance(Point &); //*功能 :打印线段两个端点坐标
T getX(){return x;} template
T getY(){return y;} void Line::Display() }; {
//**************************** cout<<"x = "<< x <<",y = "<< y <<",x2 = "<< x2 //*成员函数: Point::Distance <<",y2 = "<< y2 << endl; //*参数 : Point对象的引用 }
//*返回值 : 两点间距离 //**************************** template //*友元函数:operator<<
T Point :: Distance(Point &a) //*返回值 :ostream &
{ //*功能 :重载“<< ”
return sqrt( (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y) ); template
} ostream & operator<<(ostream & stream, Line obj)
//*********************** {
//*声明Line类 stream<<"使用重载 << ,输出线段属性如下:"<< //*********************** endl;
template stream<< obj.getX() <<","<< obj.getY() <<"," class Line : public Point << obj.getX2() <<","<< obj.getY2() << endl; { return stream;
double x2, y2; }
public: void main()
Line(T, T, T, T ); {
Line(Line & ); Pointa;
void Display(); Pointb(7.8, 9.8);
T Distance(); Pointc(34.5, 67.8);
T getX2(){return x2;} a = c;
T getY2(){return y2;} a.Display();
friend void Disp(Line & t){cout< b.Display();
Line :: Line(T a1, T a2, T a3, T a4):Point(a1, a2), x2(a3), cout<<"两点距离:"<< a.Distance(b) << endl; y2(a4){}
template Line s(7.8, 9.8, 34.5, 67.8); Line :: Line(Line &s) : Point(s.x, s.y), x2(s.x2), y2(s.y2){} Disp(s);
Line s1(s);
cout<<"使用Display函数输出线段属性如下:"<< endl;
s1.Display();
cout<<"线段长度:"<< s1.Distance() << endl;
cout<<"基类对象的属性"<< endl;
a.Display();
a = s;//派生类的对象可以赋给基类
cout<<"派生类的对象赋给基类对象"<< endl;
a.Display();
cout<<"派生类的对象赋给基类的指针"<< endl;
Point *p = &s1;
p->Display();
cout<<"基类对象引用派生类的对象"<< endl;
Point &d = s1;
d.Display();
}