Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////// WithStack
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import java.util.Stack;
- public class WithStack {
- public static void main(String[] args) throws FileNotFoundException {
- Stack <Character> stack=new Stack();
- File file=new File("Laba3_1.txt");
- Scanner scanner=new Scanner(file);
- while(scanner.hasNext()){
- char[] chars = new char[0];
- chars=scanner.next().toCharArray();
- for(char i:chars){
- if(i=='('||i==')'||i=='{'||i=='}'||i=='['||i==']')
- stack.add(i);
- }
- }
- int counter1=0; // (
- int counter_1=0; // )
- int counter2=0; // {
- int counter_2=0; // }
- int counter3=0; // [
- int counter_3=0; // ]
- for(char i:stack){
- System.out.print(i);
- switch(i){
- case '(':
- counter1++;
- break;
- case ')':
- counter_1++;
- break;
- case '{':
- counter2++;
- break;
- case '}':
- counter_2++;
- break;
- case '[':
- counter3++;
- break;
- case ']':
- counter_3++;
- break;
- }
- }
- System.out.println();
- boolean prov1=counter1==counter_1;
- boolean prov2=counter_2==counter2;
- boolean prov3=counter3==counter_3;
- System.out.println("(:"+counter1+"\n):"+counter_1+"\n{:"+counter2+"\n}:"+counter_2+"\n[:"+counter3+"\n]:"+counter_3);
- if(prov1&&prov2&&prov3){
- System.out.println("корректно");
- }
- else
- {
- int counter=0;
- System.out.println("некорректно");
- if(counter1>counter_1){
- System.out.println("Отсутствует ')'");
- }
- if(counter1<counter_1)
- System.out.println("Отсутствует '('");
- if(counter2>counter_2)
- System.out.println("Лишняя '{'");
- if(counter2<counter_2)
- System.out.println("Лишняя '}'");
- if(counter3>counter_3)
- System.out.println("Лишняя '['");
- if(counter3<counter_3)
- System.out.println("Лишняя ']'");
- }
- }
- }
- ////////////// class WithQueque
- mport java.io.File;
- import java.io.FileNotFoundException;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- public class WithQueque {
- public static void main(String[] args) throws FileNotFoundException {
- Queue<Character> queue=new LinkedList<>();
- File file=new File("Laba3_2.txt");
- Scanner scanner=new Scanner(file);
- while(scanner.hasNext()){
- char[] chars = new char[0];
- chars=scanner.next().toCharArray();
- for(char i:chars){
- if(i=='('||i==')'||i=='{'||i=='}'||i=='['||i==']')
- queue.add(i);
- }
- }
- int counter1=0; // (
- int counter_1=0; // )
- int counter2=0; // {
- int counter_2=0; // }
- int counter3=0; // [
- int counter_3=0; // ]
- for(char i:queue){
- System.out.print(i);
- switch(i){
- case '(':
- counter1++;
- break;
- case ')':
- counter_1++;
- break;
- case '{':
- counter2++;
- break;
- case '}':
- counter_2++;
- break;
- case '[':
- counter3++;
- break;
- case ']':
- counter_3++;
- break;
- }
- }
- System.out.println();
- boolean prov1=counter1==counter_1;
- boolean prov2=counter_2==counter2;
- boolean prov3=counter3==counter_3;
- System.out.println("(:"+counter1+"\n):"+counter_1+"\n{:"+counter2+"\n}:"+counter_2+"\n[:"+counter3+"\n]:"+counter_3);
- if(prov1&&prov2&&prov3){
- System.out.println("корректно");
- }
- else
- {
- System.out.println("некорректно");
- if(counter1>counter_1)
- System.out.println("Лишняя '('");
- if(counter1<counter_1)
- System.out.println("Лишняя ')'");
- if(counter2>counter_2)
- System.out.println("Лишняя '{'");
- if(counter2<counter_2)
- System.out.println("Лишняя '}'");
- if(counter3>counter_3)
- System.out.println("Лишняя '['");
- if(counter3<counter_3)
- System.out.println("Лишняя ']'");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement