Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. void run() {
  2. String inputData = Helper.getInputFromFile("input.c");
  3. inputData = Helper.removeQuotes(inputData);
  4. inputData = Helper.removeSinglelineComments(inputData);
  5. inputData = Helper.removeMultilineComments(inputData);
  6.  
  7. cyclo(inputData);
  8. }
  9.  
  10. void cyclo(String inputData) {
  11. String pattern = "(void|int|float|char|double)\s+(.*)\(.*?\)\s*\{";
  12. Pattern p = Pattern.compile(pattern);
  13. Matcher m = p.matcher(inputData);
  14. while (m.find()) {
  15. int openingIndex = m.end();
  16. int closingIndex = findMatching(inputData, openingIndex);
  17. int noDecisionPoints = findDecisionPoints(inputData.substring(openingIndex, closingIndex));
  18. System.out.println(m.group(2) + " " + (noDecisionPoints+1));
  19. }
  20. }
  21.  
  22. int findDecisionPoints(String data) {
  23. int count = 0;
  24. String pattern = "(if|while|for)\s*\(.*?\)";
  25. Pattern p = Pattern.compile(pattern);
  26. Matcher m = p.matcher(data);
  27. while (m.find()) {
  28. //System.out.println(m.group(0));
  29. count++;
  30. }
  31. return count;
  32. }
  33.  
  34. int findMatching(String inputData, int openingIndex) {
  35. int i = openingIndex;
  36. int count = 1;
  37. int matchingIndex = -1;
  38. while (true) {
  39. if (inputData.charAt(i) == '{') {
  40. count++;
  41. } else if (inputData.charAt(i) == '}') {
  42. count--;
  43. }
  44. if (count == 0) {
  45. matchingIndex = i;
  46. break;
  47. }
  48. i++;
  49. }
  50. return matchingIndex;
  51. }
  52.  
  53. public static void main(String[] args) {
  54. new Main().run();
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement