提供一种新思路,利用atoi,实现字符串有效判断以及转换。

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

int main(void)
{
    char str[10001] = {0};
    char tmp[1000] = {0};
    int x = 0;
    int y = 0;
    int len;
    int i;
    int flag = 0;  //1-x+, 2-x-, 3-y+, 4-y-
    int tmp_idx = 0;
   
    gets(str);
    len = strlen(str);
    for (i = 0; i < len; i++) {
        if (flag != 0 && (str[i] == 'A' || str[i] == 'D' || str[i] == 'W' || str[i] == 'S')) {
            tmp_idx = 0;
            memset(tmp, 0, 1000);
            flag = 0;
        }
        
        if (str[i] == 'A') flag = 2;
        else if (str[i] == 'D') flag = 1;
        else if (str[i] == 'W') flag = 3;
        else if (str[i] == 'S') flag = 4;
        else if (str[i] == ';') {
            if (flag == 1) x += atoi(tmp);
            else if (flag == 2) x -= atoi(tmp);
            else if (flag == 3) y += atoi(tmp);
            else if (flag == 4) y -= atoi(tmp);
            tmp_idx = 0;
            memset(tmp, 0, 1000);
            flag = 0;
        } else {
            tmp[tmp_idx++] = str[i];
        }
        //printf("%c, x=%d, y=%d, %s\n", str[i], x, y, tmp);
    }
    printf("%d,%d\n", x, y);
    return 0;
}