Cattle and Passenger Network & Huawei Computer Test & Coordinate Moving

Cattle and Passenger Network & Huawei Computer Test & Coordinate Moving


code implementation

#include<iostream>
#include<string>
#include<vector>
#include<cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
	string str;
	int len1 = 0;
	int len2 = 0;
    int x = 0;
	int y = 0;
    int index1 = 0;
	int index2 = 0; 

    //While (cin > > str) // Cyclic input data
    while (getline(cin, str))
    {
        len1 = str.size();
        
        while (index2 != len1)  //Judge whether';'Is the subscript to the last string, and if so, end the loop.
        {
            if (str[index2] == ';')  //Characters can be compared directly with c1==c2. Strings can be compared with strcmp (c1,c2). Equal to 0 is equal.
            {
                len2 = index2 - index1;
                if((str[index1] == 'A') || (str[index1] == 'D') || (str[index1] == 'W') || (str[index1] == 'S'))  //consider order
                {
                    if (len2 == 2)  //One letter, one number
                    {
                        if (isdigit(str[index1 + 1]) && (index2 == index1 + 2))
                        {
                            switch(str[index1])
                            {
                                case 'A':x = x - (str[index1 + 1] - '0');break;
                                case 'D':x = x + (str[index1 + 1] - '0');break;
                                case 'W':y = y + (str[index1 + 1] - '0');break;
                                case 'S':y = y - (str[index1 + 1] - '0');break;
                                default: x = x ,y = y;break;
                            }
                        }
                    }
           

                     if (len2 == 3)  //One letter, two digits
                    {
                        if (isdigit(str[index1 + 1]) && isdigit(str[index1 + 2]) && (index2 == index1 + 3))
                        {
                            switch(str[index1])
                            {
                                case 'A':x = x - ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
                                case 'D':x = x + ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
                                case 'W':y = y + ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
                                case 'S':y = y - ((str[index1 + 1] - '0') * 10 + (str[index1 + 2] - '0'));break;
                                default: x = x ,y = y;break;
                            }
                        }    
                    }
                }
                index1 = index2 + 1;
            }
            index2 = index2 + 1;
        }

        	cout << x << ',' << y << endl;
            x = 0;  //Because it is a circular input, some variables must be initialized
            y = 0;
            index1 = 0;
            index2 = 0; 
     }

	return 0;
}

Programming Notes

  • Code implementation solution ideas: 1. Loop input; 2. Next layer of loop, judgment;'Whether the subscript of the last string is the end of the loop; 3. Judgment';'Whether the characters in the middle meet the requirements; 4. After a string operation is completed, some variables must be initialized!!!! To test the next input string.
  • Familiar with the use of cctype character library.

Keywords: network Programming

Added by snarkiest on Sat, 12 Oct 2019 20:51:04 +0300