Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class PB {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner io = new Scanner(new File("pe.txt"));
- Stack<Character> st = new Stack<Character>();
- int comment1 = 0;
- int comment2 = 0;
- while(io.hasNextLine())
- {
- String line = io.nextLine();
- for(int i=0;i<line.length();i++)
- {
- char p = line.charAt(i);
- String p2 = (i==line.length()-1)? line.charAt(i)+"" : line.charAt(i)+""+line.charAt(i+1)+"" ;
- if(st.isEmpty()==true&&p2.equals("/*"))
- st.push('*');
- else if(!st.isEmpty()&&p2.equals("*/"))
- if(st.peek()=='*'&&p2.equals("*/")){
- st.pop();
- comment2++;
- }
- else if(st.isEmpty()==true&&p2.equals("//"))
- st.push('/');
- else if(!st.isEmpty()&&i==(line.length()-1))
- if(st.peek()=='/'&&i==(line.length()-1))
- {
- st.pop();
- comment1++;
- }
- else if(st.isEmpty()==true && p=='"')
- st.push('"');
- else if(!st.isEmpty()&&p=='"')
- if(st.peek()=='"'&&p=='"')
- st.pop();
- else if(st.isEmpty()==true && p=='\'')
- st.push('\'');
- else if(!st.isEmpty()&&p=='\'')
- if( st.peek()=='\''&&p=='\'')
- st.pop();
- }
- }
- System.out.println(comment1);
- System.out.println(comment2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement