Advertisement
Matteogoli

mcd_FMI

May 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. package it.unitn.disi.lingprog.fmi;
  2.  
  3. import javafx.application.Application;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.control.TextField;
  7. import javafx.scene.layout.HBox;
  8. import javafx.scene.layout.VBox;
  9. import javafx.scene.text.Font;
  10. import javafx.scene.text.Text;
  11. import javafx.stage.Stage;
  12.  
  13.  
  14. public class FMI extends Application {
  15.  
  16.     int a, b;
  17.     static Text t = new Text();
  18.     @Override
  19.     public void start(Stage primaryStage) {
  20.         t.setFont(Font.font(22));
  21.         Text t1 = new Text("(");
  22.         t1.setFont(Font.font(22));
  23.        
  24.         TextField i1 = new TextField();
  25.        
  26.         Text t2 = new Text(", ");
  27.         t2.setFont(Font.font(22));
  28.        
  29.         TextField i2 = new TextField();
  30.        
  31.         Text t3 = new Text(")");
  32.         t3.setFont(Font.font(22));
  33.        
  34.         Button btn = new Button("Calcola");
  35.        
  36.         HBox primariga = new HBox(t1, i1, t2, i2, t3, btn);
  37.        
  38.         btn.setOnAction(event -> {
  39.             a = Integer.parseInt(i1.getText());
  40.             b = Integer.parseInt(i2.getText());
  41.             mcd(a, b);
  42.         });
  43.        
  44.         VBox root = new VBox(primariga, t);
  45.         Scene scene = new Scene(root, 600, 200);
  46.         primaryStage.setTitle("Fondamenti Matematici per l'Informatica");
  47.         primaryStage.setScene(scene);
  48.         primaryStage.show();
  49.     }
  50.    
  51.     static void mcd (int x, int y){
  52.         final int cx = x;
  53.         final int cy = y;
  54.         int q, r = 1, snext, sprev = 1, scur = 0, tprev = 0, tcur = 1, tnext;
  55.         while(r != 0) {
  56.             q = x / y;
  57.             r = x % y;
  58.             if(r != 0){
  59.                 snext = sprev - q * scur;
  60.                 tnext = tprev - q * tcur;
  61.                 x = y; y = r;
  62.                 sprev = scur; scur = snext;
  63.                 tprev = tcur; tcur = tnext;
  64.             }
  65.         }
  66.         System.out.println("("+cx+", "+cy+") = "+y+", x  = "+scur+", y = "+tcur+"");
  67.         t.setText("("+cx+";   "+cy+") = "+y+";   x  = "+scur+";   y = "+tcur+"");
  68.     }
  69.  
  70.     public static void main(String[] args) {
  71.         launch(args);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement