I'm currently implement left shift using int[] arrays in php and need to get back the decimal after operation. So I have written the following snippet to attempt conversion of binary array to decimal.
function bin2dec($bin)
{
$length = count($bin) - 1;
$sum = 0;
//convert using doubling
for($i = 0; $i < $length; $i++)
{
//use string_add if doubling bigger than int32
if($i >= 16)
{
$double = $this->string_add("$sum", "$sum");
$cr = $bin[$i];
if($cr == 0)
{
$sum = $this->string_add($sum, $double);
}
else{
return $i;//WHAT's UP??!
$add = $this->string_add($double, "$cr");
$sum = $this->string_add($sum, $add);
}
}
else{
$sum += ($sum * 2) + $bin[$i];
}
}
return $sum;
}
Now the weird problem is in the loop where $cr != 0, $i returns an unbelievable value already not satisfying the loop condition but I can't figure out why this is happening. Here's the rest of the relevant code.
function string_add($a, $b)
{
$lena = strlen($a); $lenb = strlen($b);
if($lena == $lenb)
{
$len = $lena - 1;//any
}
else if($lena > $lenb)
{
$b = str_pad($b, $lena, "0", STR_PAD_LEFT);
$len = $lena - 1;
}
else if($lenb > $lena){
$a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
$len = $lenb - 1;
}
$result = "";
for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
{
$add1 = $i < 0 ? 0 : $a[$i];
$add2 = $i < 0 ? 0 : $b[$i];
$add = $add1 + $add2 + $carry;
if ($add > 9) {
$carry = 1;
$add -= 10;
}
else {
$carry = 0;
}
$result .= $add;
}
return strrev($result);
}
$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test
I have also implemented a working version on ideone for testing. Does anyone understand why this is happening?
Thanks.
Ok, so apparently the problem was adding more than needed and unnecessarily subtracting 1 from length in bin2dec. Here's the final working version:
<?php
class MyClass{
function bin2dec($bin)
{
$length = count($bin);
$sum = 0;
//convert using doubling
for($i = 0; $i < $length; $i++)
{
//use string_add if doubling bigger than int32
if($i >= 16)
{
$sum = $this->string_add("$sum", "$sum");
$cr = $bin[$i];
if($cr != 0){
$sum = $this->string_add($sum, "$cr");
}
}
else{
$sum += $sum + $bin[$i];
}
}
return $sum;
}
function string_add($a, $b)
{
$lena = strlen($a); $lenb = strlen($b);
if($lena == $lenb)
{
$len = $lena - 1;//any
}
else if($lena > $lenb)
{
$b = str_pad($b, $lena, "0", STR_PAD_LEFT);
$len = $lena - 1;
}
else if($lenb > $lena){
$a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
$len = $lenb - 1;
}
$result = "";
for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
{
$add1 = $i < 0 ? 0 : $a[$i];
$add2 = $i < 0 ? 0 : $b[$i];
$add = $add1 + $add2 + $carry;
if ($add > 9) {
$carry = 1;
$add -= 10;
}
else {
$carry = 0;
}
$result .= $add;
}
return strrev($result);
}
}
$man = new MyClass();
$arr = array_pad(array(1), 62, 0);
$dec = $man->bin2dec($arr);
echo $dec;
Using strings...
function bin2dec($bin)
{
$length = count($bin);
$sum = "";
for ( $i = 0; $i < $length; $i++)
{
$sum = $sum . ( $bin[$i} == 0 ? '0' : '1');
}
return $sum;
}
function string_add($a, $b)
{
// not efficient, but obvious
$maxlen = ( strlen($a) > strlen($b) ? strlen($a) : strlen($b);
// make them both same length as longest
$a = str_pad($a, $maxlen, "0", STR_PAD_LEFT);
$b = str_pad($b, $maxlen, "0", STR_PAD_LEFT);
$result = "";
$carry = 0;
// start from the right end
for ($i = $maxlen - 1; $i >= 0; $i--)
{
$val = $a[$i] + $b[$i] + $carry;
$result = ( $val % 10 ) . $result;
$carry = $val / 10;
}
// handle final carry if present
if ( $carry > 0 )
{
$result = $carry . $result;
}
return $result;
}
$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test
I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}
I am working on a PHP project in which I need to combine rows (the rule is: if the first two numbers matched then add the rest numbers)
I have this array :
array_b4_combine= [
[2,15,1,1,0],
[2,15,3,3,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0],
[3,24,1,1,0]];
the output should be :
combined= [
[2,15,5,5,0],
[2,21,2,2,0],
[2,24,9,9,0],
[3,15,8,8,0],
[3,24,1,1,0]];
This is my code :
$num1 = $array_b4_combine[0][0];
$num2 = $array_b4_combine[0][1];
$sum1 = 0;
$sum2 = 0 ;
$sum3 = 0 ;
$combined ;
for ( $i = 0 ; $i < count($array_b4_combine) ; $i++)
{
if ($num1 == $array_b4_combine[$i+1][0] && $num2 == $array_b4_combine[$i+1][1])
{
$sum1 = $sum1 + $array_b4_combine[$i][2];
$sum2 = $sum2 + $array_b4_combine[$i][3];
$sum3 = $sum3 + $array_b4_combine[$i][4];
}
else
{
$combined[] = array($num1 , $num2 , $sum1, $sum2, $sum3);
$day = $array_b4_combine[$i][0];
$time = $array_b4_combine[$i][1];
$sum1 = $array_b4_combine[$i][2];
$sum2 = $array_b4_combine[$i][3];
$sum3 = $array_b4_combine[$i][4];
}
}
the output for my code is this:
combined=
[[2,15,4,4,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0]];
Am I doing the reset clause in wrong order.. can someone figure out what is the problem here.
Thanks
I really cannot understand your code, try this one:
$combined = [];
foreach ($array_b4_combine as $i => $arrayRow) {
$k = $arrayRow[0].' '.$arrayRow[1];
if (isset($combined[$k]))
for ($i=2; $i<5; $i++)
$combined[$k][$i] += $arrayRow[$i];
else
$combined[$k] = $arrayRow;
}
$combined = array_values($combined);
Here is how i solved your problem . code can be optimized .
<?php
$array_b4_combine = [
[2,15,1,1,0],
[2,15,3,3,0],
[2,15,1,1,0],
[2,21,2,2,0],
[2,24,7,7,0],
[2,24,2,2,0],
[3,15,1,1,0],
[3,15,7,7,0],
[3,24,1,1,0]];
$j =0;
for ( $i = 0 ; $i < count($array_b4_combine) -1 ; $i++)
{
if($i == 0)
{
$sum1 = $array_b4_combine[$i][2];
$sum2 = $array_b4_combine[$i][3];
$sum3 = $array_b4_combine[$i][4];
}
if(($array_b4_combine[$i][0] == $array_b4_combine[$i+1][0]) && ($array_b4_combine[$i][1] == $array_b4_combine[$i+1][1]) )
{
$sum1 = $sum1 + $array_b4_combine[$i+1][2];
$sum2 = $sum2 + $array_b4_combine[$i+1][3];
$sum3 = $sum3 + $array_b4_combine[$i+1][4];
$combined[$j][0] = $array_b4_combine[$i][0];
$combined[$j][1] = $array_b4_combine[$i][1];
$combined[$j][2] = $sum1;
$combined[$j][3] = $sum2;
$combined[$j][4] = $sum3;
}
else
{ $j++;
$combined[$j][0] = $array_b4_combine[$i+1][0];
$combined[$j][1] = $array_b4_combine[$i+1][1];
$combined[$j][2] = $array_b4_combine[$i+1][2];
$combined[$j][3] = $array_b4_combine[$i+1][3];
$combined[$j][4] = $array_b4_combine[$i+1][4];
$sum1 = $combined[$j][2];
$sum2 = $combined[$j][3];
$sum3 = $combined[$j][4];
}
}
echo "<pre>";
print_r($combined);
echo "</pre>";
?>
I have this code that is working perfectly. My question is how can I sum all the value of $manpowerCost and $otherCharges?
for ($i = 1; $i<=9; $i++) {
$m = '0'.$i;
$sumMc = array();
$revenue = revenue($project, $year.'-'.$m);
$manpowerCost = manpowerCost($project, $year.'-'.$m);
$otherCharges = otherCharges($project, $year.'-'.$m);
$netIncome = netIncome($revenue, $manpowerCost, $otherCharges);
if ($manpowerCost<=0 && $m <= $projected_date){
$manpowerCost = round(($projected_mc * $percentage_mc),2);
$projected_total_mc += $manpowerCost;
$pt = $pt + $manpowerCost;
} else {
$projected_mc = $manpowerCost;
$manpowerCost = $projected_mc;
}
if ($otherCharges<=0 && $m <= $projected_date){
$otherCharges = round(($projected_oc * $percentage_oc),2);
$projected_total_oc += $otherCharges;
$pt = $pt + $otherCharges;
} else {
$projected_oc = $otherCharges;
$otherCharges = $projected_oc;
}
echo '<td>RE:'.number_format($revenue, 2, '.', '').'<br />MC:'.number_format($manpowerCost, 2, '.', '').'<br />OC:'.number_format($otherCharges, 2, '.', '').'<br />NI:'.$netIncome.'</td>';
}
U can Try like this
$powercost_total = '0';
for ($i = 1; $i<=9; $i++) {
$manpowerCost = manpowerCost($project, $year.'-'.$m);
$powercost_total = $manpowerCost + 4powercost_total;
....
Declare $TotalManPowerCost and $TotalOtherCharges variables outside of the loop, and add $manpowercost and $otherCharges to them in each iteration of the loop