标题:初学c++我赋了初始值,但是y坐标的没显示,运行起来也有问题
只看楼主
遗情处有诗章
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2017-3-10
结帖率:75%
已结贴  问题点数:15 回复次数:2 
初学c++我赋了初始值,但是y坐标的没显示,运行起来也有问题
程序代码:

#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;
}






搜索更多相关主题的帖子: int void return position case 
2017-10-21 11:52
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:15 
cout << "(" << x << "," << "y" << ")";
改为
cout << "(" << x << "," << y << ")";
2017-10-21 16:30
遗情处有诗章
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2017-3-10
得分:0 
回复 2楼 rjsp
谢谢啦
2017-10-21 17:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481619-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.323012 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved