Advertisement
lozanotux

fibonacci.vala

Jan 22nd, 2016
1,170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Gtk;
  2.  
  3. public long fibonacci (int n)
  4. {
  5.     if (n < 2) {
  6.         return n;
  7.       } else {
  8.         return (fibonacci(n - 1) + fibonacci(n - 2));
  9.     }
  10. }
  11.  
  12. public static int main (string[] args)
  13. {
  14.     Gtk.init (ref args);
  15.  
  16.     var window = new Window ();
  17.     window.title = "Fibonacci GTK+";
  18.     window.set_border_width (12);
  19.     window.set_default_size (70, 70);
  20.     window.set_position (Gtk.WindowPosition.CENTER);
  21.     window.destroy.connect (Gtk.main_quit);
  22.  
  23.     var loop = new MainLoop();
  24.  
  25.     long result = 0;
  26.  
  27.     var headerbar = new HeaderBar ();
  28.     var entry = new Entry ();
  29.     var button = new Button.with_label ("Calcular");
  30.     var label = new Label ("Resultado");
  31.     var box = new Box (Orientation.VERTICAL, 0);
  32.  
  33.     headerbar.title = "Fibonacci GTK+";
  34.     headerbar.show_close_button = true;
  35.  
  36.     box.pack_start (entry, false, true, 0);
  37.     box.pack_start (label, false, true, 12);
  38.     box.pack_start (button, false, true, 0);
  39.  
  40.     window.set_titlebar (headerbar);
  41.  
  42.     window.add (box);
  43.     window.show_all ();
  44.  
  45.     /*** LOGICA ***/
  46.     button.clicked.connect ( ()=> {
  47.         result = fibonacci (int.parse (entry.get_text ()));
  48.             label.set_label (result.to_string ());
  49.         loop.quit();
  50.     });    
  51.  
  52.     loop.run ();
  53.  
  54.     Gtk.main ();
  55.     return 0;
  56. }
Advertisement
RAW Paste Data Copied
Advertisement