gpt4 book ai didi

CS50 - 调用方法时出现浮点异常

转载 作者:行者123 更新时间:2023-11-30 17:31:39 24 4
gpt4 key购买 nike

我是一名新程序员,试图通过做 CS50 的 psets 来自学。我编写了以下代码,运行没有问题。

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


string vencipher(string text, string key)
{
for (int i=0, j=0, n =strlen(text); i < n; i++,j++)
{
int m = strlen(key);
if (text[i] >= 65 && text[i] <= 90 && key[j % m] >= 65 && key[j % m] <= 90)
{
text[i] = 65 + ((text[i] - 65) + (key[j % m] - 65)) % 26;
}
else if (text[i] >= 65 && text[i] <= 90 && key[j % m] >= 97 && key[j % m] <= 123)
{
text[i] = 65 + ((text[i] - 65) + (key[j % m] - 97)) % 26;
}
else if (text[i] >= 97 && text[i] <= 123 && key[j % m] >= 65 && key[j % m] <= 90)
{
text[i] = 97 + ((text[i] - 97) + (key[j % m] - 65)) % 26;
}
else if (text[i] >= 97 && text[i] <= 123 && key[j % m] >= 97 && key[j % m] <= 123)
{
text[i] = 97 + ((text[i] - 97) + (key[j % m] - 97)) % 26;
}
else
{
text[i] = text[i];
j = j - 1;
}
}
return text;
}

int keyvalidator(string text)
{
int alphalen = 0;
for (int i=0, n=strlen(text); i < n; i++)
{
if ((text[i] >= 97 && text[i] <= 123) || (text[i] >= 65 && text[i] <= 90))
{
alphalen = alphalen + 1;
}
}
if (alphalen == strlen(text))
{
return 1;
}
else
{
return 0;
}
}

int main(int argc, string argv[])
{
if (argc != 2 || keyvalidator(argv[1]) != 1)
{
printf("That is not a valid secret key!\n");
return 1;
}

if (argc == 2)
{
string secretKey = argv[1];
string plainText = GetString();
printf("%s\n", vencipher(plainText, secretKey));
}
return 0;
}

我想尝试将 vencipher 拆分为一些不同的方法,以尝试提高代码的可读性。这就是我所做的

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

string keycaseID(string key)
{
for (int i=0, n=strlen(key); i < n; i++)
{
if (key[i] >= 65 && key[i] <= 90)
{
key[i] = 1;
}

else
{
key[i] = 0;
}
}
return key;
}

string setkeycase(string key)
{
for (int i=0, n=strlen(key); i < n; i++)
{
if (keycaseID(key)[i] == 1)
{
key[i] = key [i] - 65;
}
else if (keycaseID(key)[i] == 0)
{
key[i] = key [i] - 97;
}
}
return key;
}

string vencipher(string text, string key)
{
for (int i=0, j=0, n =strlen(text); i < n; i++,j++)
{
int m = strlen(key);
if (text[i] >= 65 && text[i] <= 90 && keycaseID(key)[j % m] == 1)
{
text[i] = 65 + ((text[i] - 65) + setkeycase(key)[j % m]) % 26;
}
else if (text[i] >= 65 && text[i] <= 90 && keycaseID(key)[j % m] == 0)
{
text[i] = 65 + ((text[i] - 65) + setkeycase(key)[j % m]) % 26;
}
else if (text[i] >= 97 && text[i] <= 123 && keycaseID(key)[j % m] == 1)
{
text[i] = 97 + ((text[i] - 97) + setkeycase(key)[j % m]) % 26;
}
else if (text[i] >= 97 && text[i] <= 123 && keycaseID(key)[j % m] == 0)
{
text[i] = 97 + ((text[i] - 97) + setkeycase(key)[j % m]) % 26;
}
else
{
text[i] = text[i];
j = j - 1;
}
}
return text;
}

int keyvalidator(string text)
{
int alphalen = 0;
for (int i=0, n=strlen(text); i < n; i++)
{
if ((text[i] >= 97 && text[i] <= 123) || (text[i] >= 65 && text[i] <= 90))
{
alphalen = alphalen + 1;
}
}
if (alphalen == strlen(text))
{
return 1;
}
else
{
return 0;
}
}

int main(int argc, string argv[])
{
if (argc != 2 || keyvalidator(argv[1]) != 1)
{
printf("That is not a valid secret key!\n");
return 1;
}

if (argc == 2)
{
string secretKey = argv[1];
string plainText = GetString();
printf("%s\n", vencipher(plainText, secretKey));
}
return 0;
}

较新的代码可以编译,但当我运行它时,我得到一个“浮点异常”,根据我的研究,这是模除以 0 的结果。我多次搜索了我的代码,但找不到任何实例除以 0。我想知道是否有人可以帮助我找到我的错误并向我解释是什么导致了这里的浮点异常。

最佳答案

我还看到了内存损坏的浮点异常,因此考虑到代码中没有 / 字符,这种情况更有可能发生。

我会告诉你一些事情你做错了。通过对同一个字符串多次调用 keycaseID()(如在 setkeycase() 中所做的那样),您保证最终会得到一个全零的字符串 (0而不是'0')。

第一次它会将所有元素根据其大小写转换为 1 或 0(因此您会丢失其原始值)。第二次,因为它们要么都是0,要么都是1,所以它们将小于65,因此全部设置为0。

假设您的 key 全部是字母字符(大写或小写),您可以使用类似的方法将其转换为 0 到 25 的值:

for (int i = strlen (key) - 1; i >= 0; i--)  // needs string.h
if (isupper (key[i])) // needs ctype.h
key[i] -= 'A';
else
key[i] -= 'a';

或者,更短:

for (int i = strlen (key) - 1; i >= 0; i--)  // needs string.h
key[i] = toupper (key[i]) - 'A'; // needs ctype.h

这两者都不是完全可移植的,因为 C 并不强制要求 A-Z 是连续的代码点,但是,只要您避开奇怪的非 ASCII 环境,就应该没问题:-)

关于CS50 - 调用方法时出现浮点异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com