Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class test{
  4.     public static void main(String[] args){
  5.         Scanner sc = new Scanner(System.in);
  6.        
  7.         int n = sc.nextInt();
  8.        
  9.             switch(n){
  10.             case 1:
  11.                 int times = sc.nextInt();
  12.                 displayResult(method1(times));
  13.                 break;
  14.             case 2:
  15.                 displayResult(method2());
  16.                 break;
  17.             case 3:
  18.                 displayResult(method3());
  19.                 break;
  20.             default:
  21.                 System.out.print("Invalid option. try again");
  22.             }
  23.        
  24.     }
  25.    
  26.     public static ArrayList<Integer> method1(int times){
  27.         ArrayList<Integer> intStack = new ArrayList<Integer>();
  28.         Scanner sc = new Scanner(System.in);
  29.        
  30.         for(int i = 1; i <= times ; i++){
  31.             String str = sc.nextLine();
  32.             int result = evaluate(str);
  33.             if (result == -1){
  34.                 System.out.println("Invalid line");
  35.             }else{
  36.                 intStack.add(result);
  37.             }
  38.            
  39.         }
  40.        
  41.         return intStack;
  42.     }
  43.    
  44.     public static ArrayList<Integer> method2(){
  45.         ArrayList<Integer> intStack = new ArrayList<Integer>();
  46.         Scanner sc = new Scanner(System.in);
  47.         String str = sc.nextLine();
  48.        
  49.         while(!str.equals("0")){
  50.             int result = evaluate(str);
  51.             if (result == -1){
  52.                 System.out.println("Invalid line");
  53.             }else{
  54.                 intStack.add(result);
  55.             }
  56.            
  57.             str = sc.nextLine();
  58.         }
  59.        
  60.         return intStack;
  61.     }
  62.    
  63.     public static  ArrayList<Integer> method3(){
  64.         ArrayList<Integer> intStack = new ArrayList<Integer>();
  65.         Scanner sc = new Scanner(System.in);
  66.        
  67.         while (sc.hasNextLine()){
  68.             String str = sc.nextLine();
  69.             if (str.equals("")){
  70.                 break;
  71.             }else{
  72.                 int result = evaluate(str);
  73.             if (result == -1){
  74.                 System.out.println("Invalid line");
  75.             }else{
  76.                 intStack.add(result);
  77.             }
  78.            
  79.             str = sc.nextLine();
  80.             }
  81.            
  82.         }
  83.        
  84.         return intStack;
  85.        
  86.     }
  87.    
  88.     public static int evaluate(String line){
  89.         if ( line.indexOf("AND") == 0){
  90.             int bit1 = line.charAt(4) - '0';
  91.             int bit2 = line.charAt(6) - '0';
  92.            
  93.             return bit1 == 1 && bit1 == bit2 ? 1 : 0;
  94.            
  95.         }else if(line.indexOf("OR") == 0){
  96.            
  97.             int bit1 = line.charAt(3) - '0';
  98.             int bit2 = line.charAt(5) - '0';
  99.            
  100.             return bit1 == 1 || bit2 == 1 ? 1 : 0;
  101.            
  102.         }else{
  103.             return -1;
  104.         }
  105.     }
  106.    
  107.     public static void displayResult(ArrayList<Integer> intStack){
  108.         for(Integer i : intStack){
  109.             System.out.println(i);
  110.         }
  111.     }
  112.    
  113.    
  114.    
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement