educoder 1
#include?<iostream>
#include?<stdio.h>
#include?<string.h>
using?namespace?std;
#define?N?26
void?Encrypt(int?K,?char*?clearText,?char*?cipherText)
{
????//?請?jiān)谶@里補(bǔ)充代碼,完成本關(guān)任務(wù)
????/*********?Begin?*********/
????int?i,z=0;
???for(i=0;i<strlen(clearText);i++)
???{
????????if?(clearText[i]?>=?'A'?&&?clearText[i]?<=?'Z')?{
????????????cipherText[z]?=?(?(clearText[i]?-?'A')?+?K)?%?26?+?'A';
????????}
????????else?if?(clearText[i]?>=?'a'?&&?clearText[i]?<=?'z')?{
????????????cipherText[z]?=?((clearText[i]?-?'a')?+?K)?%?26?+?'a';
????????}
????????else?{?
????????????cipherText[z]?=?clearText[i];
????????}
????????z++;
???}
???cipherText[i]='\0';
?
????/*********?End?*********/
}
void?Dencrypt(int?K,?char*?clearText,?char*?cipherText)
{
????//?請?jiān)谶@里補(bǔ)充代碼,完成本關(guān)任務(wù)
????/*********?Begin?*********/
???int?i,z=0;
???for(i=0;i<strlen(cipherText);i++)
???{
???????if?(cipherText[i]?>=?'A'?&&?cipherText[i]?<=?'Z')?{
????????????clearText[z]?=?(((cipherText[i]?-?'A')?-?K))?%?26?+?'A';
????????????if?(((cipherText[i]?-?'A')?-?K)?<?0)?{
????????????????clearText[z]?=?clearText[z]?+?26;
????????????}
????????}
????????else?if?(cipherText[i]?>=?'a'?&&?cipherText[i]?<=?'z')?{
????????????clearText[z]?=?(?((cipherText[i]?-?'a')?-?K))?%?26?+?'a';
????????????if?(((cipherText[i]?-?'a')?-?K)?<?0)?{??//處理負(fù)數(shù)
????????????????clearText[z]?=?clearText[z]?+?26;
????????????}
????????}
????????else?{??//判斷是否是空格
????????????clearText[z]?=?cipherText[i];
????????}
????????z++;
???}
???clearText[i]='\0';
????/*********?End?*********/
}
int?main(int?argc,?const?char?*?argv[])?{
????
????int?K;
????char?ClearText[100];
????char?ClearText2[100];
????char?CipherText[100];
????
????scanf("%d",?&K);
????scanf("%s",?ClearText);
????printf("密鑰:%d\n",?K);
????printf("明文:%s\n",?ClearText);
????
????Encrypt(K,?ClearText,?CipherText);
????printf("密文:%s\n",?CipherText);
????
????Dencrypt(K,?ClearText2,?CipherText);
????printf("解密:%s\n",?ClearText2);
????
????return?0;
}
//
//??main.cpp
//??step2
//
//??Created?by?ljpc?on?2018/10/16.
//??Copyright??2018年?ljpc.?All?rights?reserved.
//
#include?<iostream>
#include?<algorithm>
#include?<stdio.h>
#include?<string.h>
using?namespace?std;
void?Encrypt(int*?cipherTab,?int?len,?char*?clearText,?char*?cipherText)
{
????//?請?jiān)谶@里補(bǔ)充代碼,完成本關(guān)任務(wù)
????/*********?Begin?*********/
??for(int?i=0;i<len;i++)?
??{
??????int?a=cipherTab[i];
??????cipherText[i]=clearText[a];
??}
????/*********?End?*********/
}
int?main(int?argc,?const?char?*?argv[])?{
????
????int?cipherTab[100];
????char?ClearText[100];
????char?ClearText2[100];
????char?CipherText[100];
????
????
????scanf("%s",?ClearText);
????int?len?=?int(strlen(ClearText));
????for?(int?i=0;?i<len;?i++)?{
????????scanf("%d",?&cipherTab[i]);
????}
????
????printf("密鑰:\n");
????for?(int?i=0;?i<len;?i++)?{
????????printf("%d?",?i);
????}
????printf("\n");
????for?(int?i=0;?i<len;?i++)?{
????????printf("%d?",?cipherTab[i]);
????}
????printf("\n");
????
????printf("明文:%s\n",?ClearText);
????
????Encrypt(cipherTab,?len,?ClearText,?CipherText);
????printf("密文:%s\n",?CipherText);
????
????printf("解密:%s\n",?ClearText);
????
????return?0;
}