gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Primeri za postfiksni kalkulator
authorDoni Pracner <quinnuendo@gmail.com>
Mon, 25 May 2015 15:02:08 +0000 (17:02 +0200)
committerDoni Pracner <quinnuendo@gmail.com>
Mon, 25 May 2015 15:02:08 +0000 (17:02 +0200)
kodovi/stek-i-red-opsluzivanja/InteraktivniPostFix.java [new file with mode: 0644]
kodovi/stek-i-red-opsluzivanja/PostFix.java [new file with mode: 0644]

diff --git a/kodovi/stek-i-red-opsluzivanja/InteraktivniPostFix.java b/kodovi/stek-i-red-opsluzivanja/InteraktivniPostFix.java
new file mode 100644 (file)
index 0000000..ffea624
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * Postfiksna notacija prvo izlistava operande, a potom operacije.
+ * 
+ * Ovaj program predstavlja interaktivni postfiksni kalkulator u tome
+ * se unose 
+ *
+ */
+public class InteraktivniPostFix {
+
+       public static void main(String[] args) {
+
+               System.out.println("Postfiksni kalkulator.");
+               System.out.println("Za kraj unesite tačku '.'");
+               String str = Svetovid.in.readLine("-ulaz?");
+
+               Stek<Integer> stek = new Stek<Integer>(100);
+               int broj = 0;
+
+               while (!str.equals(".")) {
+                       if (jeOperator(str)) {
+                               char c = str.charAt(0);
+                               try {
+                                       obradiOperaciju(c, stek);
+                               } catch (Exception e) {
+                                       System.out.println("Greska:" + e.getMessage());
+                               }
+                       } else {
+                               // nije operator, probamo da li je broj
+                               boolean jeBroj = true;
+                               try {
+                                       broj = Integer.valueOf(str);
+                               } catch (NumberFormatException e) {
+                                       jeBroj = false;
+                               }
+                               if (jeBroj) {
+                                       stek.stavi(broj);
+                               } else {
+                                       System.out.println("Greska - nije ni broj ni operacija!");
+                                       System.out.println("Za kraj unesite tačku '.'");
+                               }
+
+                       }
+                       stanjeSteka(stek);
+                       System.out.print("-ulaz? ");
+                       str = Svetovid.in.readLine();
+               }
+               stanjeSteka(stek);
+               System.out.println("-kraj rada");
+       }
+
+       /**
+        * @param c
+        * @param s
+        */
+       public static void obradiOperaciju(char c, Stek<Integer> s) {
+               int op2 = s.skiniVrh();
+               int op1 = s.skiniVrh();
+
+               switch (c) {
+               case '+':
+                       s.stavi(op1 + op2);
+                       break;
+               case '-':
+                       s.stavi(op1 - op2);
+                       break;
+               case '*':
+                       s.stavi(op1 * op2);
+                       break;
+               case '/':
+                       s.stavi(op1 / op2);
+                       break;
+               case '%':
+                       s.stavi(op1 % op2);
+                       break;
+               }
+       }
+
+       public static boolean jeOperator(String str) {
+               if (str.length() == 1) {
+                       char c = str.charAt(0);
+                       if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%')
+                               return true;
+               }
+               return false;
+       }
+
+       public static void stanjeSteka(Stek<Integer> s) {
+               if (s.jePrazan()) {
+                       System.out.println("rezultat: ne postoji");
+               } else {
+                       int vrh = s.vrh();
+                       System.out.println("rezultat: " + vrh);
+               }
+       }
+
+}
diff --git a/kodovi/stek-i-red-opsluzivanja/PostFix.java b/kodovi/stek-i-red-opsluzivanja/PostFix.java
new file mode 100644 (file)
index 0000000..293b308
--- /dev/null
@@ -0,0 +1,51 @@
+/**\r
+ * Postfixni kalkulator prima prvo operande, a onda operacije, npr 3 2 + umesto\r
+ * 3+2.\r
+ * \r
+ * Zadatak je napraviti mali postfiksni kalkulator koji obrađuje uneti String,\r
+ * pri čemu se predpostavlja da mogu da postoje operacije +,-,*,/ i %, te da su\r
+ * u pitanju jednocifreni celi brojevi.\r
+ * \r
+ */\r
+public class PostFix {\r
+       public static void main(String[] args) {\r
+               int op1, op2;\r
+               char c;\r
+\r
+               System.out.print("Unesite string: ");\r
+               String str = Svetovid.in.readLine();\r
+\r
+               Stek<Integer> s = new Stek<Integer>(100);\r
+\r
+               for (int i = 0; i < str.length(); i++) {\r
+                       c = str.charAt(i);\r
+                       if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%') {\r
+                               op2 = s.vrh();\r
+                               s.skiniVrh();\r
+                               op1 = s.vrh();\r
+                               s.skiniVrh();\r
+                               switch (c) {\r
+                               case '+':\r
+                                       s.stavi(op1 + op2);\r
+                                       break;\r
+                               case '-':\r
+                                       s.stavi(op1 - op2);\r
+                                       break;\r
+                               case '*':\r
+                                       s.stavi(op1 * op2);\r
+                                       break;\r
+                               case '/':\r
+                                       s.stavi(op1 / op2);\r
+                                       break;\r
+                               case '%':\r
+                                       s.stavi(op1 % op2);\r
+                                       break;\r
+                               }\r
+                       } else\r
+                               s.stavi(Character.getNumericValue(c));\r
+               }\r
+               op1 = s.vrh();\r
+               System.out.print("=");\r
+               System.out.print(op1);\r
+       }\r
+}
\ No newline at end of file
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner