Advertisement
rafaraj

Untitled

Mar 26th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. void trim(string *s) {
  2.     for (int i = 0; i < s->size(); i++) {
  3.         if ((*s)[i] == ' ') {
  4.             s->erase(i, 1);
  5.         }
  6.     }
  7. }
  8.  
  9. string cut(string* s, int n, int l) {
  10.     string t = s->substr(n, l);
  11.     s->erase(n, l);
  12.     return t;
  13. }
  14.  
  15. bool scontains(string &s, char c) {
  16.     for (int i = 0; i < s.length(); i++)
  17.         if (s[i] == c) return true;
  18.     return false;
  19. }
  20.  
  21.  
  22.  
  23. string F::toString() {
  24.     ostringstream oss;
  25.     oss << name;
  26.     if (FA.size() > 0) {
  27.         oss << "(";
  28.         for (int i = 0; i < FA.size() - 1; i++) {
  29.             oss << FA[i].toString();
  30.             oss << ", ";
  31.         }
  32.         oss << FA[FA.size() - 1].toString();
  33.         oss << ")";
  34.     }
  35.     return oss.str();
  36. }
  37.  
  38. F F::stringToF(string s) {
  39.     if (!scontains(s, '(')) return F(s);
  40.    
  41.     int i = 0;
  42.     while (s[i++] != '(');
  43.     string name = s.substr(0, i - 1);
  44.    
  45.     int c = 1;
  46.     int j = i;
  47.  
  48.     while (c != 0) (s[++j] == '(') ? c++ : (s[j] == ')') ? c-- : 0;
  49.     string content = s.substr(i, j - i);
  50.    
  51.     vector<string> sv;
  52.     sv.reserve(3);
  53.  
  54.     c = 0;
  55.     int o = 0;
  56.     for (int i = 0; i < content.size(); i++) {
  57.         if (content[i] == '(') c++;
  58.         else if (content[i] == ')') c--;
  59.         if (c == 0 && content[i] == ',') {
  60.             o++;
  61.             sv.push_back(cut(&content, 0, i));
  62.             content.erase(0, 1);
  63.             i = 0;
  64.         }
  65.     }
  66.     sv.push_back(content);
  67.    
  68.     F f(name);
  69.     f.FA.reserve(sv.size());
  70.     for (int i = 0; i < sv.size(); i++)
  71.         f.FA.push_back(f.stringToF(sv[i]));
  72.    
  73.     return f;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement