From ca5a8516fd24b25612463018455f927965a50aec Mon Sep 17 00:00:00 2001 From: HORVILLE Ewen Date: Wed, 26 Oct 2022 11:13:11 +0200 Subject: [PATCH] TP04 --- DEV 3.2/TP03/Luminance/LumListener.java | 1 - DEV 3.2/TP03/Luminance/LumPanel.java | 22 +++++++- DEV 3.2/TP03/Luminance/Luminance.java | 7 ++- DEV 3.2/TP04/Arithmetique/Arithmetique.java | 49 +++++++++++++++++ DEV 3.2/TP04/Chaine/Arithmetique.java | 44 ++++++++++++++++ DEV 3.2/TP04/Chaine/Chaine.java | 25 +++++++++ DEV 3.2/TP04/Chaine/ChainePile.java | 47 +++++++++++++++++ DEV 3.2/TP04/Chaine/Pile.java | 6 +++ DEV 3.2/TP04/Derecursivation/ArrayPile.java | 55 ++++++++++++++++++++ DEV 3.2/TP04/Derecursivation/Derecursif.java | 34 ++++++++++++ DEV 3.2/TP04/Derecursivation/Pile.java | 6 +++ DEV 3.2/TP04/Derecursivation/Recursif.java | 12 +++++ DEV 3.2/TP04/Tableau/Arithmetique.java | 44 ++++++++++++++++ DEV 3.2/TP04/Tableau/ArrayPile.java | 55 ++++++++++++++++++++ DEV 3.2/TP04/Tableau/Pile.java | 6 +++ 15 files changed, 409 insertions(+), 4 deletions(-) create mode 100644 DEV 3.2/TP04/Arithmetique/Arithmetique.java create mode 100644 DEV 3.2/TP04/Chaine/Arithmetique.java create mode 100644 DEV 3.2/TP04/Chaine/Chaine.java create mode 100644 DEV 3.2/TP04/Chaine/ChainePile.java create mode 100644 DEV 3.2/TP04/Chaine/Pile.java create mode 100644 DEV 3.2/TP04/Derecursivation/ArrayPile.java create mode 100644 DEV 3.2/TP04/Derecursivation/Derecursif.java create mode 100644 DEV 3.2/TP04/Derecursivation/Pile.java create mode 100644 DEV 3.2/TP04/Derecursivation/Recursif.java create mode 100644 DEV 3.2/TP04/Tableau/Arithmetique.java create mode 100644 DEV 3.2/TP04/Tableau/ArrayPile.java create mode 100644 DEV 3.2/TP04/Tableau/Pile.java diff --git a/DEV 3.2/TP03/Luminance/LumListener.java b/DEV 3.2/TP03/Luminance/LumListener.java index fd8642e..5f7b2f3 100644 --- a/DEV 3.2/TP03/Luminance/LumListener.java +++ b/DEV 3.2/TP03/Luminance/LumListener.java @@ -6,7 +6,6 @@ public class LumListener implements MouseListener { public void mouseClicked(MouseEvent e) { LumPanel p = (LumPanel)e.getSource(); p.removeParralel(e.getX(), e.getY()); - } @Override diff --git a/DEV 3.2/TP03/Luminance/LumPanel.java b/DEV 3.2/TP03/Luminance/LumPanel.java index c4bd874..dec1514 100644 --- a/DEV 3.2/TP03/Luminance/LumPanel.java +++ b/DEV 3.2/TP03/Luminance/LumPanel.java @@ -1,17 +1,20 @@ import javax.swing.JPanel; import java.awt.*; +import java.util.LinkedList; import java.util.Random; public class LumPanel extends JPanel { - private ListChain l; + private LinkedList l; int width = 30; int offset = 30; int height = 60; int spacing = 5; + int mx, my = 0; + public LumPanel() { - l = new ListChain(); + l = new LinkedList(); Random r = new Random(); for (int i = 0; i < 10; i++) { @@ -41,6 +44,18 @@ public class LumPanel extends JPanel { } } + public void setMCoords(int x, int y) { + double dx = (double)x; + double dy = (double)y; + + dx -= lerp(offset, 0, dy / (double)height); + dx -= spacing * Math.floor(dx / width); + + this.mx = (int)dx; + this.my = (int)dy; + repaint(); + } + @Override protected void paintComponent(Graphics g) { Graphics gg = g.create(); @@ -61,6 +76,9 @@ public class LumPanel extends JPanel { p.addPoint(x + width, getY() + height); p.addPoint(x, getY() + height); + gg.setColor(Color.BLACK); + gg.fillOval(mx-3, my-3, 6, 6); + gg.setColor(c); gg.fillPolygon(p); gg.setColor(Color.BLACK); diff --git a/DEV 3.2/TP03/Luminance/Luminance.java b/DEV 3.2/TP03/Luminance/Luminance.java index ab25c60..b6e2d29 100644 --- a/DEV 3.2/TP03/Luminance/Luminance.java +++ b/DEV 3.2/TP03/Luminance/Luminance.java @@ -1,5 +1,5 @@ import javax.swing.JFrame; - +import java.awt.*; /** * Luminance */ @@ -21,5 +21,10 @@ public class Luminance { f.setVisible(true); lm.addMouseListener(new LumListener()); + + while (true) { + Point p = MouseInfo.getPointerInfo().getLocation(); + lm.setMCoords(p.x - lm.getLocationOnScreen().x, p.y - lm.getLocationOnScreen().y); + } } } \ No newline at end of file diff --git a/DEV 3.2/TP04/Arithmetique/Arithmetique.java b/DEV 3.2/TP04/Arithmetique/Arithmetique.java new file mode 100644 index 0000000..ffdb865 --- /dev/null +++ b/DEV 3.2/TP04/Arithmetique/Arithmetique.java @@ -0,0 +1,49 @@ +import java.util.ArrayDeque; + +/** + * Arithmetique + */ +public class Arithmetique { + public static void main(String[] args) { + ArrayDeque pile = new ArrayDeque<>(); + + for (String arg : args) { + try { + int n = Integer.parseInt(arg); + pile.addFirst(n); + } catch (NumberFormatException e) { + if (pile.size() < 2) { + System.err.println("Invalid stack size."); + return; + } + + int n2 = pile.pollFirst(); + int n1 = pile.pollFirst(); + + switch (arg) { + case "+": + pile.addFirst(n1 + n2); + break; + + case "-": + pile.addFirst(n1 - n2); + break; + + case "/": + pile.addFirst(n1 / n2); + break; + + case "x": + pile.addFirst(n1 * n2); + break; + + default: + System.err.println("Invalid operator"); + return; + } + } + } + + System.out.println(pile.pollFirst()); + } +} \ No newline at end of file diff --git a/DEV 3.2/TP04/Chaine/Arithmetique.java b/DEV 3.2/TP04/Chaine/Arithmetique.java new file mode 100644 index 0000000..21fdfcf --- /dev/null +++ b/DEV 3.2/TP04/Chaine/Arithmetique.java @@ -0,0 +1,44 @@ +public class Arithmetique { + public static void main(String[] args) { + ChainePile pile = new ChainePile<>(); + + for (String arg : args) { + try { + int n = Integer.parseInt(arg); + pile.addFirst(n); + } catch (NumberFormatException e) { + if (pile.size() < 2) { + System.err.println("Invalid stack size."); + return; + } + + int n2 = pile.pollFirst(); + int n1 = pile.pollFirst(); + + switch (arg) { + case "+": + pile.addFirst(n1 + n2); + break; + + case "-": + pile.addFirst(n1 - n2); + break; + + case "/": + pile.addFirst(n1 / n2); + break; + + case "x": + pile.addFirst(n1 * n2); + break; + + default: + System.err.println("Invalid operator"); + return; + } + } + } + + System.out.println(pile.pollFirst()); + } +} \ No newline at end of file diff --git a/DEV 3.2/TP04/Chaine/Chaine.java b/DEV 3.2/TP04/Chaine/Chaine.java new file mode 100644 index 0000000..103df7e --- /dev/null +++ b/DEV 3.2/TP04/Chaine/Chaine.java @@ -0,0 +1,25 @@ +public class Chaine { + + public Chaine() { + + } + + private E value; + private Chaine next; + + public E getValue() { + return value; + } + + public void setValue(E value) { + this.value = value; + } + + public Chaine getNext() { + return next; + } + + public void setNext(Chaine next) { + this.next = next; + } +} diff --git a/DEV 3.2/TP04/Chaine/ChainePile.java b/DEV 3.2/TP04/Chaine/ChainePile.java new file mode 100644 index 0000000..b32eff3 --- /dev/null +++ b/DEV 3.2/TP04/Chaine/ChainePile.java @@ -0,0 +1,47 @@ +public class ChainePile implements Pile { + + private Chaine lastChain; + + public ChainePile() {} + + @Override + public E getFirst() { + if (lastChain != null) { + return lastChain.getValue(); + } else { + return null; + } + } + + @Override + public E pollFirst() { + if (lastChain != null) { + E value = lastChain.getValue(); + lastChain = lastChain.getNext(); + return value; + } else { + return null; + } + } + + @Override + public void addFirst(E e) { + Chaine link = new Chaine(); + link.setValue(e); + link.setNext(lastChain); + lastChain = link; + } + + @Override + public int size() { + int count = 0; + Chaine curr = lastChain; + while (curr != null) { + curr = curr.getNext(); + count++; + } + + return count; + } + +} diff --git a/DEV 3.2/TP04/Chaine/Pile.java b/DEV 3.2/TP04/Chaine/Pile.java new file mode 100644 index 0000000..0f75b0b --- /dev/null +++ b/DEV 3.2/TP04/Chaine/Pile.java @@ -0,0 +1,6 @@ +public interface Pile { + public E getFirst(); + public E pollFirst(); + public void addFirst(E e); + public int size(); +} diff --git a/DEV 3.2/TP04/Derecursivation/ArrayPile.java b/DEV 3.2/TP04/Derecursivation/ArrayPile.java new file mode 100644 index 0000000..1fc1f63 --- /dev/null +++ b/DEV 3.2/TP04/Derecursivation/ArrayPile.java @@ -0,0 +1,55 @@ +public class ArrayPile implements Pile{ + + int size = 16; + int currentElement = 0; + + E[] table; + + public ArrayPile() { + table = (E[])new Object[size]; + }; + + private void resizeStack() { + size *= 2; + E[] newTable = (E[])new Object[size]; + + for (int i = 0; i < table.length; i++) { + newTable[i] = table[i]; + } + + table = newTable; + } + + @Override + public E getFirst() { + if (currentElement == 0) { + return null; + } else { + return table[currentElement-1]; + } + } + + @Override + public E pollFirst() { + if (currentElement == 0) { + return null; + } else { + E e = table[currentElement-1]; + currentElement--; + return e; + } + } + + @Override + public void addFirst(E e) { + if (currentElement >= table.length) resizeStack(); + table[currentElement] = e; + currentElement++; + } + + @Override + public int size() { + return currentElement; + } + +} diff --git a/DEV 3.2/TP04/Derecursivation/Derecursif.java b/DEV 3.2/TP04/Derecursivation/Derecursif.java new file mode 100644 index 0000000..dad0f7a --- /dev/null +++ b/DEV 3.2/TP04/Derecursivation/Derecursif.java @@ -0,0 +1,34 @@ +public class Derecursif { + + public static int f(int n) { + ArrayPile pile = new ArrayPile<>(); + + pile.addFirst(n); + + int result = 0; + + while (pile.size() > 0) { + n = pile.pollFirst(); + + switch (n) { + case 0: + break; + + case 1: + result += 1; + break; + + default: + pile.addFirst(n / 2); + pile.addFirst(n / 3); + break; + } + } + + return result; + } + + public static void main(String[] args) { + System.out.println(f(150)); + } +} diff --git a/DEV 3.2/TP04/Derecursivation/Pile.java b/DEV 3.2/TP04/Derecursivation/Pile.java new file mode 100644 index 0000000..0f75b0b --- /dev/null +++ b/DEV 3.2/TP04/Derecursivation/Pile.java @@ -0,0 +1,6 @@ +public interface Pile { + public E getFirst(); + public E pollFirst(); + public void addFirst(E e); + public int size(); +} diff --git a/DEV 3.2/TP04/Derecursivation/Recursif.java b/DEV 3.2/TP04/Derecursivation/Recursif.java new file mode 100644 index 0000000..f7df629 --- /dev/null +++ b/DEV 3.2/TP04/Derecursivation/Recursif.java @@ -0,0 +1,12 @@ +public class Recursif { + public static double f(double v) { + if (v == 0) return 0.0; + if (v == 1) return 1.0; + + return v + f(Math.floor(v / 2)) - f(Math.floor(v / 3)); + } + + public static void main(String[] args) { + System.out.println(f(250)); + } +} diff --git a/DEV 3.2/TP04/Tableau/Arithmetique.java b/DEV 3.2/TP04/Tableau/Arithmetique.java new file mode 100644 index 0000000..4bc72a2 --- /dev/null +++ b/DEV 3.2/TP04/Tableau/Arithmetique.java @@ -0,0 +1,44 @@ +public class Arithmetique { + public static void main(String[] args) { + ArrayPile pile = new ArrayPile<>(); + + for (String arg : args) { + try { + int n = Integer.parseInt(arg); + pile.addFirst(n); + } catch (NumberFormatException e) { + if (pile.size() < 2) { + System.err.println("Invalid stack size."); + return; + } + + int n2 = pile.pollFirst(); + int n1 = pile.pollFirst(); + + switch (arg) { + case "+": + pile.addFirst(n1 + n2); + break; + + case "-": + pile.addFirst(n1 - n2); + break; + + case "/": + pile.addFirst(n1 / n2); + break; + + case "x": + pile.addFirst(n1 * n2); + break; + + default: + System.err.println("Invalid operator"); + return; + } + } + } + + System.out.println(pile.pollFirst()); + } +} \ No newline at end of file diff --git a/DEV 3.2/TP04/Tableau/ArrayPile.java b/DEV 3.2/TP04/Tableau/ArrayPile.java new file mode 100644 index 0000000..1fc1f63 --- /dev/null +++ b/DEV 3.2/TP04/Tableau/ArrayPile.java @@ -0,0 +1,55 @@ +public class ArrayPile implements Pile{ + + int size = 16; + int currentElement = 0; + + E[] table; + + public ArrayPile() { + table = (E[])new Object[size]; + }; + + private void resizeStack() { + size *= 2; + E[] newTable = (E[])new Object[size]; + + for (int i = 0; i < table.length; i++) { + newTable[i] = table[i]; + } + + table = newTable; + } + + @Override + public E getFirst() { + if (currentElement == 0) { + return null; + } else { + return table[currentElement-1]; + } + } + + @Override + public E pollFirst() { + if (currentElement == 0) { + return null; + } else { + E e = table[currentElement-1]; + currentElement--; + return e; + } + } + + @Override + public void addFirst(E e) { + if (currentElement >= table.length) resizeStack(); + table[currentElement] = e; + currentElement++; + } + + @Override + public int size() { + return currentElement; + } + +} diff --git a/DEV 3.2/TP04/Tableau/Pile.java b/DEV 3.2/TP04/Tableau/Pile.java new file mode 100644 index 0000000..0f75b0b --- /dev/null +++ b/DEV 3.2/TP04/Tableau/Pile.java @@ -0,0 +1,6 @@ +public interface Pile { + public E getFirst(); + public E pollFirst(); + public void addFirst(E e); + public int size(); +}