描述
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。例如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
处理过程: 起点(0,0)
-
A10 = (-10,0)
-
S20 = (-10,-20)
-
W10 = (-10,-10)
-
D30 = (20,-10)
-
x = 无效
-
A1A = 无效
-
B10A11 = 无效
-
一个空 不影响
-
A10 = (10,-10)
结果 (10, -10)
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int x = 0, y = 0;
string in_str;
getline(cin, in_str);
vector<string> vec;
stringstream ss(in_str);
string s;
cout << "in_str\t" << in_str << endl;
while (getline(ss, s, ';')) {
vec.emplace_back(s);
cout << "s\t" << s << endl;
}
for (int i = 0; i < vec.size(); i++) {
if (vec[i][0] == 'A') {
x -= atoi(s.c_str() + 1);
}
if (vec[i][0] == 'S') {
y -= atoi(s.c_str() + 1);
}
if (vec[i][0] == 'W') {
y += atoi(s.c_str() + 1);
}
if (vec[i][0] == 'D') {
x += atoi(s.c_str() + 1);
}
}
cout << x << ',' << y << endl;
return 0;
}