凯撒编码译码
大家看看有啥问题 出不了结果#include <stdio.h>
#include <math.h>
char encode(char plain,int key);
char decode(char cipher,int key);
int main(void){
FILE *inp,*outp;
char p;
int key,choise;
printf("enter the key");
scanf("%d",&key);
printf("Do you want to 1.encode or 2.decode?");
scanf("%d",choise);
switch(choise){
case1:
inp=fopen("plaintext.txt","r");
outp=fopen("ciphertext.txt","w");
while((p=fgetc(inp))!=EOF)
{
fputc(encode(p,key),outp);
}
fclose(inp);
fclose(outp);
break;
case2:
inp=fopen("ciphertext.txt","r");
outp=fopen("meassage.txt","w");
while((p=fgetc(inp))!=EOF)
{
fputc(decode(p,key),outp);
}
fclose(inp);
fclose(outp);
break;}
return(0);
}
char encode(char plain,int key)
{char result;
result=(plain-65+key%26)%26+65;
return(result);}
char decode(char cipher,int key)
{char result;
if(cipher-65>=(key%26)){result=(cipher-65+26-key%26)%26+65;}
else{result=cipher+26-key%26;}
return(result);}
[ 本帖最后由 tedwugood 于 2010-10-24 11:39 编辑 ]