- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名新程序员,试图通过做 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/
这个问题已经有答案了: Invalid types 'double [100][double]' for array subscript (3 个回答) 已关闭 6 年前。 我已复制下面的整个代码并在
您有 2 个功能; f(x)= x(((x+1)^(1/2))-(x^(1/2))) g(x)= x/(((x+1)^(1/2))+(x^(1/2))) 哪个更准确? 旁注:如果你能解释为什么,
我正在从事一个关于java的研究项目,其中必须完成一些艰难的计算。然而,我已经完成了大部分工作,但停留在某个点上。我必须计算以下内容: (2.1-2.3) raised to power 0.3. 但
int main() { float x = 50; float y = 1/x; float result = y * x; float test = 41;
有没有安全的方法来可靠地确定整数类型 T可以存储浮点整数值 f (所以 f == floor(f) )没有任何溢出? 请记住,不能保证浮点类型 F与 IEC 559 (IEEE 754) 兼容,并且有
// value will always be in the range of [0.0 - maximum] float obtainRatio(float value, float maximum
就在今天,我遇到了我们正在使用的第三方软件,在他们的示例代码中,有以下内容: // Defined in somewhere.h static const double BAR = 3.14; //
是否有推荐的方法来清除 jQuery Flot 图表?我在 API 引用中找不到任何内容。 最佳答案 “清除”是指“破坏整个图表”还是只是清除数据? 要核对整个图表:$('#canvas_id').e
我正在学习单精度并想了解错误传播。根据this nice website ,加法是一个危险的操作。 所以我编写了一个小的 C 程序来测试错误累积的速度。我不完全确定这是否是一种有效的测试方法。如果是,
我正在尝试查询数据库,我需要获取权重等于 60.5 的客户列表。问题是 60.5 是一个实数,我以前从未在 where 子句中使用实数查询过数据库。 我已经尝试过这个: SELECT Name FRO
这是我的“ProjectEntity”类中的代码部分(我在其中使用 hibernate 进行 SQL 调用) @Column(name = "BUDGET") private float budget
我用 Haskell 编写了一个应用程序,它调用 Z3 求解器来解决一些复杂公式的约束。感谢 Haskell,我可以快速切换正在使用的数据类型。 当使用 SBV 的 AlgReal 类型进行计算时,我
在 C 中 double/float 有一个集合类型说明符:%f %F %g %G %e %E .有什么区别吗 %f和 %F , %g和 %G , %e和 %E ? 根据 printf和 scanf输
我正在开发一个适用于 Android 的可视化应用程序(包括运行 Android 2.2 的旧设备)。 我的应用程序的输入模型包含一个区域,该区域通常由数万个顶点组成。典型模型有 50000-1000
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我被要求编写一个程序来查找我大学中两个输入的总和,因此我应该首先检查输入是否有效。 例如,如果我输入 2534.11s35,程序应该检测到它不是该程序的有效输入,因为输入中存在 s。 最佳答案 to
我正在尝试降低 FPGA 的逻辑利用率,但在网上找不到任何好的 float fastpow。我所说的“好”是指充分减少所使用的逻辑。如果我使用双版本我几乎没有什么改进。如果我使用其他依赖日志的 flo
我有一个 128 字节的内存位置。我尝试用从 1...127 开始的数据填充内存。 我需要编写一个代码来获取两个参数,如偏移量、数据类型。根据参数,我需要将内存中的数据转换为提到的特定数据类型。 举个
我希望能够做到以下几点: float func() { if( error ) return InvalidFloatingPointValue; else return 0.0f;
假设我有两个 float ,我想比较它们。如果一个大于另一个,程序应该采用一个 fork。如果情况正好相反,它应该走另一条路。并且它应该做同样的事情,如果被比较的值在一个仍然应该使它比较真实的方向上被
我是一名优秀的程序员,十分优秀!