定义一个二维坐标结构position,用(x,y)来表示其坐标,又分别定义出A、D、S、W类方法来表示其移动。思路是对输入的字符串按分号进行切割,把切割出来的字符串apply到position对象上,apply时按题目要求对数据进行检查

#include <stdio.h>
#include <string>
#include <iostream>

using namespace std;
class position
{
private:
    int m_x;
    int m_y;
    void A(int dist)
    {
        m_x -= dist;
    }
    void D(int dist)
    {
        m_x += dist;
    }
    void S(int dist)
    {
        m_y -= dist;
    }
    void W(int dist)
    {
        m_y += dist;
    }
public:
    position(int x, int y)
    {
        m_x = x;
        m_y = y;
    }

    void output()
    {
        printf("%d,%d\n", m_x, m_y);
    }
    void apply(const string& str)
    {
        int dist = 0;
        if(str.length() == 2)
        {
            if(str[1] >= '0' && str[1] <= '9')
            {
                dist = str[1] -'0';
            }
        }
        else if(str.length() == 3)
        {
            if(str[1] >= '0' && str[1] <= '9' && str[2] >= '0' && str[2] <= '9')
            {
                dist = (str[1]-'0') * 10 + (str[2]-'0');
            }
        }
        else
        {
            //不合法,直接返回;
            return;
        }
        switch(str[0])
        {
            case 'A':
                A(dist);
                break;
            case 'D':
                D(dist);
                break;
            case 'S':
                S(dist);
                break;
            case 'W':
                W(dist);
                break;
            default:
                break;
        }
    }
};
int main()
{
    string str;
    while(cin >> str)
    {
        position pos(0,0);
        while(str.length() > 0)
        {
            size_t offset = str.find(';');
            string tmp = str.substr(0,offset);
            pos.apply(tmp);
            str = str.substr(offset+1);
        }
        pos.output();
    }
    return 0;
}