CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    May 2010
    Posts
    12

    Help fix my code. +1000 internets for you!

    My program evaluates a postfix expression. I'm trying to edit my code so that the user does not need to enter the equal sign '=' as part of the input. I have two solutions but am having a very hard time implementing either of them. I can either: input the '=' for the user, or implement an EOF to act as the equal sign. Please help me implement either of these.

    below is my code:
    Code:
    //This programm is post fix calculator
    #include "stdafx.h" 
    #include <iostream> 
    using namespace std; 
    
    struct pq {
    
    	char st[15]; 
    	pq *next;
    
    };
    
    struct es 
    {
    	int value; 
    	es* next;
    
    };
    
    
    bool is_integer(char answer[]); 
    es *push(es *h, int n); 
    es *pop_es_head(es *es_head);
    pq *pop_pq_head(pq *head);
    void printlistpq(struct pq *p);
    void printlistes(struct es *e);
    
    
    int main() 
    {
    	pq *head, *current, *temp; 
    	string s;
    
    	head = new pq; 
    	cout << "Enter the number and signs"<<endl; 
    	cin >> head->st;
    
    	if(head->st[0] != '=') 
    	{
    		head->next = NULL;
    		current = head;
    		temp = new pq; 
    		cin >> temp->st;
    
    		while(temp->st[0] != '=') 
    		{
    			temp->next = NULL;
    			current->next = temp;
    			current = current->next;
    			temp = new pq; 
    			cin >> temp->st;
    		}
    		delete temp; 
    	}
    	printlistpq(head);
    	es *es_head; 
    	bool integer; 
    	int result; 
    	integer = is_integer(head->st);
    
    	if(integer == true) 
    	{
    		es_head = new es; 
    		es_head->value = atoi(head->st);
    		es_head->next = NULL;
    		temp = head->next;
    		delete head; 
    		head = temp;
    
    		while(head != NULL) 
    		{
    			integer = is_integer(head->st);
    			if(integer == true) 
    			{
    				es_head = push(es_head, atoi(head->st));
    				printlistpq(head);
    			}
    			else 
    			{
    				switch(head->st[0])
    				{
    				case '+': 
    					result = es_head->value + es_head->next->value; 
    					printlistpq(head);
    					es_head = pop_es_head(es_head);
    					es_head = pop_es_head(es_head);
    					es_head = push(es_head, result);
    					break; 
    				case '-': 
    					result = es_head->next->value - es_head->value; 
    					printlistpq(head);
    					es_head = pop_es_head(es_head);		
    					es_head = pop_es_head(es_head);	
    					es_head = push(es_head, result);
    					break; 
    				case '*': 
    					result = es_head->value * es_head->next->value; 
    					printlistpq(head);
    					es_head = pop_es_head(es_head);
    					es_head = pop_es_head(es_head);
    					es_head = push(es_head, result);
    					break; 
    				case '/': 
    					result = es_head->next->value / es_head->value; 	
    					printlistpq(head);
    					es_head = pop_es_head(es_head);
    					es_head = pop_es_head(es_head);
    					es_head = push(es_head, result);
    					break; 
    				default: cout << "Invalid operator.";
    				} 
    			} 
    			head = pop_pq_head(head);
    		} 
    
    	} 
    	printlistpq(head);
    	if (head == NULL) 
    	{
    		cout << "The answer is " << es_head->value << "."; 
    	} 
    	system("pause");
    	return 0; 
    }
    
    
    
    bool is_integer(char answer[])         //  checks the input contains non-integer characters.
    {
    	bool integer = true; 
    	int len = int(strlen(answer)); 
    
    	for (int i = 0; i < len; i++) 
    	{
    		if (answer[i] <= '9' && answer[i] >= '0') 
    			integer = true; 
    		else 
    		{
    			integer = false; 
    			break; 
    		}
    	}
    	return integer; 
    } 
    
    
    
    es *push(es *h, int n)  // pushes values into the evaluation stack
    {
    	es *temp;
    	temp = h;
    	h = new es; 
    	h->value = n;
    	h->next = temp;
    	return h; 
    }
    
    
    es *pop_es_head(es *es_head)     // pops the head of the evaluation stack
    {
    	es *es_temp;
    	es_temp = es_head->next;
    	delete es_head; 
    	es_head = es_temp;
    	return es_head; 
    }
    
    
    
    pq *pop_pq_head(pq *head)         // pops the head of the polish queue
    {
    	pq *temp;
    	temp = head->next;
    	delete head; 
    	head = temp;
    	return head; 
    } 
    
    void printlistpq(struct pq *p)
    {
    	struct pq *listptr=p;
    
    	cout<<"the polish queue is :\n";
    	while(listptr!=0)
    	{
    		cout<<listptr->st<<endl;
    		listptr=listptr->next;
    	}
    }
    
    void printlistes(struct es *e)
    {
    	struct es *list=e;
    
    	cout<<"the evaluation stack is :\n";
    	while(list!=0)
    	{
    		cout<<list->value<<endl;
    		list=list->next;
    	}
    }
    Last edited by l3ecl; June 28th, 2010 at 02:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured