Advertisement
Guest User

/

a guest
Jul 7th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9. /* Abstract class for all types of lexem*/
  10.  
  11. class lexeme {
  12. protected:
  13.     lexeme** children;                                      // part of tree of relations
  14.     int beginPos;                                           // number of position in string, that matches beginning of current lexem
  15.  
  16. public:
  17.     virtual void parse (char* str) = 0;                             // function for parsing string
  18.  
  19.     virtual int countRes () = 0;
  20.  
  21.     lexeme() {
  22.         // make union constructor
  23.     }
  24. };
  25.  
  26.  
  27. class digit : public lexeme {
  28. private:
  29.     char res;
  30.  
  31. public:
  32.     void parse (char* str) {   
  33.        
  34.     }
  35.    
  36.     int countRes () {
  37.         return 0;         // change to atoi(res)
  38.     }
  39.  
  40.     digit(int _beginPos, char* str) {
  41.         beginPos = _beginPos;
  42.         res = str[beginPos];
  43.         cout << "Read digit: " << res << endl;
  44.     }
  45. };
  46.  
  47.  
  48. /* Class for summand. Rule:  */
  49.  
  50. class summand : public lexeme {
  51. private:
  52.     int index;
  53.  
  54. public:
  55.     void parse (char* str) {
  56.         cout<< "Summand parsed";
  57.         if (beginPos <= strlen(str) - 1) {             
  58.             int currPos = beginPos;
  59.             index = 0;
  60.             while (str[currPos] != '+' && currPos <= (strlen(str) - 1) ) {
  61.                 if (str[currPos] != ' ' ) {
  62.                     lexeme *child;
  63.                     child = new digit(currPos, str);
  64.                     children[index] = child;
  65.                     index++;
  66.                 }
  67.                 currPos++;
  68.             }
  69.         }  
  70.     }
  71.  
  72.     int countRes () {
  73.         int res = 0;
  74.         char* getStr;
  75.         getStr = new char;
  76.         for(int i = 0; i <= index; i++) {
  77.             //getStr = getStr + children[i]->countRes;
  78.         }
  79.         cout << getStr;
  80.         //convert getStr to int and put in res
  81.         return res;
  82.     }
  83.  
  84.     summand(int _beginPos) {
  85.         beginPos = _beginPos;
  86.         children = new lexeme*;
  87.     }
  88. };
  89.  
  90.  
  91. /* Class for expression. Rule: */
  92.  
  93. class expression : public lexeme {
  94. private:
  95.     int index;
  96.  
  97. public:
  98.     void parse (char* str) {
  99.         if (beginPos != strlen(str)) {                 
  100.             int currPos = beginPos;
  101.             index = 0;
  102.             while (currPos <= (strlen(str) - 1)) { 
  103.                 if (str[currPos] != '+') {
  104.                     lexeme *child;
  105.                     child = new summand(currPos);
  106.                     child->parse(str);
  107.                     children[index] = child;
  108.                     index++;
  109.                     while(str[currPos] != '+' && currPos <= (strlen(str) - 1)){
  110.                         currPos++;
  111.                     }
  112.                 } else {
  113.                     currPos++;
  114.                 }
  115.             }
  116.         }
  117.     }
  118.  
  119.  
  120.     expression(int _beginPos) {
  121.         beginPos = _beginPos;
  122.         children = new lexeme*;
  123.     }                                                       // init of array
  124.  
  125.     int countRes () {
  126.         int res = 0;
  127.         for(int i = 0; i <= index; i++) {
  128.         //  res = res + children[i]->countRes;
  129.         }
  130.         return res;
  131.     }
  132.  
  133.     ~expression() {
  134.         // free children
  135.     }
  136. };
  137.  
  138.  
  139. int main () {
  140.     char* str;
  141.     str = new char;
  142.     cout << "Enter the string: ";
  143.     gets(str);
  144.     cout << str;
  145.     lexeme *expr = new expression(0);
  146.     expr->parse(str);
  147.     cout << "Answer:" << endl;
  148.     cout << expr->countRes;
  149.     system("pause");
  150.     return 2;
  151. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement