C++求助,求高手帮忙改下程序
c++:建立一个二维坐标系的类TwoCoor,用x、y表示坐标值,实现两坐标点的加、减运算,计算两坐标点间的距离,并重载输入/输出运算符,使之能够直接输入/输出坐标点的坐标值程序代码:
#include<iostream> #include<cmath> using namespace std; class TwoCoor { private: double x,y; public: TwoCoor(double X=0,double Y=0):x(X),y(Y) {}; friend TwoCoor operator+(TwoCoor p1,TwoCoor p2); friend TwoCoor operator-(TwoCoor p1,TwoCoor p2); friend double dist(TwoCoor p1,TwoCoor p2); friend TwoCoor &operator<<(ostream &os,TwoCoor &t); friend TwoCoor &operator>>(istream &is,TwoCoor &t); }; TwoCoor operator+(TwoCoor p1,TwoCoor p2) { return TwoCoor(p1.x+p2.x,p1.y+p2.y); } TwoCoor operator-(TwoCoor p1,TwoCoor p2) { return TwoCoor(p1.x-p2.x,p1.y-p2.y); } double dist(TwoCoor p1,TwoCoor p2) { double x=(p1.x-p2.x); double y=(p1.y-p1.y); return sqrt(x*x+y*y); } TwoCoor &operator<<(ostream &os,TwoCoor &t) { os<<"\t"<<t.x<<"\t"; os<<t.y<<endl; return t; } TwoCoor &operator>>(istream &is,TwoCoor &t) { cout<<"坐标点的横坐标:"; is>>t.x; cout<<"坐标点的纵坐标:"; is>>t.y; return t; } int main() { TwoCoor t1,t2; char select; while(1) { cout<<"------------------坐标点计算系统-----------------------"<<endl; cout<<" 0 计算两坐标点对应坐标之和 "<<endl; cout<<" 1 计算两坐标点对应坐标之差 "<<endl; cout<<" 2 计算两坐标点的距离 "<<endl; cout<<" 3 退出系统 "<<endl; cout<<"*******************************************************"<<endl; cout<<"请输入你的选择:"; cin>>select; if(select=='0') { cout<<"请输入坐标点1的坐标:"<<endl; cin>>t1; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"点1"; cout<<t1; cout<<"---------------------------------------"<<endl; cout<<"请输入坐标点2的坐标:"<<endl; cin>>t2; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"点2"; cout<<t2; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"\t"<<"\t"<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"两坐标点的对应坐标相加为:"<<operator+(t1,t2); } else if(select=='1') { cout<<"请输入坐标点1的坐标:"<<endl; cin>>t1; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"点1"; cout<<t1; cout<<"---------------------------------------"<<endl; cout<<"请输入坐标点2的坐标:"<<endl; cin>>t2; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"点2"; cout<<t2; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"\t"<<"\t"<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"两坐标点的对应坐标相减为:"<<operator-(t1,t2); } else if(select=='2') { cout<<"请输入坐标点1的坐标:"<<endl; cin>>t1; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"点1"; cout<<t1; cout<<"---------------------------------------"<<endl; cout<<"请输入坐标点2的坐标:"<<endl; cin>>t2; cout<<"---------------------------------------"<<endl; cout<<"\t"<<"横坐标"<<"\t"<<"纵坐标"<<endl; cout<<"点2"; cout<<t2; cout<<"---------------------------------------"<<endl; cout<<"两坐标点的距离为:"<<dist(t1,t2)<<endl; } else if(select=='3') exit(0); else { cout<<"输入错误,请重新输入"<<endl; continue; } } }