Daily WTF Contest Entry

This was my entry for The Daily WTF Programming Contest, held in early 2007. The goal was to create a calculator UI with confusing behavior and/or implementation. I had just graduated university, so I took inspiration from things I'd seen while helping other students with their assignments:

I've done some minor cleanup with clang-format, everything else (including bad buffer sizing) is original.

// g++ -o calc calc.cc `pkg-config --cflags --libs gtk+-2.0`
#include <cstdio>
#include <cstdlib>
#include <gtk/gtk.h>

char *eval(const char *);

class Window {
public:
  Window() {
    gtk_init(NULL, NULL);

    GtkWindow *window = init_window();
    gtk_widget_show_all(GTK_WIDGET(window));

    gtk_main();
  }

  struct {
    GtkWindow *operator()() {
      GtkWidget *widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      GtkWidget *vbox = gtk_vbox_new(FALSE, 5);
      GtkWidget *entry = gtk_entry_new();

      typedef struct {
        static void on_destroy(GtkWidget *, gpointer) { gtk_main_quit(); }

        static void on_changed(GtkWidget *widget, gpointer data) {
          GtkComboBox *combo = GTK_COMBO_BOX(widget);
          GtkEntry *entry = GTK_ENTRY(data);
          const gchar *text = gtk_combo_box_get_active_text(combo);
          if (g_ascii_strcasecmp(text, "Clear") == 0) {
            gtk_entry_set_text(entry, "");
            return;
          } else if (g_ascii_strcasecmp(text, "=") == 0) {
            struct {
              gsize operator()(const gchar *rope, char the_char) {
                gsize ii, count = 0;
                struct {
                  size_t operator()(const gchar *rope) {
                    size_t counter = 0;
                    while (counter++ [rope])
                      ;
                    return counter;
                  }
                } rope_len;
                for (ii = 0; ii < rope_len(rope) - 1; ii++) {
                  if (rope[ii] == the_char) {
                    count = count + 1;
                  }
                }
                return count;
              }
            } char_count;

            if (char_count(gtk_entry_get_text(entry), ' ') == 2) {
              char *new_text = eval((const char *)(gtk_entry_get_text(entry)));
              gtk_entry_set_text(entry, new_text);
              return;
            }
          }

          else if (g_ascii_strcasecmp(text, "1") == 0 ||
                   g_ascii_strcasecmp(text, "2") == 0 ||
                   g_ascii_strcasecmp(text, "3") == 0 ||
                   g_ascii_strcasecmp(text, "4") == 0 ||
                   g_ascii_strcasecmp(text, "5") == 0 ||
                   g_ascii_strcasecmp(text, "6") == 0 ||
                   g_ascii_strcasecmp(text, "7") == 0 ||
                   g_ascii_strcasecmp(text, "8") == 0 ||
                   g_ascii_strcasecmp(text, "9") == 0) {
            char *new_text = g_strconcat(gtk_entry_get_text(entry), text, NULL);
            gtk_entry_set_text(entry, new_text);
            g_free(new_text);
          }

          else {
            char *new_text =
                g_strconcat(gtk_entry_get_text(entry), " ", text, " ", NULL);
            gtk_entry_set_text(entry, new_text);
            g_free(new_text);
          }
        }
      } signal_handlers;

      gtk_container_add(GTK_CONTAINER(widget), vbox);
      gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 5);
      gtk_window_set_title(GTK_WINDOW(widget), "Calculator");
      gtk_entry_set_editable(GTK_ENTRY(entry), FALSE);
      g_signal_connect(widget, "destroy",
                       G_CALLBACK(signal_handlers::on_destroy), NULL);

      struct {
        GtkComboBox *operator()() {
          GtkWidget *combo = gtk_combo_box_new_text();
          static const gsize text_size = 20;
          gsize ii;

          enum {
            TEXT_LOCATION_CLEAR,
            TEXT_LOCATION_0,
            TEXT_LOCATION_1,
            TEXT_LOCATION_2,
            TEXT_LOCATION_3,
            TEXT_LOCATION_4,
            TEXT_LOCATION_5,
            TEXT_LOCATION_6,
            TEXT_LOCATION_7,
            TEXT_LOCATION_8,
            TEXT_LOCATION_9,
            TEXT_LOCATION_PLUS,
            TEXT_LOCATION_MINUS,
            TEXT_LOCATION_TIMES,
            TEXT_LOCATION_DIVIDE,
            TEXT_LOCATION_EQUALS
          };

          for (ii = 0; ii < text_size; ii++) {
            switch (ii) {
            case TEXT_LOCATION_0:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "1");
              break;

            case TEXT_LOCATION_1:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "2");
              break;

            case TEXT_LOCATION_2:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "3");
              break;

            case TEXT_LOCATION_3:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "4");
              break;

            case TEXT_LOCATION_4:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "5");
              break;

            case TEXT_LOCATION_5:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "6");
              break;

            case TEXT_LOCATION_6:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "7");
              break;

            case TEXT_LOCATION_7:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "8");
              break;

            case TEXT_LOCATION_8:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "9");
              break;

            case TEXT_LOCATION_9:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "0");
              break;

            case TEXT_LOCATION_PLUS:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "+");
              break;

            case TEXT_LOCATION_MINUS:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "-");
              break;

            case TEXT_LOCATION_TIMES:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "*");
              break;

            case TEXT_LOCATION_DIVIDE:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "/");
              break;

            case TEXT_LOCATION_CLEAR:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Clear");
              break;

            case TEXT_LOCATION_EQUALS:
              gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "=");
              break;
            }
          }

          return GTK_COMBO_BOX(combo);
        }
      } init_combo;

      GtkComboBox *combo = init_combo();
      gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(combo), TRUE, TRUE, 5);
      g_signal_connect(combo, "changed",
                       G_CALLBACK(signal_handlers::on_changed), entry);
      return GTK_WINDOW(widget);
    }
  } init_window;
}

static const eval_ctx;

char *eval(const char *rope) {
  struct {
    void operator()(const char *rope, double &first, char &op, double &second) {
      char *first_rpe, *op_rpe, *second_rpe;

      struct {
        char *operator()(char *rope, char part) {
          size_t ii = 0;
          while (ii[rope] && (ii[rope] != part)) {
            ++ii;
          }
          return rope + ii + 1;
        }
      } find;

      struct {
        char *operator()(const char *rope) {
          size_t ii;

          struct {
            size_t operator()(const char *rope) {
              size_t counter = 0;
              while (counter++ [rope])
                ;
              return counter;
            }
          } rope_len;

          char *new_rope = new char[rope_len(rope)];
          for (ii = 0; ii < rope_len(rope); ii++) {
            ii[new_rope] = ii[rope];
            rope_len(rope)[new_rope + 1] = 0; // NIL killer
          }
          return new_rope;
        }
      } copy_rope;

      first_rpe = copy_rope(rope);
      op_rpe = find(first_rpe, ' ');
      second_rpe = find(op_rpe, ' ');

      op_rpe[-1] = 0;
      second_rpe[-1] = 0;

      first = strtod(first_rpe, NULL);
      op = op_rpe[0];
      second = strtod(second_rpe, NULL);
      delete[] first_rpe;
    }
  } parse;

  struct {
    char *operator()(double first, char op, double second) {
      struct {
        void operator()(char *rope) {
          size_t ii;
          for (ii = 0; ii < 10; ii++) {
            rope[ii] = 0;
          }
        }
      } clear;

      static const size_t BUFFER_SIZE = 10;
      static char rope[BUFFER_SIZE];

      clear(rope);

      switch (op) {
        struct {
          void operator()(char *rope_a, const char *rope_b) {
            struct {
              size_t operator()(const char *rope) {
                size_t counter = 0;
                while (counter++ [rope])
                  ;
                return counter;
              }
            } rope_len;
            size_t ii;

            for (ii = 0; ii < rope_len(rope_b); ii++)
              ii[rope_a] = ii[rope_b];
            rope_a[ii] = 0; // NIL killer
          }
        } copy_rope;
      case '+':
        sprintf(rope, "%f", first + second);
        break;

      case '-':
        sprintf(rope, "%f", first - second);
        break;

      case '*':
        sprintf(rope, "%f", first * second);
        break;

      case '/':
        if (second == 0)
          copy_rope(rope, "Err");
        else
          sprintf(rope, "%f", first / second);
        break;
      }

      return rope;
    }
  } run;

  char *new_rope;
  double first, second;
  char op;

  parse(rope, first, op, second);

  new_rope = run(first, op, second);

  struct {
    void operator()(char *new_rope) {
      int ii;

      for (ii = 9; ii >= 0; ii--) {
        char old = new_rope[ii];

        if (new_rope[ii] == 0 || new_rope[ii] == 060 || new_rope[ii] == 056)
          new_rope[ii] = 0;

        if (!(old == '0' || old == 0))
          break;
      }
    }
  } trim;

  trim(new_rope);

  return new_rope;
}

int main() { return 0; }

  1. See Copper for more about how to write C++ when "gcc 2.96" is the best option available.

  2. Wikipedia wasn't well known or we'd have found https://en.wikipedia.org/wiki/Rope_(data_structure)