Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. /*output
  2. in: 1+2-38*45+6
  3. in.start == 0xbffffc18
  4. maxpos pos== 0xbffffc3c
  5. maxpos== 3
  6. in.start == 0xbffffbfc
  7. maxpos pos== 0xbffffc3c
  8. tmp == 1+2-38*45+6!ps%@ps%@þÿÿÿ1+2-ans == 1710
  9. */
  10.  
  11.  
  12.  
  13.  
  14. #include <iostream>
  15. #include <string>
  16. #include <cmath>
  17. #include <sstream>
  18.  
  19. using std::string;
  20. using std::cin;
  21. using std::cout;
  22. using std::stringstream;
  23. using namespace std;
  24.  
  25. string::iterator max_prsadance(string in)
  26. {
  27.     //3+4*2
  28.     string::iterator ptr = in.begin();
  29.     string::iterator ptr2 = ptr;
  30.     int maxpos = 0;
  31.  
  32.     for(int pos = 0; pos < in.size();pos++)
  33.     {
  34.         if(*ptr == *ptr2 && *ptr != '(');//if the new one is the same and is not ( do nothing
  35.         else if(*ptr2 == '(');
  36.         else if(*ptr == '(')
  37.         {
  38.             ptr2 = ptr;
  39.             maxpos = pos;
  40.         }
  41.         else if(*ptr2 == '*' || *ptr2 == '/');
  42.         else if(*ptr == '*' || *ptr == '/')
  43.         {
  44.             ptr2 = ptr;
  45.             maxpos = pos;
  46.         }
  47.         else if(*ptr2 == '+' || *ptr2 == '-');
  48.         else if(*ptr == '+' || *ptr == '-')
  49.         {
  50.             ptr2 = ptr;
  51.             maxpos = pos;
  52.         }
  53.         ptr++;
  54.     }
  55.     return ptr2;
  56. }
  57.  
  58. string::iterator start_number(string::iterator tmpptr, string in, string ops)
  59. {
  60.     bool endflag = false;
  61.     tmpptr--;
  62.     while(tmpptr!=in.begin() && endflag != true)
  63.     {
  64.       //  cout << endl << "ops " << ops << "\ntmpptr: " << *tmpptr;
  65.         if(ops.find(tmpptr[0])==-1);  //do nothing if it is not a +-()*/
  66.         else
  67.         {
  68.            tmpptr++;
  69.            return tmpptr;
  70.         }
  71.         tmpptr--;
  72.     }
  73.     return tmpptr;
  74. }
  75.  
  76. string::iterator end_number(string::iterator tmpptr, string in, string ops)
  77. {
  78.     bool startflag = false;
  79.     tmpptr++;
  80.     while(tmpptr!=in.end() && startflag != true)
  81.     {
  82.        // cout << endl << "ops " << ops << "\ntmpptr: " << *tmpptr;
  83.         if(ops.find(tmpptr[0])==-1); //do nothing
  84.         else
  85.         {
  86.             tmpptr--;
  87.             return tmpptr;
  88.         }
  89.         tmpptr++;
  90.     }
  91.     return tmpptr;
  92. }
  93.  
  94. int grab_start(string::iterator tmpmax, string::iterator instart, string ops)
  95. {
  96.     string::iterator tmp = tmpmax;
  97.     int ii = 0;
  98.     for(;ops.find(tmp[0])==-1&&tmp!=instart;tmp++);      //gets a iter to the end
  99.     string str;
  100.     str.assign(tmpmax, tmp);        //a substring using two iters
  101.     stringstream ss;
  102.     ss << str;
  103.     ss >> ii;
  104.     //cout << "\nii: " << ii; //debug
  105.     return ii;
  106. }
  107.  
  108. int grab_end(string::iterator tmplow, string::iterator inend, string ops)
  109. {
  110.     string::iterator tmp = tmplow;
  111.     int ii = 0;
  112.     for(;ops.find(tmp[0])==-1;tmp--);    //gets a iter to the end
  113.     ii = 0;
  114.     string str;
  115.     tmplow++;
  116.     tmp++;
  117.     str.assign(tmp, tmplow);        //a substring using two iters
  118.     stringstream ss;
  119.     ss << str;
  120.     ss >> ii;
  121.     //cout << "\nii: " << ii; //debug
  122.     return ii;
  123. }
  124.  
  125. int do_math(int left, string::iterator oper, int right)
  126. {
  127.     if(oper[0] == '*')
  128.         return left*right; //to be replaced with assembly
  129. }
  130.  
  131. int main()
  132. {
  133.     string ops = "()+-*/";
  134.     string in;
  135.     string tmp;
  136.    // getline(cin,in);
  137.     in = "1+2-38*45+6";      //temp tsting valuses
  138.     cout << "in: " <<in;        //* i can get the end ptr, start ptr, now i should grab out the numbers */
  139.    const string::iterator instart = in.begin();
  140.    const string::iterator inend = in.end();
  141.     string::iterator this_opr =  max_prsadance(in);
  142.     string::iterator max_pos = start_number(this_opr,in,ops);
  143.  
  144.     const string::iterator end_pos = max_pos;
  145.         cout << "\nin.start == " << &in.begin();
  146.     cout << "\nmaxpos pos== "<<&max_pos;
  147.     string::iterator min_pos = end_number(this_opr,in,ops);
  148.     const string::iterator start_pos = min_pos;
  149.     int start = grab_start(max_pos,instart, ops);
  150.     int end = grab_end(min_pos, inend, ops);
  151.     int ans = do_math(start,this_opr,end);
  152.     cout << "\nmaxpos== "<<max_pos[0];
  153.     cout << "\nin.start == " << &in.begin();
  154.     cout << "\nmaxpos pos== "<<&max_pos;
  155.     tmp.clear();
  156.     tmp.assign(in.begin(),end_pos);//max_pos);
  157.     cout << "\ntmp == "<< tmp;
  158.     cout <<"ans == "<<ans;// << " maxpos== " << maxpos;
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement