<?php

require_once "../config.php";

function formula1($year, $month, $date) {
	$count = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
	$tempTotal = 0;

	for ($i = 0; $i < strlen($year); $i++) {
		$tempNum = intval(substr($year, $i, 1));
		$tempTotal += $tempNum;

		switch($tempNum) {
			case 1:
				$count[0]++;
				break;
			case 2:
				$count[1]++;
				break;
			case 3:
				$count[2]++;
				break;
			case 4:
				$count[3]++;
				break;
			case 5:
				$count[4]++;
				break;
			case 6:
				$count[5]++;
				break;
			case 7:
				$count[6]++;
				break;
			case 8:
				$count[7]++;
				break;
			case 9:
				$count[8]++;
				break;
			default:
		}
	}

	for ($j = 0; $j < strlen($month); $j++) {
		$tempNum = intval(substr($month, $j, 1));
		$tempTotal += $tempNum;

		switch($tempNum) {
			case 1:
				$count[0]++;
				break;
			case 2:
				$count[1]++;
				break;
			case 3:
				$count[2]++;
				break;
			case 4:
				$count[3]++;
				break;
			case 5:
				$count[4]++;
				break;
			case 6:
				$count[5]++;
				break;
			case 7:
				$count[6]++;
				break;
			case 8:
				$count[7]++;
				break;
			case 9:
				$count[8]++;
				break;
			default:
		}
	}

	for ($k = 0; $k < strlen($date); $k++) {
		$tempNum = intval(substr($date, $k, 1));
		$tempTotal += $tempNum;

		switch($tempNum) {
			case 1:
				$count[0]++;
				break;
			case 2:
				$count[1]++;
				break;
			case 3:
				$count[2]++;
				break;
			case 4:
				$count[3]++;
				break;
			case 5:
				$count[4]++;
				break;
			case 6:
				$count[5]++;
				break;
			case 7:
				$count[6]++;
				break;
			case 8:
				$count[7]++;
				break;
			case 9:
				$count[8]++;
				break;
			default:
		}
	}

	while ($tempTotal > 9) {
		$temp = strval($tempTotal);
		$tempTotal = 0;

		for ($a = 0; $a < strlen($temp); $a++) {
			$tempNum = intval(substr($temp, $a, 1));
			$tempTotal += $tempNum;

			switch($tempNum) {
				case 1:
					$count[0]++;
					break;
				case 2:
					$count[1]++;
					break;
				case 3:
					$count[2]++;
					break;
				case 4:
					$count[3]++;
					break;
				case 5:
					$count[4]++;
					break;
				case 6:
					$count[5]++;
					break;
				case 7:
					$count[6]++;
					break;
				case 8:
					$count[7]++;
					break;
				case 9:
					$count[8]++;
					break;
				default:
			}
		}
	}

	switch($tempTotal) {
		case 1:
			$count[0]++;
			break;
		case 2:
			$count[1]++;
			break;
		case 3:
			$count[2]++;
			break;
		case 4:
			$count[3]++;
			break;
		case 5:
			$count[4]++;
			break;
		case 6:
			$count[5]++;
			break;
		case 7:
			$count[6]++;
			break;
		case 8:
			$count[7]++;
			break;
		case 9:
			$count[8]++;
			break;
		default:
	}

	return array($tempTotal, $count);
}

function formula2($date) {
	$tempTotal = 0;

	for ($i = 0; $i < strlen($date); $i++) {
		$tempTotal += intval(substr($date, $i, 1));
	}

	while ($tempTotal > 9) {
		$temp = strval($tempTotal);
		$tempTotal = 0;

		for ($a = 0; $a < strlen($temp); $a++) {
			$tempTotal += intval(substr($temp, $a, 1));
		}
	}

	return $tempTotal;
}

function formula3($xingzuo, $count) {
	switch($xingzuo) {
		case "白羊":
		case "摩羯":
			$count[0]++;
			return array(1, $count);
			break;
		case "金牛":
		case "水瓶":
			$count[1]++;
			return array(2, $count);
			break;
		case "双子":
		case "双鱼":
			$count[2]++;
			return array(3, $count);
			break;
		case "巨蟹":
			$count[3]++;
			return array(4, $count);
			break;
		case "狮子":
			$count[4]++;
			return array(5, $count);
			break;
		case "处女":
			$count[5]++;
			return array(6, $count);
			break;
		case "天秤":
			$count[6]++;
			return array(7, $count);
			break;
		case "天蝎":
			$count[7]++;
			return array(8, $count);
			break;
		case "射手":
			$count[8]++;
			return array(9, $count);
			break;
		default:
			return 0;
	}
}

function formula4($date) {
	$temp = str_pad($date, 2, "0", STR_PAD_LEFT);

	return intval(substr($temp, 1, 1));
}

function formula5($year, $month, $date) {
	$tempTotal = 0;

	for ($i = 0; $i < strlen($year); $i++) {
		$tempTotal += intval(substr($year, $i, 1));
	}

	for ($j = 0; $j < strlen($month); $j++) {
		$tempTotal += intval(substr($month, $j, 1));
	}

	for ($k = 0; $k < strlen($date); $k++) {
		$tempTotal += intval(substr($date, $k, 1));
	}

	return intval(substr(str_pad($tempTotal, 2, "0", STR_PAD_LEFT), 1, 1));
}

function formula6($date) {
	$tempTotal = 0;

	for ($i = 0; $i < strlen($date); $i++) {
		$tempTotal += intval(substr($date, $i, 1));
	}

	while ($tempTotal > 9) {
		$temp = strval($tempTotal);
		$tempTotal = 0;

		for ($a = 0; $a < strlen($temp); $a++) {
			$tempTotal += intval(substr($temp, $a, 1));
		}
	}

	return $tempTotal;
}

function formula7($month) {
	if (intval($month) > 9) {
		$tempTotal = 0;

		for ($i = 0; $i < strlen($month); $i++) {
			$tempTotal += intval(substr($month, $i, 1));
		}

		return $tempTotal;
	} else {
		return intval($month);
	}
}

function formula8_1($year) {
	return intval(substr($year, 3, 1));
}

function formula8_2($year) {
	return intval(substr($year, 2, 1));
}

function formula8_3($year) {
	return intval(substr($year, 1, 1));
}

function formula8_4($year) {
	return intval(substr($year, 0, 1));
}

function formula8_5($year) {
	$num1 = intval(substr($year, 0, 1));
	$num2 = intval(substr($year, 1, 1));
	$num3 = intval(substr($year, 2, 1));
	$num4 = intval(substr($year, 3, 1));

	$tempTotal = $num1 + $num2 + $num3 + $num4;

	while ($tempTotal > 9) {
		$temp = strval($tempTotal);
		$tempTotal = 0;

		for ($a = 0; $a < strlen($temp); $a++) {
			$tempTotal += intval(substr($temp, $a, 1));
		}
	}

	return $tempTotal;
}

function calculateThreeNum($year, $month, $date, $xingzuo) {
	$res = formula1($year, $month, $date);
	$count = $res[1];

	$num1 = $res[0];
	$num2 = $date < 10 ? formula2($date) : 0;

	$res2 = formula3($xingzuo, $count);
	$count = $res2[1];
	
	$num3 = $res2[0];
	$num4 = formula4($date);
	$num5 = formula5($year, $month, $date);
	$num6 = formula6($date);
	$num7 = formula7($month);
	$num8_1 = formula8_1($year);
	$num8_2 = formula8_2($year);
	$num8_3 = formula8_3($year);
	$num8_4 = formula8_4($year);
	$num8_5 = formula8_5($year);

	$result1 = $num1;
	$result2 = 0;
	$result3 = 0;

	if ($num2 != 0 && $num2 != $result1) {
		$result2 = $num2;

		if ($num3 != $result1 && $num3 != $result2) {
			$result3 = $num3;
		} else if ($num4 != 0 && $num4 != $result1 && $num4 != $result2) {
			$result3 = $num4;
		} else if ($num5 != 0 && $num5 != $result1 && $num5 != $result2) {
			$result3 = $num5;
		} else if ($num6 != $result1 && $num6 != $result2) {
			$result3 = $num6;
		} else if ($num7 != $result1 && $num7 != $result2) {
			$result3 = $num7;
		} else if ($num8_1 != 0 && $num8_1 != $result1 && $num8_1 != $result2) {
			$result3 = $num8_1;
		} else if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num3 != $result1) {
		$result2 = $num3;

		if ($num4 != 0 && $num4 != $result1 && $num4 != $result2) {
			$result3 = $num4;
		} else if ($num5 != 0 && $num5 != $result1 && $num5 != $result2) {
			$result3 = $num5;
		} else if ($num6 != $result1 && $num6 != $result2) {
			$result3 = $num6;
		} else if ($num7 != $result1 && $num7 != $result2) {
			$result3 = $num7;
		} else if ($num8_1 != 0 && $num8_1 != $result1 && $num8_1 != $result2) {
			$result3 = $num8_1;
		} else if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num4 != 0 && $num4 != $result1) {
		$result2 = $num4;

		if ($num5 != 0 && $num5 != $result1 && $num5 != $result2) {
			$result3 = $num5;
		} else if ($num6 != $result1 && $num6 != $result2) {
			$result3 = $num6;
		} else if ($num7 != $result1 && $num7 != $result2) {
			$result3 = $num7;
		} else if ($num8_1 != 0 && $num8_1 != $result1 && $num8_1 != $result2) {
			$result3 = $num8_1;
		} else if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num5 != 0 && $num5 != $result1) {
		$result2 = $num5;

		if ($num6 != $result1 && $num6 != $result2) {
			$result3 = $num6;
		} else if ($num7 != $result1 && $num7 != $result2) {
			$result3 = $num7;
		} else if ($num8_1 != 0 && $num8_1 != $result1 && $num8_1 != $result2) {
			$result3 = $num8_1;
		} else if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num6 != $result1) {
		$result2 = $num6;

		if ($num7 != $result1 && $num7 != $result2) {
			$result3 = $num7;
		} else if ($num8_1 != 0 && $num8_1 != $result1 && $num8_1 != $result2) {
			$result3 = $num8_1;
		} else if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num7 != $result1) {
		$result2 = $num7;

		if ($num8_1 != 0 && $num8_1 != $result1 && $num8_1 != $result2) {
			$result3 = $num8_1;
		} else if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num8_1 != 0 && $num8_1 != $result1) {
		$result2 = $num8_1;

		if ($num8_2 != 0 && $num8_2 != $result1 && $num8_2 != $result2) {
			$result3 = $num8_2;
		} else if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num8_2 != 0 && $num8_2 != $result1) {
		$result2 = $num8_2;

		if ($num8_3 != 0 && $num8_3 != $result1 && $num8_3 != $result2) {
			$result3 = $num8_3;
		} else if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num8_3 != 0 && $num8_3 != $result1) {
		$result2 = $num8_3;

		if ($num8_4 != 0 && $num8_4 != $result1 && $num8_4 != $result2) {
			$result3 = $num8_4;
		} else if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	} else if ($num8_4 != 0 && $num8_4 != $result1) {
		$result2 = $num8_4;

		if ($num8_5 != $result1 && $num8_5 != $result2) {
			$result3 = $num8_5;
		}
	}

	return array($result1, $result2, $result3, $count);
}
