User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 26 Oct 2004, 12:57   #1
vampire_lestat
Mathamagician
 
vampire_lestat's Avatar
 
Join Date: Aug 2001
Location: At the very edge of existance
Posts: 1,803
vampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet society
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
vampire_lestat is offline   Reply With Quote
Unread 26 Oct 2004, 13:33   #2
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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.
meglamaniac is offline   Reply With Quote
Unread 26 Oct 2004, 14:53   #3
vampire_lestat
Mathamagician
 
vampire_lestat's Avatar
 
Join Date: Aug 2001
Location: At the very edge of existance
Posts: 1,803
vampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet societyvampire_lestat is a pillar of this Internet society
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
vampire_lestat is offline   Reply With Quote
Unread 26 Oct 2004, 15:02   #4
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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.
meglamaniac is offline   Reply With Quote
Unread 26 Oct 2004, 22:47   #5
Caesar2
Commander
 
Caesar2's Avatar
 
Join Date: Sep 2001
Location: Netherlands
Posts: 146
Caesar2 is just really niceCaesar2 is just really niceCaesar2 is just really niceCaesar2 is just really nice
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.
Caesar2 is offline   Reply With Quote
Unread 27 Oct 2004, 08:35   #6
SYMM
Love's Sweet Exile
 
SYMM's Avatar
 
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
SYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better placeSYMM single handedly makes these forums a better place
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
SYMM is offline   Reply With Quote
Unread 27 Oct 2004, 16:37   #7
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 27 Oct 2004, 23:09   #8
JetLinus
Friendly geek of GD :-/
 
JetLinus's Avatar
 
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
JetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud of
Arrow 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! :-/
JetLinus is offline   Reply With Quote
Unread 28 Oct 2004, 10:17   #9
SbOlly
Spelling is for pussies
 
SbOlly's Avatar
 
Join Date: Mar 2003
Location: Actually, where the feck am I........?
Posts: 446
SbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant futureSbOlly has a brilliant future
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
SbOlly is offline   Reply With Quote
Unread 28 Oct 2004, 12:55   #10
Banned
Banned
 
Banned's Avatar
 
Join Date: Jul 2003
Location: ******
Posts: 2,326
Banned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so littleBanned contributes so much and asks for so little
Re: Using brackets

Make an expression tree.
Banned is offline   Reply With Quote
Unread 1 Nov 2004, 17:48   #11
4VAlien
Paris <3
 
Join Date: Oct 2004
Location: Warez County
Posts: 15
4VAlien will become famous soon enough4VAlien will become famous soon enough
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!
4VAlien is offline   Reply With Quote
Unread 1 Nov 2004, 18:25   #12
4VAlien
Paris <3
 
Join Date: Oct 2004
Location: Warez County
Posts: 15
4VAlien will become famous soon enough4VAlien will become famous soon enough
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);
}

?>
4VAlien is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 02:23.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018