tra.h
#ifndef TRA_H
#define TRA_H
#endif
typedef struct{
double x,y;
}Coord;
class Tra{
public:
Coord a,b,c;
double len(Coord,Coord);
Tra (Coord,Coord,Coord);
private:
};
Coord getcoord();
Tra gettra();
int judge(Tra,Tra);
int trayn(Coord,Coord,Coord);
tra.cpp
#include<math.h>
#include<iostream>
using namespace std;
#include"tra.h"
double Tra::len(Coord m,Coord n){
double mn,tem1,tem2;
tem1=(m.x-n.x)*(m.x-n.x);
tem2=(m.y-n.y)*(m.y-n.y);
mn=sqrt(tem1+tem2);
return mn;
}
Tra::Tra(Coord i,Coord j,Coord k){
a=i;b=j;c=k;
}
Coord getcoord(){
double x,y;
Coord tem;
cout<<"请输入坐标:\n";
cin>>x>>y;
tem.x=x;tem.y=y;
return tem;
}
int trayn(Coord i,Coord j,Coord k){
double m;
Coord a,b;
a.x=i.x-j.x;
a.y=i.y-j.y;
b.x=k.x-j.x;
b.y=k.y-j.y;
m=a.x*b.y-a.y*b.x;
if(m==0)
return 0;
}
Tra gettra(){
Coord a,b,c;
int i;
cout<<"请输入三角形的三个坐标:\n";
as:{
a=getcoord();
b=getcoord();
c=getcoord();
i=trayn(a,b,c);}
if(i==0){
cout<<"\t【警告】这三个坐标不能构成三角形,请重新输入:\n";
goto as;
}
Tra tem(a,b,c);
return tem;
}
int judge(Tra m,Tra n){
double m_ab=m.len(m.a,m.b);
double m_bc=m.len(m.b,m.c);
double m_ac=m.len(m.a,m.c);
double n_ab=n.len(n.a,n.b);
double n_bc=n.len(n.b,n.c);
double n_ac=n.len(n.a,n.c);
double mm[3]={m_ab,m_ac,m_bc};
double nn[3]={n_ab,n_ac,n_bc};
double tem=0; int flag=0;
if(mm[0]>mm[1]) {tem=mm[0]; mm[0]=mm[1];mm[1]=tem; }
if(mm[1]>mm[2]) {tem=mm[1]; mm[1]=mm[2];mm[2]=tem;}
if(nn[0]>nn[1]) {tem=nn[0]; nn[0]=nn[1];nn[1]=tem; }
if(nn[1]>nn[2]) {tem=nn[1]; nn[1]=nn[2];nn[2]=tem;}
for(int i=0;i<3;i++){
if(mm[i]==nn[i]) {flag++;}
}
if(flag==3) return 1;
else return 0;
}
mian.cpp
#include<iostream>
using namespace std;
#include"tra.h"
int main(){
int i;
Tra tra1=gettra();
Tra tra2=gettra();
i=judge(tra1,tra2);
if(i==0) cout<<"不全等\n";
else if(i==1) cout<<"全等\n";
system("pause");
return 0;
}