| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 539 人关注过本帖
标题:凯撒编码译码
只看楼主 加入收藏
tedwugood
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-10-7
结帖率:60%
收藏
 问题点数:0 回复次数:3 
凯撒编码译码
大家看看有啥问题 出不了结果
#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 编辑 ]
搜索更多相关主题的帖子: 凯撒 译码 编码 
2010-10-24 08:31
gmac
Rank: 2
等 级:论坛游民
帖 子:174
专家分:85
注 册:2010-9-28
收藏
得分:0 
没有用上文件指针、、、
2010-10-24 10:52
ycc892009
Rank: 2
等 级:论坛游民
帖 子:34
专家分:90
注 册:2009-12-23
收藏
得分:0 
程序代码:
// 凯撒编码译码.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <math.h>
//编码
char encode(char plain,int key)
{
    int flag=plain+key;
    if( (plain>='a'&&plain<='z') ||
        (plain >= 'A'&& plain <='Z'))
    {
        if( (plain>='a'&&plain<='z'&&flag>'z') ||
            (plain >= 'A'&& plain <='Z'&& flag > 'Z'))
        {
            return (flag-26);
        }
        else
            return flag;
    }
}
//译码
char decode(char cipher,int key)
{
    int flag = cipher-key;
    if( (cipher>='a'&&cipher<='z') ||
        (cipher >= 'A'&& cipher <='Z'))
    {
        if( (cipher>='a'&&cipher<='z'&&flag<'a') ||
            (cipher >= 'A'&& cipher <='Z'&& flag < 'A'))
        {
            return flag+26;
        }
        else
            return flag;
    }

}
int main(void)
{
FILE *plaintext ;//打开编码前的文件
FILE *ciphertext;//生成编码文件
//译码后的信息

//打开文件
         
       
char plain_char;

int key,choise,sum=0;
int ifenter=0;
while(true)
{
    if( (plaintext= fopen("plaintext.txt","rb")) == NULL)
{
    printf("can't opent file_plaintext to read!\n");
}
    ifenter=0;
printf("Do you want to 1.encode or 2.decode or 3.out\n");
scanf("%d",&choise);

if(3 == choise)
break;
switch(choise)
{
    case 1:

        if( (ciphertext= fopen("ciphertext.txt","w")) == NULL)
        {
        printf("can't opent file_ciphertext to write!\n");
        }

          printf("enter the key");
          scanf("%d",&key);
          printf("The file after encoding is\n");


          while(!feof(plaintext))
               {
                     plain_char=fgetc(plaintext);//从plaintext文件中一个一个的读入字符
                    if(plain_char!=0x0a)
                    {//经编码后把字符放进ciphertext中
                        fputc(encode(plain_char,key),ciphertext);
                        //输出译码后的字符
                        putchar(encode(plain_char,key));
                        putchar(' ');
                        ifenter++;
                        if(ifenter%30==0)
                        {
                        putchar('\n');
                        }
                    }
                }
          putchar('\n');
         // fclose(message);
          fclose(ciphertext);
          fclose(plaintext);
    break;

    default:
          printf("enter the key");
          scanf("%d",&key);
          printf("The file after decoding is\n");
          //打开文件
        FILE *message;
        if( (message= fopen("message.txt","w")) == NULL)
        {
            printf("can't opent file_message to write!\n");
        }

         if( (ciphertext= fopen("ciphertext.txt","rb")) == NULL)
        {
            printf("can't opent file_ciphertext to write!\n");
        }
    //
          while((plain_char=fgetc(ciphertext))!=EOF)
           {
              if(plain_char!=0x0a)//屏蔽回车字符
            {
                  fputc(decode(plain_char,key),message);
                  putchar(decode(plain_char,key));
                  ifenter++;
                  putchar(' ');
                  //30个字符一行显示
                    if(ifenter%30==0)
                    {
                    putchar('\n');
                    }
              }
             
            }
           putchar('\n');
          fclose(message);
          fclose(ciphertext);
          fclose(plaintext);
    break;
}
//关闭文件

}

return(0);
}

文件在工程目录下,自己打开看看对比一下

到达理想的界面是我的目标,成功却不是快捷方式!
2010-10-24 12:47
zghnxzdcx
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:4
帖 子:550
专家分:1176
注 册:2010-4-6
收藏
得分:0 
你自己先把注释加上,加注释的时候,兴许你自己就看出问题在哪儿了

你永远不可能战胜一个纯傻子,因为他会把你的智商拉到和他同一个水平,然后用他的丰富经验打败你。
2010-10-24 13:42
快速回复:凯撒编码译码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.099672 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved