比这篇新的文章: 华容道结局的所有组合
比这篇旧的文章: 华容道开局的所有组合

密码转换

语言: Perl, 标签: perl 2008/10/26发布 1年前更新
作者: birdinforest, 点击919次, 评论(2), 收藏者(0), , 打分:

背景
主题: 字体:
Perl语言: 密码转换
001 #!/usr/bin/perl -w
002 #
003 #
004 ###################################################################
005 #Validate the numble of arguments, and accept them in either order.
006 ###################################################################
007 #
008 if (@ARGV eq 0)
009 {
010   #Introduction of script using.
011   print "ERROR! There is no argument. Please supply one or two arguments. One of both is encoding key \(necessary\), another one should be \"-e\" or \"-d\" meaning  \"encoding\" or \"decoding\" respectively \(That is a optional argument. This script will default to encode message if no that argument.\).\n";
012   print "NOTE\: The encoding key should ONLY be lowcase letters such as \"beach\" and can not be multiples of any letter such as \"beeaach\".\n";
013   exit;
014 }
015 elsif (@ARGV==1)
016 {
017   $key=$ARGV[0];     # Encoding key.
018   $code_mode=0;    # "0" means encoding, "1" means decoding.
019 }
020 elsif (@ARGV==2)
021 {
022   if ($ARGV[0] eq "-e")
023   {
024     $key=$ARGV[1];
025     $code_mode=0;
026   }
027   elsif ($ARGV[0] eq "-d")
028   {
029     $key=$ARGV[1];
030     $code_mode=1;
031   }
032   elsif ($ARGV[1] eq "-e")
033   {
034     $key=$ARGV[0];
035     $code_mode=0;
036   }
037   elsif ($ARGV[1] eq "-d")
038   {
039     $key=$ARGV[0];
040     $code_mode=1;
041   }
042   else
043   {
044     print "ERROR! Please supply correct arguments\: \"-e\" or \"-d\", which will meanning encoding or decoding. The default mode should be encoding if there is no this argument.\n";
045     exit;
046   }
047 }
048 else
049 {
050   print "ERROR! You can only supply one or two argument. Please try again.\n";
051   exit;
052 }
053 #
054 #
055 ##############################################
056 #Validate the encoding key
057 ##############################################
058 #
059 if ($key=~/\W/) #check whether the coding key contaning other character except letter and digits ([A-Za-z0-9]);
060 {
061   print "ERROR! The coding key can only contain lowcase letters.\n";
062   exit;
063 }
064 elsif ($key=~/[A-Z]/) #check whether the coding key containing upper case letters;
065 {
066   print "ERROR! The coding key can only contain lowcase letters.\n";
067   exit;
068 }
069 elsif ($key=~/\d/) #check whether the coding key containing digits;
070 {
071   print "ERROR! The coding key can not contain any digit.\n";
072   exit;
073 }
074 else
075 {
076   foreach $list ("a","b","c","d","e"."f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
077   {
078     if ($key=~m/$list.*$list/)
079     {
080       print "The coding key can not contain multiples of any letter.\n";
081       exit;
082     }
083   }
084     if ($code_mode==0)
085     {
086       print "Please supply the message for encoding.\n";
087     }
088     elsif ($code_mode==1)
089     {
090       print "Please supply the message for decoding.\n";
091     }
092     codingProcess(); #The function for coding;
093     }
094 #
095 #
096 #########################################################################
097 #The function to make a password table for encoding or decoding and print
098 #out the coding result;
099 ########################################################################
100 sub codingProcess
101 {
102   while($origi_msg=<STDIN>) #$origi_msg stores the original message before encoding or decoding;
103   {
104     chomp ($key);
105     chomp ($origi_msg);
106
107     ##get a password table##
108     $letterslist_0="zyxwvutsrqponmlkjihgfedcba";
109     $letterslist_1="abcdefghijklmnopqrstuvwxyz";
110     $letterslist_0=~s/[$key]//g;           #take out the letters which is contained in encoding key;
111     $letterslist_0=$key.$letterslist_0;          #put the letters which is contained in encoding key at the beginning;
112     $origi_len=length $origi_msg;
113     if($code_mode==1)                 
114     #if coding mode is decoding, exchange the $letterslist_0 for $letterslist_1;#
115     {
116       my $temp=$letterslist_0;
117       $letterslist_0=$letterslist_1;
118       $letterslist_1=$temp;
119     }
120     $outcom=codingResult($origi_msg,$letterslist_0,$letterslist_1);   #get the result of coding; codingResult() is the function to encode or decode by password table;
121     $outcom=codingResult($outcom,uc($letterslist_0),uc($letterslist_1));   #upper and lowcase letters conversion;
122     print $outcom."\n";   #print out the coding result;
123   }
124 }
125 #
126 #
127 ############################################################
128 #The function for getting the result of coding
129 ############################################################
130 #
131 sub codingResult
132 {
133   my $outcom="";
134   for($i=0;$i<$origi_len;$i++)
135   {
136     #find the letter in password table then change it;
137     my $forchanged=substr $_[0],$i,1;
138     my $location=index $_[2],$forchanged;
139     if ($location eq -1)         # Done; All of letters have been changed;
140     {
141       $outcom=$outcom.$forchanged;
142     }
143     else
144     {
145       $outcom=$outcom.substr($_[1],$location,1);
146     }
147   }
148   return $outcom;
149 }


所有评论,共2条:( 我也来说两句)

1
0
0
呵呵现在再回来读自己以前写的perl的代码,还是庆幸当初转入了Python
2
0
0
这个是刚刚做的一份作业。
我是个学生,刚刚开始学perl,以前也没有编程经验,所以这段代码写的也许不够清楚。

发表评论

注册登录后再发表评论