将简单C加密转换为C ++

时间:2012-09-23 23:23:30

标签: c++ c

我有一些令人尴尬的基本问题,我似乎找不到能够处理的答案。上次我向大家们求助时,我有一个不可思议的运气,所以我希望重复一遍。我之前没有C / C ++的任何经验,本课程假定至少有一个学期,所以它只是踢了我一点。

这是我教授给出的代码:

#include <stdio.h>
#include <string.h>

void encrypt(int offset, char *str) {

int i,l;

l=strlen(str);

printf("\nUnencrypted str = \n%s\n", str);

for(i=0;i<l;i++)
    if (str[i]!=32)  
        str[i] = str[i]+ offset;

printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

void decrypt(int offset, char *str) {

// add your code here
}

void main(void) {

char str[1024];

printf ("Please enter a line of text, max %d characters\n", sizeof(str));

if (fgets(str, sizeof(str), stdin) != NULL)
{
    encrypt(5, str);    // What is the value of str after calling "encrypt"?

    // add your method call here:
}
}

这是我想要执行的:

  • 将代码转换为C ++。替换所有printf,scanf和fgets语句。
  • 将代码添加到“decrypt”方法以解密加密文本。
  • 更改代码以使用指针操作而不是数组操作来加密和解密消息。
  • 在main方法中,调用“decrypt”方法来解密加密文本(str)。

我不是直接向作业寻求答案,但我需要帮助解决我正在犯的错误以及我需要做的事情。

这是我在作业第一部分的尝试:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

void encrypt(int offset, char *str) {

int i,l;

l=strlen(str);

cout << "\nUnencrypted str = \n%s\n", str;

for(i=0;i<l;i++)
    if (str[i]!=32)
        str[i] = str[i]+ offset;

cout << "\nEncrypted str = \n%s \nlength = %d\n", str, l;
}

void decrypt(int offset, char *str) {

// add your code here
}

int main(void) {

char str[1024];

cout << "Please enter a line of text, max %d characters\n", sizeof(str);

if (fgets(str, sizeof(str), stdin) != NULL)
{
    encrypt(5, str);    // What is the value of str after calling "encrypt"?

    // add your method call here:
}
}

问题是这就是我要解决的问题,我无法弄清楚需要做些什么。

请输入一行文字,最多%d个字符 hellow world

未加密的str = %S

加密的str = %S 长度=%d

在这一点上生病了,我已经花了几个小时谷歌搜索和尝试的东西没有真正到达任何地方,我知道这是不应该超过一个小时。

2 个答案:

答案 0 :(得分:3)

std::iostream不接受printf样式格式。相反,只需打破字符串并使用参数链接另一个<<,例如

int myInt = 5;
std::cout << "HelloWorld" << myInt << '\n';

这适用于所有基本类型,但对于自定义类,您需要手动添加ostream <<运算符。

答案 1 :(得分:0)

查看“更改代码以使用指针操作而不是数组操作来加密和解密消息”,让我们看看我是否可以解释这一点。 (我将专注于指针部分)。我会重写加密功能几次,希望能说清楚

首先,我们将for转换为while循环

void encrypt(int offset, char *str) 
{
    int i,l;

    l=strlen(str);

    printf("\nUnencrypted str = \n%s\n", str);

    i = 0;
    while(i < l)
    {
        if (str[i]!=32)  
            str[i] = str[i]+ offset;
        ++i;
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

现在,我们试图摆脱strlen电话。 strlen计算C字符串开头和空字符的第一个apperetance之间的所有字符。空字符可以写为'\0'但在C或C ++中与写0几乎相同。与比较str[i] != 32类似,只要字符串str[i] != 0的{​​{1}}位置的字符不为0,我们就可以编写i

str

void encrypt(int offset, char *str) { int i; printf("\nUnencrypted str = \n%s\n", str); i = 0; while(str[i] != 0) { if (str[i]!=32) str[i] = str[i]+ offset; ++i; } printf("\nEncrypted str = \n%s \nlength = %d\n", str, l); } 等同于以下str[i],它的编写方式不同。 所以我们可以再次重写

*(str + i)

现在我们已经只有​​使用指针的解决方案了,但我们可以通过更好地理解指针来使它变得更漂亮。

让我们再次使用另一个变量重写

void encrypt(int offset, char *str) 
{
    int i;

    printf("\nUnencrypted str = \n%s\n", str);

    i = 0;
    while(*(str + i) != 0)
    {
        if (*(str + i)!=32)  
            *(str + i) = *(str + i) + offset;
        ++i;
    }

    printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}

所以我在这里做的就是,我用新变量void encrypt(int offset, char *str) { int i; printf("\nUnencrypted str = \n%s\n", str); i = 0; char *ptr = (str + i); while(*ptr != 0) { if (*ptr!=32) *ptr = *ptr + offset; ++i; ptr = (str + i); } printf("\nEncrypted str = \n%s \nlength = %d\n", str, l); } 代替(s + i)。 在最后一步中,我们删除了i,因为它只是一个辅助变量,告诉我们有多少地方要从起始地址跳转。但由于它在每次迭代中只增加一个,我们只是直接移动指针。

ptr

然后按照Seth Carnegie的建议,通过void encrypt(int offset, char *str) { printf("\nUnencrypted str = \n%s\n", str); char *ptr = str; while(*ptr) // the same as *ptr != 0 { if (*ptr!=32) *ptr = *ptr + offset; ++ptr; } printf("\nEncrypted str = \n%s \nlength = %d\n", str, l); } 来电替换您的printf来电。

用于解密:只是尝试查看加密std::cout offset`到该角色的情况并将其分配回去。所以试着考虑你要做什么,恢复这个操作(这与C完全无关)