有一行电文,已按下面规律译成密码:A->Z a->z B->Y b->y C->X c->x … 即第一个字母变成第26个字母,第i个字母变成第(26-i+1)个字母。非字母字符不变,要求编程序将密码译回原文,并打印出密码和原文。(用C语言编写)
//这个加密算法也就是解密算法,也就是再把密文加密一次就是原文了
#include <stdio.h>
char enc(char ch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
if(ch>='a')
{
ch+=('a'+13-ch)*2-1;
}
else
{
ch+=('A'+13-ch)*2-1;
}
}
return ch;
}
int main()
{
char s[20]="123abcmz8ABMZN";
int i=0;
while(*(s+i))
{
s[i]=enc(s[i]);
i++;
}
printf("加密后:%s\n",s);
i=0;
while(*(s+i))
{
s[i]=enc(s[i]);
i++;
}
printf("原 文:%s\n",s);
return 0;
}
#include <stdio.h>
char enc(char ch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
if(ch>='a')
{
ch+=('a'+13-ch)*2-1;
}
else
{
ch+=('A'+13-ch)*2-1;
}
}
return ch;
}
int main()
{
char s[20]="123abcmz8ABMZN";
int i=0;
while(*(s+i))
{
s[i]=enc(s[i]);
i++;
}
printf("加密后:%s\n",s);
i=0;
while(*(s+i))
{
s[i]=enc(s[i]);
i++;
}
printf("原 文:%s\n",s);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-01-03
#include<stdio.h>
int main()
{
int i;
char a[100],b[100];
gets (a);
for(i=0;a[i]!='\0';i++)
{if (a[i]>=65&&a[i]<=90) b[i]=155-a[i];
else if (a[i]>=97&&a[i]<=122) b[i]=219-a[i];
else b[i]=a[i];}
printf("密码:%s\n原文:%s\n ",a,b);
return 0;
}
int main()
{
int i;
char a[100],b[100];
gets (a);
for(i=0;a[i]!='\0';i++)
{if (a[i]>=65&&a[i]<=90) b[i]=155-a[i];
else if (a[i]>=97&&a[i]<=122) b[i]=219-a[i];
else b[i]=a[i];}
printf("密码:%s\n原文:%s\n ",a,b);
return 0;
}
第2个回答 推荐于2017-09-10
#include "stdio.h"
void main()
{
char pw[81],src[81];
gets(pw);
puts(pw);
for(int i=0;pw[i];i++)
{
if(pw[i]>='A' && pw[i]<='Z')
src[i]='A'+'Z'-pw[i];
else if(pw[i]>='a' && pw[i]<='z')
src[i]='a'+'z'-pw[i];
else
src[i]=pw[i];
}
src[i]='\0';
puts(src);
}本回答被提问者采纳
void main()
{
char pw[81],src[81];
gets(pw);
puts(pw);
for(int i=0;pw[i];i++)
{
if(pw[i]>='A' && pw[i]<='Z')
src[i]='A'+'Z'-pw[i];
else if(pw[i]>='a' && pw[i]<='z')
src[i]='a'+'z'-pw[i];
else
src[i]=pw[i];
}
src[i]='\0';
puts(src);
}本回答被提问者采纳
第3个回答 2011-12-27
#include "stdio.h"
#define DECRYPT_UPPER(x) (x+((26-(x-'A'+ 1)+1)-(x-'A'+1)))
#define DECRYPT_LOWER(x) (x+((26-(x-'a'+ 1)+1)-(x-'a'+1)))
int main()
{
char i = 0;
char str[] = "123abcmz8ABMZN";
while(str[i])
{
if(str[i] >= 'a' && str[i] <= 'z')
printf("%c",DECRYPT_LOWER(str[i]));
else if(str[i] >= 'A' && str[i] <= 'Z')
printf("%c",DECRYPT_UPPER(str[i]));
else
printf("%c",str[i]);
i++;
}
printf("\n");
return 0;
}
#define DECRYPT_UPPER(x) (x+((26-(x-'A'+ 1)+1)-(x-'A'+1)))
#define DECRYPT_LOWER(x) (x+((26-(x-'a'+ 1)+1)-(x-'a'+1)))
int main()
{
char i = 0;
char str[] = "123abcmz8ABMZN";
while(str[i])
{
if(str[i] >= 'a' && str[i] <= 'z')
printf("%c",DECRYPT_LOWER(str[i]));
else if(str[i] >= 'A' && str[i] <= 'Z')
printf("%c",DECRYPT_UPPER(str[i]));
else
printf("%c",str[i]);
i++;
}
printf("\n");
return 0;
}
第4个回答 2011-12-27
if(a[i]>='a'&&a[i]<='z'||(a[i]>='A'&&a[i]<='Z')
a[i]='z'-a[i]+26+'a'+1;
a[i]='z'-a[i]+26+'a'+1;