Hey,

Next up for pom(6): consolidate the printf(3) calls.

The inline printing obfuscates what is otherwise a very
simple classification algorithm.

ok?

--
Scott Cheloha

Index: games/pom/pom.c
===================================================================
RCS file: /cvs/src/games/pom/pom.c,v
retrieving revision 1.27
diff -u -p -r1.27 pom.c
--- games/pom/pom.c     24 Dec 2017 16:59:50 -0000      1.27
+++ games/pom/pom.c     24 Dec 2017 17:54:46 -0000
@@ -75,17 +75,21 @@ main(int argc, char *argv[])
        struct tm *GMT;
        time_t tmpt;
        double days, today, tomorrow;
-       int cnt;
+       int cnt, principal, usertime;
        char buf[1024];
+       char *descriptor, *name;
+
+       principal = 1;
+       usertime = 0;
 
        if (pledge("stdio", NULL) == -1)
                err(1, "pledge");
 
        if (argc > 1) {
+               usertime = 1;
                tmpt = parsetime(argv[1]);
                strftime(buf, sizeof(buf), "%a %Y %b %e %H:%M:%S (%Z)",
                        localtime(&tmpt));
-               printf("%s:  ", buf);
        } else
                tmpt = time(NULL);
        GMT = gmtime(&tmpt);
@@ -97,27 +101,36 @@ main(int argc, char *argv[])
        for (cnt = GMT->tm_year; cnt < EPOCH; ++cnt)
                days -= isleap(cnt + 1900) ? 366 : 365;
        today = potm(days);
-       (void)printf("The Moon is ");
        if (lround(today) == 100)
-               (void)printf("Full\n");
+               name = "Full";
        else if (lround(today) == 0)
-               (void)printf("New\n");
+               name = "New";
        else {
                tomorrow = potm(days + 1);
-               if (lround(today) == 50)
-                       (void)printf("%s\n", tomorrow > today ?
-                           "at the First Quarter" : "at the Last Quarter");
-               else {
-                       (void)printf("%s ", tomorrow > today ?
-                           "Waxing" : "Waning");
+               if (lround(today) == 50) {
+                       if (tomorrow > today)
+                               name = "at the First Quarter";
+                       else
+                               name = "at the Last Quarter";
+               } else {
+                       principal = 0;
+                       if (tomorrow > today)
+                               descriptor = "Waxing";
+                       else
+                               descriptor = "Waning";
                        if (today > 50.0)
-                               (void)printf("Gibbous (%1.0f%% of Full)\n",
-                                   today);
+                               name = "Gibbous";
                        else /* (today < 50.0) */
-                               (void)printf("Crescent (%1.0f%% of Full)\n",
-                                   today);
+                               name = "Crescent";
                }
        }
+       if (usertime)
+               printf("%s:  ", buf);
+       printf("The Moon is ");
+       if (principal)
+               printf("%s\n", name);
+       else
+               printf("%s %s (%1.0f%% of Full)\n", descriptor, name, today);
        return 0;
 }
 

Reply via email to