C程序在Windows(编译器= Dev-C ++)上运行良好,但在OSX(编译器= Clang)上运行良好

时间:2019-08-14 04:15:49

标签: c windows macos

以下程序正在进行中,旨在让某人在非常原始的图形环境中将模拟图片挂在模拟墙上。

我使用Dev-C ++在Windows上编译了程序,并且程序按预期运行。我使用Clang在Mac OSX上编译了它,但没有。在OSX上,2D字符数组无法正确打印。最右边的墙面最终被打印在墙的 中,并且当程序打印“图片”时,图片根本无法正确打印。他们放弃了最右侧墙面的间距(导致该墙面上的某些星号出现了锯齿,并超出了适当的列)。

这是两个不同的编译器的功能吗?我不知道怎么回事,除非我的代码中发生未定义的行为?

还是两个不同的命令行环境的函数?也就是说,字符输出可以在Windows命令提示符下正常工作,但不能在Mac的终端上工作?

另外,请不要在开头的math.h标头中忽略我对round()函数的使用;最初,我想确保舍入是正确的,但是我很快意识到,由于我只将整数除以2,因此无论程序是向上还是向下舍入都没有太大的区别(在任何方向上均为0.5)。这就是为什么函数出现在程序的开头而不是结尾的原因。我要完全删除它,或将其用于程序中的所有部门。

代码:

#include <stdio.h>
#include <math.h>

int main(void)
{
    char wall[1000][1000];
    int rows;
    int columns;
    int wfeet;
    int winches;
    int hfeet;
    int hinches;
    int wtotalinches;
    int htotalinches;
    int xcoordscols;
    int ycoordsrows;
    int rowunderscorechars;
    int columnpipechars;
    int i;
    int j;
    int k;
    int p;
    int xcoordTL[500];
    int ycoordTL[500];
    int xcoordTR[500];
    int ycoordTR[500];
    int xcoordBL[500];
    int ycoordBL[500];
    int xcoordBR[500];
    int ycoordBR[500];
    int picwidth[500];
    int picheight[500];

    p = 0;

    printf("How wide is the wall? You'll first enter feet only. Then you'll need to enter inches (if any).\n");
    printf("Please enter the width of the wall in feet (don't worry about inches yet):\n");
    scanf("%d", &wfeet);
    printf("Please enter any extra inches now.\n");
    scanf("%d", &winches);
    printf("Great! Now how TALL is the wall? Again, feet first, inches second.\n");
    printf("Please enter the height of the wall in feet (don't worry about inches yet):\n");
    scanf("%d", &hfeet);
    printf("Please enter any extra inches now.\n");
    scanf("%d", &hinches);

    wtotalinches = (wfeet * 12) + winches;
    htotalinches = (hfeet * 12) + hinches;

    ycoordsrows = round((float)htotalinches / 2);
    xcoordscols = round((float)wtotalinches / 2);

    for (i = 0; i < xcoordscols; i++)
    {
        wall[0][i] = '*';

    }
    for (i = 0; i < xcoordscols; i++)
    {
        wall[ycoordsrows - 1][i] = '*';
    }

    for (i = 0; i < ycoordsrows; i++)
    {
        wall[i][0] = '*';
    }

    for (i = 0; i < ycoordsrows; i++)
    {
        wall[i][xcoordscols - 1] = '*';
    }

    for (k = 0; k < ycoordsrows; k++)
    {
        for (j = 0; j < xcoordscols; j++)
        {
            printf("%c", wall[k][j]);
            printf(" ");
        }
    printf("\n");
    }

    while (1)
    {
        printf("Great! Now I need the x and y coordinates for the UPPER LEFT hand corner of the picture or object to be hung.\n");
        printf("So pick out where you want the picture or object hung, then figure out where the upper left hand corner of that picture or object ");
        printf("would be on the wall. Then, count the *s to get the x and y coordinates for the UPPER LEFT hand corner of the picture or object.\n");
        printf("First, the x coordinate (of the upper left hand corner of the picture or object):\n");
        scanf("%d", &xcoordTL[p]);
        printf("Great! Now, the y coordinate (counting DOWN from the top):\n");
        scanf("%d", &ycoordTL[p]);
        printf("Great! Now, enter the dimensions of the picture, in INCHES only.\n");
        printf("First, the width of the picture or object (in inches):\n");
        scanf("%d", &picwidth[p]);
        printf("Now the height of the picture or object (in inches):\n");
        scanf("%d", &picheight[p]);

        xcoordTR[p] = xcoordTL[p] + ((picwidth[p] / 2) - 1);
        ycoordTR[p] = ycoordTL[p];

        xcoordBL[p] = xcoordTL[p];
        ycoordBL[p] = ycoordTL[p] + ((picheight[p] / 2) - 1);

        xcoordBR[p] = xcoordTR[p];
        ycoordBR[p] = ycoordBL[p];

        for (i = xcoordTL[p] - 1; i <= xcoordTR[p] - 1; i++)
        {
            wall[ycoordTL[p] - 1][i] = '*';
        }

        for (i = xcoordBL[p] - 1; i <= xcoordBR[p] - 1; i++)
        {
            wall[ycoordBL[p] -1][i] = '*';
        }

        for (i = ycoordTL[p] - 1; i <= ycoordBL[p] - 1; i++)
        {
            wall[i][xcoordTL[p] - 1] = '*';
        }

        for (i = ycoordTR[p] - 1; i <= ycoordBR[p] - 1; i++)
        {
            wall[i][xcoordTR[p] - 1] = '*';
        }

        for (k = 0; k < ycoordsrows; k++)
        {
            for (j = 0; j < xcoordscols; j++)
            {
                printf("%c", wall[k][j]);
                printf(" ");
            }
        printf("\n");
        }
    p++;
    }
    return 0;
}

0 个答案:

没有答案