Click here to Skip to main content
15,918,976 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionwant to access any windows input elements from a win32 or mfc program Pin
Jayapal Chandran20-Jul-09 7:32
Jayapal Chandran20-Jul-09 7:32 
AnswerRe: want to access any windows input elements from a win32 or mfc program Pin
Hari Mahadevan20-Jul-09 15:27
professionalHari Mahadevan20-Jul-09 15:27 
GeneralRe: want to access any windows input elements from a win32 or mfc program Pin
Jayapal Chandran23-Jul-09 8:41
Jayapal Chandran23-Jul-09 8:41 
QuestionList focus setting problem Pin
bhanu_reddy0920-Jul-09 6:08
bhanu_reddy0920-Jul-09 6:08 
QuestionRe: List focus setting problem Pin
David Crow20-Jul-09 6:50
David Crow20-Jul-09 6:50 
Generalplease suggest me some good pregram using datastructure. Pin
pavarathyRock20-Jul-09 5:46
pavarathyRock20-Jul-09 5:46 
GeneralRe: please suggest me some good pregram using datastructure. Pin
David Crow20-Jul-09 6:54
David Crow20-Jul-09 6:54 
GeneralRe: please suggest me some good pregram using datastructure. Pin
pavarathyRock20-Jul-09 17:36
pavarathyRock20-Jul-09 17:36 
Hi Thanks for suggession,

I have started on that.
till now I have done this much for infix notation.
The algorithm I have tried is "Shunting-yard algorithm".
This program is having some problems, that I need to fix......


and you experts can give suggession in good coding practices also....

//--------------------------------------------//



//program.hpp
#include<iostream>
using namespace std;

const int maxExpressionLength = 100;
class stack
{
	char data[maxExpressionLength];
	int top;
public:
	stack();
	bool push(char input);
	bool pop(char & output);
	void print();
};

//--------------------------------------------//

//------------------------------------------------------//
//program.cpp
#include "program.h"
#include "ctype.h"

stack::stack()
{
	memset(data, 0, sizeof(data));
	top = -1;
}
bool stack::push(char input)
{
	if (top >= maxExpressionLength - 1)
	{
		cout<<"Error : expression size is more than supported\n";
		return false;
	}
	top++;
	data[top] = input;
	return true;
}

bool stack::pop(char &output)
{
	if (top <= -1)
	{
		//cout<<"Error : nothing to pop\n";
		return false;
	}

	output = data[top--];
	return true;
}

void stack::print()
{
	if (top == -1)
	{
		return;
	}

	for (int i = 0; i <= top; i++)
	{
		cout<<data[i]<<"  ";
	}
}

int strilen(int maxLength, char * string_data)
{
	if (NULL == string_data)
	{
		return 0;
	}
	for (int i = 0; i < maxLength; i++)
	{
		if (string_data[i] == 0)
		{
			return i;
		}
	}
	return maxLength;
}

bool isValidOperator(char oper)
{
	if ((oper == '^') || (oper == '*') || (oper == '/') || (oper == '+') || (oper == '-') || (oper == '(') || (oper == ')'))
	{
		return true;
	}
	else
	{
		return false;
	}
}

int presedence(char op1)
{
	if (op1 == '^')
	{
		return 5;
	}
	else if ((op1 == '*') || (op1 == '/'))
	{
		return 4;
	}
	else if ((op1 == '+') || (op1 == '-'))
	{
		return 3;
	}
	else
	{
		return 0;
	}
}

int main()
{
	char expression[maxExpressionLength + 1] = {0};
	
	cout<<"Enter the expression\n";
	cin>>expression;
	//strcpy(expression, "3+4*2/(1-5)^2^3");

	if (maxExpressionLength < strilen(maxExpressionLength + 1, expression))
	{
		cout<<"Error : The size of expression is more than supported["<<__LINE__<<"]\n";
		getchar();
		getchar();
		return -1;
	}
	
	stack obj;
	
	char rpnOut[maxExpressionLength + 1] = {0};
	int expressionCounter = 0;
	char c = expression[0];
	int counter = 0;
	while (c != 0)
	{
		if (isdigit(c))
		{
			rpnOut[expressionCounter++] = c;
		}
		else
		{
			if (false == isValidOperator(c))
			{
				cout<<"Error : input expression is not correct. problem with "<<c<<" please check the input["<<__LINE__<<"]\n";
				getchar();
				return -1;
			}
			else if (c == '(')
			{
				obj.push(c);
				c = expression[++counter];
				continue;
			}
			else if (c != ')')
			{
				char oper = 0;
				while (true == obj.pop(oper))
				{
					if (presedence(oper) > presedence(c))
					{
						rpnOut[expressionCounter++] = oper;
					}
					else if (presedence(oper) == presedence(c))
					{
						rpnOut[expressionCounter++] = oper;
						break;
					}
					else
					{						
						obj.push(oper);						
						break;
					}
				}
				obj.push(c);
			}
			else if (c == ')')
			{				
				char oper = 0;
				while (oper != '(')
				{
					if (false == obj.pop(oper))
					{
						cout<<"Error while parsing the input expression. please check input["<<__LINE__<<"]\n";
						getchar();
						getchar();
						return -1;
					}
					if (oper != '(')
					{
						rpnOut[expressionCounter++] = oper;
					}
				}
			}
			else
			{
				obj.push(c);
			}
		}

		c = expression[++counter];
	}
	
	char oper = 0;
	while (false != obj.pop(oper))
	{
		rpnOut[expressionCounter++] = oper;
	}
	cout<<"The expression in RPN = "<<rpnOut<<endl;
	getchar();
	getchar();
	getchar();
	getchar();
	return 0;
}

//------------------------------------------------------//

GeneralRe: please suggest me some good pregram using datastructure. Pin
Rozis20-Jul-09 10:45
Rozis20-Jul-09 10:45 
GeneralRe: please suggest me some good pregram using datastructure. Pin
pavarathyRock20-Jul-09 17:39
pavarathyRock20-Jul-09 17:39 
QuestionEXIT_FAILURE, EXIT_SUCCESS Pin
Sauce!20-Jul-09 4:58
Sauce!20-Jul-09 4:58 
AnswerRe: EXIT_FAILURE, EXIT_SUCCESS Pin
Sarath C20-Jul-09 8:27
Sarath C20-Jul-09 8:27 
AnswerRe: EXIT_FAILURE, EXIT_SUCCESS Pin
Stuart Dootson20-Jul-09 12:37
professionalStuart Dootson20-Jul-09 12:37 
GeneralRe: EXIT_FAILURE, EXIT_SUCCESS Pin
Sauce!21-Jul-09 1:24
Sauce!21-Jul-09 1:24 
QuestionCapture Events Written to Application Log Pin
Raghu.Amil20-Jul-09 4:47
Raghu.Amil20-Jul-09 4:47 
Questionfont for TabCtrl Pin
kumar sanghvi20-Jul-09 3:59
kumar sanghvi20-Jul-09 3:59 
QuestionReading .csv files Pin
prithaa20-Jul-09 3:15
prithaa20-Jul-09 3:15 
QuestionRe: Reading .csv files Pin
David Crow20-Jul-09 3:25
David Crow20-Jul-09 3:25 
AnswerRe: Reading .csv files Pin
Sarath C20-Jul-09 3:46
Sarath C20-Jul-09 3:46 
GeneralRe: Reading .csv files Pin
prithaa20-Jul-09 4:19
prithaa20-Jul-09 4:19 
QuestionUsing COM Dll Without Registering It Pin
Ash_VCPP20-Jul-09 3:05
Ash_VCPP20-Jul-09 3:05 
AnswerRe: Using COM Dll Without Registering It Pin
Sarath C20-Jul-09 3:47
Sarath C20-Jul-09 3:47 
GeneralRe: Using COM Dll Without Registering It Pin
Ash_VCPP21-Jul-09 0:05
Ash_VCPP21-Jul-09 0:05 
Questionaccessing shared folder by hard coding the credentials Pin
ranjithgoud20-Jul-09 2:59
ranjithgoud20-Jul-09 2:59 
QuestionRe: accessing shared folder by hard coding the credentials Pin
David Crow20-Jul-09 3:12
David Crow20-Jul-09 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.