ly.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <stdio.h>
  2. #include <time.h>
  3. int main (int argc, char** argv) {
  4. int c = 0;
  5. int max = 1;
  6. int y;
  7. if (argc < 3) {
  8. time_t current = time(NULL);
  9. struct tm *strTime = localtime(&current);
  10. y = strTime->tm_year + 1900;
  11. if (argc == 2) {
  12. if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "?")) {
  13. printf ("Leap Year Calculator\n\n");
  14. printf ("Usage:\n");
  15. printf ("\t~ %s ([number] ([year]))\n", argv[0]);
  16. printf (" where [number] and [year] are positive integers. Both [number] and [year] are optional; if only one is given, it is interpreted as [number].\n\n");
  17. printf ("For example:\n");
  18. printf ("\t~ %s 20 1970\n", argv[0]);
  19. printf (" would return the 20 leap years following 1970,\n");
  20. printf ("\t~ %s 20\n", argv[0]);
  21. printf (" would return the next 20 leap years following the current year, and\n");
  22. printf ("\t~ %s\n", argv[0]);
  23. printf (" would return the next leap year following the current year.\n\n");
  24. printf ("If the current year is a leap year, a message is displayed noting that fact before the remainder of the output is given.\n");
  25. return 0;
  26. } else { max = atoi(argv[1]); }
  27. }
  28. if ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)) {
  29. printf ("This is a leap year. ");
  30. }
  31. if (max > 1) { printf ("The next %d leap years are:\n", max); }
  32. else { printf ("The next leap year is "); }
  33. } else {
  34. max = atoi(argv[1]);
  35. y = atoi(argv[2]);
  36. if ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)) {
  37. printf ("%d is a leap year. ", y);
  38. }
  39. if (max > 1) { printf ("The next %d leap years following %d are:\n", max, y); }
  40. else { printf ("The next leap year following %d is ", y); }
  41. }
  42. while (c < max) {
  43. y++;
  44. if ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)) {
  45. if (max == 1) { printf ("%d.\n", y); }
  46. else {
  47. if ((c + 1) < 10) { printf (" %d.) %d\n", c + 1, y); }
  48. else { printf ("%d.) %d\n", c + 1, y); }
  49. }
  50. c++;
  51. }
  52. }
  53. return 0;
  54. }