Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. /*
  2. * ex0b.cc
  3. *
  4. * Created on: Mar 20, 2018
  5. * Author: mohammedja
  6. */
  7. /*File: ex0b.cc
  8. ex0b : programmer comments remover
  9. //===========================================================================//
  10. Written by: Ameer mohammed Jamal, id = 313331878, login = mohammedja
  11.  
  12. this program takes in a supposed "code" and removes all
  13. the comments from it including comments such as those
  14. that start with // or these that are / star star /
  15.  
  16. Input: file with text
  17. Output: commentless code/text
  18. **/
  19. //===========================================================================//
  20. #include <iostream>
  21. #include <fstream>
  22. #include <cctype>
  23. #include <cstring>
  24. #include <cstdlib>
  25. #include <iomanip>
  26. #include <string>
  27. #include <stdio.h>
  28. //===========================================================================//
  29. // includes
  30. using std::cin;
  31. using std::cout;
  32. using std::setw;
  33. using std::ifstream;
  34. using std::ofstream;
  35. using std::getline;
  36. //===========================================================================//
  37. //constants
  38. const int MAX_STR_LEN = 200;
  39. //===========================================================================//
  40. //prototypes
  41. void filter_comment(char str[MAX_STR_LEN],int &counter_log,
  42. int &counter_comment);
  43. //===========================================================================//
  44. /*
  45. * the main functions run over every line from the input and then
  46. * it takes the line and puts it through filter which clears it out
  47. * and returns it .
  48. */
  49. int main()
  50. {
  51.  
  52. int counter_log=0; // counter for star type comment
  53. int counter_comment=0; //counter for double slash comment
  54. char str[MAX_STR_LEN];
  55. while ( !cin.eof() ) //run on it till its end of file
  56. {
  57. cin.getline(str,MAX_STR_LEN); //take the line
  58. filter_comment(str,counter_log,counter_comment);
  59.  
  60. if(!cin.eof()) //is it end of the file?
  61. {
  62. if((!counter_log)&&(counter_comment==0)) //print the /n
  63. {
  64. cout<<std::endl;
  65. }
  66. else if ( counter_comment==1)//ignoring the /n
  67. {
  68. counter_comment=0;
  69. }
  70. }
  71. }
  72.  
  73.  
  74. //the error message
  75. if(counter_log!=0)
  76. {
  77. std::cerr << "Error in input. Program ended in a comment. depth = "
  78. <<counter_log<<std::endl;
  79. }
  80.  
  81. }
  82.  
  83. //===========================================================================//
  84. /*
  85. * this function checks when the double slash or star type comment is
  86. * and ignores everything behind it for printing and untill it stomps
  87. * till the end of it
  88. * in case of double slash ignore the whole line including /n
  89. * in case of star type comment wait till the closer but keep counting
  90. * inner ones
  91. */
  92.  
  93.  
  94. void filter_comment(char str[],int &counter_log, int &counter_comment)
  95. {
  96. int size =(int)strlen(str);
  97.  
  98. for(int i =0; i<size;i++) //run on the line size
  99. {
  100.  
  101. //avoid bad type case /*/ and */* are includes in both
  102. //first and second condition
  103. if((str[i]=='/')&&(str[i+1]=='*')&&(str[i-1]!='*'))
  104. {
  105. counter_log++;
  106. }
  107. else if((str[i-1]=='*')&&(str[i]=='/')&&(str[i-2]!='/'))
  108. {
  109. counter_log--;
  110. }
  111. //break if type double slash
  112. else if((str[i]=='/')&&(str[i+1]=='/'))
  113. {
  114. counter_comment=1;
  115. break;
  116. }
  117. else if(counter_log==0)
  118. {
  119. cout<<str[i];
  120. }
  121. }
  122. }
  123.  
  124. //===========================================================================//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement