perl代码解释说明

实习生入职,只学过一点C被分到研发部,带我的人给我了两段perl代码,让我研究,实际我只会看一些基本架构,麻烦在座的各位帮我解释一下,我也好学习一下,前面的几句注释是我自己写的,也就仅限于此了,

img

#! /usr/bin/perl

use  IO::Dir;
use  Encode;
use  File::Copy;
use  strict;
   
sub  changeFileEncoding
{
     die  "Error data!"  if  not  defined  $_ [0] or not  defined  $_ [1];
       
     my  ( $file ,  $base ) =  @_ ;
     my  $dir_fh  = IO::Dir->new( $file ) or  die  "Failed to open file : $file\n" ;
     while  ( defined ( my  $filename  =  $dir_fh -> read ))//循环函数,定义了文件名就开始循环以下代码
     {
         print  ">> Current task $filename.\n" ;//输出文件名
         if  ( $filename  eq  '.'  ||  $filename  eq  '..' )//if else条件循环函数:如果文件名是.或者..那么进行以下操作
         {  next ; }
         else
         {
             my  $inputFilePath  =  "$file\\$filename" ;
             my  $fileTrans  =  quotemeta  $file ;
             my  $trans ;
             if  ( $inputFilePath  =~ / $fileTrans /)
             {
                 $trans  = $';
             }
             my  $outputFilePath  =  "$base$trans" ;
             print  "Output file path is: $outputFilePath.\n" ;
             if  (-d  $inputFilePath )
             {
                 if  (-e  $outputFilePath )
                 {
                     changeFileEncoding( $inputFilePath ,  $outputFilePath );
                 }
                 else
                 {
                     print  "Mkdir $outputFilePath\n" ;
                     mkdir  $outputFilePath ;
                     changeFileEncoding( $inputFilePath ,  $outputFilePath );
                 }
             }
             else
             {
                 my  @line  =  split  /\./,  $outputFilePath ;
                 if  ( $line [-1] eq  'h'  ||  $line [-1] eq  'cpp' )
                 {
                     open  my  $readUTF ,  "<" ,  $inputFilePath  or  die  "Failed to open file $inputFilePath. \n" ;
                     open  my  $writeGBK ,  ">>" ,  $outputFilePath  or  die  "Failed to open write file $outputFilePath. \n" ;
                     for  (< $readUTF >)
                     {
                         my  $l  = encode( "gbk" , decode( "utf-8" ,  $_ ));
                         print  $writeGBK  $l ;
                     }
                     close  $readUTF ;
                     close  $writeGBK ;
                 }
                 else
                 {
                     copy( $inputFilePath ,  $outputFilePath );
                 }
             }
         }
     }
}
&changeFileEncoding( shift ,  shift );

还有一个是这个:

#! /usr/bin/perl

use  strict;
use  warnings;
use  File::Basename;
 
my   $fullname = '/workfile/demo/perl_test.pl' ;
my   @suffixlist =qw(.pl .txt .sv .v);
my  ( $name ,  $path ,  $suffix )=fileparse( $fullname ,  @suffixlist );
 
print   "name=$name\n" ;
print   "path=$path\n" ;
print   "suffix=$suffix\n" ;
 
$name =fileparse( $fullname ,  @suffixlist );
print   "name=$name\n" ;
 
my   $Basename =basename( $fullname ,  @suffixlist );
print   "Basename=$Basename\n" ;
 
my   $dirname  = dirname( $fullname );
print   "dirname=$dirname\n" ;



基于Monster 组和GPT的调写:


#! /usr/bin/perl  # 指定解释器为Perl

use IO::Dir;   # 导入IO::Dir模块,用于打开目录句柄
use Encode;    # 导入Encode模块,用于编码转换
use File::Copy;    # 导入File::Copy模块,用于复制文件
use strict;    # 开启严格模式,强制声明变量

sub changeFileEncoding   # 定义子函数changeFileEncoding
{
    die "Error data!" if not defined $_ [0] or not defined $_ [1]; # 判断参数是否存在,如果不存在则输出错误信息并退出

    my ($file, $base) = @_; # 将源文件路径和目标文件路径分别赋值给$file和$base变量
    my $dir_fh = IO::Dir->new($file) or die "Failed to open file : $file\n"; # 打开源文件目录句柄,如果失败则输出错误信息并退出
    while (defined(my $filename = $dir_fh->read))   # 使用while循环遍历源文件目录中的所有文件名
    {
        print ">> Current task $filename.\n";   # 输出当前处理的文件名

        if ($filename eq '.' || $filename eq '..')   # 如果文件名是"."或"..",则跳过当前循环
        {
            next;
        }
        else    # 处理目录或文件
        {
            my $inputFilePath = "$file\\$filename"; # 拼接源文件路径和文件名得到完整路径
            my $fileTrans = quotemeta $file; # 将源文件路径中的特殊字符进行转义
            my $trans;

            if ($inputFilePath =~ /$fileTrans/) # 正则表达式匹配获取目录路径
            {
                $trans = $';
            }

            my $outputFilePath = "$base$trans"; # 将目标文件路径和目录路径拼接得到完整路径
            print "Output file path is: $outputFilePath.\n"; # 输出目标文件路径

            if (-d $inputFilePath) # 如果文件是目录
            {
                if (-e $outputFilePath) # 如果目标目录存在,则递归调用自身处理目标目录
                {
                    changeFileEncoding($inputFilePath, $outputFilePath);
                }
                else    # 如果目标目录不存在,则创建目标目录并递归调用自身处理目标目录
                {
                    print "Mkdir $outputFilePath\n"; # 输出创建目录的信息
                    mkdir $outputFilePath;  # 创建目标目录
                    changeFileEncoding($inputFilePath, $outputFilePath); # 递归调用自身处理目标目录
                }
            }
            else    # 如果文件不是目录
            {
            if ($line[-1] eq  'h' | | $line[-1] eq  'cpp')
            {
                open my $readUTF, "<", $inputFilePath or die "Failed to open file $inputFilePath. \n"; # 打开源文件
                open my $writeGBK, ">>", $outputFilePath or die "Failed to open write file $outputFilePath. \n"; # 打开目标文件
                for (<$readUTF>)    # 使用for循环遍历源文件中的每一行
                {
                    my $l = encode("gbk", decode("utf-8", $_)); # 将源文件中的每一行进行编码转换
                    print $writeGBK $l; # 将转换后的编码写入目标文件
                }
                close $readUTF; # 关闭源文件
                close $writeGBK;    # 关闭目标文件
            }
            else
            {
                copy($inputFilePath, $outputFilePath); # 将源文件复制到目标文件
            }
            }
}
}
}

&changeFileEncoding( shift ,  shift );  # 调用子函数changeFileEncoding

该回答引用ChatGPT
第一个:

use IO::Dir;
use Encode;
use File::Copy;
use strict;

sub changeFileEncoding {
    # 传入两个参数,原文件路径 $file 和输出文件路径 $base
    die "Error data!" if not defined $_[0] or not defined $_[1];

    my ($file, $base) = @_;

    # 打开目录句柄,读取文件名
    my $dir_fh = IO::Dir->new($file) or die "Failed to open file: $file\n";
    while (defined(my $filename = $dir_fh->read)) {
        print ">> Current task: $filename.\n"; # 输出当前任务的文件名
        # 如果文件名是 . 或 ..,则忽略不处理
        if ($filename eq '.' or $filename eq '..') {
            next;
        } else {
            my $inputFilePath = "$file/$filename";
            my $fileTrans = quotemeta $file;
            my $trans;
            # 使用正则表达式匹配文件名中的相对路径部分(不包含 $file)
            if ($inputFilePath =~ /$fileTrans/) {
                $trans = $';
            }
            my $outputFilePath = "$base$trans";
            print "Output file path is: $outputFilePath.\n";
            if (-d $inputFilePath) { # 如果是目录
                if (-e $outputFilePath) { # 如果输出目录已存在
                    changeFileEncoding($inputFilePath, $outputFilePath);
                } else { # 如果输出目录不存在,则新建该目录
                    print "Mkdir $outputFilePath\n";
                    mkdir $outputFilePath;
                    changeFileEncoding($inputFilePath, $outputFilePath);
                }
            } else { # 如果是文件
                my @line = split /\./, $outputFilePath;
                if ($line[-1] eq 'h' or $line[-1] eq 'cpp') {
                    # 如果是 .h 或 .cpp 文件
                    # 以 utf-8 编码打开文件进行读操作,以 gbk 编码打开文件进行写操作
                    open my $readUTF, "<", $inputFilePath or die "Failed to open file $inputFilePath. \n";
                    open my $writeGBK, ">", $outputFilePath or die "Failed to open write file $outputFilePath. \n";
                    for (<$readUTF>) {
                        my $l = encode("gbk", decode("utf-8", $_));
                        print $writeGBK $l;
                    }
                    close $readUTF;
                    close $writeGBK;
                } else { # 否则,直接将文件复制到输出目录中
                    copy($inputFilePath, $outputFilePath);
                }
            }
        }
    }
}

# 以命令行参数的形式传入原文件路径 $ARGV[0] 和输出目录路径 $ARGV[1]
&changeFileEncoding($ARGV[0], $ARGV[1]);

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
第一段代码解释:

  1. #! /usr/bin/perl 表示该脚本使用的解释器是 Perl,建议放在文件的首行。

  2. use IO::Dir; use Encode; use File::Copy; 表示声明使用 Perl 的 IO::Dir、Encode、File::Copy 模块,可以方便地进行文件、编码相关的操作。

  3. use strict; 宣布使用严格模式,这种模式下 Perl 会强制检查变量的定义情况,帮助防止一些错误。

  4. sub changeFileEncoding 表示定义了一个名为 changeFileEncoding 的子函数。

  5. die "Error data!" if not defined $_ [0] or not defined $_ [1]; 表示如果函数的第一个或第二个参数为空,则会输出 "Error data!" 并停止脚本。

  6. my ( $file , $base ) = @_ ; 表示将函数的所有参数都传递给了 $file$base 两个变量。

  7. $dir_fh = IO::Dir->new( $file ) or die "Failed to open file : $file\n" ; 表示通过指定文件路径 $file 创建一个 IO::Dir 对象。如果创建失败,则会输出 "Failed to open file : $file" 并停止脚本。

  8. while ( defined ( my $filename = $dir_fh -> read )) 表示使用 read 方法从目录句柄 $dir_fh 中读取文件名,并将其赋值给 $filename,只要有元素就会循环。

  9. print ">> Current task $filename.\n" ; 表示输出当前目标文件的文件名。

  10. if ( $filename eq '.' || $filename eq '..' ) 表示如果 $filename 的值是 "." 或 "..",则执行下一次循环。

  11. $inputFilePath = "$file\\$filename" ; 表示将 $file$filename 组合成目标文件的完整路径。

  12. $fileTrans = quotemeta $file ; 表示将 $file 中所有特殊字符都进行转义。

  13. $trans ;if ( $inputFilePath =~ / $fileTrans /){$trans = $';} 表示根据 $file 的转义版本 $fileTrans 获取到 $inputFilePath 后面的字符串赋值给 $trans

  14. $outputFilePath = "$base$trans" ; 表示将 $trans 拼接为目标文件转码的路径。

  15. if (-d $inputFilePath ) 表示如果 $inputFilePath 表示的是一个目录,则执行以下代码块。

  16. if (-e $outputFilePath ) 表示如果 $outputFilePath 已存在,则执行以下代码块。

  17. else 意味着如果 $outputFilePath 不存在,则执行以下代码块。

  18. mkdir $outputFilePath ; 表示创建指定的目录。

  19. changeFileEncoding( $inputFilePath , $outputFilePath ); 表示递归遍历该目录,将其中的所有文件都进行转码。

  20. else 意味着如果 $inputFilePath 不是一个目录,则执行以下代码块。

  21. my @line = split /\./, $outputFilePath ; 表示将 $outputFilePath 以 "." 为分隔符分割成数组,并将其赋值给 @line 数组。

  22. if ( $line [-1] eq 'h' || $line [-1] eq 'cpp' ) 表示如果数组最后一个元素是 "h" 或 "cpp",则执行以下代码块。

  23. open my $readUTF , "<" , $inputFilePath 表示打开 $inputFilePath 表示的文件,以只读模式读取。

  24. open my $writeGBK , ">>" , $outputFilePath 表示打开 $outputFilePath 表示的文件,并以追加模式写入。

  25. for (< $readUTF >) 表示将 $readUTF 中的每一行读取出来,根据循环进行转码。

  26. my $l = encode( "gbk" , decode( "utf-8" , $_ )); 表示将当前行从 UTF-8 格式转换为 GBK 格式。

  27. print $writeGBK $l ; 表示将转码后的行写入到 $writeGBK 中。

  28. close $readUTF ; 表示关闭 $readUTF 文件句柄。

  29. close $writeGBK ; 表示关闭 $writeGBK 文件句柄。

  30. copy( $inputFilePath , $outputFilePath ); 表示将 $inputFilePath 文件复制到 $outputFilePath 文件中。

  31. &changeFileEncoding( shift , shift ); 表示将命令行参数传递给 changeFileEncoding 子函数,并执行该子函数。

第二段代码解释:

  1. use strict; use warnings; 表示开启严格模式和警告模式。

  2. use File::Basename; 表示声明使用 Perl 的 File::Basename 库,可以方便地处理文件路径。

  3. $fullname = '/workfile/demo/perl_test.pl' ; 表示定义了一个变量 $fullname,赋值为字符串 "/workfile/demo/perl_test.pl"。

  4. @suffixlist =qw(.pl .txt .sv .v); 表示定义了一个包含 ".pl"、".txt"、".sv"、".v" 四个元素的数组 @suffixlist

  5. my ( $name , $path , $suffix )=fileparse( $fullname , @suffixlist ); 表示使用 fileparse 函数将 $fullname 解析成文件名(不包含后缀)、目录路径和文件后缀,并将它们赋值给 $name$path$suffix 三个变量。

  6. print "name=$name\n" ; print "path=$path\n" ; print "suffix=$suffix\n" ; 表示输出 $name$path$suffix 三个变量的值。

  7. $name =fileparse( $fullname , @suffixlist ); 表示在不传入第二个参数时,默认使用搜寻文件名中的后缀信息并返回文件名(不包含后缀)。

  8. print "name=$name\n" ; 表示输出 $name 的值。

  9. my $Basename =basename( $fullname , @suffixlist ); 表示将 $fullname 解析成路径中的文件名(包含后缀),并将其赋值给 $Basename

  10. print "Basename=$Basename\n" ; 表示输出 $Basename 的值。

  11. my $dirname = dirname( $fullname ); 表示将 $fullname 解析成路径中的目录名,并将其赋值给 $dirname

  12. print "dirname=$dirname\n" ; 表示输出 $dirname 的值。

完整代码:

#! /usr/bin/perl

use  IO::Dir;
use  Encode;
use  File::Copy;
use  strict;

sub  changeFileEncoding
{
     die  "Error data!"  if  not  defined $_ [0] or not defined $_ [1];

     my  ( $file ,  $base ) =  @_ ;
     my  $dir_fh  = IO::Dir->new( $file ) or  die  "Failed to open file : $file\n" ;
     while  ( defined ( my  $filename  =  $dir_fh -> read ))
     {
         print  ">> Current task $filename.\n" ;
         if  ( $filename  eq  '.'  ||  $filename  eq  '..' )
         {  next ; }
         else
         {
             my  $inputFilePath  =  "$file\\$filename" ;
             my  $fileTrans  =  quotemeta  $file ;
             my  $trans ;
             if  ( $inputFilePath  =~ / $fileTrans /)
             {
                 $trans  =  $';
             }
             my  $outputFilePath  =  "$base$trans" ;
             print  "Output file path is: $outputFilePath.\n" ;
             if  (-d  $inputFilePath )
             {
                 if  (-e  $outputFilePath )
                 {
                     changeFileEncoding( $inputFilePath ,  $outputFilePath );
                 }
                 else
                 {
                     print  "Mkdir $outputFilePath\n" ;
                     mkdir  $outputFilePath ;
                     changeFileEncoding( $inputFilePath ,  $outputFilePath );
                 }
             }
             else
             {
                 my  @line  =  split  /\./,  $outputFilePath ;
                 if  ( $line [-1] eq  'h'  ||  $line [-1] eq  'cpp' )
                 {
                     open  my  $readUTF ,  "<" ,  $inputFilePath  or  die  "Failed to open file $inputFilePath. \n" ;
                     open  my  $writeGBK ,  ">>" ,  $outputFilePath  or  die  "Failed to open write file $outputFilePath. \n" ;
                     for  (< $readUTF >)
                     {
                         my  $l  = encode( "gbk" , decode( "utf-8" ,  $_ ));
                         print  $writeGBK  $l ;
                     }
                     close  $readUTF ;
                     close  $writeGBK ;
                 }
                 else
                 {
                     copy( $inputFilePath ,  $outputFilePath );
                 }
             }
         }
     }
}
&changeFileEncoding( shift ,  shift );

#!/usr/bin/perl

use  strict;
use  warnings;
use  File::Basename;

my   $fullname = '/workfile/demo/perl_test.pl' ;
my   @suffixlist =qw(.pl .txt .sv .v);
my  ( $name ,  $path ,  $suffix )=fileparse( $fullname ,  @suffixlist );

print   "name=$name\n" ;
print   "path=$path\n" ;
print   "suffix=$suffix\n" ;

$name =fileparse( $fullname ,  @suffixlist );
print   "name=$name\n" ;

my   $Basename =basename( $fullname ,  @suffixlist );
print   "Basename=$Basename\n" ;

my   $dirname  = dirname( $fullname );
print   "dirname=$dirname\n" ;

如果我的回答解决了您的问题,请采纳!

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 关于该问题,我找了一篇非常好的博客,你可以看看是否有帮助,链接:C语言 文件操作中一些问题的解决方法(空文件处理&重复读取)
  • 除此之外, 这篇博客: 20道经典C语言编程题(初级)中的        编者按:以下20道题目为自己练习所写的代码,均已提交检测通过,其中有些题也许有更优的解法,所以写的不好望读者包涵,由于个人练习,所以没有写注释,有什么问题的地方,可以留言一起交流学习。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    第1道: 

    #include<stdio.h>
    int main()
    {
        int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
        int year,month,day;
        while(scanf("%d/%d/%d",&year,&month,&day)!=EOF)
        {
            int sum=0;
            for(int i=0;i<month-1;i++)
            sum=sum+a[i];
            sum=sum+day;
            if((year%4==0&&year%100!=0)||year%400==0)
                if(month>2)
                    sum++;
            printf("%d\n",sum);
        }
        return 0;
     }
    
    
    //备注:其中在判断闰月加1天的时候必须当月份大于2时,才可以加1,否则将导致错误。

    第2道:

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int len;
        int a,b;
        char str[51];
        char str2[200];
        int i=0 ;
        gets(str);
        len=strlen(str);
        a=len;
        b=len;
        strcpy(str2,str);
        while(i<len)
        {
            str2[a++]=str[--b];
            i++;
        }
        str2[a]='\0';
        puts(str2);
    
        return 0;
    }
    
    //备注:
    //注意在编程过程中,当i为循环条件参数时,不可对其进行更改,解决办法是在定义一个变量对其备份。

     第3道:

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int len;
        char str[256];
        int i=0 ;
        int j=0;
        gets(str);
        len=strlen(str);
        for(i=0,j=len-1;i<j;i++,j--)
        {
            if(str[i]!=str[j])
            {
                break;
    
            }
    
        }
    
        if(i==j||i>j)
            printf("Y");
        else
            printf("N");
    
    
        return 0;
    }
    
    
    //备注:
    //在写判断条件的时候一定要注意,要将字符串为单数和双数的情况都考虑进去,否则将导致错误。

     第4道:

    #include<stdio.h>
    int main()
    {
        int a[41];
        int n;
        int i;
        scanf("%d",&n);
        a[0]=1;
        a[1]=1;
        for (i=2;i<41;i++)
        {
            a[i]=a[i-2]+a[i-1];
        }
        for (i=0;i<n;i++)
        {
           printf("%d ",a[i]);
        }
    
        return 0;
    }
    

    第5道: 

     

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int i;
        int len;
        char str[200];
        while(gets(str)!=NULL)
        {
            len =strlen(str);
            for (i=0;i<len-1 ;i++)
            {
                if(str[i]=='/'&&str[i+1]=='/')
                    str[i]='\0';
            }
            if(str[0]!='\0')
                printf("%s\n",str);
        }
        return 0;
    }
    
    
    //备注:
    //这里涉及到多行输入的方法,以后遇到类似问题也可以这么干。

     第6道:

     

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int n;
        int i;
        int count=0;
        int temp;
        scanf("%d",&n);
    
        for(i=1;i<=n;i++)
        {
            temp=i;
            if(i%7==0)
            {
                count++;
            }
            else
            {
                while(temp)
                {
                    if(temp%10==7)
                    {
                        count++;
                        break;
                    }
                    temp=temp/10;
                }
            }
    
        }
    
        printf("%d\n",count);
        return 0;
    }
    
    //备注:
    //这道题的难点不在于找出7的倍数,而在于找出不是7的倍数但包含7的数字,这里用了移位的思想,
    //这个思想很重要,以后遇到需要对数据的每一位操作时候的题目也可以考虑用这种思想试试。
    

    第7道: 

     

    #include<stdio.h>
    int main()
    {
        int n,m;
        int i=0;
        int flag=0;
        //a+b=n;
        //2*a+4*b=m;
        while( scanf("%d%d",&n,&m)==2)
        {
            flag=0;
            for(i=0;i<=n;i++)
        {
            if((n-i)*4+2*i==m)
            {
                printf("%d %d\n",i,n-i);
                flag=1;
            }
        }
    
        if(flag==0)
        {
            printf("No answer\n");
        }
        }
    
    
        return 0;
    }
    
    //备注:
    //1.本题运用遍历思想。
    //2.数据多组输入的方法需要掌握一下。

    第8道: 

     

    #include<stdio.h>
    int main()
    {
        int m,n;
        int c;
        int temp;
        scanf("%d%d",&m,&n);
        c=m*n;
        if(m<n)
        {
            temp=m;
            m=n;
            n=temp;
        }
    
        while(m%n)
        {
            temp=m%n;
            m=n;
            n=temp;
        }
        printf("%d %d\n",n,c/n);
        return 0;
    }
    
    //备注:
    //”辗转相除法“求最大公约数,两数的和除以最大公约数求最小公倍数,这两种办法需要记住。
    

    第9道: 

    #include<stdio.h>
    int main()
    {
        int n;
        double m,s;
        int i;
        scanf("%lf%d",&m,&n);
        s=m;
        for(i=0;i<n;i++)
        {
    
            m=m/2;
            s=s+2*m;
        }
        s=s-2*m;
        printf("%.2f %.2f\n",m,s);
        return 0;
    }
    
    
    //备注:
    //一定要读清楚题目要求,举例子画图,不然及其容易出错。

     第10道:

     

    #include<stdio.h>
    int main()
    {
        int i,j;
        for(i=1;i<=9;i++)
        {
            for (j=1;j<=i;j++)
            {
                printf("%d*%d=%-2d ",j,i,i*j);
            }
            printf("\n");
        }
        return 0;
    }
    
    
    //备注:
    //该题看起来很简单,实际上很容易出错,错就错在打印格式,对齐方式等一定要注意。

    第11道:

     

    #include<stdio.h>
    int main()
    {
        int n;
        int i;
        int sum=1;
        scanf("%d",&n);
        for(i=1;i<n;i++)
        {
    
        sum=2*(sum+1);
    
    
        }
        printf("%d",sum);
        return 0;
    }
    //备注:
    //此题考察逆向思维,难点在于循环次数,为什么经历n天却只能循环n-次,
    //原因是题目说了当天就吃了一半多一个,且最后一天还没吃就已经是1个了。所以只能循环n-1次。

     第12道:

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int x1,y1,x2,y2;
        double d=0;
        while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)==4)
        {
            printf("%.2f\n",sqrt(pow(abs(x1-x2),2)+pow(abs(y1-y2),2)));
        }
        return 0;
    }
    
    
    //此题没有难度,只是为了熟悉常用的sqrt函数和pow函数。

    第13道: 

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int i=3025;
        for(i=1000;i<9999;i++)
        {
    
    
            if((i/100+i%100)*(i/100+i%100)==i)
            {
                printf("%d  ",i);
            }
        }
    
        return 0;
    }
    
    
    //备注:
    //此题很简单,但是作者在做的时候有个犯了错误,原因是pow函数返回值是double类型,
    //不可以和int类型的值进行比较,前玩不能用强转,转成int类型在进行比较
    //原因是这样pow得到的结果会与真实结果有误差,因为会舍去小数部分。

    第14道: 

     

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int n;
        int i,j;
        int x,y;
        int a[10][10];
        int max=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                 scanf("%d",&a[i][j]);
                 if(fabs(a[i][j])>fabs(max))
                 {
                     max=a[i][j];
                     x=i+1;
                     y=j+1;
                 }
            }
        }
    
        printf("%d %d %d",max,x,y);
        return 0;
    }
    
    //备注:
    //主要注意多行输入以及下标需要加1。

     第15道:

     

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        double a,b,c;
        double t;
        double x1,x2;
        scanf("%lf%lf%lf",&a,&b,&c);
        t=pow(b,2)-4*a*c;
        if(t>0)
        {
            x1=(-b+sqrt(t))/2*a;
            x2=(-b-sqrt(t))/2*a;
    
        }
        else
        {
                x1=x2=-b/2*a;
        }
    
        printf("%.2f %.2f\n",x1,x2);
        return 0;
    }
    
    //备注:
    //此题需要注意的一点就是在同时接收int类型和char类型的时候,空格会影响接收
    //解决办法就是在scanf的%d之间也加上同样多的空格。

    第16道: 

    #include<stdio.h>
    int main()
    {
        int n;
        int a[17];
        int i=0;
        while(scanf("%d",&n)==1)
        {
             i=0;
             printf("%d-->",n);
              if(n<0)
             {
                 printf("-");
                 n=-n;
             }
              if(n==0)
             {
                 printf("0");
             }
    
            while(n)
            {
                a[i++]=n%2;
                n=n/2;
            }
    
            while(i--)
            {
                printf("%d",a[i]);
            }
             printf("\n");
        }
        return 0;
    }
    
    
    //备注:
    //1.所有的进制转换都可以用这种方法
    //2.这里最需要注意的是while(i--),这里while里面的i与while括号里面的i相差1.

    第17道: 

    #include<stdio.h>
    int main()
    {
        int i,j,k;
        for(i=0;i<=20;i++)
        {
            for(j=0;j<=34;j++)
            {
                for(k=0;k<=100;k++)
                {
                    if(i+j+k==100&&5*i+3*j+k/3==100 &&k%3==0)
                    {
                        printf("cock=%d,hen=%d,chicken=%d\n",i,j,k);
                    }
                }
            }
        }
        return 0;
    }
    
    
    //备注:
    //1.这里采用的是暴力算法,遍历思想;
    //2.这里用嵌套的先后顺序,巧妙的解决了题目要求的字典排序输出。
    //3.尽管用暴力算法,也要想办法优化代码,比如鸡翁不可能超过20只,
    //鸡母不可能超过34只,大大减少循环次数。
    //4.鸡雏的价钱不能简单的用k/3,因为例如k为10的话,结果也为3,所以要加上k%==0这个条件。

     第18道:

     

     

    #include<stdio.h>
    #include<string.h>
    
    
    int prime(int n)
    {
        int i;
        int flag=0;
        for (i=2;i<n;i++)
        {
            if(n%i==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            return 0;
    
        return 1;
    }
    int main()
    {
        int n;
        int i;
        int count=0;
        scanf("%d",&n);
        for(i=2;i<=n/2;i++)
        {
            if(prime(i)&&prime(n-i))
            {
                count++;
            }
        }
    
        printf("%d\n",count);
        return 0;
    }
    
    
    //备注:
    //1.一是要注意用函数对子功能进行封装,例如本题判断是不是素数。
    //2.二是要注意重复情况的考虑,例如本体只遍历一般,将重复的情况去掉了。

    第19道: 

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int a,b;
        char c;
        scanf("%d %d %c",&a,&b,&c);
        switch(c)
        {
            case '+':printf("%d",a+b);break;
            case '-':printf("%d",a-b);break;
            case '*':printf("%d",a*b);break;
            case '/':printf("%d",a/b);break;
            case '%':printf("%d",a%b);break;
            default :break;
        }
        return 0;
    }
    
    
    //备注:
    //这道题比较简单,主要是对switch的语法进行复习。

    第20道: 

     

    #include<stdio.h>
    enum hand{rock,paper,scissors};
    enum ans{fail=-1,draw,win};
    int main()
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(a==rock&&b==scissors||a==scissors&&b==paper||a==paper&&b==rock)
        {
            printf("%d\n",win);
        }
        else if(b==rock&&a==scissors||b==scissors&&a==paper||b==paper&&a==rock)
        {
            printf("%d\n",fail);
        }
        else
        {
            printf("%d\n",draw);
        }
        return 0;
    }
    
    
    //备注:本题主要是对枚举进行复习。

     

     

     


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^