上一天和第二天,C代码

时间:2015-10-04 14:56:34

标签: c algorithm exception

#include <stdio.h>
#include <stdlib.h>

int number_of_days[13] = {0,31,28,31,30,31,30,31,30,31,30,31};

int leap_year(int gg)
{
    if(y%400==0) return 1;

    if(y%4==0 && y%100!=0) return 1;

    return 0;
}

int okay(int d, int m, int y)
{
    if(d<1 || d>number_of_days[m]) return 0;

    if(m<1 || m>12) return 0;

    return 1;
}

int main()
{
   int n, i;
   int d, m, y;
   int nd, nm, ny;
   int pd, pm, py;

   for(i=0; i<n, i++)
   {
       scanf("%d %d %d", d, m,y);
       printf("Date: %d.%d.%d ", d, m, y);

       if(leap_year(y)) number_of_days[2] = 29;
       else number_of_days[2] = 28;

       if(!okay(dd, mm, y))
       {
           printf("not okay");
           continue;
       }

       nd = d + 1;
       nm = m;
       ny = y;

       if(nd > number_of_days[nm])
       {
           nd = 1;
           nm++;
       }

       if(nm > 12)
       {
           nm = 1;
           ny++;
       }

       pd = d - 1;
       pm = m;
       pg = y;

       if(pd < 1)
       {
           pm--;

           if(pm < 1)
           {
               pg--;
           }

           pd = number_of_days[pm];

       }
       printf("Previous %d.%d.%d. Next %d.%d.%d.  year\n", pd, pm, py, nd, nm, ny);
   }
   return 0;
}

算法对我来说很好,但我无法运行此代码。我在Windows上使用CodeBlocks。其中一个错误是:

  

返回前的预期表达。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

该计划有很多错误。

可能的解决方法:

#include <stdio.h>
#include <stdlib.h>

int number_of_days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /* add 31 for August */

int leap_year(int gg)
{
    int y=gg; /* add this because y was undefined */
    if(y%400==0) return 1;

    if(y%4==0 && y%100!=0) return 1;

    return 0;
}

int okay(int d, int m, int y)
{
    (void)y; /* to avoid warning that y is unused */
    if(d<1 || d>number_of_days[m]) return 0;

    if(m<1 || m>12) return 0;

    return 1;
}

int main()
{
    int n, i;
    int d, m, y;
    int nd, nm, ny;
    int pd, pm, py;
    int pg; /* add this because it was undefined */
    n = 1; /* add this to initialize n */

    for(i=0; i<n; i++) /* , -> ; possibly typo */
    {
        scanf("%d %d %d", &d, &m, &y); /* add & because you have to pass pointers to read the integers to */
        printf("Date: %d.%d.%d ", d, m, y);

        if(leap_year(y)) number_of_days[2] = 29;
        else number_of_days[2] = 28;

        if(!okay(d, m, y)) /* dd -> d, mm -> m possibly typo */
        {
            printf("not okay");
            continue;
        }

        nd = d + 1;
        nm = m;
        ny = y;

        if(nd > number_of_days[nm])
        {
            nd = 1;
            nm++;
        }

        if(nm > 12)
        {
            nm = 1;
            ny++;
        }

        pd = d - 1;
        pm = m;
        pg = y;

        if(pd < 1)
        {
            pm--;

            if(pm < 1)
            {
                pg--;
                pm = 12; /* add this because month 0 seems wrong */
            }

            pd = number_of_days[pm];

        }
        py = pg; /* add this because py was uninitialized */
        printf("Previous %d.%d.%d. Next %d.%d.%d.  year\n", pd, pm, py, nd, nm, ny);
    }
    return 0;
}
相关问题