Click here to Skip to main content
15,916,941 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CDragListBox Pin
Joaquín M López Muñoz10-Jul-02 19:59
Joaquín M López Muñoz10-Jul-02 19:59 
GeneralRe: CDragListBox Pin
Lucky200210-Jul-02 21:06
Lucky200210-Jul-02 21:06 
GeneralRe: CDragListBox Pin
Joaquín M López Muñoz11-Jul-02 8:42
Joaquín M López Muñoz11-Jul-02 8:42 
GeneralRe: CDragListBox Pin
Lucky200211-Jul-02 23:55
Lucky200211-Jul-02 23:55 
QuestionComments on Dynamic multidimensional CString Array??? Pin
Nitron10-Jul-02 6:11
Nitron10-Jul-02 6:11 
AnswerRe: Comments on Dynamic multidimensional CString Array??? Pin
Joaquín M López Muñoz10-Jul-02 8:17
Joaquín M López Muñoz10-Jul-02 8:17 
GeneralUrgent help needed !!! Pin
bisserke10-Jul-02 5:30
bisserke10-Jul-02 5:30 
QuestionStack problem? Pin
innomino10-Jul-02 5:03
innomino10-Jul-02 5:03 
Hey gus I currently working on a project which change an math expression into a binary tree. But it seems that a stack problem has raised probably because of the stack. I tried to solved the problem but failed. Could someone do me a favor? Thanks!
//Source
#include <ctype.h>
#include <stdlib.h>
#include <stack>
#include <iostream.h>
#include <string>

struct Node
{
char* key;
Node* left;
Node* right;
};

bool ischar(char);
void calculate();
void evaluate(char*);
void inorder(Node*);
std::stack<char> stackOperator;
std::stack<node*> stackOperand;

void main()
{
char infix[50];
Node* node;

cout << "Expression: ";
cin >> infix;
evaluate(infix);
node = stackOperand.top();
cout << "print" << endl;
inorder(stackOperand.top());
}

bool ischar(char ch)
{
return isalpha(ch) || isdigit(ch) || ch == '.';
}

void calculate()
{
//Create a node combined with left and right trees
char op;
Node* temp;
Node* left;
Node* right;

op = stackOperator.top();
stackOperator.pop();
right = stackOperand.top();
cout << "r add: " << right << endl;
stackOperand.pop();
left = stackOperand.top();
cout << "l add: " << left << endl;
stackOperand.pop();

temp = (Node*) malloc(sizeof(Node));
temp->key = &op;
temp->left = left;
temp->right = right;
stackOperand.push(temp);
}

void evaluate(char* infix)
{
int sign = 1, decimal, length;
char variable[20];
bool readyForUnaryOperator = true;
Node* temp;

stackOperator.push('(');
while(*infix != '\0')
{
cout << *infix << endl;
while(isspace(*infix)) infix++;
if(ischar(*infix))
{
//Get number or variable
length = 0;
do
{
variable[length++] = *infix;
infix++;
}
while (ischar(*infix));

variable[length] = '\0';

temp = (Node*) malloc(sizeof(Node)); temp->key = ecvt(atof(variable), strlen(variable), &decimal, &sign);
cout << "variable: " << temp->key << endl;
temp->left = NULL;
temp->right = NULL;
stackOperand.push(temp);
cout << "VAR " << variable << endl;
sign = 1;
readyForUnaryOperator = false;
infix--;
}
else if(*infix == '(')
{
stackOperator.push('(');
readyForUnaryOperator = true;
}
else if(*infix == ')')
{
while(stackOperator.top() != '(')
{
cout << stackOperator.top() << endl;
calculate();
}
stackOperator.pop();
readyForUnaryOperator = false;
}
else if(*infix == '*' || *infix == '/')
{
if(stackOperator.top() == '*' || stackOperator.top() == '/')
calculate();
stackOperator.push(*infix);
readyForUnaryOperator = true;
}
else
{
if(readyForUnaryOperator)
{
if(*infix == '-') sign = -sign;
}
else
{
if(stackOperator.top() != '(')
calculate();
stackOperator.push(*infix);
}
readyForUnaryOperator = true;
}
infix++;
}
while(stackOperator.top() != '(')
calculate();
}

void inorder(Node* root)
{
//Print the tree in inorder form
if(root == NULL)
{
return;
}
inorder(root->left);
cout << *(root->key) << endl;
inorder(root->right);
}
AnswerRe: Stack problem? Pin
Rage10-Jul-02 5:20
professionalRage10-Jul-02 5:20 
AnswerRe: Stack problem? Pin
Rage10-Jul-02 5:28
professionalRage10-Jul-02 5:28 
GeneralActiveX Help Pin
Anonymous10-Jul-02 4:48
Anonymous10-Jul-02 4:48 
GeneralGot It! Pin
Anonymous10-Jul-02 6:32
Anonymous10-Jul-02 6:32 
GeneralDatabase Problem Pin
BHBAD10-Jul-02 4:24
sussBHBAD10-Jul-02 4:24 
GeneralRe: Database Problem Pin
Martin Ziacek10-Jul-02 4:31
Martin Ziacek10-Jul-02 4:31 
GeneralRe: Database Problem Pin
BHBAD10-Jul-02 4:36
sussBHBAD10-Jul-02 4:36 
GeneralRe: Database Problem Pin
Martin Ziacek10-Jul-02 4:44
Martin Ziacek10-Jul-02 4:44 
GeneralRe: Database Problem Pin
Mike Nordell11-Jul-02 1:40
Mike Nordell11-Jul-02 1:40 
GeneralRe: Database Problem Pin
Martin Ziacek11-Jul-02 8:18
Martin Ziacek11-Jul-02 8:18 
GeneralRe: Database Problem Pin
Christian Graus10-Jul-02 4:36
protectorChristian Graus10-Jul-02 4:36 
GeneralRe: Database Problem Pin
BHBAD10-Jul-02 4:39
sussBHBAD10-Jul-02 4:39 
GeneralRe: Database Problem Pin
Carlos Antollini10-Jul-02 4:57
Carlos Antollini10-Jul-02 4:57 
GeneralRe: Database Problem Pin
Michael P Butler10-Jul-02 4:38
Michael P Butler10-Jul-02 4:38 
GeneralRe: Database Problem Pin
Brian Azzopardi10-Jul-02 4:50
Brian Azzopardi10-Jul-02 4:50 
GeneralRe: Database Problem Pin
Matt Gullett10-Jul-02 4:47
Matt Gullett10-Jul-02 4:47 
GeneralPM_QS_PAINT question. Pin
Michael Liu10-Jul-02 3:54
Michael Liu10-Jul-02 3:54 

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.