SHARE
TWEET

Untitled

a guest Sep 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <<?php
  2. /*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:*/
  3. //Opening file here
  4. echo <<<_END
  5.     <html>
  6.     <head><title>PHP Form Upload</title></head>
  7.     <body>
  8.     <form method="post" action="assignment4.php" enctype="multipart/form-data">
  9.     Choose a "txt" File and upload it :
  10.     <input type='file' name='filename' size ='1000'>
  11.     <input type="submit" value="Upload"></form>
  12. _END;
  13.  
  14.     if ($_FILES) {
  15.         if ($_FILES['filename']['type'] == 'text/plain') {
  16.             if (file_exists($_FILES['filename']['tmp_name'])) {
  17.                 $string = file_get_contents($_FILES['filename']['tmp_name']);
  18.                 $max_product = find_max($string);
  19.                echo "The maximum product of 5 adjacent #s is :" .$max_product[1]. "<br>";
  20.                 echo "The five adjacent numbers whose product is ".$max_product[1] ." are " .$max_product[0];
  21.                 echo "Factorial of ". $max_product[1]. " is " . $max_product[2];
  22.         }
  23.         else die("File does not exist");
  24.         }
  25.         else die("File is not a text/plain file");
  26.         }
  27.    
  28.                
  29. function max_factorial($num)
  30. {
  31.     if($num<=1)
  32.     {
  33.         return 1;
  34.     }
  35.     else
  36.     {
  37.         return $num*max_factorial($num -1);
  38.     }
  39. }
  40. function find_max ($num_str){
  41.      if(strlen($num_str) >1000 || strlen($num_str)<1000){
  42.                     echo "File is not in the correct format!!";            
  43.                     }
  44.                 else{
  45.                 $V =is_numeric($num_str)? true : false;
  46.                 if($V ==false){
  47.                     echo "File is not in the correct format!!";
  48.                      var_dump ($V);
  49.                 }
  50.                 else{
  51.                 $currentMax =0;
  52.                
  53.                 for($i =0; $i<995; $i++)
  54.                 {
  55.                     $nextMax =1;
  56.                     for ($j=$i; $j<$i+5; $j++)
  57.                     {
  58.                         $nextMax*= $num_str[$j];
  59.                        
  60.                 }
  61.                     if($nextMax>$currentMax){
  62.                         $currentMax = $nextMax;
  63.                         $var = substr($num_str, $i, 5);
  64.                     }
  65.                    
  66.             }
  67.                     $remainder =0;
  68.                     $fact =0;
  69.                    for($i = 0; $i<strlen($currentMax) ;$i++)
  70.                    {
  71.                        $remainder = $currentMax%10;
  72.                         $currentMax = (int)$currentMax/10;
  73.                        $fact+= max_factorial($remainder);
  74.                        }      
  75.             }  
  76.             }
  77.                return array($var,$currentMax, $fact);
  78.             }
  79.    
  80.  
  81. function tester (){
  82.    
  83. }
  84. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top