Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class test{
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- switch(n){
- case 1:
- int times = sc.nextInt();
- displayResult(method1(times));
- break;
- case 2:
- displayResult(method2());
- break;
- case 3:
- displayResult(method3());
- break;
- default:
- System.out.print("Invalid option. try again");
- }
- }
- public static ArrayList<Integer> method1(int times){
- ArrayList<Integer> intStack = new ArrayList<Integer>();
- Scanner sc = new Scanner(System.in);
- for(int i = 1; i <= times ; i++){
- String str = sc.nextLine();
- int result = evaluate(str);
- if (result == -1){
- System.out.println("Invalid line");
- }else{
- intStack.add(result);
- }
- }
- return intStack;
- }
- public static ArrayList<Integer> method2(){
- ArrayList<Integer> intStack = new ArrayList<Integer>();
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- while(!str.equals("0")){
- int result = evaluate(str);
- if (result == -1){
- System.out.println("Invalid line");
- }else{
- intStack.add(result);
- }
- str = sc.nextLine();
- }
- return intStack;
- }
- public static ArrayList<Integer> method3(){
- ArrayList<Integer> intStack = new ArrayList<Integer>();
- Scanner sc = new Scanner(System.in);
- while (sc.hasNextLine()){
- String str = sc.nextLine();
- if (str.equals("")){
- break;
- }else{
- int result = evaluate(str);
- if (result == -1){
- System.out.println("Invalid line");
- }else{
- intStack.add(result);
- }
- str = sc.nextLine();
- }
- }
- return intStack;
- }
- public static int evaluate(String line){
- if ( line.indexOf("AND") == 0){
- int bit1 = line.charAt(4) - '0';
- int bit2 = line.charAt(6) - '0';
- return bit1 == 1 && bit1 == bit2 ? 1 : 0;
- }else if(line.indexOf("OR") == 0){
- int bit1 = line.charAt(3) - '0';
- int bit2 = line.charAt(5) - '0';
- return bit1 == 1 || bit2 == 1 ? 1 : 0;
- }else{
- return -1;
- }
- }
- public static void displayResult(ArrayList<Integer> intStack){
- for(Integer i : intStack){
- System.out.println(i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement