【问题标题】:Segmentation fault - Core dumped in the code分段错误 - 核心转储在代码中
【发布时间】:2017-03-21 22:28:52
【问题描述】:

此代码用于获取n 日期并按升序对其进行排序。我认为getdates 功能无法正常工作。

main函数实现:

#include <stdio.h>
#define max 30
int leapyear(int year);/*to check whether it's leap year or not*/
int dater(int x);/*to find the date from a month begin with the beginning of the year */
void getdates(int f);/*get the date in put*/
int *caldays(int p);
int n, i, q;
int t, d, leap;
int day[30];
int month[30];
int year[30];
char ca[30];
char cb[30];
int dd[30];
int da[30];
int j, a, x;
char c1, c2;
int dayn, monthn, yearn;

int main()
{
     printf("Please enter the number of dates and Press ENTER to finish\n");
     /*get the numbers of dates*/
     scanf("%i", &n);
     printf("You have entered %i \n", n);
     getdates(n);
     printf("end");

    for (i = 0; i <= n-1; i++)
    {
        printf("end");
        while (day[i] < 1 || day[i] > 31 || month[i] < 1 ||
            month[i] > 12 || year[i] < 1 || year[i] > 10000)
        {
            fprintf( stderr, "The date entered is not right, please enter again\n");
            printf("Please enter the number of dates and Press ENTER to finish\n");
            /*get the numbers of dates*/
            scanf("%i", &n);
            printf("You have entered %i \n", n);
            getdates(n);
        }

        while (month[i] == 2 && day[i] > 29)
        {
            fprintf( stderr, "The date entered is not right, please enter again\n");
            printf("Please enter the number of dates and Press ENTER to finish\n");
            /*get the numbers of dates*/
            scanf("%i", &n);
            printf("You have entered %i \n", n);
            getdates(n);
        }

        while ((month[i] == 4 ||month[i] == 6 || 
                month[i] == 9 ||month[i] == 11) && day[i] > 30)
        {
            fprintf( stderr, "The date entered is not right, please enter again\n");
            printf("Please enter the number of dates and Press ENTER to finish\n");
            /*get the numbers of dates*/
            scanf("%i", &n);
            printf("You have entered %i \n", n);
            getdates(n);
        }
    }
    /*3 while loops are used to give msg and re-enter again when get an error input*/

    caldays(n);
    for (i = 0; x < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (dd[i] > dd[j])
            {
                a =  dd[i];
                dd[i] = dd[j];
                dd[j] = a;
            }
            /*sort the days in asending order in days array*/
        }
    }
    printf("The %i dates in ascending order are\n", n);
    for (i = 0; i < n; ++i)
        printf("%d%c%d%c%d\n", day[i], ca[i], month[i], cb[i], year[i]);
    /*print all the date in ascending order*/
}


 /*find out wheter it's leap year or not*/
int leapyear(int year)
{
    if (year % 4000 == 0)
    {
        return 1;
    } 
    else if (year % 400 == 0)
    {
       return 1;
    } 
    else if (year % 40 == 0)
    {
       return 1;
    } 
    else if (year % 4 == 0)
    {
       return 1;
    } 
    else
    {
       return 0;
    }
}


/*find out the days for the month input*/
int dater(x)
{ 
    int y=0;
    switch(x)
    {
        case 1: y=0; break;
        case 2: y=31; break;
        case 3: y=59; break;
        case 4: y=90; break;
        case 5: y=120; break;
        case 6: y=151; break;
        case 7: y=181; break;
        case 8: y=212; break;
        case 9: y=243; break;
        case 10:y=273; break;
        case 11:y=304; break;
        case 12:y=334; break;
        default: fprintf( stderr, "the value entered is not right\n");
    }
    return y;
}


void getdates(int f)
{ 

    for(i=0;i<f;i++)
    {
        q = i+1;
        printf("Please Enter %i dates (DD/MM/YYYY or DD-MM-YYYY) and
                Press ENTER to finish each one\n", n);
        scanf("%d%c%d%c%d", &dayn, &c1, &monthn, &c2, &yearn);
        printf("You have entered %i date %i%c%i%c%i\n", q, dayn, c1, monthn, c2, yearn);
        day[i] = dayn;
        month[i] = monthn;
        year[i] = yearn;
        ca[i] = c1;
        cb[i] = c2;
    }
    return;
}


int *caldays(int p)
{
    for (i=0; i < p-1; i++)
    {   
        leap = leapyear(year[i]);
        t = 0;
        if(leap == 1)
        {
            t++;
        }
        /*if there is a leap add one day */
        /*find for the days for month entered*/
        d = dater(month[i]);
        /* find out the total days from the date entered begin with 0 days*/
        d = d + dayn + t + (yearn * 365);
      dd[i] = d;/*put all the days in an array*/
    }  
    return dd;
}

【问题讨论】:

  • 如果可能,您能否提供一个显示问题的最小示例?这将有助于您以及其他可能想要提供帮助的人理解它。
  • 我看你是初学者。所以请阅读meta.stackexchange.com/questions/5234/…。此外,您不应在问题得到满意回答后对其进行编辑。

标签: c date logic


【解决方案1】:

错误是代替

caldays(n);
for (i = 0; x < n; ++i)

main函数中,应该是

caldays(n);
for (i = 0; i < n; ++i)

getdates() 和任何需要的地方,使用"%d %c %d %c %d" 而不是%d%c%d%c%d

一旦你的程序运行起来,我建议你将它发布到https://codereview.stackexchange.com/ 以供审查。那里有经验的人会帮助你拆开你的代码,让你的生活更轻松,更好地理解readability of code

我建议你阅读Difference between format specifiers %i and %d in printf

如果可能,请在发布问题时从下次开始关注MCVE

【讨论】:

  • 非常感谢您的宝贵时间,再问一个问题,逻辑部分仍然无法正常工作,它只是给出了我输入的顺序。我希望它给我日期的升序。请问可以帮我解决问题吗?谢谢
  • stackoverflow.com/help/someone-answers。请不要编辑相同的问题,而是将其添加为不同的问题。编辑相同的问题可能会使社区的 cmets 和答案无效。
猜你喜欢
  • 2022-10-13
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多