我想自动测试运行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
s数组的指针。编译器应该抱怨被要求将指针转换为char
。这也适用于text_x = ""
:编译器返回一个指向空字符串的指针,并尝试将其存储在中text_x
,这将不起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句