测试运行null和字母?

约翰

我想自动测试运行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_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"返回指向chars数组的指针编译器应该抱怨被要求将指针转换为char这也适用于text_x = "":编译器返回一个指向空字符串的指针,并尝试将其存储在中text_x,这将不起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

着色golang测试运行输出

来自分类Dev

作为Maven测试运行

来自分类Dev

再现ScalaCheck测试运行

来自分类Dev

Couchbase测试运行失败

来自分类Dev

Intellij测试运行历史

来自分类Dev

Serverspec测试运行问题

来自分类Dev

使用JUNIT测试运行AsyncHttpClient

来自分类Dev

在测试运行期间浏览

来自分类Dev

基于Visual Studio和Nuget的测试运行器/适配器

来自分类Dev

适用于NUnit,MSTest和xUnit的选择性测试运行器

来自分类Dev

repl和测试运行程序之间的不一致

来自分类Dev

ava测试运行程序写入EPIPE和ECONNRESET错误

来自分类Dev

Visual Studio 2017 和 Resharper 测试运行程序

来自分类Dev

cypress 测试运行时无法加载地图和 css 的图块

来自分类Dev

测试运行程序与 HttpClient 和 Mocking HttpMessageRequest XUnit 不一致

来自分类Dev

可以在测试运行期间获得先前的测试

来自分类Dev

如何使用dotnet测试运行特定测试?

来自分类Dev

使单元测试作为集成测试运行

来自分类Dev

Visual Studio测试运行无法加载测试

来自分类Dev

Android的测试运行失败:没有测试结果

来自分类Dev

Web测试运行期间无效的URI错误

来自分类Dev

业力测试运行器-无法捕获Chrome

来自分类Dev

C#测试运行配置(* .runsettings)

来自分类Dev

如何通过Mocha测试运行时参数?

来自分类Dev

业力测试运行程序比预期的慢

来自分类Dev

C ++ Google测试运行两次

来自分类Dev

是否有PHPUnit的分层测试运行器?

来自分类Dev

摩卡测试运行两次

来自分类Dev

黄瓜每次测试运行的不同参数