以下是一个输入三点判断并求三角形面积的程序
可能是运算符>>重载的问题,哪位能帮忙指点一下。
#include<iostream> #include<cstdlib> #include<cmath> using namespace std; class point { private: double x,y; public: point(double xx=0,double yy=0){x=xx;y=yy;} friend void triangle(point &,point &,point &); friend istream &operator>>(istream &,point &); }; void triangle(point &p1,point &p2,point &p3) { double k=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)), l=sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y)), s=sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y)); if(k+l>s && k-l<s) { double m=(k*k+l*l-s*s)/(2*k*l); //这里应该注意一下,一定要括号 cout<<"能构成三角形,面积为:"<<0.5*k*l*sqrt(1-m*m)<<endl; } else cout<<"不能构成三角形"<<endl; } istream &operator>>(istream &input,point &p) { cin>>p.x>>p.y; return input; } int main() { point p1,p2,p3; cout<<"请输入三个点:"; cin>>p1>>p2>>p3; triangle(p1,p2,p3); system("pause"); }