Topic
Develop a coordinate calculation tool, a for moving to the left, D for moving to the right, W for moving up, and s for moving down. Moves from the (0,0) point, reads some coordinates from the input string, and outputs the final input to the output file.
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
The following is a simple example:
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)
Title Category: String
Difficulty: Intermediate
Operating time limit: 10Sec
Memory Limit: 128MByte
Stage: Pre-employment practice
Input:
Line string
Output:
Final coordinates, to, separating
Sample input:
A10; S20; W10;d30; X A1A; B10A11;; A10;
Sample output:
10,-10
Code
/* ---------------------------------------* Date: 2015-06-29* sjf0115* title: Coordinate movement * Source: Huawei on-Machine--------------------------- --------------*/#include <iostream>#include <vector>#include <string>using namespace STD;voidPointmove (stringStrint&x,int&y) {intSize = Str.size ();if(Size = =0){return; }//if vector<string>VecintStart =0, end =0;//To extract the contents of two semicolons while(End! =-1) {end = Str.find (";", start); Vec.push_back (Str.substr (Start,end-start)); Start = end+1; }//while //Coordinate movement intCount = Vec.size (); for(inti =0; i < count;++i) {stringWord = vec[i];intLen = Word.size ();if(Len <1|| Len >3){Continue; }//if if(word[0] ==' A '|| word[0] ==' D '|| word[0] ==' W '|| word[0] ==' S '){intnum =0;BOOLFlag =true;//Calculate the distance traveled for(intj =1; J < Len;++j) {if(Word[j] <' 0 '|| WORD[J] >' 9 ') {flag =false; Break; }//ifnum = num *Ten+ Word[j]-' 0 '; }//for //Mobile distance illegal if(!flag) {Continue; }//if if(word[0] ==' A ') {x-= num; }//if Else if(word[0] ==' D ') {x + = num; }//else Else if(word[0] ==' W ') {y + = num; }//else Else if(word[0] ==' S ') {y-= num; }//else}//if}//for}intMain () {stringStr//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin); while(Getline (Cin, str)) {intx =0, y =0; Pointmove (Str,x,y);cout<<x<<","<<y<<endl; }//while return 0;}
[Huawei on-machine exercises] 9. Coordinate movement