-3

首先让我说我知道这不是编写这个程序的最有效方式,但这是我所做的方式,不管是否草率,我只需要帮助理解为什么我不能从我的数组中获取值。

如果你运行这个怪物,它会为我的临时变量 i 和 t 吐出 0。我还没有完成所有错误停止的设置,但这不是我的问题。我只是对为什么我不能从我的数组中获取值并将它们分配给新变量感到困惑和迷失。任何投入将不胜感激。谢谢你。

#include <stdio.h>

int main(void)
{
    int day1, day2, mnth1, mnth2;
    int x, y, i, t;
    int jan[31] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
    int feb[28] = {32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59};
    int mar[31] = {60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
    int apr[30] = {91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120};
    int may[31] = {121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151};
    int jun[30] = {152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181};
    int jul[31] = {182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212};
    int aug[31] = {213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243};
    int sep[30] = {244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273};
    int oct[31] = {274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304};
    int nov[30] = {305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334};
    int dec[31] = {335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365};

    printf("Please enter the information that is asked only\n");
    printf("Please enter the first date: ");
    scanf("%d", &mnth1);
    scanf("%d", &day1);

    printf("%d", mnth1);
    printf(" ");
    printf("%d\n", day1);

    if (mnth1 < 1 | day1 < 1) {
        return 0;
    }

    if (mnth1 == 1) {
        if (day1 = jan[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 == 2) {
        if (day1 = feb[i]) {
            x = i + 1;
        } else if (day1 > 28) {
            return 0;
        }
    } else if (mnth1 == 3) {
        if (day1 = mar[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 == 4) {
        if (day1 = apr[i]) {
            x = i + 1;
        } else if (day1 > 30) {
            return 0;
        }
    } else if (mnth1 == 5) {
        if (day1 = may[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 == 6) {
        if (day1 = jun[i]) {
            x = i + 1;
        } else if (day1 > 30) {
            return 0;
        }
    } else if (mnth1 == 7) {
        if (day1 = jul[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 == 8) {
        if (day1 = aug[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 == 9) {
        if (day1 = sep[i]) {
            x = i + 1;
        } else if (day1 > 30) {
            return 0;
        }
    } else if (mnth1 == 10) {
        if (day1 = oct[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 == 11) {
        if (day1 = nov[i]) {
            x = i + 1;
        } else if (day1 > 30) {
            return 0;
        }
    } else if (mnth1 == 12) {
        if (day1 = dec[i]) {
            x = i + 1;
        } else if (day1 > 31) {
            return 0;
        }
    } else if (mnth1 > 12) {
        return 0;
    }

    printf("Please enter the second date: ");
    scanf("%d", &mnth2);
    scanf("%d", &day2);

    printf("%d", mnth2);
    printf(" ");
    printf("%d\n", day2);

    if (mnth2 == 1) {
        if (day2 = jan[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 == 2) {
        if (day2 = feb[t]) {
            y = t + 1;
        } else if (day2 > 28) {
            return 0;
        }
    } else if (mnth2 == 3) {
        if (day2 = mar[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 == 4) {
        if (day2 = apr[t]) {
            y = t + 1;
        } else if (day2 > 30) {
            return 0;
        }
    } else if (mnth2 == 5) {
        if (day2 = may[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 == 6) {
        if (day2 = jun[t]) {
            y = t + 1;
        } else if (day2 > 30) {
            return 0;
        }
    } else if (mnth2 == 7) {
        if (day2 = jul[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 == 8) {
        if (day2 = aug[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 == 9) {
        if (day2 = sep[t]) {
            y = t + 1;
        } else if (day2 > 30) {
            return 0;
        }
    } else if (mnth2 == 10) {
        if (day2 = oct[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 == 11) {
        if (day2 = nov[t]) {
            y = t + 1;
        } else if (day2 > 30) {
            return 0;
        }
    } else if (mnth2 == 12) {
        if (day2 = dec[t]) {
            y = t + 1;
        } else if (day2 > 31) {
            return 0;
        }
    } else if (mnth2 > 12) {
        return 0;
    }

    printf("The difference between these two dates is:\n");
    printf("%d\n", x);
    printf("%d\n", y);
    //  printf("%d\n", y - x);
}
4

1 回答 1

3

首先,您的许多辅助if语句都使用赋值运算符=而不是比较==

i其次,最初应该在哪里设置?如果您的目标是x成为数组中的值,只需设置它:x = dec[day1]

于 2013-09-19T16:20:37.170 回答