Help: Storing tokenized 2-digit char (infix->postfix converter)

This is a discussion on Help: Storing tokenized 2-digit char (infix->postfix converter) within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to store a tokenized cstring (consisting of a mathematical expression) into a templated char stack. However, when ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    Help: Storing tokenized 2-digit char (infix->postfix converter)

    Hi,

    I'm trying to store a tokenized cstring (consisting of a mathematical expression) into a templated char stack. However, when I print the contents of the stack, the char tokens that represented 2-digit int were cut off after the first digit. Here's an example:

    Code:
    Input:    12 + 14
    Output:  1 + 1
    I expected this. Can I get help trying to store 2-digit tokens into the stack? Perhaps convert it so I can use the tokens later to calculate in RPN?

    Here's the code that stores a cstring into the stack:

    Code:
    void Stack<E>::str2Stack(char cstring[])
    {
    	char* token;
    	token = strtok(cstring, " \n");
    	while (token != NULL)
    	{
    		push(*token);
    		cout<<"Token "<<token<<" pushed"<<endl;
    		token = strtok(NULL, " \n");
    	}
    }
    Thank you!

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    Code:
    push(*token);
    pushes a single character. You want to allocate enough space to hold strlen(token) + 1, and then pass a pointer to this new memory to push(). I think.

    Better yet, use std::string! Unless this project has severe space limitations, your life will be sweeter if you use std::string and stringstream.
    Last edited by CodeMonkey; 09-26-2011 at 07:50 AM. Reason: improvement
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    What are your exact requirements for this assignment? This is important for us to know as you are mixing C and C++ here, which is generally not a good idea unless you are required to. As for some ideas:
    1. You are going to need to know about the Shunting yard Algorithm
    2. Char arrays should be avoided, you could use std::string here instead.
    3. Strtok is a C function, you should be using std::stringstream for tokenizing.
    4. Unless you are required to create your own stack class, use std::stack.

    That is about all I can say until you let is know what your exact requirements are.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Quote Originally Posted by AndrewHunter View Post
    You are going to need to know about the Shunting yard Algorithm
    I read up on it before the project, and I incorporated it into my program.
    Quote Originally Posted by AndrewHunter View Post
    Char arrays should be avoided, you could use std::string here instead. Strtok is a C function, you should be using std::stringstream for tokenizing
    This was my first thought. I turned to c strings as a suggestion from the professor, though.

    Quote Originally Posted by AndrewHunter View Post
    Unless you are required to create your own stack class, use std::stack.
    I was.


    Here is my infix->postfix function
    Code:
    template <typename E>
    Stack<E> Stack<E>::infix2Post() throw(MismatchedParen)
    {
    	Stack<char> opStk(50);
    	Stack<char> outStk(50);
    	int i=0; 
    
    	while(isOperator(S[i])||isOperand(S[i])||S[i]=='('||S[i]==')')
    	{
    		if(isOperand(S[i]))
    		{
    			outStk.push(S[i]);
    			i++;
    		}
    		if(isOperator(S[i]))
    		{	
    			while((!opStk.empty())&&((S[1]!='^'&& precedence(S[i])<=precedence(opStk.top()))||
    				(S[1]=='^'&&precedence(S[i])<precedence(opStk.top()))))
    
    				{
    					outStk.push(opStk.top());
    					opStk.pop();
    				}
    			opStk.push(S[i]);
    			i++;
    		}
    
    		if(S[i]=='(')
    		{
    			opStk.push('(');
    			i++;
    		}
    
    		if(S[i]==')')
    		{
    			while(opStk.top()!='(')
    			{
    				outStk.push(opStk.top());
    				opStk.pop();
    			}
    
    			if(opStk.top()=='(')
    			{
    				opStk.pop();
    				i++;	
    			}
    
    			if(opStk.empty())
    				throw MismatchedParen("No closing parenthesis");
    
    	
    		}
    
    	}
    
    	while(!(opStk.empty()))
    	{
    		if(opStk.top()=='(' || opStk.top()==')')
    			throw MismatchedParen("No closing parenthesis");
    		outStk.push(opStk.top());
    		opStk.pop();
    	}
    
    	return outStk;
    
    }
    And my driver:
    Code:
    #include <iostream>
    #include"Stack.h"
    #include"Stack.cpp"				
    #include"Exception.h"
    #include<string>
    #include<cstring>
    #include<cstdlib>
    using namespace std;
    
    #pragma warning( disable : 4290 )
    
    int main()
    {	
    	char infix[50];
    	char temp[50];
    	
    	Stack<char> inStk(50);
    
    
    	cout<<"Enter an expression: ";
    	cin.getline (infix, 50, '\n' );
    
    	strcpy(temp, infix);
    
    	cout<<infix<<endl;
    
    	cout<<"NOW PRINTING LOCAL INFIX CHAR ARRAY"<<endl;
    	char* token=NULL;
    	token = strtok(temp, " \n");
    	while (token != NULL)
    	{
    		cout<<token<<endl;
    		token = strtok(NULL, " \n");
    	}
    
    	strcpy(temp, infix);
    
    	cout<<"\nNOW PUSHING INFIX CHAR ARRAY TO INFIXSTACK CLASS ARRAY"<<endl;
    	inStk.str2Stack(temp);
    
    	cout<<"\n\nNOW PRINTING INFIXSTACK CLASS ARRAY"<<endl;
    
    	inStk.printStack();
    
    	strcpy(temp, infix);
    
    	cout<<"\n\nNOW COPYING INFIXSTACK CLASS ARRAY TO POSTFIXSTACK CLASS ARRAY"<<endl;
    
    	Stack<char> postStk(50);
    
    	postStk=inStk;
    	
    	postStk.printStack();
    
    	cout<<"\n\nNOW CONVERTING INFIXSTACK CLASS ARRAY TO POSTFIXSTACK CLASS ARRAY"<<endl;
    
    	postStk=inStk.infix2Post();
    
    	cout<<"\n\nNOW PRINTING POSTFIXSTACK CLASS ARRAY"<<endl;
    	postStk.printStack();
    
    	cout<<"\n\nNOW PRINTING EVALUATING POSTFIXSTACK CLASS ARRAY"<<endl;
    	cout<<postStk.evalPost()<<endl;
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2010, 01:30 AM
  2. infix to postfix
    By Haytham in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 05:06 PM
  3. Replies: 4
    Last Post: 03-12-2006, 01:17 PM
  4. infix to postfix
    By caramel in forum C Programming
    Replies: 8
    Last Post: 11-19-2005, 09:11 AM
  5. infix to postfix
    By condorx in forum C++ Programming
    Replies: 2
    Last Post: 12-18-2002, 11:29 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21