fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3.  
  4. double g2jd(int year, int month, int day)
  5. {
  6. int t;
  7. double jd;
  8.  
  9. if (month < 3) {
  10. year--;
  11. month += 12;
  12. }
  13. t = year / 100;
  14. jd = (int)(year * 365.25) - t + t / 4;
  15. jd += (int)(30.6001 * (month + 1)) + day + 1720996.5;
  16. return jd;
  17. }
  18.  
  19. void jd2g(double jd, int *pyear, int *pmonth, int *pday)
  20. {
  21. int z, aa, a, b, c, k, e;
  22.  
  23. jd += 0.5;
  24. z = (int)jd;
  25. aa = (int)((z - 1867216.25) / 36524.25);
  26. a = (int)(z + 1 + aa - aa / 4);
  27. b = a + 1524;
  28. c = (int)((b - 122.1) / 365.25);
  29. k = (int)(365.25 * c);
  30. e = (int)((b - k) / 30.6001);
  31. *pday = b - k - (int)(30.6001 * e);
  32. *pmonth = (e <= 13) ? e - 1 : e - 13;
  33. *pyear = (*pmonth > 2) ? c - 4716 : c - 4715;
  34. }
  35.  
  36. int main()
  37. {
  38. char buf[256];
  39. int year, month, day, days;
  40. double jd;
  41.  
  42. if (fgets(buf, sizeof buf, stdin) == NULL) {
  43. printf("quit\n");
  44. return 1;
  45. }
  46. if (sscanf(buf, "%4d%2d%2d %d", &year, &month, &day, &days) != 4) {
  47. printf("yyyymmdd days\n");
  48. return 1;
  49. }
  50. printf("%d %d %d %d\n", year, month, day, days);
  51.  
  52. jd = g2jd(year, month, day);
  53. printf("%f\n", jd);
  54.  
  55. jd += days;
  56. jd2g(jd, &year, &month, &day);
  57. printf("%d/%d/%d %.2s\n", year, month, day, "SuMoTuWeThFrSa" + (((int)jd + 2) % 7) * 2);
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0s 2056KB
stdin
20150129 -29
stdout
2015 1 29 -29
2457051.500000
2014/12/31 We