Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

camcbri

chopping up a string and using the parts.

This topic is 5939 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello All! I''m writing a program for a data structures class... The data structure stuff is easy, but I''m stuck on user input! The program is an exercise in stack usage.. I need to take in something like "2 5 + 9 3 / *" and then do some math by pushing and popping... What I need help with is pulling the numbers and operators out of the string. Right now my only solution is absolutely horrible. If anyone can point me to a way to substring without all my searching I would love you! What I''m trying to find is to have a string like "2 5 + 9 3 / *" and call substring to pull out "2" and remove the trailing space. Here is my ROTTEN way of doing it..
  
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include "stack.hpp"

using namespace std;

int DoOperation(char op, int a, int b);

int main(int argv, char* argc[])
{
   Stack<int> iStack;
   
   cout << "Enter the operation: ";
   char* operation = new char[80];
   memset(operation, ''\0'', 80);
   
   cin.getline(operation, 80);

   string sTemp(operation);
   string sTemp2;

   int  iSubStrLen = 0;
   bool bDone = false;

   //* Getting the length of a substring...

   for(unsigned int i=0; i< sTemp.length(); i++)
   {
      iSubStrLen = 0;

      char cTempOp = operation[i];
      if(cTempOp != ''+'' &&
         cTempOp != ''-'' &&
         cTempOp != ''*'' &&
         cTempOp != ''/'' &&
         cTempOp != ''%'' &&
         cTempOp != '' '')
      {
        iSubStrLen++;

         for(unsigned int j=i+1; j<sTemp.length(); j++)
         {
            if(!bDone)
            {
               if(operation[j] != ''+'' &&
                  operation[j] != ''-'' &&
                  operation[j] != ''*'' &&
                  operation[j] != ''/'' &&
                  operation[j] != ''%'' &&
                  operation[j] != '' '')
               {
                  iSubStrLen++;
               }

               if(operation[j] == '' '')
               {
                  bDone = true;
               }
            }
         }
      
         bDone = false;

         if(iSubStrLen != 0)
         {
            sTemp2 = sTemp.substr(i, iSubStrLen);
            i += (iSubStrLen-1);

            int iTemp = 0;
            sscanf(sTemp2.c_str(), "%i", &iTemp);

            iStack.Push(iTemp);
            iStack.Display();
         }
      }      
      else
      {      
         if(cTempOp == ''+'' ||
            cTempOp == ''-'' ||
            cTempOp == ''*'' ||
            cTempOp == ''/'' ||
            cTempOp == ''%'')
         {
            int a = iStack.Pop();
            int b = iStack.Pop();

            int t = 0;

            t = DoOperation(operation[i], a, b);

            iStack.Push(t);
            iStack.Display();
         }
         
      }
   }

   return 0;
}

int DoOperation(char op, int a, int b)
{
   int t = 0;

   switch(op)
   {
      case ''+'':
         cout << a << "+" << b << endl;
         t = a + b;
         break;
      case ''-'':
         cout << b << "-" << a << endl;
         t = b - a;
         break;
      case ''*'':
         cout << a << "*" << b << endl;
         t = a * b;
         break;
      case ''/'':
         cout << b << "/" << a << endl;
         t = b / a;
         break;
      case ''%'':
         cout << b << "%" << a << endl;
         t = b % a;
         break;
   }

   return t;
}

  

Share this post


Link to post
Share on other sites
Advertisement
Maybe you could start with something like this...

  
#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main()
{
string inputLine;
getline(cin, inputLine);
istringstream data(inputLine);

string token;
while (data >> token) {
// do stuff with token

cout << "<" << token << ">" << endl;
}
}

Share this post


Link to post
Share on other sites
I love you.


quote:
Original post by Beer Hunter
Maybe you could start with something like this...


    
#include <iostream>

#include <sstream>

#include <string>

using namespace std;

int main()
{
string inputLine;
getline(cin, inputLine);
istringstream data(inputLine);

string token;
while (data >> token) {
// do stuff with token

cout << "<" << token << ">" << endl;
}
}


Share this post


Link to post
Share on other sites
quote:
Original post by xtrmntr
strtok?

Dragons be here.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence.


  Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!