首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

初学者的有关问题,代码有点长

2012-04-22 
菜鸟的问题,代码有点长#includeiostream#includeiomanipusing namespace stdchar c1,class Point

菜鸟的问题,代码有点长
#include"iostream"
#include"iomanip"
using namespace std;
char c1=',';

class Point
{

  public: static int count1,count2;
  static Point *pi;
  static Point *pj;
  Point (double a11,double c11);
  Point(double a11);
  ~Point();
  Point (Point &w);
  Point ();

  void show()
  {
  cout<<std::setprecision(16)<<"Point : "<<'('<<x1<<c1<<' '<<y1<<')'<<'\n';

  }
  double x1,y1,aa1,cc1;
  static void showCounter()
  {
  std::cout<<"Current"<<' '<<":"<<' '<<count1<<' '<< "points.\n";

  }
  double x(){return x1;}
  double x(double m){x1=m; return x1;}
  double y(double n){y1=n; return y1;}
  double y(){return y1;}
  double getX(){return x1;}
  double getY(){return y1;}
  double setX(double x){x1=x; return x1;}
  double setY(double y){y1=y; return y1;}
  Point& setPoint(double a,double b);
  double setPoint(double a)
  {
  x1=a;y1=a;
  return x1;
  }
  static void showSumOfPoint()
  {
  std::cout<<"In total : "<<count2<< " points.\n";

  }


};
  Point& Point:: setPoint(double a,double b)
  {
  x1=a;y1=b;
  return *this;
  }

  Point::Point (double a11,double c11):x1(0),y1(0)
  {
  x1=a11;y1=c11;
  count2++;
  count1++;
  }
  Point::Point ():x1(0),y1(0){ count1++;count2++;}

  Point::~Point()
  {
  count1--;
  }
  void showPoint(Point m,Point n,Point y)
  {
  double af,cf,af1,cf1,af2,cf2;

  af=m.x1;
  cf=m.y1;
  std::cout<<std::setprecision(16)<<"Point : "<<'('<<af<<c1<<' '<<cf<<')'<<'\n';
  af1=n.x1;
  cf1=n.y1;
  std::cout<<std::setprecision(16)<<"Point : "<<'('<<af1<<c1<<' '<<cf1<<')'<<'\n';
  af2=y.x1;
  cf2=y.y1;
  std::cout<<std::setprecision(16)<<"Point : "<<'('<<af2<<c1<<' '<<cf2<<')'<<'\n';
  Point::count2=Point::count2-3;
  }
  Point::Point(Point &w)
  {
  count1++;
  count2++;
  x1=w.x1;
  y1=w.y1;

  }
  Point::Point(double a11)
  {
  count1++;
  count2++;
  x1=a11;
  y1=a11;
  }
int Point::count1;
int Point::count2;







void ShowPoint(Point p)
  {
  cout<<std::setprecision(16)<<"Point : ("<<p.x()<<", "<<p.y()<<")"<<endl;


  }
void ShowPoint(double x, double y)
  {
  Point p(x, y);
  cout<<std::setprecision(16)<<"Point : ("<<p.x()<<", "<<p.y()<<")"<<endl;
  }

void ShowPoint(Point &p, double x, double y)
  {
  cout<<std::setprecision(16)<<"Point : ("<<p.x(x)<<", "<<p.x(y)<<")"<<endl;
  }

int main()
{
  int l(0);
  char c;
  double a, b;
  Point pt[60];
  while(std::cin>>a>>c>>b)
  {
  if(a == b)
  ShowPoint(pt[l].setPoint(a, b));
  if(a > b)
  ShowPoint(a, b);
  if(a < b)
  ShowPoint(pt[l], a, b);
  l++;
  }
  Point p(a), q(b);
  ShowPoint(q);
  double x(0), y(0);
  for(int i = 0; i < l; i++)
  x += pt[i].getX(), y -= pt[i].getY();
  ShowPoint(pt[l].setX(x), pt[l].setY(y));
  cout<<"==========gorgeous separator=========="<<endl;
  for(int i = 0; i <= l; i++)
  pt[i].show();
  q.setPoint(q.x() - p.x() + a, q.y() - p.y() + b).show();
  q.show();
  cout<<"==========gorgeous separator=========="<<endl;
  p.showSumOfPoint();
}
测试样例是1,2 3,3 2,1 ^Z
结果是
Point : (1, 2)
Point : (3, 3)
Point : (2, 1) 
Point : (1, 1) 
Point : (4, -3) 
==========gorgeous separator==========
Point : (1, 0)
Point : (3, 3)
Point : (0, 0)
Point : (4, -3) 
Point : (1, 0) 
Point : (1, 0) 
==========gorgeous separator========== 
In total : 66 points.
如何修改使得变成
Point : (1, 2)
Point : (3, 3) 
Point : (2, 1)
Point : (1, 1)
Point : (4, -3) 
==========gorgeous separator==========
Point[1] : (1, 0) 
Point[2] : (3, 3)
Point[3] : (0, 0) 
Point[4] : (4, -3)
Point[64] : (1, 0)
Point[64] : (1, 0) 
==========gorgeous separator========== 
In total : 66 points.


[解决办法]
给show函数加个参数,然后改变打印内容为:
void show(int index)
{
cout<<std::setprecision(16)<<"Point["<<index<<"] : "<<'('<<x1<<c1<<' '<<y1<<')'<<'\n';

}
index就是你的1,2,3,4,64,64
[解决办法]
很考智力
[解决办法]
建议楼主写程序的时候加上注释,这样方便别人理解你写的程序

热点排行