解释:排序与查找题目与前面传重了,这个题目也没有面向对象,嗯,没有提醒老师,我不想多写题 狗头
基于类的链表稍后整理模板的时候上传吧,这题就不另传了,tas上有基于结构体的链表实现
运算符重载
//by YewLi
#include <iostream>
#include<cmath>
using namespace std;
class Complex{ //自定义复数类型
private:
double real;
double imag;
public:
Complex(double a=0, double b=0){//构造函数
real = a;
imag = b;
}
Complex operator+(Complex a){
return Complex(a.real + real,a.imag + imag);
}
Complex operator-(Complex a){
return Complex(a.real - real,a.imag - imag);
}
bool operator==(Complex a){
return (a.real == real) && (a.imag == imag);
}
friend Complex operator*(Complex a,Complex b);
friend Complex operator/(Complex a,Complex b);
friend ostream& operator<<(ostream & os,Complex a);
};
Complex operator*(Complex a,Complex b){
return Complex(a.real*b.real - a.imag*b.imag ,a.real*b.imag + a.imag*b.real);
}
Complex operator/(Complex a,Complex b){
//if((b.real == 0) && (b.imag == 0))
return Complex((a.real*b.real + a.imag*b.imag)/(b.real*b.real + b.imag*b.imag),(a.imag*b.real - a.real*b.imag)/(b.real*b.real + b.imag*b.imag));
}
ostream& operator<<(ostream & os,Complex a){
if(a.imag > 0){
os<<a.real<<" + "<<a.imag<<"i";
return os;
}
if(a.imag == 0){
os<<a.real;
return os;
}
if(a.imag < 0){
os<<a.real<<" - "<<abs(a.imag)<<"i";
return os;
}
}
int main(){
Complex com1(20,1),com2(10,19);
cout<<"com1 + com2 = "<<com1 + com2<<endl;//此处仅仅是为了说明需要重载运算符"<<"。
cout<<"com1 - com2 = "<<com1 - com2<<endl;
cout<<"com1 * com2 = "<<com1 * com2<<endl;
cout<<"com1 / com2 = "<<com1 / com2<<endl;
}
这好像是上课顺便跟着写的,没再调,问题应该不大,输入参数是我随手写的。
加减乘除都好说,输出流重载记得ostream &os 然后输出到os 返回os即可。
模板题,无内鬼。
//by YewLi
// 熟悉类的声明,定义和使用对象
#include <iostream>
#include <cmath>
#define pi 3.14
using namespace std;
class Point{
public:
double x,y;
Point(double xx = 0,double yy = 0):x(xx),y(yy){
}
void print(){
cout<<"Point is x: "<<x<<"y: "<<y<<endl;
}
double distance(Point &p){
return sqrt(abs(x - p.x)*abs(x - p.x) + abs(y - p.y)*abs(y - p.y));
}
//在此定义构造函数
//在屏幕上打印Point对象的函数
//计算这个点到另外一个点的距离的函数
};
class Circle{
public:
Point location; //圆心的坐标
double r; //圆半径
public:
Circle(double a,double b,double c){
location.x = a,location.y = b,r = c;
}
int stateOfCircle(Circle c){
if(location.distance(c.location) == 0)
return 0;//同心
if(location.distance(c.location) == abs(r - c.r) || location.distance(c.location) == abs(r + c.r))
return 1;//相切
if(location.distance(c.location) < abs(r - c.r) || location.distance(c.location) > abs(r + c.r))
return 2;//相离
if(location.distance(c.location) > abs(r - c.r) || location.distance(c.location) < abs(r + c.r))
return 3;//相交
}
double area(){
return pi*r*r;
}
//构造函数
//两个圆的位置关系函数 stateOfCircle(Circle c)
//计算圆面积的函数 area( )
//计算两个圆的圆心距离 distance(Point p)
void print(){}
};
int main(){
Circle c1(19,10,2),c2(15,12,2);
int fl = c1.stateOfCircle(c2);
if(fl == 0)
cout<<"同心"<<endl;
else if(fl == 1)
cout<<"相切"<<endl;
else if(fl == 2)
cout<<"相离"<<endl;
else
cout<<"相交"<<endl;
return 0;
}
圆和点,题目要求定义面积函数,但可能没啥用,我也就没调试。
主程序调试是手动赋的值可以更改调试。
point类作为circle的一个成员注意构造函数附一个初始值,否则可能导致只传一个参数给Point构造函数无法匹配。
然后判断就行,无内鬼++