|
26 Oct 2004, 12:57
|
#1
|
Mathamagician
Join Date: Aug 2001
Location: At the very edge of existance
Posts: 1,803
|
Using brackets
I'm making what is, essentially, a calculator in visual basic (i know, it should be in a proper language, but i cant be bothered to learn one), and I can't for the life of me work out how I could implement brackets into it. Any ideas would be useful, cheers.
__________________
I think I just had an evilgasm
|
|
|
26 Oct 2004, 13:33
|
#2
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
Re: Using brackets
From what I can remember from one of my first year CompSci modules, you have to convert the 'standard' (parenthesised, algebraic) notation to Reverse Polish Notation.
I can't fully remember
a) how to do that
b) what you do once you have
Not very helpful I know.
What I do remember is that there were two stacks. One for numbers, one for operators.
Once you've got your two stacks I *think* you can then pull off a number and an operator, a number and an operator etc and it should all process in the correct order.
Obviously you and google are going to have to spend some time together on this one.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
26 Oct 2004, 14:53
|
#3
|
Mathamagician
Join Date: Aug 2001
Location: At the very edge of existance
Posts: 1,803
|
Re: Using brackets
after much time with google i decided to do it "another way". Any ideas how to make a program (again in visual basic) restart itself. It seems all google wants to tell me is how to make VB restart windows.
__________________
I think I just had an evilgasm
|
|
|
26 Oct 2004, 15:02
|
#4
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
Re: Using brackets
You'd have spawn another process which would then call the first process back again as far as I know.
The simplest way would be to have another exe called "restart.exe" which you run immediately before exiting the program. Make "restart" wait a second before it launches your program again to give it time to exit.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
26 Oct 2004, 22:47
|
#5
|
Commander
Join Date: Sep 2001
Location: Netherlands
Posts: 146
|
Re: Using brackets
There are probably better ways, but this works:
Code:
Private Sub Form_Unload(Cancel As Integer)
Shell (App.Path & "\" & App.EXEName & ".exe")
End Sub
Although it won't restart itself when you kill it (thank god!)
And if you wanna test it in design mode, you should make an .exe (File --> Make .exe) first.
__________________
Quote:
Originally posted by Cochese
Cathaar are not overpowered.
You were just "bashed", live with it.
|
|
|
|
27 Oct 2004, 08:35
|
#6
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
Re: Using brackets
Quote:
Originally Posted by vampire_lestat
I'm making what is, essentially, a calculator in visual basic (i know, it should be in a proper language, but i cant be bothered to learn one), and I can't for the life of me work out how I could implement brackets into it. Any ideas would be useful, cheers.
|
I think the word megla was looking for was "postfix" ([edit] which apparently is called Reverse Polish Notation- you learn something every day) notation, see here
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
27 Oct 2004, 16:37
|
#7
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Re: Using brackets
Quote:
Originally Posted by vampire_lestat
I'm making what is, essentially, a calculator in visual basic (i know, it should be in a proper language, but i cant be bothered to learn one), and I can't for the life of me work out how I could implement brackets into it. Any ideas would be useful, cheers.
|
I'm going to hit you with a shovel if you continue using VB. I'm using VB6 in a "professional" environment now, and I HATE it... that's even an understatement. DISPISE it! It all looks like spaghetti to me.
C++ > *
__________________
"Yay"
|
|
|
27 Oct 2004, 23:09
|
#8
|
Friendly geek of GD :-/
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
|
Re: Using brackets
Well and I love it, so...
Anyway: - Can't you just parse the brackets? Find the most inner pair (i.e. the most opened count, right? you know where my idea goes, no?), evaluate the expression, and repeat...
- If you use the MS Scripting Class somewhere in the Add-Ins or Libraries, or some other ready-made pre-defined object, you can just say "Result = Something.Evaluate(MyCalculationString)" or so, and it's all well done...
- To restart an application, that's right, you could just use the shell-command. If you don't like the "unload"-event, try the QuereUnload or Terminate Event (this might even work after task manager kills, I don't know). And experiment with infinite loops and what happens once the get terminated etc.
Or do the advanced "another process watches me" trick...
__________________
[ »] Entropy increases! :-/
|
|
|
28 Oct 2004, 10:17
|
#9
|
Spelling is for pussies
Join Date: Mar 2003
Location: Actually, where the feck am I........?
Posts: 446
|
Re: Using brackets
Quote:
Originally Posted by Structural Integrity
I'm going to hit you with a shovel if you continue using VB. I'm using VB6 in a "professional" environment now, and I HATE it... that's even an understatement. DISPISE it! It all looks like spaghetti to me.
C++ > *
|
*cough*
C > C++
Never get bored of that.
Anyhow, couldn't you just tokenise the entire string? And then resolve each token until it became a simple token?
I.e
2 + (3 + 4) * (3 / (1 * 1))
Would become
A + B * C
A = 2
B = (3 + 4)
C = (9 / (1 * 3))
Recursively, it would then become :
A + B * D
A = 2
B = (3 + 4)
C = (1 * 3)
D = (9 / C)
Then just resolve to simple expressions.
__________________
If God made me in his image, he's one fat ugly biatch.
I always get the soggy biscuit
Veni Vidi Codi
|
|
|
28 Oct 2004, 12:55
|
#10
|
Banned
Join Date: Jul 2003
Location: ******
Posts: 2,326
|
Re: Using brackets
Make an expression tree.
|
|
|
1 Nov 2004, 17:48
|
#11
|
Paris <3
Join Date: Oct 2004
Location: Warez County
Posts: 15
|
Re: Using brackets
elaborating expression tree:
assuming this order on which goes first:
^
*
/
+
-
we will now introduce :
EXPR = an expression containg anything including ()
POW = EXPR without brackets
MUL = POW without any *
DIV = etc
PLUS = etc
MIN = etc
NUM = only needed if you allow -3.14e10 like expressions
you should have functions matching these expressions. You start out with passing the whole expression to EXPR. EXPR should scan for (BLA) and replace it with POW BLA. After that you should pass the modified expression to POW which should then pass the left and right hand sides of the ^ operator to the function below and then you can do lefthand_result ^ righthand_result. You go down that way for each operator, and make sure you can handle double operators like 2+2+2 :-)
That should be processed as
PLUS('2','2+2')
PLUS(NUM(2), PLUS('2','2'))
PLUS(2,PLUS(NUM(2),NUM(2)))
PLUS(2,4) = 6
i so hope this is clear :-/ its essentially an introduction to languages/compilers, so you could perhaps use those materials to help you.
PS if your calculator only does ints than a good design would make all NUM,POW functions etc return integers!
|
|
|
1 Nov 2004, 18:25
|
#12
|
Paris <3
Join Date: Oct 2004
Location: Warez County
Posts: 15
|
Re: Using brackets
because i know i can't explain i just made this, its buggy but show the way to go:
<?php
$expr="(2^2)^(2*5^3)*9";
echo EXPR($expr);
function EXPR($string) {
$offset = 0;
while(true) {
$leftbrace = strpos($string,'(',$offset);
if(!is_integer($leftbrace)) break;
$rightbrace= strpos($string,')',$offset);
$lefthand = substr($string,0,$leftbrace);
$braceexpr = substr($string,$leftbrace+1,$rightbrace-$leftbrace-1); //braces eliminated!
$remainder = substr($string,$rightbrace+1);
$string = $lefthand.EXPR($braceexpr).$remainder;
$offset = $leftbrace;
}
return POWf($string);
}
function POWf($string) {
$offset = 0;
echo 'POWF:'.$string."\r\n";
$power = strpos($string,'^');
if(!is_integer($power)) return MUL($string);
$lefthand = substr($string,0,$power);
$righthand= substr($string,$power+1); // eliminating ^ sign
return pow($lefthand,POWf($righthand));
}
function MUL($string) {
$offset = 0;
echo 'MUL :'.$string."\r\n";
$power = strpos($string,'*');
if(!is_integer($power)) return NUM($string);
$lefthand = substr($string,0,$power);
$righthand= substr($string,$power+1); // eliminating * sign
return $lefthand*MUL($righthand);
}
function NUM($string) {
echo 'NUM :'.$string."\r\n";
return intval($string);
}
?>
|
|
|
|
All times are GMT +1. The time now is 01:56.
| |