Advertisement
Nakumas

Egzamin PP&JP: 2014, Zad.3 A

Feb 11th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package zad3a2014;
  7.  
  8. import java.io.*;
  9.  
  10. /**
  11.  *
  12.  * @author Szymek
  13.  */
  14. public class Zad3A2014 {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args)
  20.     {  
  21. //        String plik = "notacja.dat";
  22. //        try(RandomAccessFile raf = new RandomAccessFile(plik, "rw");)
  23. //        {
  24. //            raf.setLength(0);
  25. //            raf.writeUTF("a -1");
  26. //            raf.writeUTF("-+abba -1");
  27. //            raf.writeUTF("/ab+a-ba -1");
  28. //            raf.writeUTF("+abc -1");
  29. //            raf.writeUTF("+-ab* -1");
  30. //        }
  31. //        catch(Exception e)
  32. //        {}
  33.        
  34.         // zmienne a i b, + - * /
  35.         String plik = "notacja.dat";
  36.         String linia;
  37.         String[][] tablica;
  38.         try( RandomAccessFile raf = new RandomAccessFile(plik, "rw"); )
  39.         {
  40.             long dlugoscPliku = raf.length();
  41.             while(raf.getFilePointer() < dlugoscPliku)
  42.             {
  43.                 long pozycjaPrzed = raf.getFilePointer();
  44.                 linia = raf.readUTF();
  45.                 long pozycjaPo = raf.getFilePointer();
  46.                
  47.                 int znaki = 0;
  48.                 int zmienne = 0;
  49.                
  50.                 if(linia.contains("-1"))
  51.                 {
  52.                     String wyrazenie = linia.split(" ")[0];
  53.                     for (int i = 0; i < wyrazenie.length(); i++)
  54.                     {
  55.                         switch(wyrazenie.charAt(i))
  56.                         {
  57.                             case '+':
  58.                                 znaki++;
  59.                                 break;
  60.                             case '-':
  61.                                 znaki++;
  62.                                 break;
  63.                             case '*':
  64.                                 znaki++;
  65.                                 break;
  66.                             case '/':
  67.                                 znaki++;
  68.                                 break;
  69.                             case 'a':
  70.                                 zmienne++;
  71.                                 break;
  72.                             case 'b':
  73.                                 zmienne++;
  74.                                 break;
  75.                         }
  76.                     }
  77.  
  78.                    System.out.println( (zmienne-znaki == 1) ? wyrazenie+": prawidłowe!" : wyrazenie + ": nieprawidlowe" );
  79.                    if(zmienne-znaki==1)
  80.                    {
  81.                        raf.seek(pozycjaPrzed);
  82.                        raf.writeUTF(wyrazenie + " " + "1 ");
  83.                        raf.seek(pozycjaPo);
  84.                    }
  85.                    else
  86.                    {
  87.                        raf.seek(pozycjaPrzed);
  88.                        raf.writeUTF(wyrazenie + " " + "0 ");
  89.                        raf.seek(pozycjaPo);
  90.                    }
  91.  
  92.                 }
  93.             }
  94.         }
  95.         catch(IOException e)
  96.         {
  97.             System.out.println("Wyjątek: " + e.getLocalizedMessage());
  98.         }
  99.     }
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement