有没有办法可以将 'but' 放在 if 语句中?
Is there a way I can put 'but' in an if statement?
在我的 CS50 课程中,我制作了一个程序,根据用户输入的字符对消息进行加密。
如果用户输入 3,则消息的每个字母向右移动 3 个单位。为此,我将这些字母转换为 ASCII 码。不幸的是,当用户尝试使用任何形式的 Z 加密任何消息时,用户会被发回一个特殊字符,例如圆括号或方括号。当原始 ASCII 码加上密钥(由用户输入)大于 90 或大于 122 时,也会发生这种情况。ASCII 码 90 是 Z,122 是 z。为了解决这个问题,我设置了一个条件,说明当 ASCII 码大于 90 或 122 时,减去 key 的值。这当然也是行不通的,因为当输入一个值时,例如 a 的值是 3。例如:当用户输入 ZzAa 时。除了 a 之外的每个字母都被加密为一个字母。另一方面,'a' 被加密为“^”。 a 的原因是 ASCII 码中的 97,而 97 大于 90 但不是 122,因此它被减少为 94,即 '^'。
我想知道 if
语句中是否有 'but
' 条件,所以我可以输入条件:大于 90 但小于 97,因此 (97
) 不会减少到 94 (^
)
我试过输入逻辑或和逻辑与。 None 其中似乎有效。它不起作用的一个例子是当您输入 3 作为密钥并输入 ZzAa 作为被加密的测试消息时。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc != 2)
// If the user uses the program incorrectly, it shows them how to do it and quits the program
{
printf("Usage : ./caesar key\n");
return 1;
}
// stores the second value inputted by the user(key) into an integer key
int key = atoi(argv[1]) % 26;
char *ptext = get_string("Plaintext : ");
for (int i = 0, n = strlen(ptext); i < n; i++)
{
if(ptext[i] + key >= 90 || ptext[i] >= 122)
{
printf("Cipher text: %c\n ", ptext[i] - key);
printf("Cipher text: %i\n ", ptext[i] - key);
}
else
{
printf("Cipher text: %c\n ", ptext[i] + key);
printf("Cipher text: %i\n ", ptext[i] + key);
}
}
return 0;
}
This worked for the most part
使用括号对相互包含的逻辑表达式进行分组。你的 but
实际上是一个 and
(&&
),像这样:
if( ( ptext[i] + key >= 90 && ptext[i] + key < 97 ) || ptext[i] >= 122 )
{
// etc
}
就是说,我会使用一个中间变量,这样 reader 就可以立即清楚发生了什么:
const char clear = ptext[i];
const char shifted = ptext[i] + key;
if( ( clear => 90 && clear < 97 ) || shifted >= 122 )
{
// etc
}
或者考虑引入命名布尔值使代码自文档化:
#include <stdbool.h>
...
const char clear = ptext[i];
const char shifted = ptext[i] + key;
const bool isAscii = clear => 90 && clear < 97;
const bool isOutsideRange = shifted >= 122;
if( isAscii || isOutsideRange )
{
// etc
}
(请注意,在大多数编程语言(以及几乎所有编译语言)中,中间变量根本不会影响性能,因为编译器足够聪明,知道它们根本不会改变函数的实际行为。有时他们甚至可以使程序更快,因为编译器可以推断出更多关于您的意图)。
在我的 CS50 课程中,我制作了一个程序,根据用户输入的字符对消息进行加密。
如果用户输入 3,则消息的每个字母向右移动 3 个单位。为此,我将这些字母转换为 ASCII 码。不幸的是,当用户尝试使用任何形式的 Z 加密任何消息时,用户会被发回一个特殊字符,例如圆括号或方括号。当原始 ASCII 码加上密钥(由用户输入)大于 90 或大于 122 时,也会发生这种情况。ASCII 码 90 是 Z,122 是 z。为了解决这个问题,我设置了一个条件,说明当 ASCII 码大于 90 或 122 时,减去 key 的值。这当然也是行不通的,因为当输入一个值时,例如 a 的值是 3。例如:当用户输入 ZzAa 时。除了 a 之外的每个字母都被加密为一个字母。另一方面,'a' 被加密为“^”。 a 的原因是 ASCII 码中的 97,而 97 大于 90 但不是 122,因此它被减少为 94,即 '^'。
我想知道 if
语句中是否有 'but
' 条件,所以我可以输入条件:大于 90 但小于 97,因此 (97
) 不会减少到 94 (^
)
我试过输入逻辑或和逻辑与。 None 其中似乎有效。它不起作用的一个例子是当您输入 3 作为密钥并输入 ZzAa 作为被加密的测试消息时。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc != 2)
// If the user uses the program incorrectly, it shows them how to do it and quits the program
{
printf("Usage : ./caesar key\n");
return 1;
}
// stores the second value inputted by the user(key) into an integer key
int key = atoi(argv[1]) % 26;
char *ptext = get_string("Plaintext : ");
for (int i = 0, n = strlen(ptext); i < n; i++)
{
if(ptext[i] + key >= 90 || ptext[i] >= 122)
{
printf("Cipher text: %c\n ", ptext[i] - key);
printf("Cipher text: %i\n ", ptext[i] - key);
}
else
{
printf("Cipher text: %c\n ", ptext[i] + key);
printf("Cipher text: %i\n ", ptext[i] + key);
}
}
return 0;
}
This worked for the most part
使用括号对相互包含的逻辑表达式进行分组。你的 but
实际上是一个 and
(&&
),像这样:
if( ( ptext[i] + key >= 90 && ptext[i] + key < 97 ) || ptext[i] >= 122 )
{
// etc
}
就是说,我会使用一个中间变量,这样 reader 就可以立即清楚发生了什么:
const char clear = ptext[i];
const char shifted = ptext[i] + key;
if( ( clear => 90 && clear < 97 ) || shifted >= 122 )
{
// etc
}
或者考虑引入命名布尔值使代码自文档化:
#include <stdbool.h>
...
const char clear = ptext[i];
const char shifted = ptext[i] + key;
const bool isAscii = clear => 90 && clear < 97;
const bool isOutsideRange = shifted >= 122;
if( isAscii || isOutsideRange )
{
// etc
}
(请注意,在大多数编程语言(以及几乎所有编译语言)中,中间变量根本不会影响性能,因为编译器足够聪明,知道它们根本不会改变函数的实际行为。有时他们甚至可以使程序更快,因为编译器可以推断出更多关于您的意图)。