![]() |
#2
rjsp2017-10-21 16:30
|

#include<iostream>
using namespace std;
class mypoint {
int x, y;
public:
mypoint(int X, int Y) :x(X), y(Y) {}
void set(int X, int Y) { x = X, y = Y; }
int getx()const { return x; }
int gety()const { return y; }
void print()const
{
cout << "(" << x << "," << "y" << ")";
}
void left() { x--; }
void right() { x++; }
void down() { y++; }
void up() { y--; }
bool equal(mypoint p)const
{
if (x == p.x&&y == p.y)
return true;
else return false;
}
};
enum { LEFT, RIGHT, DOWN, UP };
class myobject {
private:
mypoint position;
int size;
int weight;
public:
myobject(int x, int y, int s, int w) :position(x, y) {
size = s; weight = w;
}
myobject(mypoint p, int s, int w) :position(p) {
size = s; weight = w;
}
~myobject() {}
void move(int d)
{
switch (d)
{
case LEFT:position.left(); break;
case RIGHT: position.right(); break;
case DOWN: position.down(); break;
case UP:position.up(); break;
}
}
bool reach(mypoint p)
{
if (position.equal(p))
return true;
else return false;
}
void show()const
{
position.print(); cout << ""<<size << "" << weight << "" << endl;
}
};
int cmdtod(char c)
{
c = toupper(c);
switch (c)
{
case'A':return LEFT;
case'S':return DOWN;
case'D':return RIGHT;
case'W':return UP;
}
return -1;
}
int main()
{
mypoint start(0,0),target(10,5);
myobject ob(start, 1, 1);
cout << "初始位置:";
start.print();
cout << "目标位置:";
target.print();
while (!ob.reach(target))
{
char cmd;
cin >> cmd;
int d = cmdtod(cmd);
if (d ==-1)
continue;
ob.move(d);
ob.show();
}
cout << 到达目标";
ob.show();
system("pause");
return 0;
}
只有本站会员才能查看附件,请 登录