New child language - (Function member address (structure))

Pages: 1... 45678... 20
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.
Last edited on
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.
@helios
Other expression :
x = 5 + 7 * (10 - 5) / 4 + 9


m = 5;stack.Push(&m, Add);

Attach a variable with its value (5) (+)

Result += 5 (But using Equal is more accurate)

m = 7;stack.Push(&m, Add);

Attach a variable with its value (7) (+)

Result += 5; Result += 7;

m = 10;stack.Push(&m, Multiply);

Attach a variable with its value (10) (*)

Result += 5; Result += 7 * 10;

m = 5;stack.data[2].Push(&m, Subtract);

Attach a variable with its value (5) (-)

Result += 5; Result += 7 * (10 - 5);

m = 4;stack.Push(&m, Divide);

Attach a variable with its value (5) (/)

Result += 5; Result += 7 * (10 - 5) / 4;

m = 9;stack.Push(&m, Add);


Result += 5; Result += 7 * (10 - 5) / 4; Result += 9;

That.
Last edited on
Further to helios' post:

The whole point of a RPN stack type program is to input a string like :

5 7 + 10 5 - 4 / *
which does the equivalent (using BEDMAS) of
5 + 7 * (10 - 5) / 4


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.
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.
x = 72 - 54 * 54 / 6 + (6 - 52 * 7 - 65 / 5 + (2 * 75) / 5) / 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.
I'm sure. About speed? Efficiency? And performance?
Now I can write :
x = 72 - 54 * 54 / 6 + (6 - 52 * 7 - 65 / 5 + (2 * 75) / 5) / 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double Result = 0;
Stack stack;stack.PushItSelf(&Result, Add, true);

stack.Push(72, Equal);
stack.Push(54, Subtract);
stack.Push(54, Multiply);
stack.Push(6, Divide);
stack.Push(6, Add);
//////Next small expression
stack.data[4].Push(52, Subtract);
stack.data[4].Push(7, Multiply);
stack.data[4].Push(65, Subtract);
stack.data[4].Push(5, Divide);
stack.data[4].Push(2, Add);
//////Next small expression
stack.data[4].data[4].Push(75, Multiply);
////////////////////////////////////////////////////////////
stack.data[4].Push(5, Divide);
////////////////////////////////////////////////////////////
stack.Push(3, Divide);
////////////////////////////////////////////////////////////
stack.Cal();


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
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?"
Last edited on
closed account (z05DSL3A)

---------------=======#=======---------------
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.
Last edited on
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
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
Does anyone have any idea?

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.
Thanks all (m4ster r0shi and EssGeEich)!!!

Ok. I've just done the expression parser. Here is the code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
char temp[64];
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Expression(const char *str, unsigned int len, Stack &stack, unsigned short Start = 0, bool bChild = false){
bool FindNumber = true;
bool bReturn = false;
unsigned int nChrCount = 0; //Size of number string
double fVal = 0; //Value (atof)
char nMode = Equal;
///////////////////////////////////////////////////////////////////////////
for(unsigned int i = Start;i < len;i++)
{

if(str[i] == ')' && bChild)return i;
///////////////////////////////////////////////////////////////////////////////////
if(FindNumber)
{

	if(str[i] == '('){ //Next small expression...
		stack.Push(0.0, nMode); // Push a null value

		i = Expression(str,len,stack.data[stack.data.size() - 1], i + 1, true); //Start parsing the small expression

		if(bChild && str[i] == ')')return i + 1;
	FindNumber = false;nMode = -1;continue;
	}


	while(i < len && (str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '-')){
		if(str[i] != '-')temp[nChrCount] = str[i];else 
		if(!nChrCount) temp[nChrCount] = str[i]; 
		else break;
		
		nChrCount++l

if(str[i] == ')' && bChild){bReturn = true;break;}}

	if(nChrCount){ //(nChrCount == 1)
	temp[nChrCount] = 0;fVal = atof(temp);
	stack.Push(&fVal, nMode);

	if(bReturn)return i; //Next index value

	FindNumber = false;nChrCount = 0;nMode = 0; //Clean result
}}
///////////////////////////////////////////////////////////////////////////////////

if(!FindNumber) //Start finding an operator
{
	if(str[i] == '%')nMode = Modulus;
	if(str[i] == '*')nMode = Multiply;
	if(str[i] == '+')nMode = Add;
	if(str[i] == '-')nMode = Subtract;
	if(str[i] == '/')nMode = Divide;
	if(str[i] == '=')nMode = Equal;
	if(nMode != -1){FindNumber = true;
	cout << "found : (str[" << i << "] : " << str[i] << endl;
	
	}

}


}
return i;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define MAX_LEN 256
#define cBackspace 8
#define cEnter 13

int main(){unsigned char chr;
double Result = 0;
unsigned int len = 0;

Stack stack;stack.PushItSelf(&Result, Add, true);

char str[MAX_LEN];
cout << "Please, enter an expression : ";


while((chr = getche()) != cEnter || !len){

if(chr == 8 && len || chr == cEnter){
	if(chr == cBackspace){str[len - 1] = 0;len--;}system("cls");cout << "Please, enter an expression : "; cout << str;continue;}

	if(len == MAX_LEN - 1)break;str[len] = chr;len++;}
str[len] = 0;

cout << endl;

cout << str << endl;
Expression(str, len, stack);

cout << endl;

stack.Cal();

cout << "The result : " << Result << endl;

return 0;


How to input : Example :
50 + 2 * (10 - 5) / 2


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
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) :)
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...
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
Pages: 1... 45678... 20