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.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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)