鱼C论坛

 找回密码
 立即注册
查看: 2099|回复: 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 编辑
#  用Python写的,希望你能参考到。
#  主要ASCII值之间的转换,以前看别人写的很简单,自己写出来很繁琐。
#  应该有更好的方法的,但是我不知道怎么搞,期待高人出没。

def func(text, key = 4, task = 'e'):  # 'e' -> encode, other -> decode
    s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    if task == 'e':
        result = ''
        for i in text:
            if i in s:
                if i.islower():
                    result += chr((ord(i)+key-97)%26+97)
                elif i.isupper():
                    result += chr((ord(i)+key-65)%26+65)
            else:
                result += i
    else:
        result = ''
        for i in text:
            if i in s:
                if i.islower():
                    a = ord(i)-key
                    if a < 97:
                        result += chr(a+26)
                    else:
                        result += chr(a)
                elif i.isupper():
                    a = ord(i)-key
                    if a < 65:
                        result += chr(a+26)
                    else:
                        result += chr(a)
            else:
                result += i
    return result

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

使用道具 举报

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

#define ARR_SIZE 10

int main(void) {

        char str[ARR_SIZE] = { 0 };

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

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

        for (int i = 0; i < ARR_SIZE; i++) {
                if (str[i] == 'W' || str[i] == 'w') {
                        if (str[i] == 'W') {
                                str[i] = 'A';
                        }
                        else {
                                str[i] = 'a';
                        }
                }
                else if (str[i] == 'X' || str[i] == 'x') {
                        if (str[i] == 'X') {
                                str[i] = 'B';
                        }
                        else {
                                str[i] = 'b';
                        }
                }
                else if (str[i] == 'Y' || str[i] == 'y') {
                        if (str[i] == 'Y') {
                                str[i] = 'C';
                        }
                        else {
                                str[i] = 'c';
                        }
                }
                else if (str[i] == 'Z' || str[i] == 'z') {
                        if (str[i] == 'Z') {
                                str[i] = 'D';
                        }
                        else {
                                str[i] = 'd';
                        }
                }
        }

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

使用道具 举报

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

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

#define ARR_SIZE 10

int main(void) {

        char str[ARR_SIZE] = { 0 };

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

        for (int i = 0; i < ARR_SIZE; i++) {
                if ((str[i] >= 'A'&& str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
                        //str[i] += 4;
                        switch ( str[ i ] ) {
                                case 'z':
                                        str[i] = 'd';
                                        break;
                                case 'Z':
                                        str[i] = 'D';
                                        break;
                                case 'y':
                                        str[i] = 'c';
                                        break;
                                case 'Y':
                                        str[i] = 'C';
                                        break;
                                case 'x':
                                        str[i] = 'b';
                                        break;
                                case 'X':
                                        str[i] = 'B';
                                        break;
                                case 'w':
                                        str[i] = 'a';
                                        break;
                                case 'W':
                                        str[i] = 'A';
                                        break;
                                default:
                                        str[i] = str[i] + 4;
                                        break;
                        }
                }
        }

        for (int i = 0; i < ARR_SIZE; i++) {
                if (str[i] == 'W' || str[i] == 'w') {
                        if (str[i] == 'W') {
                                str[i] = 'A';
                        }
                        else {
                                str[i] = 'a';
                        }
                }
                else if (str[i] == 'X' || str[i] == 'x') {
                        if (str[i] == 'X') {
                                str[i] = 'B';
                        }
                        else {
                                str[i] = 'b';
                        }
                }
                else if (str[i] == 'Y' || str[i] == 'y') {
                        if (str[i] == 'Y') {
                                str[i] = 'C';
                        }
                        else {
                                str[i] = 'c';
                        }
                }
                else if (str[i] == 'Z' || str[i] == 'z') {
                        if (str[i] == 'Z') {
                                str[i] = 'D';
                        }
                        else {
                                str[i] = 'd';
                        }
                }
        }

        printf("string to is :%s", str);
        system("pause");
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> 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-10-1 19:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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