Jump to content


Check out our Community Blogs

bexita

Member Since 17 Oct 2011
Offline Last Active Dec 26 2011 05:24 PM
-----

Topics I've Started

Calculate an expression using stack

27 November 2011 - 08:45 AM

Hi , i need to calculate an expression with +/- only , it will get result once it see '=' sign using stack linked list (for exp: 200-100=) ,result 100.

My code is not working , can someone enlighten me ? Thanks a lot :)


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <ctime>
using namespace std;

const int MAX =100;
int getResult(char *);

class Stack
{
    public:
        Stack();
        ~Stack();
        void push(int);
        int pop();
		int top ();
		
        bool isEmpty () const;
        

    private:

        struct Node;
        typedef Node* NodePtr ;

        struct Node
        {
            int data;
            NodePtr next;

        };
		
		NodePtr head;

};

Stack::Stack()
{
	head =NULL;	   
}
Stack::~Stack()
{
	//Let the compiler to do it
}
void Stack::push (int item)
{
	NodePtr pNew= new Node;
	pNew -> data = item;
	pNew -> next = head;
	head = pNew;
}

int Stack::pop()
{
	int item = NULL;
	try
	{
		if ( head == NULL)
			throw exception ();
		
		
		NodePtr temp = head;
		head = head ->next;
		
		item = temp ->data;
		
		delete temp;
	}
	catch (exception e)
	{
		cout << " Empty exception caught : Remove head failed "<< endl;
	}
	return item;
}
	

bool Stack::isEmpty () const
{
	return head == NULL;
}

int Stack::top ()
{
	if (!isEmpty())
		return head-> data;
}



int main ()
{
	char * exp = new char[MAX];
	cout <<"Enter an expression" <<endl;
   cin.getline(exp,MAX);
	int result = getResult (exp);
	
	cout <<result <<endl;

}


int getResult(char *exp)
{
	
	int i =0;
	int k,k1,k2;
	int result = 0;
	char *p;
	Stack s;
	p = &exp[0];
	
	// Calculate the result if last element is '=' sign
	while (*p!='=')
	{
		/* To remove spaces and tabs*/
		while (*p ==' ' || *p =='\t')
		{
			p++;
		}
		
		// If is operand
		if (isdigit(*p))
		{
			s.push(*p);
			
		}
		else
		{
		//If is operator +,-
				k1 = s.pop();
				k2 = s.pop();
				
			switch (*p)
			{
				case '+' : 	result =k1 + k2;
							
							
				case '-' :  result =k1 - k2;
							
				
				default : cout<<"\nInvalid operator. Program exit.";
						  
				
			// push back to stack
			s.push(result);
			}
					
		
		}
		
		p++; // Go to next element
		
	}
	
	
	result= s.pop();
	
	

	return result;
}




Searching in linked list problem!

09 November 2011 - 05:44 AM

Hi,
I wanted to search an element (char) in the list S, but everytime it said that 02x is not in S
How to declare char input "target" in a manner way? i think the declaration is my mistake for this code. Im stucking on this. ^^


struct Node;typedef Node* NodePtr;
typedef void* VoidPtr;
typedef char* Ptr;
typedef int* Pi;
struct Node
{
    VoidPtr data;
    NodePtr next;
};


bool Search(NodePtr& head, VoidPtr& target)    
{
    bool found = false;
    
    NodePtr here = head;   
    
    while (here != NULL && !found) 
    {
        if (here->next ==target)
            found =true;
        else
            here = here ->next;
            return found;
    }
    
    
}




void Search_Menu (NodePtr& head)
{
    VoidPtr target;
    cout <<setw(70)<<setfill('=')<<endl;
    PrintList(head);
    
    cout <<"\nWhich element?: ";
    cin >>target;
    
    if((Search(head, target)==true))
    {
        cout <<target<<" is in S ."<<endl;
    }
    else
    {
        cout <<target <<" is not in S."<<endl;
    }
}


[/QUOTE]

Help on seach element in array using poitner C++

06 November 2011 - 06:49 AM

Hi,

I have a array string such as a set S= {W,P,A,Z}

I only manage to search the first element which is W , but not for others ( it will say not found if i search for P, for instance), here is my code for searching part:
whats wrong with my code?

cout <<"\nWhich element?: ";
						cin >>which;
						
						if((Search(&a[0], size, which)==true))
						{
							cout <<which<<" is in S ."<<endl;
						}
						else
						{
							cout <<which <<" is not in S."<<endl;
						}
						break;

bool Search(char *a, int size, char *which) // Search element 
{
	Ptr c =&which[0];
	Ptr b= &a[0];
	
	for (int i=0;i <size;i++)
	{
	
		if (b[i]==*c)
		{
				
			return true; // true if found
			
		}
			
		else
		{
			return false;
		 // not Found element.
		}
		
	}
	
	
}

Thanks:crying:

How to read text file into array in C++?

26 October 2011 - 08:41 AM

Hi,
I have this text file, the format looks like this:

2      (no of modules)
Module123   (Module code)
Module100    

5
Module123   (Module code)
Module111
Module245
Module232
Module343

I want to read this into array of structure :
struct student {
int no_module;
--> how to declare variables for the module ?? <----
};
student a[];
file.open("data.txt" , ios ::in);

-->Because its based on the number of modules, if they are 2 , there are two modules which will be read or if they are 5 , there are 5 modules which will be read only. Anyone know how to read this text files ?
Thanks a lot.

How to Append binary files and compare strings in C++?

18 October 2011 - 07:54 AM

Hi
I have problem with comparison when input new staff info to binary file:

For exp: I have a info.text containing
name[tab]title[tab]position[tab]phone number [tab] ID

This file already transfered to info.dat.

Now , I want to append this binary file info.dat by adding new staff until
i need to take note that ID , as long as the ID is as same as (case sensitive)
the one in the staff info.dat , it will continue to ask user to input a new unique
ID.


How do I do that ?
Can I use stricm() function to compare ? And How do I compare between user-key-in
to the info inside the binary info.dat?

assuming I have constructed struct Staff{name, tittle...);

Thanks.:pinguin:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download