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 :
1 2 3 4 5 6
double Result = 0;
Stack stack;stack.PushItSelf(&Result, Add, true);
double m = 1;
for(double u = 0;u < 10;u++)
stack.Push(&m, Add);
stack.Cal();
Calculate complex expression :
x = 5 + 7 * (10 - 5) / 4
1 2 3 4 5 6 7 8 9 10
double Result = 0;
Stack stack;stack.PushItSelf(&Result, Add, true);
double m;
m = 5;stack.Push(&m, Add);
m = 7;stack.Push(&m, Add);
m = 10;stack.Push(&m, Multiply);
m = 5;stack.data[2].Push(&m, Subtract);
m = 4;stack.Push(&m, Divide);
stack.Cal();
//I'm going to add Set() functions...
cout << "Result : " << Result << endl;
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.
m = 5;stack.Push(&m, Add);
m = 7;stack.Push(&m, Add);
m = 10;stack.Push(&m, Multiply);
m = 5;stack.data[2].Push(&m, Subtract);
m = 4;stack.Push(&m, Divide);
This is an n-ary tree, not a stack.
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.
What do you mean by stack? I have no idea what you're saying.
I'm not about to impart a class of Algorithms and Data Structures 1 over a forum. Go grab a book, or take a look at the code I posted earlier. It implements a true Turing-complete stack machine.
double Result = 0;
Stack stack;stack.PushItSelf(&Result, Add, true);
double m;
m = 72;stack.Push(&m, Equal);
m = 54;stack.Push(&m, Subtract);
m = 54;stack.Push(&m, Multiply);
m = 6;stack.Push(&m, Divide);
m = 6;stack.Push(&m, Add);
//////Next small expression
m = 52;stack.data[4].Push(&m, Subtract);
m = 7;stack.data[4].Push(&m, Multiply);
m = 65;stack.data[4].Push(&m, Subtract);
m = 5;stack.data[4].Push(&m, Divide);
m = 2;stack.data[4].Push(&m, Add);
//////Next small expression
m = 75;stack.data[4].data[4].Push(&m, Multiply);
////////////////////////////////////////////////////////////
m = 5;stack.data[4].Push(&m, Divide);
////////////////////////////////////////////////////////////
m = 3;stack.Push(&m, Divide);
////////////////////////////////////////////////////////////
stack.Cal();
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
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:
1 2 3 4 5
m = 5;stack.Push(&m, Add);
m = 7;stack.Push(&m, Add);
m = 10;stack.Push(&m, Multiply);
m = 5;stack.data[2].Push(&m, Subtract);
m = 4;stack.Push(&m, Divide);
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.
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.
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.
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?"
---------------=======#=======--------------- NB: This post is not meant to offend anyone.
---------------=======#=======---------------
TheIdeasMan wrote:
Edit: I hope you can back me up in what I am saying as you have in the past, because I see you as being one of the sensible realistic people.
On the one hand you do raise some valid concerns. On the other hand, if readers didn't know the background they may just see you 'attacking' Jackson Marie at every opportunity. As a whole, I think that you have the best of intentions for Jackson Marie and the community as a whole.
[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.
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.
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?
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.
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) :)