目录
1. 课本上P.222~224 3. 定义并实现Triangle(三角形)类,
2. 课本上P252-1.设计一个基类:点类Point和其派生类:彩色点类ColorPoint。
5. 定义一个矩阵类,并实现两个矩阵的“+”和“-”运算符重载。
6. 定义一个多项式类,并实现两个多项式的“+”、“-”和“*”运算符重载。
1. 课本上P.222~224 3. 定义并实现Triangle(三角形)类,
其成员变量包括三个边长变量,
成员函数包括判断是否合法、计算面积,
以及是否构成直角三角形、锐角三角形和钝角三角形等函数。
并使用主函数定义其对象来完成测试。
补充知识
直角:两条短边的平方和=长边的平方和
钝角:两条短边的平方和<长边的平方和
锐角:任意两条边的平方和>第三条边的平方和
#include<iostream>
#include<cmath>
using namespace std;
//1.课本上P.222~224
// 3.定义并实现Triangle(三角形)类,
//其成员变量包括三个边长变量,
//成员函数包括判断是否合法、计算面积,
//以及是否构成直角三角形、锐角三角形和钝角三角形等函数。
//并使用主函数定义其对象来完成测试。
void max(double *a,double *b,double *c)
{
if(*a<*b)
{
double temp=*a;
*a=*b;
*b=temp;
}
if(*a<*c)
{
double temp=*a;
*a=*c;
*c=temp;
}
}
class triangle
{
private:
double a;
double b;
double c;
public:
void set(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
bool istriangle()
{
int flag=false;
if((a+b>c)&&(a+c>b)&&(b+c>a))
{
flag=true;
}
return flag;
}
double area()
{
double s=(a+b+c)/2;
double mianji=sqrt(s*(s-a)*(s-b)*(s-c));
return mianji;
}
void show()
{
max(&a,&b,&c);
cout<<"三角形三边长分别是"<<a<<'\t'<<b<<'\t'<<c<<endl;
}
void tri_kind()
{
max(&a,&b,&c);
if(pow(a,2)==pow(b,2)+pow(c,2))
{
cout<<"这是直角三角形"<<endl;
}
else if(pow(a,2)>pow(b,2)+pow(c,2))
{
cout<<"这是钝角三角形"<<endl;
}
else
{
cout<<"这是锐角三角形"<<endl;
}
}
};
int main()
{
triangle T1;
T1.set(3,4,5);
if(T1.istriangle())
{
T1.show();
T1.tri_kind();
cout<<T1.area();
}
else
{
cout<<"不是三角形";
}
return 0;
}
2. 课本上P252-1.设计一个基类:点类Point和其派生类:彩色点类ColorPoint。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
//2.课本上P252-1.设计一个基类:点类Point和其派生类:彩色点类ColorPoint。
class Point
{
protected:
int x;
int y;
double r;
public:
Point():x(0),y(0),r(0){}
double area()
{
return M_PI*pow(r,2);
}
double perimeter()
{
return 2*M_PI*r;
}
virtual void set(int a,int b,double R)
{
x=a;
y=b;
r=R;
}
};
class ColorPoint:public Point
{
char color[15];
public:
void set(int a,int b,int R,char* yanse)
{
x=a;
y=b;
r=R;
strcpy(color,yanse);
}
void show()
{
cout<<"圆心点坐标是:("<<x<<','<<y<<')'<<endl;
cout<<"面积是:"<<area()<<endl;
cout<<"周长是:"<<perimeter()<<endl;
cout<<"颜色是:"<<color<<endl;
}
};
int main()
{
ColorPoint circle1;
circle1.show();
circle1.set(3,3,5,"yellow");
circle1.show();
return 0;
}
有点好奇这个感叹好是个啥
3. 课本上P286-2编写一个存储艺术作品的程序。
艺术作品分为三类:Painting、Music和Chamber,Chamber是Music中的嵌套类。要求如下所述:
(1)每件作品都要标明著者、作品标题、作品诞生年份及其所属分类。
(2)Painting类要求显示画的宽和高等尺寸信息。
(3)Music类要求显示能够代表其中内容的关键信息,例如:“D Major”。
(4)Chamber类要求显示其中包括的演奏人员的数目。
补充知识
c++嵌套类的学习博客http://liam2199.blog.51cto.com/2879872/1172203
#include<iostream>
#include<cstring>
using namespace std;
//艺术作品分为三类:Painting、Music和Chamber,Chamber是Music中的嵌套类。要求如下所述:
//(1)每件作品都要标明著者、作品标题、作品诞生年份及其所属分类。
//(2)Painting类要求显示画的宽和高等尺寸信息。
//(3)Music类要求显示能够代表其中内容的关键信息,例如:“D Major”。
//(4)Chamber类要求显示其中包括的演奏人员的数目。
class work
{
protected:
char author[10];
char title[20];
int year;
work()
{
strcpy(author,"XXX");
strcpy(title,"XXX");
year=0;
}
};
class Painting :public work
{
private:
double length;
double width;
public:
Painting():length(0),width(0){}
void set(char*A,char*T,int y,double L,double W)
{
strcpy(author,A);
strcpy(title,T);
year=y;
length=L;
width=W;
}
void show()
{
cout<<"kind:painting"<<endl;
cout<<"author:"<<author<<endl;
cout<<"title"<<title<<endl;
cout<<"length"<<length<<endl;
cout<<"width:"<<width<<endl;//可能有问题
cout<<"year:"<<year<<endl;
}
};
class Music:public work
{
protected:
class Chamber
{
public:
int players;
Chamber():players(0){};
void set(int n)
{
players=n;
}
};
private:
char theme[30];
Chamber obj;
public:
Music(){strcpy(theme,"XXX");}
void set(char*A,char*T,int y,int n,char*zhuti)
{
strcpy(author,A);
strcpy(title,T);
strcpy(theme,zhuti);
year=y;
obj.set(n);
}
void show()
{
cout<<"kind:music"<<endl;
if(obj.players!=0)
{
cout<<"Chamber"<<endl;
}
cout<<"author:"<<author<<endl;
cout<<"title:"<<title<<endl;
cout<<"theme:"<<theme<<endl;
cout<<"year:"<<year<<endl;
cout<<"player:"<<obj.players<<endl;//可能有问题
}
};
int main()
{
Painting work1;
work1.show();
work1.set("wang","water",2000,1000,1000);
work1.show();
Music work2,work3;
work2.show();
work2.set("wu","fire",1999,0,"play fire");
work2.show();
work3.set("Jia","grass",1988,12,"Cut the grass");
work3.show();
return 0;
}
4. 课本上P286-4定义一个形状抽象类Shape,
在此基础上派生出Square类、Rectangle类、Circle类和Trapezoid类,四个派生类都有成员函数CaculateArea计算几何图形的面积,CaculatePerim计算几何图形的周长。
要求用基类指针数组,使它每一个元素指向一个派生类对象,计算并输出各自图形的面积和周长。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
//定义一个形状抽象类Shape,
//在此基础上派生出Square类、Rectangle类、Circle类和Trapezoid类
//四个派生类都有成员函数CaculateArea计算几何图形的面积,CaculatePerim计算几何图形的周长。
//要求用基类指针数组,使它每一个元素指向一个派生类对象,计算并输出各自图形的面积和周长。
class Shape
{
public:
virtual double CaculateArea(){
return 0;};
virtual double CaculatePerim(){
return 0;};
};
class Square:public Shape//正方形
{
private:
double a;
public:
void set(double x)
{
a=x;
}
double CaculateArea()
{
return pow(a,2);
}
double CaculatePerim()
{
return 4*a;
}
};
class Rectangle:public Shape
{
private:
double a,b;
public:
void set(double x,double y)
{
a=x;
b=y;
}
double CaculateArea()
{
return a*b;
}
double CaculatePerim()
{
return 2*(a+b);
}
};
class Circle:public Shape
{
private:
double r;
public:
void set(double x)
{
r=x;
}
double CaculateArea()
{
return M_PI*pow(r,2);
}
double CaculatePerim()
{
return 2*M_PI*r;
}
};
class Trapezoid:public Shape
{
private:
double a,b,h1,h2,h;
public:
void set(double x,double y,double i,double j,double k)
{
a=x;
b=y;
h1=i;
h2=j;
h=k;
}
double CaculateArea()
{
return (a+b)*h/2;
}
double CaculatePerim()
{
return (a+b+h1+h2);
}
};
int main()
{
Square S1;
S1.set(2);
Shape* X1[4];
X1[0]=&S1;
cout<<"Square_Area:"<<X1[0]->CaculateArea()<<endl;
cout<<"Square_Perim:"<<X1[0]->CaculatePerim()<<endl;
return 0;
}
5. 定义一个矩阵类,并实现两个矩阵的“+”和“-”运算符重载。
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
//5.定义一个矩阵类,并实现两个矩阵的“+”和“-”运算符重载。
//两个矩阵相加,阶数一样,直接赋值吧,就不外部输入了,就基于三阶来计算
class matrix
{
protected:
int x[3][3];
public:
void set(int n[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
x[i][j]=n[i][j];
}
}
}
void show()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<x[i][j]<<'\t';
}
cout<<endl;
}
}
matrix operator + (matrix v)
{
matrix u;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
u.x[i][j]=x[i][j]+v.x[i][j];
}
}
return u;
}
matrix operator - (matrix v)
{
matrix u;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
u.x[i][j]=x[i][j]-v.x[i][j];
}
}
return u;
}
matrix operator * (matrix v)
{
matrix u;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
u.x[i][j]=x[i][j]*v.x[i][j];
}
}
return u;
}
};
int main()
{
int x[3][3]={{1,2,3},{4,5,6},{7,8,9}};
matrix x1;
x1.set(x);
cout<<"初始数组"<<endl;
x1.show();
matrix x2;
x2.set(x);
cout<<"加法"<<endl;
matrix x3;
x3=x1+x2;
x3.show();
cout<<"减法"<<endl;
matrix x4;
x4=x1-x2;
x4.show();
cout<<"乘法"<<endl;
matrix x5;
x5=x1*x2;
x5.show();
return 0;
}
6. 定义一个多项式类,并实现两个多项式的“+”、“-”和“*”运算符重载。
#include<iostream>
#include<cmath>
//6.定义一个多项式类,并实现两个多项式的“+”、“-”和“*”运算符重载。
//多项式系数进行加减
using namespace std;
class polynomial
{
private:
int x1,x2,x3,x4,x5;
public:
polynomial(){
x1=0;x2=0;x3=0;x4=0;x5=0;
}
void set(int a1,int a2,int a3,int a4,int a5)
{
x1=a1;x2=a2;x3=a3;x4=a4;x5=a5;
}
void display()
{
cout<<"多项式:";
if(x1==0&&x2==0&&x3==0&&x4==0&&x5==0)
{
cout<<0;
return;
}
if(x5!=0)
{
cout<<x5<<"x^4";
}
if(x4!=0)
{
if(x4>0)
{
if(x5!=0)
{
cout<<'+';
}
cout<<x4<<"x^3";
}
else
{
cout<<x4<<"x^3";
}
}
if(x3!=0)
{
if(x3>0)
{
if(x4!=0)
{
cout<<'+';
}
cout<<x3<<"x^2";
}
else
{
cout<<x3<<"x^2";
}
}
if(x2!=0)
{
if(x2>0)
{
if(x3!=0)
{
cout<<'+';
}
cout<<x2<<"x";
}
else
{
cout<<x2<<"x";
}
}
if(x1!=0)
{
if(x1>0)
{
if(x2!=0)
{
cout<<'+';
}
cout<<x1;
}
else
{
cout<<x1;
}
cout<<endl;
}
//cout<<"多项式:"<<x3<<"x^2"<<<<'+'<<x3;
}
polynomial operator +(polynomial v)
{
polynomial u;
u.x1=x1+v.x1;
u.x2=x2+v.x2;
u.x3=x3+v.x3;
return u;
}
polynomial operator -(polynomial v)
{
polynomial u;
u.x1=x1-v.x1;
u.x2=x2-v.x2;
u.x3=x3-v.x3;
return u;
}
polynomial operator *(polynomial v)
{
polynomial u;
u.x1=v.x1*x1;
u.x2=v.x1*x2+v.x2*x1;
u.x3=v.x1*x3+v.x2*x2+v.x3*x1;
u.x4=v.x2*x3+v.x3*x2;
u.x5=v.x3*x3;
return u;
}
};
int main()
{
polynomial y1,y2,y3;
y1.set(1,2,3,0,0);
y1.display();
y2.set(1,1,0,0,0);
y2.display();
cout<<"加法"<<endl;
y3=y1+y2;
y3.display();
cout<<"减法"<<endl;
y3=y1-y2;
y3.display();
cout<<"乘法"<<endl;
y3=y1*y2;
y3.display();
return 0;
}