A specific format of a string with a number and a character together representing a specific element

I have a line that looks like "a 3e, 6s, 1d, 3g, 22r, 7c 3g, 5r, 9c 19.3" , how do I go through it and extract integers and assign them to the corresponding letter variable ?. (I have integer variables d, r, e, g, s and c). The first letter in the line is a function, “3e, 6s, 1d, 3g, 22r, 7c” and “3g, 5r, 9c” are two separate containers. And the last decimal value is the number that needs to be broken down into these variable numbers.

my problem is to extract these integers with letters after it and assign them to the corresponding letter. and any number with a negative sign or a space between the number and the letter is invalid. How am i doing this?

+3
source share
3 answers

The description of the string format is not entirely clear, but I think I can answer your question anyway (extracting integers with letters and adding (?) Them to the corresponding int variable).

So starting from this line:

char * was = "3e, 6s, 1d, 3g, 22r, 7c"; // was == weird ass string

probably the easiest way to use it is using strtok .

char* token = strtok (was,",");
while (token != NULL) {
    assign(token); // first token is 3e, second 6s etc...
    token = strtok (NULL, ",");
}

sscanf, .

void assign(char* token) {
    char letter;
    int number;
    if (0 != sscanf(token, "%d%c", number, letter)) {
        // the first token produces letter 'e' and number '3'
        // now you can switch on letter and add number 
        // to the proper variable in each case
    } else {
        //matching failure!!
    }
}

( (?)), . , , , .

, , , sscanf.

+1

. .

, , , :

^([a-zA-Z]) (-?\d{1,2}) ?e,(-?\d{1,2}) ?s,(-?\d{1,2}) ?d,(-?\d{1,2}) ?g,(-?\d{1,2}) ?r,(-?\d{1,2}) ?c (-?\d{1,2}) ?g,(-?\d{1,2}) ?r,(-?\d{1,2}) ?c ([0-9.]{1,4})

, , .

, :

#include <regex>

using std::string;
using std::tr1::cmatch;
using std::tr1::regex;

const regex pattern("\\.([^\\.]+)$");
cmatch result;

string dateiname("test.abc");
string erweiterung;

if(regex_search(dateiname.c_str(), result, pattern) == true)
    erweiterung = result[1];
+3

A simple state machine looks like a way to do this. I am not sure that the rules you have given are quite complete. In particular, I do not understand the function of spaces or what you mean by “separate containers”. You should add more code to check for invalid states, but this should get you started.

// the string we want to parse.
char * psz = "a 3e,6s,1d,3g,22r,7c 3g,5r,9c 19.3";

// this is the states that our parser can be in.
enum {
   state_init,
   state_number,
   state_letter,
   state_comma,
   state_space,
   state_decimal,
   };

// storage for our letter values
int letter_vals['z' - 'a' + 1] = 0;

int val = 0; 
int state = state_init;
while (psz[0])
{
   char ch = psz[0];
   if (ch >= '0' && ch <= '9')
   {
      if (state == state_decimal)
      {
         // this is the last value that needs special treatment.
         double dval = (double)val + (ch / 10.0);
      }
      else if (state == state_number)
      {
         val = (val * 10) + ch - '0';
      }
      else
      {
         // we expect state to be state_space or state_comma here
         val = ch;
      }

      state = state_num;
   }
   else if (ch >= 'a' && ch <= 'z')
   {
      if (state == state_num)
      {
         letter_vals[ch - 'a'] = val;
         val = 0;
      }
      else if (state == state_init)
      {
         // ch is our "function"
      }
      else
      {
         // this is a letter that isn't after a number 
      }
      state = state_letter;
   }
   else if (ch == ',')
   {
      // state should be state_letter here
      state = state_comma;
   }
   else if (ch == ' ')
   {
      if (state == state_number)
      {
         // a space in the middle of the number or after a number is invalid!
      }
      else if (state == state_letter)
      {
         // this is a space after a letter, this means what?
      }
      else if (state == state_space)
      {
         // are multiple spaces invalid?
      }
      state = state_space;
   }
   else if (ch == '.')
   {
      if (state == state_number)
      {
         // this is normal 
      } 
      else
      {
         // this is an invalid state, a decimal not inside a number.
      }
      state = state_decimal;
   }
   else if (ch == '-')
   {
      // this is an invalid character
   }
   else
   {
      // this is an invalid letter.
   }


   ++psz;
}
+2
source

Source: https://habr.com/ru/post/1736765/


All Articles