Guest User

Untitled

a guest
Nov 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include "Language.h"
  5.  
  6. int main(int argc, char **argv) {
  7.  
  8. if (--argc < 2) {
  9. std::cout << "Usage: chains REGEX LENGTH" << std::endl;
  10. return 1;
  11. }
  12.  
  13. Language lang(argv[1]);
  14. lang.GenerateWords(atoi(argv[2]));
  15.  
  16. return 0;
  17. }
  18.  
  19. #include <iostream>
  20. #include <cctype>
  21. #include "Language.h"
  22.  
  23. using namespace std;
  24.  
  25. static string word;
  26.  
  27. Language::Language(string reg)
  28. {
  29. this->reg = regex(reg);
  30.  
  31. for (int i = 0; i < reg.length(); i++)
  32. if (isalpha(reg[i]))
  33. alphabet.insert(reg[i]);
  34. }
  35.  
  36. Language::Language(string alphabet, string reg)
  37. {
  38. this->reg = regex(reg);
  39.  
  40. for (int i = 0; i< alphabet.length(); i++)
  41. this->alphabet.insert(alphabet[i]);
  42. }
  43.  
  44. void Language::_GenerateWords(int i)
  45. {
  46. for (auto& letter : alphabet) {
  47. word[i] = letter;
  48. if (i < word.length() - 1)
  49. _GenerateWords(i+1);
  50. else if (regex_match(word, reg))
  51. cout << word << endl;
  52. }
  53. }
  54.  
  55. void Language::GenerateWords(int length)
  56. {
  57. word = string(length, *(alphabet.begin()));
  58.  
  59. _GenerateWords(0);
  60. }
  61.  
  62. #include <regex>
  63. #include <set>
  64.  
  65. class Language {
  66. std::set<char> alphabet;
  67. std::regex reg;
  68.  
  69. void _GenerateWords(int);
  70.  
  71. public:
  72. Language(std::string);
  73. Language(std::string, std::string);
  74. void GenerateWords(int = 1);
  75. };
Add Comment
Please, Sign In to add comment