Mitrezzz

АПС Лаб 4 Шалтерот на МВР

Nov 19th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.NoSuchElementException;
  3.  
  4. class Gragjanin{
  5.     protected String ime;
  6.     protected int karta,pasos,vozacka;
  7.     public Gragjanin() {
  8.     }
  9.     public Gragjanin(String ime, int karta, int pasos, int vozacka) {
  10.         this.ime = ime;
  11.         this.karta = karta;
  12.         this.pasos = pasos;
  13.         this.vozacka = vozacka;
  14.     }
  15. }
  16.  
  17. class SllNode<E>{
  18.     protected E element;
  19.     protected SllNode<E> succ;
  20.     public SllNode(E element, SllNode<E> succ){
  21.         this.element = element;
  22.         this.succ = succ;
  23.     }
  24. }
  25.  
  26. class Queue<E>{
  27.     private SllNode<E> front, rear;
  28.     private int length;
  29.     public Queue(){
  30.         front = null;
  31.         rear = null;
  32.         length = 0;
  33.     }
  34.     public boolean isEmpty(){
  35.         return (length == 0);
  36.     }
  37.     public int size(){
  38.         return length;
  39.     }
  40.     public E peek(){
  41.         if(length > 0)
  42.             return front.element;
  43.         else
  44.             throw new NoSuchElementException();
  45.     }
  46.     public void enque(E x){
  47.         SllNode<E> temp = new SllNode(x, null);
  48.         if(length > 0){
  49.             rear.succ = temp;
  50.             rear = temp;
  51.         }
  52.         else
  53.             front = rear = temp;
  54.         length++;
  55.     }
  56.     public E deque(){
  57.         if(length > 0){
  58.             E temp = front.element;
  59.             front = front.succ;
  60.             length--;
  61.             return temp;
  62.         }
  63.         else
  64.             throw new NoSuchElementException();
  65.     }
  66.     @Override
  67.     public String toString(){
  68.         String s = new String();
  69.         SllNode<E> temp = front;
  70.         while(temp != null){
  71.             s += temp.element + "\n";
  72.             temp = temp.succ;
  73.         }
  74.         return s;
  75.     }
  76. }
  77.  
  78. public class MVR {
  79.     public static void pecati(Gragjanin[] g){
  80.         Queue<Gragjanin> lkarta = new Queue();
  81.         Queue<Gragjanin> pasos = new Queue();
  82.         Queue<Gragjanin> vozacka = new Queue();
  83.         for(int i = 0; i < g.length; i++){
  84.             if(g[i].karta == 1)
  85.                 lkarta.enque(g[i]);
  86.             else if(g[i].pasos == 1)
  87.                 pasos.enque(g[i]);
  88.             else if(g[i].vozacka == 1)
  89.                 vozacka.enque(g[i]);
  90.         }
  91.         Gragjanin temp = new Gragjanin();
  92.         while(!lkarta.isEmpty()){
  93.             temp = lkarta.deque();
  94.             if(temp.pasos == 0 && temp.vozacka == 0)
  95.                 System.out.println(temp.ime);
  96.             else if(temp.pasos == 1)
  97.                 pasos.enque(temp);
  98.             else
  99.                 vozacka.enque(temp);
  100.         }
  101.         while(!pasos.isEmpty()){
  102.             temp = pasos.deque();
  103.             if(temp.vozacka == 0)
  104.                 System.out.println(temp.ime);
  105.             else
  106.                 vozacka.enque(temp);
  107.         }
  108.         while(!vozacka.isEmpty())
  109.             System.out.println(vozacka.deque().ime);
  110.     }
  111.     public static void main(String[] args) {
  112.         Scanner br = new Scanner(System.in);
  113.         int N = Integer.parseInt(br.nextLine());
  114.         Gragjanin[] g = new Gragjanin[N];
  115.         for(int i = 1; i <= N; i++){
  116.             String imePrezime = br.nextLine();
  117.             int lKarta = Integer.parseInt(br.nextLine());
  118.             int pasos = Integer.parseInt(br.nextLine());
  119.             int vozacka = Integer.parseInt(br.nextLine());
  120.             Gragjanin covek = new Gragjanin(imePrezime, lKarta, pasos, vozacka);
  121.             g[i - 1] = covek;
  122.         }
  123.         pecati(g);
  124.     }
  125. }
Add Comment
Please, Sign In to add comment