| Jackson Marie (450) | ||||||||
|
Here are some other examples : Note : You can use Debug functions (PrintW() and CalDebug()) No error(s) have been found. - Sum all integers between 1 and 10 :
Calculate complex expression :
//I'm going to add Set() functions...
Edit : Because, C++....@helios, I really need a machine which can handles and calculates all various variable calculation expressions... But, be more detailed? Have you tested the example yet? What do you mean by stack? I have no idea what you're saying. | ||||||||
|
Last edited on
|
||||||||
| helios (10126) | |||
I have no idea how this code can even begin to give back the correct result (though it does, apparently), but in any case, there's another problem. An evaluation function should evaluate the structure passed to it, and do nothing more. Your evaluation function is implicitly performing grouping of subexpressions by precedence (part of a parsing algorithm), which would account for its complexity.
| |||
|
|
|||
| Jackson Marie (450) | ||||||||
|
@helios Other expression :
Attach a variable with its value (5) (+) Result += 5 (But using Equal is more accurate)
Attach a variable with its value (7) (+) Result += 5; Result += 7;
Attach a variable with its value (10) (*) Result += 5; Result += 7 * 10;
Attach a variable with its value (5) (-) Result += 5; Result += 7 * (10 - 5);
Attach a variable with its value (5) (/) Result += 5; Result += 7 * (10 - 5) / 4;
Result += 5; Result += 7 * (10 - 5) / 4; Result += 9; That. | ||||||||
|
Last edited on
|
||||||||
| TheIdeasMan (1562) | |||
|
Further to helios' post: The whole point of a RPN stack type program is to input a string like :
and have the program calculate the answer, NOT write more code to calculate the answer. So you have missed the point with stacks & RPN. Remember the algorithm is: If it's a number, push onto the stack. If it's an operator, pop the stack & do the math. | |||
|
|
|||
| helios (10126) | |
|
Correction: Infix: 5 + 7 * (10 - 5) / 4 RPN: 5 7 10 5 - 4 / * + But yes, the point of RPN is to not have to code precedences into the parsing algorithm. | |
|
|
|
| Jackson Marie (450) | ||||
Ok... But Is this related to assembly? Perhaps C++ doesn't support asm stack for programmer users. And, certainly I really need (at least) a function... :D | ||||
|
|
||||
| TheIdeasMan (1562) | |||
|
OK, but to spell out my point to JM even more clearly: You have not achieved anything at all by having to do this code:
because you could have just done this: m = 5 + 7 * (10 - 5) / 4;and the cost has been to write much more (because of multiple statements on 1 line) than 180 lines of code. | |||
|
|
|||
| Jackson Marie (450) | |
| I'm sure. About speed? Efficiency? And performance? | |
|
|
|
| Jackson Marie (450) | ||||
Now I can write :
And : m = 5 + 7 * (10 - 5) / 4;If I knew I would do so directly (C++). But suppose it's a real code... (And only assembly, assembly and assembly...) | ||||
|
Last edited on
|
||||
| TheIdeasMan (1562) | |
|
You are still missing the point entirely. You are hard coding the problem into your code, not getting input. With helios' stack program, one could type any expression that contained the supported operators as input, up to a very long length (say 1000 numbers connected by operators), and the program would give the answer. If one wants a different expression, then the program is run again, or it is trivial to get it to loop. I haven't checked to see if this the case, it doesn't matter for this argument. With your program, you would have to edit the code, with 1000 of new lines of code at least (hopefully not making any mistakes), then recompile, and the resulting program only works for that 1 expression. The program cannot be distributed to users, and is useful only to you. The whole thing is pointless, because you may as well do a C++ expression. And it misses the point for the programming paradigm, which is: Get some input, do some processing, produce output. | |
|
|
|
| helios (10126) | |
|
To be fair, JM's code can be modified to accept user input, but not without a radical redesign (mostly because of parentheses) or a very disgusting kludge (yes, an even worse one). The real problem here is that certain variables of the parsing algorithm are directly hard-coded into the logic of the evaluation function. Adding new operators or modifying the precedence or associativity of existing ones is unnecessarily difficult. For example, the current algorithm gives unary minus and % the lowest precedence (lower than +), when unary minus should have the highest precedence (being a unary operator) and % should have the same precedence as *. Adding exponentiation or boolean operators would be even harder. Then there's the problem that evaluation and parsing are joined together like Siamese twins, which also makes things much harder than they need to be. | |
|
|
|
| Jackson Marie (450) | |
|
The class has the "user input" feature by default. Why does my "Push" function have the parameter "bool bPointer"? I'll need to make some small modifications.... Now it's a calculator parser, not a stack. Sorry, I used wrong concept (I don't know much what it means). But based on C++ programming interface, I'd like to know "Is there any method better than mine which can do this (C++)? Or just leave C++ and then start again with a new assembly low-level language?" | |
|
Last edited on
|
|
| Grey Wolf (3172) | |||
|
---------------=======#=======--------------- NB: This post is not meant to offend anyone. ---------------=======#=======---------------
[deleted]I was going say a few words about Jackson Marie [/deleted] What I will say take you time, some of your posts are erratic and unfocused. Have a quick read of How To: Ask Questions The Smart Way and the following post How To Answer Questions in a Helpful Way and you may get an insight into why TheIdeasMan says what he says. http://www.cplusplus.com/forum/beginner/1/#msg6680 Above all else do something about you coding style, it is too hard to scan and get a feel for what you are doing. | |||
|
Last edited on
|
|||
| EssGeEich (681) | |
|
Sorry for replying late - Haven't been getting over here for a couple days working on another little project. Well anyways if you want to see the program, it's over here on my dropbox: http://dl.dropbox.com/u/83943521/Apps/Coter.rar It mustn't be the most performing program but it does its job. Let me say it wasn't fully ready to be released. Also the default localization is italian. type 'setloc en' to 'fix' this. In the Doc.txt you have a fast tutorial to load and play a song with bass.dll which is included in the archive, and all the complete documentation of the program and commands. | |
|
Last edited on
|
|
| Jackson Marie (450) | |
|
Thanks @EssGeEich, I've just gotten your interpreter. (I was too busy)... :) But, I still have to improve my C++ programming skill :). So I'm going to write a calculator expression parser, so, string -> number and operators. Does anyone have any idea? | |
|
Last edited on
|
|
| m4ster r0shi (2044) | ||
If you're serious about implementing your own language, a good idea is to have a look at this -> https://www.coursera.org/course/compilers It covers all compilation phases (lexical analysis, parsing, semantic analysis, optimization, code generation) and it's completely free. You can sign up for self study, download the videos and watch them at your own pace. | ||
|
|
||
| Jackson Marie (450) | ||||
|
Thanks all (m4ster r0shi and EssGeEich)!!! Ok. I've just done the expression parser. Here is the code :
How to input : Example :
Let me know. I'd like to know some opinions and suggestions. And certainly, I'll need to fix my bad code writing... :) | ||||
|
Last edited on
|
||||
| Jackson Marie (450) | |
|
Next step : In short I'm going to make a variable managing and detecting machine... (now must accept letters...) Any opinion or suggestion about this huge tool construction? (Any help would be greatly appreciated) :) | |
|
|
|
| hamsterman (4325) | |
| The link master roshi gave you should be all the help you could ever need. It sounds a bit as if you're trying to get somebody to do this for you... | |
|
|
|
| Jackson Marie (450) | |
|
I'm learning...Don't worry. But, when someday I am ready I will really make an interpreter (programming language). And ? Immediately? No, not now... | |
|
Last edited on
|
|