求完善的代码
希望写代码的是新手,一起刚学习C的鱼油们ASCII+4,不希望有任何多余的东西出现
昨天思考了一天,也没有弄懂到底怎样才能达到想要的效果如下:
输入:ABcdef!!
输出:EFghki!!
本帖最后由 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')) 本帖最后由 风过无痕丶 于 2018-4-10 11:13 编辑
#pragma warning(disable : 4996)
#include <stdio.h>
#include <stdlib.h>
#define ARR_SIZE 10
int main(void) {
char str = { 0 };
printf("Enter encrypted the string :");
scanf("%s", str);
for (int i = 0; i < ARR_SIZE; i++) {
if ((str >= 'A'&& str <= 'Z') || (str >= 'a' && str <= 'z')) {
str += 4;
}
}
for (int i = 0; i < ARR_SIZE; i++) {
if (str == 'W' || str == 'w') {
if (str == 'W') {
str = 'A';
}
else {
str = 'a';
}
}
else if (str == 'X' || str == 'x') {
if (str == 'X') {
str = 'B';
}
else {
str = 'b';
}
}
else if (str == 'Y' || str == 'y') {
if (str == 'Y') {
str = 'C';
}
else {
str = 'c';
}
}
else if (str == 'Z' || str == 'z') {
if (str == 'Z') {
str = 'D';
}
else {
str = 'd';
}
}
}
printf("string to is :%s", str);
system("pause");
return 0;
} // EnctryStr.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#pragma warning(disable : 4996)
#include <stdio.h>
#include <stdlib.h>
#define ARR_SIZE 10
int main(void) {
char str = { 0 };
printf("Enter encrypted the string :");
scanf("%s", str);
for (int i = 0; i < ARR_SIZE; i++) {
if ((str >= 'A'&& str <= 'Z') || (str >= 'a' && str <= 'z')) {
//str += 4;
switch ( str[ i ] ) {
case 'z':
str = 'd';
break;
case 'Z':
str = 'D';
break;
case 'y':
str = 'c';
break;
case 'Y':
str = 'C';
break;
case 'x':
str = 'b';
break;
case 'X':
str = 'B';
break;
case 'w':
str = 'a';
break;
case 'W':
str = 'A';
break;
default:
str = str + 4;
break;
}
}
}
for (int i = 0; i < ARR_SIZE; i++) {
if (str == 'W' || str == 'w') {
if (str == 'W') {
str = 'A';
}
else {
str = 'a';
}
}
else if (str == 'X' || str == 'x') {
if (str == 'X') {
str = 'B';
}
else {
str = 'b';
}
}
else if (str == 'Y' || str == 'y') {
if (str == 'Y') {
str = 'C';
}
else {
str = 'c';
}
}
else if (str == 'Z' || str == 'z') {
if (str == 'Z') {
str = 'D';
}
else {
str = 'd';
}
}
}
printf("string to is :%s", str);
system("pause");
return 0;
} #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");
} 549444387@qq.co 发表于 2018-4-10 21:57
#include
这种最适合新手初学者思考的出发点比我完善特别多 理想小青年 发表于 2018-4-11 08:21
这种最适合新手初学者思考的出发点比我完善特别多
我也是刚学没多久 可以一起沟通交流
页:
[1]