|
If you try to do everything at once, it's very hard. Breaking it into steps makes it simpler:
1. Turn the expression string into a list of tokens. A token can be a variable name, an operator, or a parenthesis.
2. Turn the token list into a tree of tokens, where each operator token has child tokens of its operands.
3. Integrate the lowest-level sub-expressions, than combine them according to the operators, e.g. the integral of A + B is the integral of A plus the integral of B.
====================================
For step 2, apply two rules to your token list until you have a single operator token left in the list, representing the whole expression. (If you end up with more than one token in the list, you probably started with an illegal expression):
1. If you have the pattern: "(", [token], ")", replace it with [token], i.e. delete parenthesis tokens around a single value.
2. In the most deeply-nested parentheses, replace [token] [operator] [token] in the list with [operator], and set [operator]'s left and right child-node pointers to the two token operands. Do this for the highest-precedence operators first.
|
|
|
|
|
Hi,
Well, the End result depends very much on the lanuage definitions you assume, and, what you want to do with the end result.
You need to define Language Rules, for instance, where do f,x,y,a,b,c come from.
In 'C' the compiler builds a Symbol Table, which is a cross referenced database of all variables encountered in a module.
What do you want to do with it. If you want to store it as a 'General Formula, your best bet is probably to convert it to RPN (Reverse Polish Notation) and write an RPN Evaluator for when the rubber hits the road,
In general, I would start with writing a Scanner,the Scanner breaks down the text into Tokens
the 'C' rules are like: A Variable starts with 'A..Z','a..z' or '_'
A Variable name can then be followed with the same, and the chars '0..9'
A Numerical starts with '0..9'
A White Space is a ' '(space), '\t' or a '\n'
So , in the above, the name of the first var is 'f', the next char is a '(' which is not a
legal char for a varname. So, the First token is a Lexical Token, with Identifier 'f'
The Next Token would be a Special Token, LPAREN,
Going on in general, (Your Language Definition does not allow for comments),
I would also write a Parser
The Parser calls the scanner , to pick off tokens One by One, ignoring intervening whitespace, or comments, and creates a parsing tree (But it all still depends what you want to do with the expression in the larger scheme of things.
A Book: Writing Compilers & Interpreters (An Applied Approach) by Ronald Mak, ISBN 0-471-50968-X
Bram
Bram van Kampen
|
|
|
|
|
I have tried to use a combination of get_offsetheight,get_offsetwidth,get_offsettop and getoffsetleft but the values returned are in reference to the immediate parent.
|
|
|
|
|
What do you mean by screen coordinates? Did you try "walking up" the inheritance tree, adding the offsets till you reach the top?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Hello.
That's exactly what the method you're invoking returns, the height/width referenced to its parent coordinate system.
If you want to get the offset referenced to the screen (1st level of DOM) you can get it by adding all offset values invoking recursively to 'get_offsetParent' method (n-1) times.
Hope it helps.
Regards
|
|
|
|
|
can any one give me a hint on how to write a code in for loop that prints only even numbers with an input of n?
i.e. 0, 2, 4, 6, .....
|
|
|
|
|
What is n supposed to be?
This sounds a lot like a homework question... so I'll just give you a hint... divide by 2.
|
|
|
|
|
What would YOUR code look like without the "only even numbers" constraint?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
David: this code is for print n from 0 to n (n is an input number from the user).
int n;
cout << "Enter a number and press ENTER: ";
cin >> n;
for (int i = 0; i <= n; i = i++)
{
cout << i << " ";
}
cout << "\n";
I am suppose to do something to i++ + 2, or n-- - 2, or something.
|
|
|
|
|
Yolande MR wrote: for (int i = 0; i <= n; i = i++)
This is questionable at best. Try:
for (int i = 0; i <= n; i = i + 1)
Yolande MR wrote: I am suppose to do something to i++ + 2, or n-- - 2, or something. If each iteration of your loop is incrementing i by 1, then why not try incrementing by...
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Incrementing by....well, certain number... works, but only if you start from an already even number. Important to note that.
|
|
|
|
|
Albert Holguin wrote: ...but only if you start from an already even number. Important to note that.
It was already noted...zero. My suggestion reflected that.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
Chris:
I got i+=2, works the same!
|
|
|
|
|
That doesn't really give you even numbers if i happens to be an odd number. If you want to do a better job, look at using the modulus operator.
|
|
|
|
|
Why would anyone vote this down? It's a fact... care to explain yourself downvoter?
|
|
|
|
|
5 got you 3! oops.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Finally a reply that covers all issues in one operator. Can't go wrong on the modulo.
My 5.
Cheers, AT
Cogito ergo sum
|
|
|
|
|
Here[^] is a useful reference for you.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Divide the user input by 2 and check for remainder. If there is an remainder means it is an odd number, vice versa. 
|
|
|
|
|
No kidding?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Probably shouldn't have posted this as a reply to Richard... but you are correct.
|
|
|
|
|
They've written a book on even numbers now?
|
|
|
|
|
Use the modulus operator % to get the remainder
For instance: if (number % 2 == 0) then its even, and if (number % 2 != 0) then its odd.
Code to print only EVEN numbers..
#include <iostream>
using namespace std;
int main()
{
int num;
cout <<"Printing only EVEN numbers..\n\n ";
for(num = 1; num < 210;num++)
{
if ( num % 2 == 0 )
{
cout << num << " is even\t ";
}
}
cout <<"\n ";
return 0;
}
|
|
|
|
|
Or you could use the simple expression
int odd = num & 1;
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|