<h3 class="whitemid tcenter bluebar">Scientific Calculator</h3>
<?php
if(isset($_GET['type'])){
                        $type = $_GET['type'];
                    }else{
                        $type = "";
                    }
if($type == 'fail'){
    echo "<p class='redbar whitemid tcenter'>Process failed, please check and try again.</p>";
}
if(isset($_POST['submitmain'])){ //If post is submitted
if(!isset($_POST['spam']) || trim($_POST['spam']) == ''){
    if(empty($_POST['solve'])){
    header("Location: "); //Display error message
    die();
}
    if(isset($_POST['g-recaptcha-response'])) { //Google Recaptcha security verification
				$captcha = $_POST['g-recaptcha-response'];
	}else{
	    $captcha = "";
	}
	if(!$captcha || empty($captcha)) { 
				header("Location: ?type=fail");
				die();
	}
	        $secretKey = '6LeYB-obAAAAAEis1EPRq1NHGO3fw86SQYGGtIDw';

			$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.urlencode($secretKey).'&response='.urlencode($captcha);
			$response = file_get_contents($url);
			$responseKeys = json_decode($response, true);
			if($responseKeys["success"]) {
			    
if (isset($_POST['solve'])) {
    $solve = escapeshellarg($_POST['solve']); // Sanitize user input for shell usage			    
}
// Compiles the C++ code
$compileOutput = shell_exec('g++ -o Intder.exe Intder.cpp 2>&1');
// Check if compilation was successful or display errors

if (strpos($compileOutput, 'error') !== false) {
    echo "Compilation errors:\n" . $compileOutput;
} else {
    echo "Compilation successful.\n";
}

$executable = './Intder.exe';
// Execute the C++ program with the user input as an argument. Output is result.
$output = shell_exec("$executable $solve");
echo "<p class='greenbar whitemid tcenter'>" .  htmlspecialchars($output) . "</p>";
}


}else{
   header("Location: "); //Display error message for spam.
   die(); 
}
}
?>

<div class="row">
    <div class="rulebox">
    Hello! I made this scientific calculator scripted in C++. I have completed this project in a span of 1 day to demonstrate my capabilities as a software engineer. The source code for the cpp script and for constructing this website is in the source files.
    Simply put in the box the math problem that you want to solve, and the calculator will get to work. The scientific calculator works with the following functions: a+b, a-b, a*b, a/b, a^b, Sin(x), Cos(x), Tan(x), Exp(x), Log(x), Sqrt(x). This calculator does not solve algebraic equations. All the best, Dylan Ma
    </div>
                <form method="post" action="" class="verticalplus">
                <div class="row spen">
                        <div class="col span-1-of-3 latex">
                        <h4 id="math-output" style="color:white;"></h4>
                        </div> 
                        <div class="col span-2-of-3">
        <input type="text" id="math-input" name="solve" placeholder="What do you want to solve?" class="textbox" onchange="updateMath()" required> 
        <!--JS function turns input into readable LATEX code-->
                        </div>
                </div>
    <div style="display:none;">
                        <input type="text" name="spam">
    </div>
    <div class="row spen">
    <div class="col span-1-of-3">
      &nbsp;  
    </div>
    <div class="col span-2-of-3">
    <div class="g-recaptcha" data-sitekey="6LeYB-obAAAAAJFjBNs3ALa5h-lkhQPJT3AQIgBu">
        <!-- Google Recaptcha-->
    </div>
    </div>
    </div>
    <div class="row spen">
    <div class="col span-1-of-3">
    <label>&nbsp;</label>
    </div>
    <div class="col span-2-of-3">
    <input type="submit" value="Solve it!" name="submitmain" class="subbie width90">
    </div>
    </div>
    </form>
                
            </div>