0
Follow
1
View

About the problem of #c language # : known C language prepared good program, please modify the program prepared for PHP language

cuixingwei05 注册会员
2023-02-26 20:08

Part of this answer references GPT, GPT_Pro to better solve the problem
First, to convert the C language program to PHP language program, Understand the differences between C and PHP first, and then adapt to your needs.

C language is represented by characters, PHP language is represented by variables and functions, this is the biggest difference between PHP and C language.

Secondly, in the process of modification, we should pay attention to the changes to the code, if the improper changes may lead to program errors or exceptions.

Example:

#include
int main()
{ 
     float score; 
     char grade; 
     printf("please input score:"); 
     scanf("%f",&score); 
     while(score >=60 || score <0) 
     { 
          printf("input error,please reinput:"); 
          scanf("%f",&score); 
     } 
     if(score>=90) grade = 'A'; 
     else if(score>=80) grade = 'B'; 
     else if(score>=70) grade = 'C'; 
     else if(score>=60) grade = 'D'; 
     else grade = 'E'; 
     printf("grade:%c\n",grade); 
} 

Above is the C language version of a small program, its function is: input a score, according to the score to judge the corresponding grade. To convert it to PHP, do this:

  

    $score=0;  

    //循环直到输入正确  

    while($score<60||$score>100)  

    {  

        echo "please input score:";  

        $score=intval(trim(fgets(STDIN)));  

    }  

    //根据成绩判断  

    if($score>=90){  

        $grade="A";  

    }else if($score>=80){  

        $grade="B";  

    }else if($score>=70){  

        $grade="C";  

    }else if($score>=60){  

        $grade="D";  

    }else{  

        $grade="E";  

    }  

    //输出成绩  

    echo "grade:".$grade."\n";  
?> 

If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-26 20:08
Update Time
2023-02-26 20:08