Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ex0b.cc
- *
- * Created on: Mar 20, 2018
- * Author: mohammedja
- */
- /*File: ex0b.cc
- ex0b : programmer comments remover
- //===========================================================================//
- Written by: Ameer mohammed Jamal, id = 313331878, login = mohammedja
- this program takes in a supposed "code" and removes all
- the comments from it including comments such as those
- that start with // or these that are / star star /
- Input: file with text
- Output: commentless code/text
- **/
- //===========================================================================//
- #include <iostream>
- #include <fstream>
- #include <cctype>
- #include <cstring>
- #include <cstdlib>
- #include <iomanip>
- #include <string>
- #include <stdio.h>
- //===========================================================================//
- // includes
- using std::cin;
- using std::cout;
- using std::setw;
- using std::ifstream;
- using std::ofstream;
- using std::getline;
- //===========================================================================//
- //constants
- const int MAX_STR_LEN = 200;
- //===========================================================================//
- //prototypes
- void filter_comment(char str[MAX_STR_LEN],int &counter_log,
- int &counter_comment);
- //===========================================================================//
- /*
- * the main functions run over every line from the input and then
- * it takes the line and puts it through filter which clears it out
- * and returns it .
- */
- int main()
- {
- int counter_log=0; // counter for star type comment
- int counter_comment=0; //counter for double slash comment
- char str[MAX_STR_LEN];
- while ( !cin.eof() ) //run on it till its end of file
- {
- cin.getline(str,MAX_STR_LEN); //take the line
- filter_comment(str,counter_log,counter_comment);
- if(!cin.eof()) //is it end of the file?
- {
- if((!counter_log)&&(counter_comment==0)) //print the /n
- {
- cout<<std::endl;
- }
- else if ( counter_comment==1)//ignoring the /n
- {
- counter_comment=0;
- }
- }
- }
- //the error message
- if(counter_log!=0)
- {
- std::cerr << "Error in input. Program ended in a comment. depth = "
- <<counter_log<<std::endl;
- }
- }
- //===========================================================================//
- /*
- * this function checks when the double slash or star type comment is
- * and ignores everything behind it for printing and untill it stomps
- * till the end of it
- * in case of double slash ignore the whole line including /n
- * in case of star type comment wait till the closer but keep counting
- * inner ones
- */
- void filter_comment(char str[],int &counter_log, int &counter_comment)
- {
- int size =(int)strlen(str);
- for(int i =0; i<size;i++) //run on the line size
- {
- //avoid bad type case /*/ and */* are includes in both
- //first and second condition
- if((str[i]=='/')&&(str[i+1]=='*')&&(str[i-1]!='*'))
- {
- counter_log++;
- }
- else if((str[i-1]=='*')&&(str[i]=='/')&&(str[i-2]!='/'))
- {
- counter_log--;
- }
- //break if type double slash
- else if((str[i]=='/')&&(str[i+1]=='/'))
- {
- counter_comment=1;
- break;
- }
- else if(counter_log==0)
- {
- cout<<str[i];
- }
- }
- }
- //===========================================================================//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement