Advertisement
puppet106

easter.c

Feb 22nd, 2020
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. // Easter Sundays calculator
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6.  
  7. char *myname;
  8. const char *religions[]={ "Orthodox", "Catholic" };
  9. const char *months[]={ "March", "April", "May" };
  10. void showusage();
  11.  
  12. int main(int argc,char *argv[])
  13. {
  14.    int date, month, year, u1, u2, u3, u4, u5, u6, u7, u8, u9, u10, u11, u12;
  15.    int religion=0, cc;
  16.    strcpy((myname=malloc(sizeof(argv[0]))), argv[0]);
  17.  
  18.     // parse command line options
  19.     while ((cc = getopt(argc, argv, ":oc")) != -1)
  20.      switch (cc) {
  21.       case 'o':
  22.        religion=0;
  23.       break;
  24.     case 'c':
  25.        religion=1;
  26.       break;
  27.       case '?':
  28.        showusage();
  29.       break;
  30.       default:
  31.        abort();
  32.     break; }
  33.     if (optind==argc || argc<3)
  34.      showusage();
  35.     year=atoi(argv[optind]);
  36.     if (year<0 || year>32767)
  37.      showusage();
  38.  
  39.      // calculations
  40.      if (!religion) { // Orthodox
  41.       u1=year%19;
  42.       u2=year%4;
  43.       u3=year%7;
  44.       u4=((19*u1)+16)%30;
  45.       u5=((2*u2)+(4*u3)+(6*u4))%7;
  46.       date=3+u4+u5;
  47.       month=2;
  48.       if (date>30) {
  49.        date-=30;
  50.      month=3; } }
  51.      else { // Catholic
  52.       u1=year%19;
  53.       u2=year/100;
  54.       u3=year%100;
  55.       u4=u2/4;
  56.       u5=u2%4;
  57.       u6=(u2+8)/25;
  58.       u7=(u2-u6+1)/3;
  59.       u8=(19*u1+u2-u4-u7+15)%30;
  60.       u9=u3/4;
  61.       u10=u3%4;
  62.       u11=(32+2*u5+2*u9-u8-u10)%7;
  63.       u12=(u1+11*u8+22*u11)/451;
  64.       month=((u8+u11-7*u12+114)/31)-2;
  65.      date=((u8+u11-7*u12+114)%31)+1; }
  66.    
  67.     // result
  68.     printf("%s Easter is on %d of %s %d\n", religions[religion], date, months[month-1], year);
  69.    
  70.  return 0;
  71. }
  72.  
  73. // show usage
  74. void showusage()
  75. {
  76.   printf("%s -o [Orthodox] -c [Catholic] year [0..32767]\n", myname);
  77.   exit(EXIT_FAILURE);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement