鱼C论坛

 找回密码
 立即注册
查看: 2707|回复: 3

[技术交流] 【C/C++】Caesar

[复制链接]
发表于 2015-3-8 22:52:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ~风介~ 于 2015-5-23 23:41 编辑

代码:
  1. /*
  2. powered by Niko!!!
  3. bbs.fishc.com
  4. */

  5. #include <stdio.h>

  6. #define N 3

  7. void createMenu();
  8. void Encryption();
  9. void Decryption();
  10. void Encryption_Block(char inputfile[20],char outputfile[20]);
  11. void Decryption_Block(char inputfile[20],char outputfile[20]);
  12. char Caecar(char c,int n);


  13. void createMenu()
  14. {
  15.         printf("1.Encrypt\n2.Decrypt\n3.Quit\n");
  16.         printf("Input Your Choice:");
  17. }

  18. void Encryption()
  19. {
  20.         char infile[20],outfile[20];
  21.         
  22.         printf("===Encryption===\n");
  23.         printf("Input Your Source File Name:");
  24.         scanf("%s",infile);
  25.         //printf("%s\n",infile);
  26.         printf("Input Your Target File Name:");
  27.         scanf("%s",outfile);
  28.         //printf("%s\n",outfile);
  29.         
  30.         Encryption_Block(infile,outfile);
  31. }

  32. void Encryption_Block(char inputfile[20],char outputfile[20])
  33. {
  34.         FILE *in,*out;
  35.         int ch;
  36.         
  37.         if ((in = fopen(inputfile,"rb")) != NULL)
  38.                 if ((out = fopen(outputfile,"wb")) != NULL)
  39.                         while ((ch = fgetc(in)) != EOF)
  40.                         {
  41.                                 ch = Caecar(ch,N);
  42.                                 fputc(ch,out);
  43.                         }
  44.                 else
  45.                         printf("Can't Open Output File!");
  46.         else
  47.                 printf("Can't Open Input File!");
  48.    
  49.         printf("\nEncrypt is over!\n");
  50.     fclose(in);
  51.     fclose(out);
  52.                
  53. }

  54. void Decryption()
  55. {
  56.         char infile[20],outfile[20];
  57.         
  58.         printf("===Decryption===\n");
  59.         printf("Input Your Source File Name:");
  60.         scanf("%s",infile);
  61.         //printf("%s\n",infile);
  62.         printf("Input Your Target File Name:");
  63.         scanf("%s",outfile);
  64.         //printf("%s\n",outfile);
  65.         
  66.         Decryption_Block(infile,outfile);
  67. }

  68. void Decryption_Block(char inputfile[20],char outputfile[20])
  69. {
  70.         FILE *in,*out;
  71.         int ch;
  72.         
  73.         if ((in = fopen(inputfile,"rb")) != NULL)
  74.                 if ((out = fopen(outputfile,"wb")) != NULL)
  75.                         while ((ch = fgetc(in)) != EOF)
  76.                         {
  77.                                 ch = Caecar(ch,(26-N));
  78.                                 fputc(ch,out);
  79.                         }
  80.                 else
  81.                         printf("Can't Open Output File!");
  82.         else
  83.                 printf("Can't Open Input File!");
  84.    
  85.         printf("\nDecrypt is over!\n");
  86.     fclose(in);
  87.     fclose(out);
  88.                
  89. }

  90. char Caecar(char c,int n)
  91. {
  92.         //printf("%d",n);
  93.         if(c>='A'&&c<='Z')
  94.         {
  95.                 return ('A'+(c-'A'+n)%26);
  96.         }
  97.         else if(c>='a'&&c<='z')
  98.         {
  99.                 return ('a'+(c-'a'+n)%26);
  100.         }
  101.         else
  102.                 return c;
  103. }

  104. int main (int argc, char *argv[])
  105. {
  106.         char ch;
  107.         createMenu();
  108.         ch = getchar();
  109.         //putchar(ch);
  110.         

  111.         if ('3' != ch)
  112.         {
  113.                 //printf("Bingo!\n");        
  114.                 if ('1' == ch)
  115.                         Encryption();
  116.                 else
  117.                         Decryption();
  118.         }
  119.         else
  120.         {
  121.                 exit(0);
  122.         }
  123.         
  124.         return 0;
  125. }
复制代码



本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-4-1 01:54:58 | 显示全部楼层
赞,介介的脚步已经扩展到C语言啦~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-4-1 09:16:10 | 显示全部楼层
小甲鱼 发表于 2015-4-1 01:54
赞,介介的脚步已经扩展到C语言啦~

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-20 15:25:34 | 显示全部楼层
{:1_1:}{:1_1:}{:1_1:}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-22 14:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表