2017-03-21 10 views
1

このコードは、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; 
} 
+0

可能であれば、問題を表示する最小限の例を提供できますか?これは、あなたがそれを理解するのに役立つだけでなく、助けてほしいかもしれない他の人たちを理解するのに役立ちます – dash2

+0

あなたは初心者です。ですので、https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-workをお読みください。また、満足のいく答えが得られた後は、質問を編集しないでください。 –

答えて

2

ミス代わりmain関数で

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

で、それはで

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

であるべきですを使用し、必要に応じて%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に従ってください。

+0

本当にありがとうございました。もう1つの質問、論理部分はまだ機能していません。ちょうど私が入力した順序を与えます。私はそれが私に日付の昇順を与えるのが好きです。問題を解決してもらえますか?ありがとうございます – Savitor

+0

http://stackoverflow.com/help/someone-answers。同じ質問を編集するのではなく、別の質問として追加してください。同じ質問を編集すると、コミュニティによるコメントと回答が無効になることがあります。 –

関連する問題