Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <<?php
- /*Using the combination of HTML and PHP, implement a web page where the users can upload a text file, exclusively in .txt format, which contains a string of 1000 numbers, such as:*/
- //Opening file here
- echo <<<_END
- <html>
- <head><title>PHP Form Upload</title></head>
- <body>
- <form method="post" action="assignment4.php" enctype="multipart/form-data">
- Choose a "txt" File and upload it :
- <input type='file' name='filename' size ='1000'>
- <input type="submit" value="Upload"></form>
- _END;
- if ($_FILES) {
- if ($_FILES['filename']['type'] == 'text/plain') {
- if (file_exists($_FILES['filename']['tmp_name'])) {
- $string = file_get_contents($_FILES['filename']['tmp_name']);
- $max_product = find_max($string);
- echo "The maximum product of 5 adjacent #s is :" .$max_product[1]. "<br>";
- echo "The five adjacent numbers whose product is ".$max_product[1] ." are " .$max_product[0];
- echo "Factorial of ". $max_product[1]. " is " . $max_product[2];
- }
- else die("File does not exist");
- }
- else die("File is not a text/plain file");
- }
- function max_factorial($num)
- {
- if($num<=1)
- {
- return 1;
- }
- else
- {
- return $num*max_factorial($num -1);
- }
- }
- function find_max ($num_str){
- if(strlen($num_str) >1000 || strlen($num_str)<1000){
- echo "File is not in the correct format!!";
- }
- else{
- $V =is_numeric($num_str)? true : false;
- if($V ==false){
- echo "File is not in the correct format!!";
- var_dump ($V);
- }
- else{
- $currentMax =0;
- for($i =0; $i<995; $i++)
- {
- $nextMax =1;
- for ($j=$i; $j<$i+5; $j++)
- {
- $nextMax*= $num_str[$j];
- }
- if($nextMax>$currentMax){
- $currentMax = $nextMax;
- $var = substr($num_str, $i, 5);
- }
- }
- $remainder =0;
- $fact =0;
- for($i = 0; $i<strlen($currentMax) ;$i++)
- {
- $remainder = $currentMax%10;
- $currentMax = (int)$currentMax/10;
- $fact+= max_factorial($remainder);
- }
- }
- }
- return array($var,$currentMax, $fact);
- }
- function tester (){
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement