Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package zad3a2014;
- import java.io.*;
- /**
- *
- * @author Szymek
- */
- public class Zad3A2014 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- // String plik = "notacja.dat";
- // try(RandomAccessFile raf = new RandomAccessFile(plik, "rw");)
- // {
- // raf.setLength(0);
- // raf.writeUTF("a -1");
- // raf.writeUTF("-+abba -1");
- // raf.writeUTF("/ab+a-ba -1");
- // raf.writeUTF("+abc -1");
- // raf.writeUTF("+-ab* -1");
- // }
- // catch(Exception e)
- // {}
- // zmienne a i b, + - * /
- String plik = "notacja.dat";
- String linia;
- String[][] tablica;
- try( RandomAccessFile raf = new RandomAccessFile(plik, "rw"); )
- {
- long dlugoscPliku = raf.length();
- while(raf.getFilePointer() < dlugoscPliku)
- {
- long pozycjaPrzed = raf.getFilePointer();
- linia = raf.readUTF();
- long pozycjaPo = raf.getFilePointer();
- int znaki = 0;
- int zmienne = 0;
- if(linia.contains("-1"))
- {
- String wyrazenie = linia.split(" ")[0];
- for (int i = 0; i < wyrazenie.length(); i++)
- {
- switch(wyrazenie.charAt(i))
- {
- case '+':
- znaki++;
- break;
- case '-':
- znaki++;
- break;
- case '*':
- znaki++;
- break;
- case '/':
- znaki++;
- break;
- case 'a':
- zmienne++;
- break;
- case 'b':
- zmienne++;
- break;
- }
- }
- System.out.println( (zmienne-znaki == 1) ? wyrazenie+": prawidłowe!" : wyrazenie + ": nieprawidlowe" );
- if(zmienne-znaki==1)
- {
- raf.seek(pozycjaPrzed);
- raf.writeUTF(wyrazenie + " " + "1 ");
- raf.seek(pozycjaPo);
- }
- else
- {
- raf.seek(pozycjaPrzed);
- raf.writeUTF(wyrazenie + " " + "0 ");
- raf.seek(pozycjaPo);
- }
- }
- }
- }
- catch(IOException e)
- {
- System.out.println("Wyjątek: " + e.getLocalizedMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement