Advertisement
aircampro

parse string for chars, money, numbers

Jun 9th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.27 KB | None | 0 0
  1. // =========================================
  2. // parse a string for words,money or numbers
  3. // report each type count
  4. //=========================================
  5. #include <iostream>
  6. #define COMMA_SEPARATE
  7. //#define REMOVE_CURRENCY
  8. //#define BALTIC_CODEPAGE_775
  9. //#define CYRILLIC_CODE_PAGE_855
  10. //#define CYRILLIC_CODE_PAGE_866
  11.  
  12. struct parsed_word_t {
  13.   unsigned char ourText[100];
  14.   unsigned char ourNums[100];
  15.   unsigned char ourMoneys[100];
  16.   int letters;
  17.   int digits;
  18.   int spaces;
  19.   int controlChars;
  20.   int symbols;  
  21. };
  22.  
  23. void statOfStr(unsigned char* str, int* letters, int* digits, int* symbols, int* spaces, int* controlChars)
  24. {
  25.     while (*str)
  26.     {
  27.         if ((*str >= 65 and *str <= 90) or (*str >= 97 and *str <= 122)) { (*letters)++; }
  28.         else if (*str >= 48 and *str <= 57) { (*digits)++; }
  29.         else if (*str == 32 or *str == 9) { (*spaces)++; }
  30.         else if (*str <= 31) { (*controlChars)++; }
  31.         else { (*symbols)++; }
  32.         str++;
  33.     }
  34. }
  35.  
  36. // this expression will parse the incoming message counting charactures
  37. // and placing message, money or numbers into separate buffers
  38. // if you define COMMA_SEPARATE it will separate the input message strings and moneys by commas
  39. // e.g. bread $10 milk $1.12 sugar £1.54 shirt £25.60
  40. // bread , milk, sugar, shirt,
  41. // $10, $1.12, £1.54, £25.60
  42. // so you can send an update from the user to the bot to update a price map for example
  43. void parseExpression(unsigned char* str, parsed_word_t* parsedObj)
  44. {
  45.     int cnt = 0;
  46.     int cnt1 = 0;
  47.     int cnt2 = 0;
  48.     int lastItem = 0;
  49.  
  50.     while (*str)
  51.     {
  52.         if ((*str >= 65 and *str <= 90) or (*str >= 97 and *str <= 122))   /* normal letters */
  53.         {
  54. #if defined(COMMA_SEPARATE)
  55.             if (lastItem == 5)
  56.             {
  57.                parsedObj->ourMoneys[cnt1] = ',';
  58.                ++cnt1;
  59.             }
  60. #endif
  61.             (parsedObj->letters)++;
  62.             parsedObj->ourText[cnt] = *str;
  63.             ++cnt;
  64.             lastItem = 1;
  65.         }
  66. #if defined(BALTIC_CODEPAGE_775)
  67.         else if (((((((*str >= 128 and *str <= 149) or (*str >= 151 and *str <= 155)) or (*str == 157)) or (*str >= 160 and *str <= 165)) or (*str >= 198 and *str <= 199)) or (*str >= 207 and *str <= 216)) or (*str >= 224 and *str <= 238)) /* extended letters */
  68.         {
  69. #if defined(COMMA_SEPARATE)
  70.             if (lastItem == 5)
  71.             {
  72.                parsedObj->ourMoneys[cnt1] = ',';
  73.                ++cnt1;
  74.             }
  75. #endif
  76.             (parsedObj->letters)++;
  77.             parsedObj->ourText[cnt] = *str;
  78.             ++cnt;
  79.             lastItem = 1;
  80.         }
  81. #elif defined(CYRILLIC_CODE_PAGE_855)
  82.         else if ((((((((*str >= 128 and *str <= 173) or (*str >= 181 and *str <= 184)) or (*str >= 189 and *str <= 190)) or (*str >= 198 and *str <= 199)) or (*str >= 208 and *str <= 216)) or (*str >= 221 and *str <= 222)) or (*str >= 224 and *str <= 238)) or (*str >= 241 and *str <= 252)) /* extended letters code page 855 Cyrillic */
  83.         {
  84. #if defined(COMMA_SEPARATE)
  85.             if (lastItem == 5)
  86.             {
  87.                parsedObj->ourMoneys[cnt1] = ',';
  88.                ++cnt1;
  89.             }
  90. #endif
  91.             (parsedObj->letters)++;
  92.             parsedObj->ourText[cnt] = *str;
  93.             ++cnt;
  94.             lastItem = 1;
  95.         }
  96. #elif defined(CYRILLIC_CODE_PAGE_866)
  97.         else if ((*str >= 128 and *str <= 175) or (*str >= 224 and *str <= 247))  /* extended letters code page 855 Cyrillic */
  98.         {
  99. #if defined(COMMA_SEPARATE)
  100.             if (lastItem == 5)
  101.             {
  102.                parsedObj->ourMoneys[cnt1] = ',';
  103.                ++cnt1;
  104.             }
  105. #endif
  106.             (parsedObj->letters)++;
  107.             parsedObj->ourText[cnt] = *str;
  108.             ++cnt;
  109.             lastItem = 1;
  110.         }
  111. #endif // end code page choice
  112.         else if (*str >= 48 and *str <= 57)  /* its a number */
  113.         {
  114.             (parsedObj->digits)++;
  115.             if ((lastItem == 5) || (lastItem == 7))
  116.             {
  117.               parsedObj->ourMoneys[cnt1] = *str;
  118.               ++cnt1;
  119.               lastItem = 5;
  120.             }
  121.             else
  122.             {
  123.               parsedObj->ourNums[cnt2] = *str;
  124.               ++cnt2;
  125.               lastItem = 2;
  126.             }
  127.         }
  128.         else if ((*str == 13) or (*str == 10)) /* CR or LF */
  129.         {
  130.             parsedObj->ourText[cnt] = ' ';
  131.             ++cnt;
  132.             lastItem = 9;
  133.         }
  134.         else if (*str == 46)   /* full stop */
  135.         {
  136.             (parsedObj->digits)++;
  137.             if (lastItem == 5)
  138.             {
  139.                parsedObj->ourMoneys[cnt1] = *str;
  140.                ++cnt1;
  141.                lastItem = 7;
  142.             }
  143.             else
  144.             {
  145.                parsedObj->ourNums[cnt2] = *str;
  146.                ++cnt2;
  147.                lastItem = 3;
  148.             }
  149.         }
  150.         else if (*str == 32 or *str == 9) /* space or tab */
  151.         {
  152.             (parsedObj->spaces)++;
  153.             switch (lastItem)
  154.             {
  155.                case 1:
  156.                parsedObj->ourText[cnt] = *str;
  157.                ++cnt;
  158.                break;
  159.  
  160.                case 2:
  161.                parsedObj->ourNums[cnt2] = *str;
  162.                ++cnt2;              
  163.                break;
  164.  
  165.                case  3:
  166. #if defined(COMMA_SEPARATE)
  167.                parsedObj->ourNums[cnt2] = '0';
  168.                parsedObj->ourNums[cnt2+1] = ',';  
  169.                parsedObj->ourNums[cnt2+2] = *str;
  170.                cnt2+=3;
  171. #else
  172.                parsedObj->ourNums[cnt2] = '0';
  173.                parsedObj->ourNums[cnt2+1] = *str;
  174.                cnt2+=2;
  175. #endif
  176.                break;
  177.  
  178.                case  5:
  179. #if defined(COMMA_SEPARATE)
  180.                parsedObj->ourMoneys[cnt1] = ',';              
  181.                parsedObj->ourMoneys[cnt1+1] = *str;
  182.                cnt1+=2;
  183. #else
  184.                parsedObj->ourMoneys[cnt1] = *str;
  185.                ++cnt1;
  186. #endif
  187.                break;
  188.  
  189.                case 6:
  190.                parsedObj->ourText[cnt] = *str;
  191.                ++cnt;
  192.                break;
  193.  
  194.                case  7:
  195.     #if defined(COMMA_SEPARATE)
  196.                parsedObj->ourMoneys[cnt1] = '0';
  197.                parsedObj->ourMoneys[cnt1+1] = ',';
  198.                parsedObj->ourMoneys[cnt1+2] = *str;
  199.                cnt1+=3;
  200.     #else
  201.                parsedObj->ourMoneys[cnt1] = '0';
  202.                parsedObj->ourMoneys[cnt1+1] = *str;
  203.                cnt1+=2;
  204.     #endif
  205.                break;
  206.  
  207.                case 8:
  208.                parsedObj->ourText[cnt] = *str;
  209.                ++cnt;
  210.                break;
  211.              
  212.             }
  213.             lastItem = 4;
  214.         }
  215.         else if ((*str == 36) || ((*str == 163) || (*str == 219)))    // preceded by a currency identifier of $ £ or euro
  216.         {
  217. #if defined(COMMA_SEPARATE)
  218.             parsedObj->ourText[cnt] = ',';
  219.             ++cnt;  
  220. #endif  
  221. #if defined(REMOVE_CURRENCY)
  222.             lastItem = 5;
  223. #else        
  224.             parsedObj->ourMoneys[cnt1] = *str;
  225.             ++cnt1;
  226.             lastItem = 5;
  227. #endif
  228.         }
  229.         else if (*str <= 31)                /* control char */
  230.         {
  231. #if defined(COMMA_SEPARATE)
  232.             if (lastItem == 5)
  233.             {
  234.                parsedObj->ourMoneys[cnt1] = ',';
  235.                ++cnt1;
  236.             }
  237. #endif
  238.             (parsedObj->controlChars)++;
  239.             lastItem = 6;
  240.         }
  241.         else                                  /* it was a symbol */
  242.         {
  243. #if defined(COMMA_SEPARATE)
  244.             if (lastItem == 5)
  245.             {
  246.                parsedObj->ourMoneys[cnt1] = ',';
  247.                ++cnt1;
  248.             }
  249. #endif
  250.             (parsedObj->symbols)++;
  251.             lastItem = 8;
  252.         }
  253.         str++;
  254.     }
  255. }
  256.  
  257. int main()
  258. {
  259.     //unsigned char str[170]{ "Hello! How are you?\n4.5.0. $35.0 æble £12. this is the text --11.74 120 $11.76 -- $16000. £11.57 and & spaces!!! new word" };
  260.     unsigned char str[170]{ "dog food : $35.0  bread : £12. random numbers grøn : 11.74 æble £1.11 120  records $11.76 -- new car , $16000. milk £11.57 and & spaces!!! čo to je" };
  261.     //unsigned char str[170]{ "dog food : $3.10  bread : £12.  grøn æble : 11.74 raw whole milk £1.11 leather jacket 120  records $11.76 -- new car , $16000. milk £11.57 and & spaces!!! čo to je" };
  262.     int letters{ 0 };
  263.     int digits{ 0 };
  264.     int symbols{ 0 };
  265.     int spaces{ 0 };
  266.     int control{ 0 };
  267.     parsed_word_t pWord;
  268.  
  269.     std::cout << str << "\n\n";
  270.     statOfStr(str, &letters, &digits, &symbols, &spaces, &control);
  271.     std::cout << "In this string there are:\n" << letters << " letters\n" << symbols << " symbols\n" << digits << " digits\n" << control << " control chars.\n" << spaces << " spaces\n";
  272.  
  273.     parseExpression(str, &pWord);
  274.     std::string printitout;
  275.     std::string printitmoneys;
  276.     std::string printitnumbers;
  277.     char * pout = (char*) &pWord.ourText[0];
  278.     printitout.append(pout);
  279.     pout = (char*) &pWord.ourMoneys[0];
  280.     printitmoneys.append(pout);
  281.     printf("%s",pWord.ourNums);
  282.     pout = (char*) &pWord.ourNums[0];
  283.     printitnumbers.append(pout);
  284.     std::cout << printitout << "\nmoney " << printitmoneys << "\nnums " << printitnumbers << std::endl;
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement