Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial PHP 制作了一个计算器,出了点问题,求解!

PHP 制作了一个计算器,出了点问题,求解!

Jun 23, 2016 pm 02:03 PM

这是我做0输入的处理代码:

===============================================================

if($_POST['num0']){
    $num=$_POST['num0'];
   if(is_dir('txt')){
            $open=fopen("txt/caculator.txt","r+");
            if(is_file("txt/caculator.txt")){
                $a=file_get_contents("txt/caculator.txt");
                $e=(int)$a;
                $b=$e."0";
                echo $b;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
                }
=================================================================
这是表单:
=================================================================




=================================================================
全部代码在二楼。
主要意思就是 先在localhost下创建文件夹txt,再在文件夹txt下创建文件caculator.txt
caculator2.txt; caculator3.txt; 单击对应数字将数字显示到文本框,并存储到caculator.txt,
然后单击运算符,将对应运算符存储到caculator3.txt中,并将caculator.txt中的字符串存入caculator2.txt,然后清空caculator.txt,这是单击数字按钮输入第二个运算数字,并将第二个运算
数字存入caculator.txt中,当单击等于号,先对caculator3.txt中存入的运算符进行判断,然后进行
相应的运算。
其他都没问题,都可以实现,问题出现在输入0的时候,程序不会把0存入到文件中,导致0的输入不能实现,
不知道什么问题,求解答。初步估计是数据类型的问题。。。


回复讨论(解决方案)









 计算器 










       if(is_dir('txt')){
        if(is_file("txt/caculator.txt")){
            if(is_readable("txt/caculator.txt")){
                echo file_get_contents("txt/caculator.txt");
            }
        }
       }else {
        echo "0";
       }
?>"/>




































































































if(!is_dir('txt')){
    if(!is_file("txt/caculator.txt")){
 echo"<script>alert('请在localhost下创建文件夹txt,并在文件夹txt下创建3个文件,分别是caculator.txt <br /> caculator2.txt caculator3.txt 然后才能正常完成任务!');</script>";
  }}
if($_POST['num0']){
    $num=$_POST['num0'];
   if(is_dir('txt')){
            $open=fopen("txt/caculator.txt","r+");
            if(is_file("txt/caculator.txt")){
                $a=file_get_contents("txt/caculator.txt");
                $e=(int)$a;
                $b=$e."0";
                echo $b;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
                }
//单击数字1的反应
        if($_POST['num1']){
    $num=$_POST['num1'];
   if(is_dir('txt')){
            $open=fopen("txt/caculator.txt","r+");
            if(is_file("txt/caculator.txt")){
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
                }
                
                if($_POST['num2']){
 //单击数字2的反应   
    $num2=$_POST['num2'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num2;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字3的反应
 if($_POST['num3']){
    
    $num=$_POST['num3'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字4的反应
 if($_POST['num4']){
    
    $num=$_POST['num4'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字5的反应
 if($_POST['num5']){
    
    $num=$_POST['num5'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字6的反应
 if($_POST['num6']){
    
    $num=$_POST['num6'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字7的反应
 if($_POST['num7']){
    
    $num=$_POST['num7'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字8的反应
 if($_POST['num8']){
    
    $num=$_POST['num8'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }
//单击数字9的反应  
 if($_POST['num9']){
    
    $num=$_POST['num9'];
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $b=$a.$num;
                file_put_contents("txt/caculator.txt",$b); 
                fclose($open);
            }
           }
               }  
               //单击 点 的反应
                if($_POST['dot']){
        
    $num=$_POST['dot'];
    
    if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $findme= ".";
                $pos=strpos($a, $findme);
                if($pos==true){
                 
                      file_put_contents("txt/caculator.txt",$a); 
                        fclose($open); 
                }
                else{
                    $b=$a.$num;
                    file_put_contents("txt/caculator.txt",$b); 
                    fclose($open);
                    }
            }
           }
               }   
//单击选项C的反应
 if($_POST['Clear']){
    
    
     if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","w+");
                $open;
                $open=fopen("txt/caculator2.txt","w+");
                $open2;
                $open=fopen("txt/caculator3.txt","w+");
                $open3;
               
            }
           }
               }  
                //单击 ← 的反应  
if($_POST['back']){
        if(is_dir('txt')){
            if(is_file("txt/caculator.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $a=file_get_contents("txt/caculator.txt");
                $a=substr($a,0,-1);
               file_put_contents("txt/caculator.txt",$a);
            }
           }
               }   
//单击+的反应
       if($_POST['add']){
        if(is_dir('txt')){
           if(is_file("txt/caculator2.txt")&&is_file("txt/caculator.txt")&&is_file("txt/caculator3.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $open2=fopen("txt/caculator2.txt","r+");
                $open2;
                $open3=fopen("txt/caculator3.txt","r+");
                $open3;
                $b="";
                $c="+";
                $a=file_get_contents("txt/caculator.txt");
                file_put_contents("txt/caculator.txt",$b);
                file_put_contents("txt/caculator2.txt",$a);
                file_put_contents("txt/caculator3.txt",$c);
            }
           }
        
       }  
//单击-的反应
 if($_POST['sub']){
        if(is_dir('txt')){
           if(is_file("txt/caculator2.txt")&&is_file("txt/caculator.txt")&&is_file("txt/caculator3.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $open2=fopen("txt/caculator2.txt","r+");
                $open2;
                $open3=fopen("txt/caculator3.txt","r+");
                $open3;
                $b="";
                $c="-";
                $a=file_get_contents("txt/caculator.txt");
                file_put_contents("txt/caculator.txt",$b);
                file_put_contents("txt/caculator2.txt",$a);
                file_put_contents("txt/caculator3.txt",$c);
            }
           }
        
       } 
 //单击乘的反应
  if($_POST['mul']){
        if(is_dir('txt')){
           if(is_file("txt/caculator2.txt")&&is_file("txt/caculator.txt")&&is_file("txt/caculator3.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $open2=fopen("txt/caculator2.txt","r+");
                $open2;
                $open3=fopen("txt/caculator3.txt","r+");
                $open3;
                $b="";
                $c="*";
                $a=file_get_contents("txt/caculator.txt");
                file_put_contents("txt/caculator.txt",$b);
                file_put_contents("txt/caculator2.txt",$a);
                file_put_contents("txt/caculator3.txt",$c);
            }
           }
        
       } 
          //单击=的反应 
       if($_POST['equal']){
        if(is_dir('txt')){
           if(is_file("txt/caculator2.txt")&&is_file("txt/caculator.txt")&&is_file("txt/caculator3.txt")){
                $open=fopen("txt/caculator.txt","r+");
                $open;
                $open2=fopen("txt/caculator2.txt","r+");
                $open2;
                $open3=fopen("txt/caculator3.txt","r+");
                $open3;
                $a=file_get_contents("txt/caculator.txt");
                $b=file_get_contents("txt/caculator2.txt");
                $c=file_get_contents("txt/caculator3.txt");
                $nul="";
                if($c=="+"){
                    $d=$b+$a;
                }
                if($c=="-"){
                    $d=$b-$a;
                }
                if($c=="*"){
                    $d=$a*$b;
                }
                if($c=="/"){
                    $d=$b/$a;
                }
                if($c=="%"){
                    $d=$b%$a;
                }
                if($c=="+"){
                    $d=$a+$b;
                }
                if($c=="√"){
                    $d=sqrt($a);
                }
                file_put_contents("txt/caculator.txt",$d);
                
            }
           }
        
       }     

以上是大部分代码,由于输入字符有限,关键代码贴出来了。

好多代码 你还是 直接说哪里出问题了吧  说关键的

你的问题在于:
if($_POST['num0']){
因为 $_POST['num0'] 等于 0,所以条件恒不成立。该分支不会进入!
应改作
if($_POST['num0'] == '0'){

你的程序设计的很不合理,需要做大幅度调整
文件操作函数的使用也欠妥
既然是用 file_get_contents、file_put_contents 读写文件,那么就不需要 fopen、fclose 再来做摆设了
file_put_contents 有一个 FILE_APPEND 参数可以附加内容到文件,而无需读出后人工追加

如果需要,可以你一个调整后的代码供参考

用AJAX,然后动态PHP编译就得了,实际把一大堆公司放进去计算好了。

你的问题在于:
if($_POST['num0']){
因为 $_POST['num0'] 等于 0,所以条件恒不成立。该分支不会进入!
应改作
if($_POST['num0'] == '0'){

你的程序设计的很不合理,需要做大幅度调整
文件操作函数的使用也欠妥
既然是用 file_get_contents、file_put_contents 读写文件,那么就不需要 fope……
版主大神啊...我还是刚开始学的...
求以后多多指教!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles