Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdlib.h>
  2. #include <gtk/gtk.h>
  3.  
  4. #define MAXTEST 100000000
  5.  
  6. typedef struct WidgetBank
  7. {
  8.     GtkWidget *window;
  9.     GtkWidget *fixedspace;
  10.     GtkWidget *button;
  11.     GtkWidget *PrimeToFind;
  12.     GtkWidget *readoutCount;
  13.     GtkWidget *readoutPrime;
  14.     GtkEntryBuffer *PrimeToFindBuffer;
  15. } Widgets;
  16.  
  17. int isitprime(unsigned long int index);
  18.  
  19. static void destroy (GtkWidget *window, gpointer data)
  20. {
  21.     gtk_main_quit ();
  22. }
  23. //callback function that starts prime finder and updates labels in main window
  24. static void FindNthPrime (GtkWidget *button, gpointer data)
  25. {
  26.     Widgets *widgets = data;
  27.     gchar primesfoundresults[100], currentprimesresults[100];
  28.     int check = 0, primecount = 0, currentprime = 0;
  29.     unsigned long int x = 0;
  30.     //get address of text in box
  31.     const gchar *PrimeToFind = (gtk_entry_buffer_get_text(widgets->PrimeToFindBuffer));
  32.     //put the ascii data at that address into an int
  33.     check = atoi(PrimeToFind);
  34.     //Here's the actual finding of the Nth prime
  35.     for (x = 2; x <= MAXTEST; x++)
  36.     {
  37.         if (isitprime(x))
  38.         {
  39.             currentprime = x;
  40.             sprintf(currentprimesresults, "Current Prime:%i", currentprime);
  41.             gtk_label_set_text((GtkLabel *)widgets->readoutPrime, currentprimesresults);
  42.             primecount++;
  43.             sprintf(primesfoundresults, "Primes Found:%i", (primecount +1));
  44.             gtk_label_set_text((GtkLabel *)widgets->readoutCount, primesfoundresults);
  45.  
  46.         }
  47.         if ((primecount+1) == check) {break;}
  48.     }
  49.  
  50. }
  51.  
  52. int main (int argc, char *argv[])
  53. {
  54.     //GtkWidget *window, *fixedspace, *button, *readoutCount, *readoutPrime;
  55.  
  56.     gtk_init (&argc, &argv);
  57.  
  58.     Widgets PE7_Widgets;
  59.     //Set up widget pointers
  60.     PE7_Widgets.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  61.     PE7_Widgets.fixedspace = gtk_fixed_new();
  62.     PE7_Widgets.button = gtk_button_new_with_label("Start");
  63.     PE7_Widgets.readoutCount = gtk_label_new("Primes Found: 0");
  64.     PE7_Widgets.readoutPrime = gtk_label_new("Current Prime: 0");
  65.     PE7_Widgets.PrimeToFindBuffer = gtk_entry_buffer_new(NULL, -1);
  66.     PE7_Widgets.PrimeToFind = gtk_entry_new_with_buffer(PE7_Widgets.PrimeToFindBuffer);
  67.  
  68.     //pack and set up locations
  69.     gtk_container_add(GTK_CONTAINER (PE7_Widgets.window), PE7_Widgets.fixedspace);
  70.     gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.button, 0, 0);
  71.     gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.readoutCount, 0, 30);
  72.     gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.readoutPrime, 0, 50);
  73.     gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.PrimeToFind, 45, 0);
  74.  
  75.     //connect signals for closing main window and the button
  76.     g_signal_connect (G_OBJECT (PE7_Widgets.window), "destroy", G_CALLBACK (destroy), NULL);
  77.     g_signal_connect (G_OBJECT (PE7_Widgets.button), "clicked", G_CALLBACK (FindNthPrime), &PE7_Widgets);
  78.  
  79.     //display and wait
  80.     gtk_widget_show_all(PE7_Widgets.window);
  81.     gtk_main ();
  82.     return 0;
  83. }