鱼C论坛

 找回密码
 立即注册
查看: 1904|回复: 6

[已解决]求完善的代码

[复制链接]
发表于 2018-4-10 09:51:55 | 显示全部楼层 |阅读模式

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

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

x
希望写代码的是新手,一起刚学习C的鱼油们
ASCII+4,不希望有任何多余的东西出现
昨天思考了一天,也没有弄懂到底怎样才能达到想要的效果如下:
输入:ABcdef!!
输出:EFghki!!
最佳答案
2018-4-10 21:57:21
#include <stdio.h>


void main()
{
        char c;
        while ( (c = getchar()) != '\n')
        {
                if ( c >= 'A' && c <= 'V' || c >= 'a' && c <= 'v')
                {
                        c = c + 4;
                }
                else if ( c >= 'W' && c <= 'Z' || c >= 'w' && c <= 'z')
                {
                        c = c - 22;
                }
                printf("%c", c);
        }
        printf("\n");
}
1523324659(1).jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-10 11:03:28 | 显示全部楼层
本帖最后由 shigure_takimi 于 2018-4-10 11:05 编辑
  1. #  用Python写的,希望你能参考到。
  2. #  主要ASCII值之间的转换,以前看别人写的很简单,自己写出来很繁琐。
  3. #  应该有更好的方法的,但是我不知道怎么搞,期待高人出没。

  4. def func(text, key = 4, task = 'e'):  # 'e' -> encode, other -> decode
  5.     s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  6.     if task == 'e':
  7.         result = ''
  8.         for i in text:
  9.             if i in s:
  10.                 if i.islower():
  11.                     result += chr((ord(i)+key-97)%26+97)
  12.                 elif i.isupper():
  13.                     result += chr((ord(i)+key-65)%26+65)
  14.             else:
  15.                 result += i
  16.     else:
  17.         result = ''
  18.         for i in text:
  19.             if i in s:
  20.                 if i.islower():
  21.                     a = ord(i)-key
  22.                     if a < 97:
  23.                         result += chr(a+26)
  24.                     else:
  25.                         result += chr(a)
  26.                 elif i.isupper():
  27.                     a = ord(i)-key
  28.                     if a < 65:
  29.                         result += chr(a+26)
  30.                     else:
  31.                         result += chr(a)
  32.             else:
  33.                 result += i
  34.     return result

  35. print(func('China!'))
  36. print(func('Glmre!', task = 'd'))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 11:07:00 | 显示全部楼层
本帖最后由 风过无痕丶 于 2018-4-10 11:13 编辑
  1. #pragma warning(disable : 4996)
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. #define ARR_SIZE 10

  5. int main(void) {

  6.         char str[ARR_SIZE] = { 0 };

  7.         printf("Enter encrypted the string :");
  8.         scanf("%s", str);

  9.         for (int i = 0; i < ARR_SIZE; i++) {
  10.                 if ((str[i] >= 'A'&& str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
  11.                         str[i] += 4;
  12.                 }
  13.         }

  14.         for (int i = 0; i < ARR_SIZE; i++) {
  15.                 if (str[i] == 'W' || str[i] == 'w') {
  16.                         if (str[i] == 'W') {
  17.                                 str[i] = 'A';
  18.                         }
  19.                         else {
  20.                                 str[i] = 'a';
  21.                         }
  22.                 }
  23.                 else if (str[i] == 'X' || str[i] == 'x') {
  24.                         if (str[i] == 'X') {
  25.                                 str[i] = 'B';
  26.                         }
  27.                         else {
  28.                                 str[i] = 'b';
  29.                         }
  30.                 }
  31.                 else if (str[i] == 'Y' || str[i] == 'y') {
  32.                         if (str[i] == 'Y') {
  33.                                 str[i] = 'C';
  34.                         }
  35.                         else {
  36.                                 str[i] = 'c';
  37.                         }
  38.                 }
  39.                 else if (str[i] == 'Z' || str[i] == 'z') {
  40.                         if (str[i] == 'Z') {
  41.                                 str[i] = 'D';
  42.                         }
  43.                         else {
  44.                                 str[i] = 'd';
  45.                         }
  46.                 }
  47.         }

  48.         printf("string to is :%s", str);
  49.         system("pause");
  50.         return 0;
  51. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 17:07:32 | 显示全部楼层
  1. // EnctryStr.cpp: 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #pragma warning(disable : 4996)
  5. #include <stdio.h>
  6. #include <stdlib.h>

  7. #define ARR_SIZE 10

  8. int main(void) {

  9.         char str[ARR_SIZE] = { 0 };

  10.         printf("Enter encrypted the string :");
  11.         scanf("%s", str);

  12.         for (int i = 0; i < ARR_SIZE; i++) {
  13.                 if ((str[i] >= 'A'&& str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
  14.                         //str[i] += 4;
  15.                         switch ( str[ i ] ) {
  16.                                 case 'z':
  17.                                         str[i] = 'd';
  18.                                         break;
  19.                                 case 'Z':
  20.                                         str[i] = 'D';
  21.                                         break;
  22.                                 case 'y':
  23.                                         str[i] = 'c';
  24.                                         break;
  25.                                 case 'Y':
  26.                                         str[i] = 'C';
  27.                                         break;
  28.                                 case 'x':
  29.                                         str[i] = 'b';
  30.                                         break;
  31.                                 case 'X':
  32.                                         str[i] = 'B';
  33.                                         break;
  34.                                 case 'w':
  35.                                         str[i] = 'a';
  36.                                         break;
  37.                                 case 'W':
  38.                                         str[i] = 'A';
  39.                                         break;
  40.                                 default:
  41.                                         str[i] = str[i] + 4;
  42.                                         break;
  43.                         }
  44.                 }
  45.         }

  46.         for (int i = 0; i < ARR_SIZE; i++) {
  47.                 if (str[i] == 'W' || str[i] == 'w') {
  48.                         if (str[i] == 'W') {
  49.                                 str[i] = 'A';
  50.                         }
  51.                         else {
  52.                                 str[i] = 'a';
  53.                         }
  54.                 }
  55.                 else if (str[i] == 'X' || str[i] == 'x') {
  56.                         if (str[i] == 'X') {
  57.                                 str[i] = 'B';
  58.                         }
  59.                         else {
  60.                                 str[i] = 'b';
  61.                         }
  62.                 }
  63.                 else if (str[i] == 'Y' || str[i] == 'y') {
  64.                         if (str[i] == 'Y') {
  65.                                 str[i] = 'C';
  66.                         }
  67.                         else {
  68.                                 str[i] = 'c';
  69.                         }
  70.                 }
  71.                 else if (str[i] == 'Z' || str[i] == 'z') {
  72.                         if (str[i] == 'Z') {
  73.                                 str[i] = 'D';
  74.                         }
  75.                         else {
  76.                                 str[i] = 'd';
  77.                         }
  78.                 }
  79.         }

  80.         printf("string to is :%s", str);
  81.         system("pause");
  82.         return 0;
  83. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 21:57:21 | 显示全部楼层    本楼为最佳答案   
#include <stdio.h>


void main()
{
        char c;
        while ( (c = getchar()) != '\n')
        {
                if ( c >= 'A' && c <= 'V' || c >= 'a' && c <= 'v')
                {
                        c = c + 4;
                }
                else if ( c >= 'W' && c <= 'Z' || c >= 'w' && c <= 'z')
                {
                        c = c - 22;
                }
                printf("%c", c);
        }
        printf("\n");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-4-11 08:21:09 | 显示全部楼层

这种最适合新手初学者  思考的出发点比我完善特别多
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-11 14:42:58 | 显示全部楼层
理想小青年 发表于 2018-4-11 08:21
这种最适合新手初学者  思考的出发点比我完善特别多

我也是刚学没多久 可以一起沟通交流
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 22:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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