我想自动测试运行 NULL 作为变量和字母,但是我不知道我做错了什么。
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
int inuti(double x, double y, double x1, double y1, double x2, double y2) {
int x_inuti;
int y_inuti;
if (x1 < x2)
x_inuti = x > x1 && x < x2;
else
x_inuti = x > x2 && x < x1;
if (y1 < y2)
y_inuti = y > y1 && y < y2;
else
y_inuti = y > y2 && y < y1;
return x_inuti && y_inuti;
}
int main(void) {
double x, y, x1, y1, x2, y2;
char text_y, text_x1, text_y1, text_x2, text_y2;
char text_x;
int resultat;
int i;
for (i = 0; i < 1; i++)
{
text_x = "lalala";
text_y = "lala";
text_x1 = "text";
text_y1 = "";
text_x2 = "";
text_y2 = "";
printf("punktens x-varde: %.1f \n", text_x);
printf("punktens y-varde: %.1f \n", text_y);
printf("\n");
printf("Ena hornets x-varde: %.1f \n", text_x1);
printf("Ena hornets y-varde: %.1f \n", text_y1);
printf("\n");
printf("Andra hornets x-varde %.1f \n", text_x2);
printf("Andra hornets y-varde %.1f \n", text_y2);
printf("\n");
resultat = inuti(text_x, text_y, text_x1, text_y1, text_x2, text_y2);
if (resultat == 1)
printf("Punkten var inuti rektangeln.\n");
else if (resultat == 0)
printf("Punkten var utanfor rektangeln.\n");
printf("\n");
printf("\n");
}
//Here I'm testing all number variables - This works fine
for (x = -10; x <= 10; x++) {
for (y = -10; y <= 10; y++) {
for (x1 =-10; x1 <= 10; x1++) {
for (y1 = -10; y1 <= 10; y1++) {
for (x2 = -10; x2 <= 10; x2++) {
for (y2 =-10; y2 <= 10; y2++){
printf("punktens x-varde: %.1f \n", x);
printf("punktens y-varde: %.1f \n", y);
printf("\n");
printf("Ena hornets x-varde: %.1f \n", x1);
printf("Ena hornets y-varde: %.1f \n", y1);
printf("\n");
printf("Andra hornets x-varde %.1f \n", x2);
printf("Andra hornets y-varde %.1f \n", y2);
printf("\n");
resultat = inuti(x, y, x1, y1, x2, y2);
if (resultat == 1)
printf("Punkten var inuti rektangeln.\n");
else
printf("Punkten var utanfor rektangeln.\n");
printf("\n");
printf("\n");
}
}
}
}
}
}
getchar();
return 0;
}
我猜测试运行字母的问题是类型是 char 而不是 double。我什至尝试没有值( ""),但它仍然没有测试运行。它只测试运行数字
最佳答案
遍历 chars
char
类型虽然通常用于存储字符,但实现为一个小整数。它可以有不同的最小值和最大值。您可以 #include <limits.h>
然后使用宏 CHAR_MIN
和 CHAR_MAX
来查找值。在大多数计算机上,它们等于 0 和 255。
您可以通过这种方式测试 char
的所有可能值:
#include <limits.h>
#include <ctype.h>
...
char text_x;
int i;
for (i = CHAR_MIN; i <= CHAR_MAX; i++) {
text_x = (char) i;
// test something with text_x here
// not all possible values are printable characters
// isprint will return true if the character is printable
if (isprint(text_x)) {
putchar(text_x);
}
}
我遍历了 int i
而不是 char text_x
,因为 i
可以比 CHAR_MAX
更高。一旦完成,您就退出循环。以这种方式循环 text_x
只会导致它的值回绕到零,并且你会有一个无限循环。
或者您可以通过这种方式遍历特定字符:
#include <string.h>
...
char text_x;
char my_favorite_characters[] = "abcdefgABCDEFG0123456789.+-";
int i, imax;
imax = strlen(my_favorite_characters);
for (i = 0; i < imax; i++) {
text_x = my_favorite_characters[i];
printf("%c is one of my favorite characters\n", text_x);
}
为什么要在 char
中存储数字?
C 中的字符只是存储为数字,所以如果你有这一行:
char mychar = 'a';
然后看起来你正在存储一个 a
,但编译器只是采用 a
的数值并将其存储在 mychar
中。几乎每个人都使用 ASCII,其中 'a' == 97
。所以当使用 ASCII 时,这一行等同于上面的那一行,尽管更加隐晦:
char mychar = 97;
有些数字将不对应于可打印字符,或者可能根本不是字符,具体取决于您的字符编码。
尝试在 char
中存储任何其他内容
C 中的变量永远不会“空”。任何数字类型,包括 char
,都不能有 NULL 值。如果您没有为变量设置值,它们要么等于零,要么未定义,具体取决于您声明变量的方式。指针可以是 NULL
,这与将指针设置为零相同。
您的 text_x = "lalala";
行将不起作用,因为 text_x
只能容纳一个字符。 "lalala"
返回一个指向 char
数组的指针。编译器应该提示被要求将指针转换为 char
。 text_x = ""
也是如此:编译器返回一个指向空字符串的指针,并尝试将其存储在 text_x
中,但这是行不通的。
关于c - 测试运行 null 和字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28312910/