hope get help in this code;
its for a calculator

when the user press "=" that containing this script the answer should apear

at first of all i make sprite(2) as the space which the process will apera

this is my code ; it dose not work !!

on mousedown
  variable1=sprite(2).text
  global s
  s=variable1.word[2]
  global plase
  case (s) of
  "+ ": plase=variable1.word[1]+variable1.word[4]
  "*":multipl=variable1.word[1]*variable1.word[3]
  otherwise:put "wrong process" after sprite(2).member
end case
end

on mouseup
case (s) of 
"+":put plase after sprite(2).member
"*":put multipl after sprite(2).member
end case
end

let the process in sprite (2) be :
1*2=

[img][/img]

Dani AI

Generated

This looks like Director/Lingo code (not ActionScript), so fix suggestions assume Lingo-style string handling and event behavior. Two immediate problems stand out and will produce wrong results or duplicated characters:

First, using word[...] depends on spaces. If the field contains 1*2= with no spaces the operator and = stay glued to the numbers, so your word indices will point at wrong tokens. Also the plus branch uses different indexes than the multiply branch, so one branch can pick up the = or the operator instead of the second operand.

Second, appending with put ... after sprite(2).member will add text to whatever is already there. If any other logic (button label code or a different handler) also writes the operator or the result, you will see duplicates. Having both on mousedown and on mouseup do related work can also lead to double-writing.

Practical fixes to apply now (no tag/architecture changes required):

  • Normalize the input first: strip a trailing = and trim surrounding spaces.
  • Find the operator by scanning the string for +, -, *, / instead of relying on fixed word positions.
  • Split into left/right substrings around that operator, trim them, then convert those substrings to numbers before computing.
  • Replace the field value with the result (overwrite) rather than appending with after.
  • Keep parsing and write/format logic in a single handler (use mouseUp only) and declare any globals once at script scope.

For multi-operator expressions or proper precedence, follow ’s hint and use a tokenizer + stack (shunting-yard) or reuse an expression evaluator rather than ad-hoc word indexing. For a quick test, change the + branch to use the same operand positions as * (or better, switch to operator-based splitting) and verify the intermediate token values with debug output so you can see exactly what each index contains.

Recommended Answers

All 2 Replies

Two questions:

1. are you parsing the values before adding or multiplying them?

Right now, it looks like you are multiplying the 1 in your sample by the equal sign.

2. Are you taking into account different lengths of numbers?

I use a stack when I parse for a calculator, since ordinary arithmetic uses infix operators with algebraic precedence.

Two questions:

1. are you parsing the values before adding or multiplying them?

Right now, it looks like you are multiplying the 1 in your sample by the equal sign.

2. Are you taking into account different lengths of numbers?

I use a stack when I parse for a calculator, since ordinary arithmetic uses infix operators with algebraic precedence.

1- SURE , I PASE THE VALUES BEFORE PRESS EQUAL

there is something not ok here , with the '+' , when press it its apeear twice in the text field

so the code i wrote to it is a little bit diff. just to solve it now.

2- no, i just want to know the basic now and the method of how to do it with simple numbers

see what i did in this link :

http://hyperupload.com/download/026f95db26/CalculaterZahraa.dir.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.