Jump to content

Recommended Posts

Hi,

Can someone help me with creating this calculator or tell me how to do it best?

 

I have 20 different fields and in each you put a different series of numbers in (each one has 6 digits in it) also I have one field called "number1" and one called "targetnumber"

 

in the field called "numebr1" you put (type) a certain series of digits (6 numbers). in the middle you have 2 fields for modifier numbers. the Script has to place different series of digits in these fields from the list of (about) 20 different options in the 2 modifier fields - so that : Number 1 + modifier1 + modyfier 2 = (will eqal) "targetnumber" (which you also need to type in the begining) until it finds 2 numbers that fit. for example :

 

Number1 : 1-2-3-4-5-6

 

Modifier 1 :

 

modifier 2 :

 

TargetNumber : 4-5-6-7-8-9

 

the script choses the correct 2 options to put between the target number to number 1 out of the 20 options you typed in the begining. and lets say 2 of your options were : 1-1-1-1-1-1 and 2-2-2-2-2-2 so the Script will put them in the modifier fields, like this :

 

Modifier 1 : 1-1-1-1-1-1

 

Modifier 2 : 2-2-2-2-2-2

 

(which is the correct answer)

 

the series of numbers are not 1 number, but 1 serie so every number is independent - for example :

 

0-1-5-9-9-9

 

+

 

1-5-5-1-1-1

 

will equale =

 

1-6-10-10-10-10

 

(also I must add - 0 is an independent and important number , and should not be ignored even if it's placed in the begining of the number) .

 

so the first digit in the serie of "number1" is added with the first digit from the series of numbers from "modifier1 " and then it gets added with the first Number of the series of numbers from "modifier2". then if it gets matched with the first digit of the TargetNumber it begins trying to match the second digit, then third...etc. if one of the resulted digits doesen't match with the TargetNumber's digit (in the same spot) it changes that series of numbers and places a different serie of digits from the given list instead , until it finds the correct 2 options that work (it has to go through all of the possible combos) .

 

that's basicly what I am trying to do.

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/89756-help-with-a-custom-calculator/
Share on other sites

Q:What if there are no combinations of modifiers to make number1 equal the target number?

A:there has to be a combination because we take the combinations from another website.

Q:What if there are multiple combnations?

A:there can only be one combination because those combinations taken from another website website.

This appears to work (based upon how I understand the problem). There is a limited amount of verification and I only did a limited amoutn of testing

 

<html>
<head></head>

<body>

<?php

if (isset($_POST)) {

  $modifier = array();
  $post_modifiers = $_POST['modifier'];
  if (is_array($_POST['modifier'])) {
    foreach ($_POST['modifier'] as $mod) {
      $mod_ary = explode('-',$mod);
      if (count($mod_ary)==6) {
        $modifier[] = $mod_ary;
      }
    }
  }

  $number1 = explode('-',$_POST['number1']);
  $target = explode('-',$_POST['target']);

  //Create an array of the differences. If the result
  //will be neg, do not add - act as a validation check
  $diff = array();
  for ($i=0; $i<6; $i++) {
    if ($target[$i]>=$number1[$i]) {
      $diff[] = $target[$i] - $number1[$i];
    }
  }

  if (count($modifier)<2 || count($diff)!=6) {

    echo "<b>Not all required fields entered or correct!</b>";

  } else {

    $soultionFound = false;

    foreach ($modifier as $modA) {

      foreach ($modifier as $modB) {

        if (($modA[0]+$modB[0])==$diff[0] && ($modA[1]+$modB[1])==$diff[1] &&
            ($modA[0]+$modB[2])==$diff[2] && ($modA[3]+$modB[3])==$diff[3] &&
            ($modA[0]+$modB[4])==$diff[4] && ($modA[5]+$modB[5])==$diff[5] && !$soultionFound) {

          $soultionFound = true;
          $mod1 = implode('-', $modA);
          $mod2 = implode('-', $modB);

        }
      }
    }
    if (!$soultionFound) {
      echo "<b>No solution found!.</b>";
    }
  }
}

?>


<form method="POST">

All values should be entered in the format 'n-n-n-n-n-n' (without the quotes):
<br><br>

  Modifier 1: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[0]; ?>" /><br>
  Modifier 2: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[1]; ?>" /><br>
  Modifier 3: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[2]; ?>" /><br>
  Modifier 4: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[3]; ?>" /><br>
  Modifier 5: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[4]; ?>" /><br>
  Modifier 6: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[5]; ?>" /><br>
  Modifier 7: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[6]; ?>" /><br>
  Modifier 8: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[7]; ?>" /><br>
  Modifier 9: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[8]; ?>" /><br>
  Modifier 10: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[9]; ?>" /><br>
  Modifier 11: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[10]; ?>" /><br>
  Modifier 12: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[11]; ?>" /><br>
  Modifier 13: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[12]; ?>" /><br>
  Modifier 14: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[13]; ?>" /><br>
  Modifier 15: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[14]; ?>" /><br>
  Modifier 16: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[15]; ?>" /><br>
  Modifier 17: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[16]; ?>" /><br>
  Modifier 18: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[17]; ?>" /><br>
  Modifier 19: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[18]; ?>" /><br>
  Modifier 20: <input type="text" name="modifier[]" value="<?php echo $post_modifiers[19]; ?>" /><br>
  <br><br>
  Number 1: <input type="text" name="number1" value="<?php echo $_POST['number1']; ?>" /><br>
  Modifier 1: <input type="text" name="mod1" value="<?php echo $mod1; ?>" readonly /><br>
  Modifier 2: <input type="text" name="mod2" value="<?php echo $mod2; ?>" readonly /><br>
  Target Number: <input type="text" name="target" value="<?php echo  $_POST['target']; ?>" /><br>
  <br>
  <button type="submit">Submit</button>
</form>

</body>
</html>

doeasn't works, I tried to test it with this:

Array (
[modifier] => Array (
[0] => 4-1-3-4-1-4
[1] => 1-4-1-3-2-4
[2] => 0-3-1-3-4-4
[3] => 3-2-4-1-2-4
[4] => 1-3-3-1-3-0
[5] => 4-4-4-1-3-4
[6] => 1-1-4-2-4-3
[7] => 1-0-1-4-2-3
[8] => 1-3-0-3-1-1
[9] => 2-2-4-3-0-3
[10] => 0-0-3-4-2-0
[11] => 1-1-2-1-3-0
[12] => 1-2-0-2-1-1
[13] => 1-4-4-4-0-0
[14] => 3-1-2-0-2-4
[15] => 2-3-0-2-0-0
[16] => 0-4-4-3-3-4
[17] => 3-3-2-2-0-0
[18] => 3-0-3-3-3-2
[19] => 1-2-3-3-4-3
)
[number1] => 1-1-1-4-0-1
[mod1] =>
[mod2] =>
[target] => 1-4-4-4-3-0
)

That's because there is no solution in that list absed upon the criteria you gave! That is why I asked before what the process should be in those cases.

 

It is easy to see that there is no solution. Here are the two input numbers:

 

1-1-1-4-0-1

1-4-4-4-3-0

 

Now, since the difference between those two bold numbers is 0, the two modifiers for the solution must have 0 as the fourth number, right? There is only one modifier number in that list of 20 which has a 0 as the 4th digit. So, it would be impossible to add the number1 digits to the digits of two of the modifiers and get the target number digits. Also, how can the last digits go from 1 to 0? That negates the possibility of a solution just with that.

 

If I am not interpreting how this works, them please explain. I built the code according to how I understood the details you provided.

This shouldn't be that hard at all. I'll explain what you should be doing with a few examples and explanations. (why don't you try doing it with just one digit at first, when that is working, go ahead and add the other 5 in to the equation, as it seems each digit can run on its own piece of code). So let's just look at getting one digit to work:

 

#1: 1

 

T#: 3

 

Subtract #1 from your target number. 3-1 = 2. Now you want to divide that by 2, getting 1. M1 and M2 can both equal 1.

 

 

#1: 6

 

T3: 3

 

Subtract #1 from your target number. 3-6 = -3. Now you want to divide that by 2, getting -1.5. You want whole numbers, so round this number down to the nearest whole number, getting -1. Get your difference between -3 and -1, being -2 and you now have a M1 of -1 and a M2 of -2.

 

The first solution seemed easier, though you need to go with the second solution if you want to be able to calculate every situation. If I understand correctly, this is what you are trying to do.. correct me if I am wrong, and second example has both negative and decimals to show that what needs to be done to make it all work correctly. Any questions or if you want some code examples just ask.

Since the numbers are entered in the format 1-2-3-4-5-6 and the '-' is the separator, I don't see how their can be negative numbers.

 

1-2-3--4-5-6 ???

 

The OP apparently doesn't wan't to take the time to describe the problen adequately enough to get a solution.

 

I took a second look at the numbers he posted and thought that maybe the numbers were base 5, but even with that there is not a solution.

Well, with what he said being that sometimes the number may go backwards, meaning you would have to add a negative number to the starting number to reach your goal. Does it have to be formatted 1-2-3-4-5-6 or could it be (1)-(2)-(3)-(-4)-(5)-(6). I wasn't worried about the formatting of the numbers, just how to achieve the goal.  If it has to be formatted a certain way, then yes that needs to be mentioned and made apparent as, if like you stated, it has to be 1-2-3-4-5-6, then you couldn't have a negative unless you had --4.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.