Develop a coordinate calculation tool, a means move to the left. D means move to the right. W means move up, s means move down.
Start from (0,0) point and read some coordinates from the input string. The input results are finally output to the output file in the polygon.
Input:
Valid coordinates are a (or D or W or s) + number (within two digits)
to divide between coordinates;
Illegal coordinate points need to be discarded. such as AA10; A1A; $%$; YAD; such as
Here is a simple sample such as:
A10; S20; W10;d30; X A1A; B10A11;; A10;
Processing process:
Starting point (0,0)
+ A10 = ( -10,0)
+ S20 = ( -10,-20)
+ W10 = ( -10,-10)
+ D30 = (20,-10)
+ x = Invalid
+ A1A = Invalid
+ B10A11 = Invalid
+ An empty does not affect
+ A10 = (10,-10)
Results (10,-10)
Example input: A10; S20; W10;d30; X A1A; B10A11;; A10;
Example output: 10. -10
#include <iostream> #include <string>using namespace Std;int x=0,y=0;void Move (string s); int main () {string Str;string temp;cin>>str;for (String::size_type index=0;index!=str.size (); ++index) {if (str[index]!= '; ') Temp.push_back (Str[index]); else{Move (temp); Temp.clear ();} cout<<x<< "," <<y<<endl;return 0;} void Move (string s) {if (S.empty ()) return;if (S.size () >3 | | s.size () <=1) return;if (! ( s[0]!= ' A ' | | s[0]!= ' S ' | | s[0]!= ' W ' | | s[0]!= ' D ')) return;if ((s[1]> ' 9 ' | | s[1]< ' 0 ') | | (S[s.size () -1]> ' 9 ' | | S[s.size () -1]< ' 0 ') return;string id;int num=0;if (s[0]== ' A ') {for (String::size_type idx=1;idx!=s.size (); ++idx) Id.push_back (S[idx]); Num=atoi (Id.c_str ()); X-=num;} if (s[0]== ' W ') {for (String::size_type idx=1;idx!=s.size (); ++idx) Id.push_back (S[idx]); Num=atoi (Id.c_str ()); Y+=num;} if (s[0]== ' s ') {for (String::size_type idx=1;idx!=s.size (); ++idx) Id.push_back (S[idx]); Num=atoi (Id.c_str ()); Y-=num;} if (s[0]== ' D ') {for (String::size_type idx=1;idx!=s.size (); ++idx) Id.push_back (S[idx]); nUm=atoi (Id.c_str ()); X+=num;}}
Huawei Test Coordinate movement