I have a linked list of operators
()+-*/ , how would I be able to compare them if they are all of type char? I looked at the ascii table, but they are all out of order so that isn't a choice.
I have a linked list of operators
()+-*/ , how would I be able to compare them if they are all of type char? I looked at the ascii table, but they are all out of order so that isn't a choice.
Like if('/' == array[i])
, or?
Like
if('/' == array[i])
, or?
sorry, I should have been more clear. This is my algorithm here.
while(operator stack is not empty AND the top of operator stack is greater or equal precedence AND top of the operator stack is not '(')
{
blah blah
}
note: the operator stack is of type char.
while(!empty_optr()
&& ((top2 -> data_optr == '/'
|| top2 -> data_optr == '*')
|| (top2 -> data_optr == '+'
|| top2 -> data_optr == '-')
&& (top2 -> next2 -> data_optr == '+'
|| top2 -> next2 -> data_optr == '-'))
&& top2 -> data_optr != '(')
this is what i came up with
put all the operators in a character array then use strchr() to see if a character is one of the operators.
const char operators[] = "/*+-()";
while( !empty_optr() && ischar(operators, top->data_optr))
{
// blabla
}
int main(int argc, char* argv[])
{
char c = 'c';
if ( c == 'c' )
{
cout << "yes working" << endl ;
}
// but
if ( c == "c")
{
cout <<" problem ! "<< endl ;
}
return 0;
}
I think that you got the point !
>>if ( c == "c")
That won't even compile -- can't use put the character in quotes like that.
Keep it simple. You should perhaps say that you want to write a parser.
I guess you already have made the stack class. At any point of time the comparison operator works with characters too.
Keep it simple. You should perhaps say that you want to write a parser.
I guess you already have made the stack class. At any point of time the comparison operator works with characters too.
yes of course !
This is the same thing that I also tried to suggest !
In this case the operator supports , if you find somewhere that doesn't support it. then write your own overloaded operator.
that way make your code more clear and more readable.
yes of course !
This is the same thing that I also tried to suggest !In this case the operator supports , if you find somewhere that doesn't support it. then write your own overloaded operator.
that way make your code more clear and more readable.
if my stack is like this(with top being at the top of course)
+
*
/
-
and i say (stack->data) > (stack->next->data)
it will read it correctly?
the reason i ask this question is because
+ has a decimal value larger than *.
so, I'm afraid it will use the + operator before *. which is incorrect.
if my stack is like this(with top being at the top of course)
+
*
/
-and i say (stack->data) > (stack->next->data)
it will read it correctly?
the reason i ask this question is because
+ has a decimal value larger than *.
so, I'm afraid it will use the + operator before *. which is incorrect.
I think now I got your problem !
well , C++ is not that much intelligent to do it. Therefore the answer is no !
That's why you can overload your own > and get these kind of exceptions into a inside a function.
I think overloading the '>' operator makes the sense here.
Inside that function just check for the operators like + and * to give
special attention. and others you can process in the normal logic.
You have two options
First --
make a enum to store all operators .
enum oper {add =1, sub=1, div=2, mu=3};
Now make link list 'stack' to store these enums, and you can do like this -
if(stack->op > stack->next->op);
As you can see you can also give same weight to add and sub in enum.
second --
Define a structure to store operators and their weights
struct oper
{
char op;
int weight;
}
and then --
oper op1 ;
op1.op = '*';
op1.wieght = 1;
oper op2;
op2.op='+';
op2.weight = 2;
You have two options
First --
make a enum to store all operators .
enum oper {add =1, sub=1, div=2, mu=3};
Now make link list 'stack' to store these enums, and you can do like this -
if(stack->op > stack->next->op);
As you can see you can also give same weight to add and sub in enum.
second --Define a structure to store operators and their weights
struct oper
{
char op;
int weight;
}and then --
oper op1 ;
op1.op = '*';
op1.wieght = 1;
oper op2;
op2.op='+';
op2.weight = 2;
warning : making that enum will break your code.
>warning : making that enum will break your code.
Agreed. (+&-) have same weight.
One alternative is ( the non-OOP way) to map a function say weight(char) which gives you the weight of any char you entered.
warning : making that enum will break your code.
Please explain how enum can cause breakege.
well, turns out i only have to compare +,-,*,/ so i created a function to do so.
since +,- are equal and *, / are equal.
we can say:
int Stacks::check_prec(char check)
{
if((int)check == 42 || (int)check == 47)
return 1;
else
{
char x;
if(top2 != NULL)
x = top2 -> data_optr;
else
return 1;
if((int)x == 42 || (int)x == 47)
return 0;
else
return 1;
}
}
thanks for all the help and support though, I really appreciate it.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.