Tuesday, June 21, 2011

Project Euler 7 Solution



It works, but I think it goes too fast and doesn't update the text until the very end so you don't get a running tally of the primes found. That's ok.

A few thoughts:

1) My variable naming is poor. I got better near the end but I am NOT consistent with uppercase/lowercase rules.

2) The way I extract an integer from the string stored in the text field is a bit... unpleasant. gtk_entry_buffer_get_text returns a pointer to the text buffer containing what's in the entry field. Then I use atoi() to pass the values to an integer variable and I then perform all the prime checking. I'm extremely puzzled that there wouldn't be a built-in function for this as part of GtkEntry. There are plenty of functions for getting/setting and even highlighting and returning the number of letters and whatnot, but nothing to tackle the times you want to use that entry box to contain and return integers. The only number entry widget is a box with an up/down arrow. Kind of ugly. Anyways, it works.

3) I got to use sprintf() in a "real life" setting. That's a first for me.

4) There is NO error checking or any way to cancel the operation (but there is a hard limit on how high of a number it will check to. Error checking or checking for NULL in any initialized pointer would have taken way too long so I skipped it. The temporary character arrays I populate with sprintf() before they're passed to the labels are size 50. Should be plenty but I didn't include any checks there.

5) In a lot of ways this program proved to me that I understand pointers, structs, and how to read the GTK API docs. I had no idea how the GtkEntry widget worked but through trial and error and lots of reading I figured it out.

I don't think I'll create a user interface for every PE problem. UI is hard. I need to get through the GTK book more so that I can get to the chapters on using Glade (the WYSIWYG UI layout editor for GTK).

No comments:

Post a Comment