Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*output
- in: 1+2-38*45+6
- in.start == 0xbffffc18
- maxpos pos== 0xbffffc3c
- maxpos== 3
- in.start == 0xbffffbfc
- maxpos pos== 0xbffffc3c
- tmp == 1+2-38*45+6!ps%@ps%@þÿÿÿ1+2-ans == 1710
- */
- #include <iostream>
- #include <string>
- #include <cmath>
- #include <sstream>
- using std::string;
- using std::cin;
- using std::cout;
- using std::stringstream;
- using namespace std;
- string::iterator max_prsadance(string in)
- {
- //3+4*2
- string::iterator ptr = in.begin();
- string::iterator ptr2 = ptr;
- int maxpos = 0;
- for(int pos = 0; pos < in.size();pos++)
- {
- if(*ptr == *ptr2 && *ptr != '(');//if the new one is the same and is not ( do nothing
- else if(*ptr2 == '(');
- else if(*ptr == '(')
- {
- ptr2 = ptr;
- maxpos = pos;
- }
- else if(*ptr2 == '*' || *ptr2 == '/');
- else if(*ptr == '*' || *ptr == '/')
- {
- ptr2 = ptr;
- maxpos = pos;
- }
- else if(*ptr2 == '+' || *ptr2 == '-');
- else if(*ptr == '+' || *ptr == '-')
- {
- ptr2 = ptr;
- maxpos = pos;
- }
- ptr++;
- }
- return ptr2;
- }
- string::iterator start_number(string::iterator tmpptr, string in, string ops)
- {
- bool endflag = false;
- tmpptr--;
- while(tmpptr!=in.begin() && endflag != true)
- {
- // cout << endl << "ops " << ops << "\ntmpptr: " << *tmpptr;
- if(ops.find(tmpptr[0])==-1); //do nothing if it is not a +-()*/
- else
- {
- tmpptr++;
- return tmpptr;
- }
- tmpptr--;
- }
- return tmpptr;
- }
- string::iterator end_number(string::iterator tmpptr, string in, string ops)
- {
- bool startflag = false;
- tmpptr++;
- while(tmpptr!=in.end() && startflag != true)
- {
- // cout << endl << "ops " << ops << "\ntmpptr: " << *tmpptr;
- if(ops.find(tmpptr[0])==-1); //do nothing
- else
- {
- tmpptr--;
- return tmpptr;
- }
- tmpptr++;
- }
- return tmpptr;
- }
- int grab_start(string::iterator tmpmax, string::iterator instart, string ops)
- {
- string::iterator tmp = tmpmax;
- int ii = 0;
- for(;ops.find(tmp[0])==-1&&tmp!=instart;tmp++); //gets a iter to the end
- string str;
- str.assign(tmpmax, tmp); //a substring using two iters
- stringstream ss;
- ss << str;
- ss >> ii;
- //cout << "\nii: " << ii; //debug
- return ii;
- }
- int grab_end(string::iterator tmplow, string::iterator inend, string ops)
- {
- string::iterator tmp = tmplow;
- int ii = 0;
- for(;ops.find(tmp[0])==-1;tmp--); //gets a iter to the end
- ii = 0;
- string str;
- tmplow++;
- tmp++;
- str.assign(tmp, tmplow); //a substring using two iters
- stringstream ss;
- ss << str;
- ss >> ii;
- //cout << "\nii: " << ii; //debug
- return ii;
- }
- int do_math(int left, string::iterator oper, int right)
- {
- if(oper[0] == '*')
- return left*right; //to be replaced with assembly
- }
- int main()
- {
- string ops = "()+-*/";
- string in;
- string tmp;
- // getline(cin,in);
- in = "1+2-38*45+6"; //temp tsting valuses
- cout << "in: " <<in; //* i can get the end ptr, start ptr, now i should grab out the numbers */
- const string::iterator instart = in.begin();
- const string::iterator inend = in.end();
- string::iterator this_opr = max_prsadance(in);
- string::iterator max_pos = start_number(this_opr,in,ops);
- const string::iterator end_pos = max_pos;
- cout << "\nin.start == " << &in.begin();
- cout << "\nmaxpos pos== "<<&max_pos;
- string::iterator min_pos = end_number(this_opr,in,ops);
- const string::iterator start_pos = min_pos;
- int start = grab_start(max_pos,instart, ops);
- int end = grab_end(min_pos, inend, ops);
- int ans = do_math(start,this_opr,end);
- cout << "\nmaxpos== "<<max_pos[0];
- cout << "\nin.start == " << &in.begin();
- cout << "\nmaxpos pos== "<<&max_pos;
- tmp.clear();
- tmp.assign(in.begin(),end_pos);//max_pos);
- cout << "\ntmp == "<< tmp;
- cout <<"ans == "<<ans;// << " maxpos== " << maxpos;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement