gpt4 book ai didi

c - 段错误 - 代码中的核心转储

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:21 25 4
gpt4 key购买 nike

此代码用于获取n 日期并将它们按升序排序。我认为 getdates 函数没有正常工作。

主要函数实现:

#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;
}

最佳答案

错误

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/ 上以供审核.那里经验丰富的人员将帮助您分解代码,让您的生活更轻松,以便更好地理解代码的可读性

我推荐你阅读Difference between format specifiers %i and %d in printf .

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

关于c - 段错误 - 代码中的核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42939368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com