Sign in to follow this  
Followers 0
lorenkinzel

temp storing values

10 posts in this topic

This is a script that converts decimal to fractions. It is to be part of a beam-camber calculator.

Could someone please show me how to do this without the 2 labels to store data?

It works fine, but seems like kicking the door down rather than picking the lock.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$form1 = GUICreate("Form 1-1/2", 623, 442, 192, 124)
$input1 = GUICtrlCreateInput("", 16, 16, 177, 21)
$convertButton = GUICtrlCreateButton("convert", 56, 56, 97, 33)
$output2 = GUICtrlCreateInput("", 16, 96, 177, 21)
$nonIntegerPortion = GUICtrlCreateLabel("", 1, 1, 0, 0);>>>>>>>>>>>>>>>>>>this one
GUICtrlSetState($nonIntegerPortion, $GUI_HIDE)
$integerPortion = GUICtrlCreateLabel("", 0, 0, 0, 0);>>>>>>>>>>>>>>>>>>>& this one
GUICtrlSetState($integerPortion, $GUI_HIDE)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $convertButton
get16th()
EndSwitch
WEnd
Func get16th()
$readInput1 = GUICtrlRead($input1)
$lessInt = $readInput1 - Int($readInput1)
$readInt = GUICtrlRead($integerPortion)
GUICtrlSetData($integerPortion, Int($readInput1) & " - ");extract & stash the integer in a safe place
$incrementOf16 = Round($lessInt * 16, 0) ;convert non-integer to an even multiple of 16
$decOf16th = $incrementOf16 / 16 ;back to (adjusted) decimal value
GUICtrlSetData($nonIntegerPortion, $decOf16th);stash non-integer in a safe place
is2th()
EndFunc ;==>get16th
Func is2th() ;begin to determine denominator
$readNonInt = GUICtrlRead($nonIntegerPortion)
$readInt = GUICtrlRead($integerPortion)
If ($readNonInt * 2) - Int($readNonInt * 2) = 0 Then
$denom = "/2" ;denominator usless for other than visual output. To-do list
$num = ($readNonInt * 2)
If $num = 2 Then ;removes the 'round-up-to-2/2' problem
$num = ""
$readInt = $readInt + 1
$denom = ""
EndIf
If $num = 0 Then ;removes the 'round-down-to-0/2' problem
$num = ""
$denom = ""
$readInt = $readInt - " - " ;might as well 86 the dash as well
EndIf
GUICtrlSetData($output2, $readInt & $num & $denom)
Else
is4th() ; if no-good as half, try out fourth
EndIf
EndFunc ;==>is2th
Func is4th() ;continue with the denominator-thingy
$readNonInt = GUICtrlRead($nonIntegerPortion)
$readInt = GUICtrlRead($integerPortion)
If ($readNonInt * 4) - Int($readNonInt * 4) = 0 Then
$denom = "/4"
$num = ($readNonInt * 4)
GUICtrlSetData($output2, $readInt & $num & $denom)
Else
is8th() ;and so-on.....
EndIf
EndFunc ;==>is4th
Func is8th()
$readNonInt = GUICtrlRead($nonIntegerPortion)
$readInt = GUICtrlRead($integerPortion)
If ($readNonInt * 8) - Int($readNonInt * 8) = 0 Then
$denom = "/8"
$num = ($readNonInt * 8)
GUICtrlSetData($output2, $readInt & $num & $denom)
Else
is16th()
EndIf
EndFunc ;==>is8th
Func is16th()
$readNonInt = GUICtrlRead($nonIntegerPortion)
$readInt = GUICtrlRead($integerPortion)
If ($readNonInt * 16) - Int($readNonInt * 16) = 0 Then
$denom = "/16"
$num = ($readNonInt * 16)
GUICtrlSetData($output2, $readInt & $num & $denom)
EndIf
EndFunc ;==>is16th

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't know about the method you are using to convert, but you need to concatenate the dividend and the divisor if you want to use one label for the output.

Edited by czardas

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I am using one label (o.k., input) for the output.

GUICtrlSetData($output2, $readInt & $num & $denom)

The whole thing works as expected, I was just looking for a way to do it without using the 2 hidden labels as data transfer medium.

Edited by lorenkinzel

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well for data transfer medium(between two distinct Scripts) you can try setting an Environment Variable

EnvSet()

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I don't understand the problem with storing the data in a normal way. Why not just use an array to store multiple variables?

Edited by czardas

Share this post


Link to post
Share on other sites

PhoenixXL; thanks, I will look into that.

Czardas; that was what I was looking for ---what is a normal way---. Thanks.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I think your issue is with understanding scope.

You'd only need to write your data to an external file or environment variables if you needed to retain the values between executions of your script, or to make the data available to a seperate program once this one had finished. This doesn't appear to be a requirement for you.

The reason storing the data in hidden GUI controls is working for you is that the controls are defined outside of any function, which forces the Control ID's $IntegerPortion and $nonIntegerPortion to have Global scope. Meaning they can be accessed from anywhere within the script. But, as you recognized, to access data stored in a GUI control requires calling GuiCtrlSetData() and GuiCtrlRead().

All you need to do is create $IntegerPortion and $nonIntegerPortion as standard data variables with global scope, that you can access directly. Just stick the line "Global $IntegerPortion, $NonIntegerPortion" after your includes, and get rid of the GUICreateLabel(), GUICtrlSetData() and GUICtrlRead() statements.

Edit: Searching this forum on the word "scope" would likely get you a kazillion more explanations of the concept, many more extensive than this one.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>

Global $sOutput

$form1 = GUICreate("Form 1-1/2", 623, 442, 192, 124)
$input1 = GUICtrlCreateInput("", 16, 16, 177, 21)
$iButton = GUICtrlCreateButton("CONVERT", 32, 52, 80, 26, $BS_DEFPUSHBUTTON)
$iResolution = GUICtrlCreateCombo("4", 140, 54, 40, 26)
GUICtrlSetData(-1, "8|16|32|64", "16")
$output2 = GUICtrlCreateInput("", 16, 96, 177, 21)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
     Case $iButton
         $sOutput = Convert(GUICtrlRead($input1), GUICtrlRead($iResolution))
         GUICtrlSetData($output2, $sOutput)
    EndSwitch
WEnd

Func Convert($iInput, $iDenominator)
    Local $iInputInt = Int($iInput) ; integer portion
    Local $iInputDec = $iInput - $iInputInt ; decimal remainder
    Local $iNumerator = Round($iInputDec / (1 / $iDenominator)) ; numerator
    If $iNumerator = 0 Then Return $iInputInt ; rounds to zero
    While Not Mod($iNumerator, 2) ; reduce fraction
        $iNumerator /= 2
        $iDenominator /= 2
    WEnd
    If $iDenominator = 1 Then Return $iInputInt + 1 ; rounds to integer
    Return $iInputInt & " " & $iNumerator & "/" & $iDenominator
EndFunc

Edit: added code to handle values that round down to 0, or up to 1

Edited by Spiff59

Share this post


Link to post
Share on other sites

Spiff59;

Good assessment.

Good explanation.

Simple fix.

:thumbsup: :thumbsup:

The script doesn't work any better without the hidden labels, but my brain does.

A little more reading on scope (among other items) is in order.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

If you want a quicker route... look in the spoiler in my prior post. I think calculating for the highest-common-denominator, and then reducing, might save you some effort.

Personally, I've always found it easier to learn stuff from studying, understanding, then modifying existing code, than trying to create it from scratch.

Edited by Spiff59

Share this post


Link to post
Share on other sites

Spiff59;

Good assessment.

Good explanation.

Simple fix.

:thumbsup: :thumbsup:

I thought so too, and I couldn't have worded it any better myself. Nice one Spiff59.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0