LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-14-2008, 11:52 AM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
help simplifying a stack calculator program


My friend gave this code about a stack base calculator where the user enter the mathematical expression as infix and then it converts it into postfix and gives out the result. Now his program works but there is stuff that I don't need like printing the postfix convertion. This is the code:
Code:
#include "Calculator.h"

Calculator::Calculator()
{
	infix = " ";

}

 
Calculator::~Calculator()
{
}


void Calculator::set(string a)
{
infix = a;
}

 

bool Calculator::Operand(char character)//check each char is a operand 
{							     //eg. numbers,x and y which represent a function in this program
	if (((character >= '0') && (character <= '9'))||(character == 'x') ||(character == 'y') ||(character == '.'))
		return true;
	
	else
		return false;
}

 

bool Calculator::Number(char character)//check each char for a number
{
	if (((character >= '0') && (character <= '9'))||(character == '.'))//include . for decimals numbers
		return true;
	else
		return false;

}

 

bool Calculator::Operator(char character)//check each char for a operator
{
	if ((character == '+') ||(character == '-') ||(character == '*') ||(character == '/'))
		return true;
	else
		return false;
}


bool Calculator::Precedence(char Operator1, char Operator2)//check the order of precedence
{														   //in reference to the infix notation
	if (Operator1 == '(')
		return false;

	else if (Operator2 == '(')
		return false;

	else if (Operator2 == ')')
		return true;

	else if ((Operator1 == '*') || (Operator1 == '/'))
		return true;

	else if ((Operator2 == '*') || (Operator2 == '/'))
		return false;
	
	else
		return true;
}

 

void Calculator::InfixtoPostfix(const string &infix, string &postfix)
{
	Stack <char> Operator;
	char top, symbol;
	

	for (int k = 0; k < infix.size(); k++)//size() for loop to go until the lenght of the infix notation
	{									  //part of the library <string>
		symbol = infix[k];
		if (Operand(symbol))// if char is an operand just add them
			postfix = postfix + symbol;

		else//delete char that is not operand and add it to Postfix
		{
			while ((! Operator.isEmpty()) &&(Precedence(Operator.top(), symbol)))
			{
				top = Operator.top();
				Operator.pop(top);
				postfix = postfix + top;
			}
			if ((! Operator.isEmpty()) && (symbol == ')'))
				Operator.pop(top); // skip ( ) matching
			else
				Operator.push(symbol);
		}
	}
	
	while (! Operator.isEmpty())
	{
		top = Operator.top();
		Operator.pop(top);
		postfix = postfix + top;
	}
}

 

string Calculator::Change(string tmp)
{
	for(int i = 0; i <tmp.size(); i++)
	{
		if(Number(tmp[i])==true)
		{
			if(Number(tmp[i+1])==false)
			{
				tmp.insert(i+1, "x");
			}
		}
	}// -ve * -ve case
	
	for (int i = 0; i < tmp.size(); i++)
	{
		if(tmp[i]=='-')
		{
			if((tmp[i-1]!='x')&&(tmp[i-1]!=')'))
			{
				tmp.replace(i,1,"y");
			}
		}
	}
	return tmp;
}

 

string Calculator::Space(string tmp)//replace the x and y inserted in Change()
{
	for(int i = 0; i < tmp.size(); i++)
	{
		if (Operator(tmp[i])==true)
		{
			tmp.insert(i+1, " ");

//Insert a space after all
//found operators
		}
		else if( tmp[i]=='x' )
		{
			tmp.replace(i,1," ");

//replace the x with a space
//for clarity
		}
	}
	for (int i = 0; i < tmp.size(); i++)
	{
		if(tmp[i]=='y')
		{
			tmp.replace(i,1,"-");
		}
	}
	return tmp;
}

 

bool Calculator::Check(string tmp)
{
//incomplete Changed check that consecutive '+', '-' signs do not exist

	for (int i = 0; i < tmp.size(); i++)//lenght() for loop until lenght of infix notation in this case tmp 
	{
		if((tmp[i]=='+')||(tmp[i]=='-'))
		{
			if((tmp[i+1]=='+')||(tmp[i+1]=='-'))
			{
				return false;
			}
		}
	}
	string array1 = "+-*/().0123456789";
	int counter = 0;
	for (int i = 0; i < tmp.size(); i++)
	{
		for(int j = 0; j < array1.size(); j++)
		{
			if(tmp[i]==array1[j])
			{
				counter++;
			}
		}
	}
	if (counter == tmp.size())
	{
		return true;
	}
	else
	{
		return false;
	}
}

 

void Calculator::Next(string tmp)
{
	vector <string> array1;
	int spaces = 0;
	for ( int a = 0; a < tmp.size(); a++ )
	{
		if(tmp[a]==' ')
		{
			spaces++;
		}
	}
	string whatever;
	istringstream input(tmp);
	
	while ( getline(input, whatever, ' ') )
	{
		array1.push_back(whatever);
	}
	
	Stack <string> stack;//initialize stack
	vector <string> temp;
	string character;

	for (int i = 0; i < spaces; i++)
	{
		string s;
		s = array1[i]; //make it easier to read
		if ((s!="+")&&(s!="*")&&(s!="-")&&(s!="/"))
		{
			stack.push(s);//push numbers onto the stack
		}
		
		else //i.e if it encounters an operator
		{
			stack.push(s);//push operator onto stack
			for ( int i = 0; i < 3; i++ )
			{
				temp.push_back(stack.top());
				stack.pop(character); //erase from the stack
			}
			double z;
			z = Operation(temp);
			ostringstream output;//so i can copy and convert example taken from www.cplusplus.com/reference/iostream/ostringstream/ostringstream.html
			output << z; // Convert value into a string.
			character = output.str();//copy string of outs and assign it to character
			stack.push(character);
			temp.clear();
			
		}
	}
	cout << character;
}

 

double Calculator::Operation(vector <string> &temp)
{
	string a,b,c;
	a = temp[2]; 
	b = temp[0]; 
	c = temp[1];
	double x,y,z;
	istringstream insert,insert2;
	insert.str(a); insert2.str(c);
	insert >> x;
	insert2 >> y;
	
	if (b == "+")
	{
		z = x + y;
		return z;
	}

	else if (b == "-")
	{
		z = x - y;
		return z;
	}

	else if (b == "*")
	{
		z = x * y;
		return z;
	}

	else if (b == "/")
	{
		z = x / y;
		return z;
	}

}

 

void Calculator::result()
{
	string postfix;
	if(Check(infix)==true)
	{
		string temp;
		temp = Change(infix);
		InfixtoPostfix(temp, postfix);
		cout << "\nThe Postfix is: "<< Space(postfix);//show the user the postfix
		//for me to know the convertion from infix to postfix went ok
		string hold;
		hold = Space(postfix);
		cout << "\n\nThe result is: ";
		Next(hold);
		cout << "\n\n";
	}
	
	else
	{
		cout << "Expression is not valid.\n";
	}
}
I already try deleting the Space and Change me part of the program but it wont give the result it prints out just blank space. Thanks. I think it has to do something in the Next function but I can't figure it out.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I measure stack usage of a C/C++ program? exit3219 Programming 4 02-02-2007 11:25 AM
simplifying wine Four Linux - Newbie 6 12-28-2005 05:02 PM
graphing calculator program microsoft/linux Debian 1 09-04-2005 10:12 PM
Adding only new text/simplifying lists invisible_ink Linux - Newbie 1 11-24-2004 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration