测试运行null和字母?

时间:2015-02-04 03:14:25

标签: c visual-studio-2013

我想自动测试运行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。我甚至尝试过没有值(“”),但它仍然没有测试运行。它只测试运行数字

1 个答案:

答案 0 :(得分:2)

循环chars

char类型虽然通常用于存储字符,但实现为小整数。它可以有不同的最小值和最大值。您可以#include <limits.h>然后使用宏CHAR_MINCHAR_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中,这将无效。