Posts Tagged ‘Programming’

Sometimes a Bug Is More Than a Nuisance

No Comments »

It took the European Space Agency 10 years and $7 billion to produce Ariane 5, a giant rocket capable of hurling a pair of three-ton satellites into orbit with each launch and intended to give Europe overwhelming supremacy in the commercial space business.
All it took to explode that rocket less than a minute into its maiden voyage last June, scattering fiery rubble across the mangrove swamps of French Guiana, was a small computer program trying to stuff a 64-bit number into a 16-bit space. Read the rest of this entry »


Numbers to Words

No Comments »

The best solution for this problem is Pear package, but if you don’t like it, you can use this PHP class for converting to Lithuanian language:

Gal = "";
		$c = $this->Tukst($a, "", "", "");
		$skaicius = intval($skaicius / 1000);
		$a = $skaicius;
		If ($a % 1000 > 0) {
			$c = $this->Tukst($a, "čių", "tis", "čiai") . " tūkstan" . $this->Gal . " " . $c;
		}
		$skaicius = intval($skaicius / 1000);
		$a = $skaicius;
		If ($a % 1000 > 0) {
			$c = $this->Tukst($a, "ų", "as", "ai") . " milijon" . $this->Gal . " " . $c;
		}
		If ($formatas == 0) {
			If ($sk > 0) {
				$c = trim($c) . " Lt ";
			}
			Else {
				$c = "nulis Lt ";
			}
			$tmp = $this->Str((1 + $sk));
			//echo "$tmp
\n";
			$tmp = $tmp*100;
			//echo "$tmp
\n";
			////$tmp = $tmp%10000;
			//echo "$tmp
\n";
			$tmp = trim($tmp);
			//echo "$tmp
\n";
			//$tmp = mid($tmp, 1, 2);
			$tmp = substr($tmp, -2);
			//echo "$tmp
\n";

			//$c = $c . mid(trim(Str((1 + $sk) * 100 % 1000)), 1, 2) . " ct";
			$c = $c . $tmp . " ct";
		}
		If ($formatas == 1) {
			If (Intval($sk) == 0) {
				$c = "nulis";
			}
			$c = $c . " vienet" . $this->GG(Intval($sk));
		}
		$c = $this->UPPER_LT($this->Mid($c, 0, 1)) . $this->Mid($c, 1, 254);
		return $c;
	}

	private Function Tukst($A , $G1, $G2, $G3)
	{
		$C = "";
		$this->Gal = "";
		If ($A == 0) {
			//return $C;
		}
		$B = $A % 10;
		If ($B == 0) {
			$this->Gal = $G1;
		}
		If ($B == 1) {
			$this->Gal = $G2;
		}
		If ($B > 1) {
			$this->Gal = $G3;
		}
		switch ($B) {
			Case 1:
				$C = "vienas";
				break;
				Case 2:
					$C = "du";
					break;
					Case 3:
						$C = "trys";
						break;
						Case 4:
							$C = "keturi";
							break;
							Case 5:
								$C = "penki";
								break;
								Case 6:
									$C = "šeši";
									break;
									Case 7:
										$C = "septyni";
										break;
										Case 8:
											$C = "aštuoni";
											break;
											Case 9:
												$C = "devyni";
												break;
		}
		$B1 = $B;
		$A = Intval($A / 10);
		$B = $A % 10;
		If ($B == 1) {
			$this->Gal = $G1;
			switch ($B1) {
				Case 0:
					$C = "dešimt";
					break;
					Case 1:
						$C = "vienuolika";
						break;
						Case 2:
							$C = "dvylika";
							break;
							Case 3:
								$C = "trylika";
								break;
								Case 4:
									$C = "keturiolika";
									break;
									Case 5:
										$C = "penkiolika";
										break;
										Case 6:
											$C = "šešiolika";
											break;
											Case 7:
												$C = "septyniolika";
												break;
												Case 8:
													$C = "aštuoniolika";
													break;
													Case 9:
														$C = "devyniolika";
														break;
			}
		}
		switch ($B) {
			Case 2:
				$C = "dvidešimt " . $C;
				break;
				Case 3:
					$C = "trisdešimt " . $C;
					break;
					Case 4:
						$C = "keturiasdešimt " . $C;
						break;
						Case 5:
							$C = "penkiasdešimt " . $C;
							break;
							Case 6:
								$C = "šešiasdešimt " . $C;
								break;
								Case 7:
									$C = "septyniasdešimt " . $C;
									break;
									Case 8:
										$C = "aštuoniasdešimt " . $C;
										break;
										Case 9:
											$C = "devyniasdešimt " . $C;
											break;
		}

		$A = Intval($A / 10);
		$B = $A % 10;
		switch ($B) {
			Case 1:
				$C = "vienas šimtas " . $C;
				break;
				Case 2:
					$C = "du šimtai " . $C;
					break;
					Case 3:
						$C = "trys šimtai " . $C;
						break;
						Case 4:
							$C = "keturi šimtai " . $C;
							break;
							Case 5:
								$C = "penki šimtai " . $C;
								break;
								Case 6:
									$C = "šeši šimtai " . $C;
									break;
									Case 7:
										$C = "septyni šimtai " . $C;
										break;
										Case 8:
											$C = "aštuoni šimtai " . $C;
											break;
											Case 9:
												$C = "devyni šimtai " . $C;
												break;
		}
		return $C;
	}

	private function GG($X)
	{
		If (Intval($X / 10) % 10 != 1) {
			switch ($X % 10)
			{
				Case 0:
					return "ų";
					break;
					Case 1:
						return "as";
						break;
					default:
						return "ai";
			}
		}
		Else {
			return "ų";
		}
		return "";
	}

	private function UPPER_LT($s)
	{
		return strtoupper($s);
	}

	private function Mid($s, $from, $len) {
		return substr($s, $from, $len);
	}

	private function Str($i)
	{
		return $i;
	}
}
?>

Excel table to PHP array

No Comments »

This little function converts string copied from Excel to PHP array. This is useful if you don’t have posibility to work with PHP5 (or higher).

function parse_excel_string($string, $columnheadings = false, $delimiter = "\t", $enclosure = "\n") {
        $row_index = 0;
        $rows = array();
        $array_of_rows = explode($enclosure, rtrim($string));
        if(is_array($array_of_rows) && !empty($array_of_rows)) {
            if($columnheadings == true) {
                $headings = explode($delimiter, rtrim($array_of_rows[0]));
            }
            foreach ($array_of_rows as $row) {
                $row_values = explode($delimiter, rtrim($row));
                if($headings && is_array($headings) && !empty($headings)) {
                    for($i=0;$i<sizeof ($headings);$i++) {
                        $rows[$row_index][$headings[$i]] = $row_values[$i];
                    }
                } else {
                    for($j=0;$j<sizeof($row_values);$j++) {
                        $rows[$row_index][$j] = $row_values[$j];
                    }
                }
                $row_index++;
            }
            if($columnheadings === true) {
                unset($rows[0]);
            }
            $rows = array_values($rows);
            return $rows;
        } else {
            return false;
        }
    }