⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decomment.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>#include <fstream>#include <cstdlib>    // for exitusing namespace std;#include "prompt.h"// Owen Astrachan 7/4/1996, revised 5/4/99// state-machine approach for removing all // comments from a file// (doesn't handle // in a string, e.g., " test // comment "class Decomment{  public:    Decomment();    void Transform(istream& input, ostream& output);      private:    void Echo(char ch, ostream& output);        const char SLASH;    const char NEWLINE;        enum ReadState{TEXT, FIRST_SLASH, COMMENT};};Decomment::Decomment() : SLASH('/'),   NEWLINE('\n'){    // constants initialized}void Decomment::Echo(char ch, ostream& output){    output << ch;}void Decomment::Transform(istream& input, ostream& output){    char ch;    ReadState currentState = TEXT;    while (input.get(ch))              // read one char at a time    {   switch(currentState)        {          case TEXT:            if (ch == SLASH)           // potential comment begins            {   currentState = FIRST_SLASH;            }            else            {   Echo(ch,output);            }            break;                      case FIRST_SLASH:            if (ch == SLASH)            {   currentState = COMMENT;            }            else                      // one slash not followed by another            {   Echo(SLASH,output);   // print the slash from last time                Echo(ch,output);      // and the current character                currentState = TEXT;  // reading uncommented text            }            break;          case COMMENT:            if (ch == NEWLINE)          // end-of-line is end of comment            {   Echo(NEWLINE,output);   // be sure to echo end of line                currentState = TEXT;            }	        break;        }	 }}int main(){      string filename = PromptString("enter filename: ");    ifstream input(filename.c_str());    if (input.fail())    {   cout << "could not open " << filename << " for reading" << endl;        exit(1);    }    Decomment dc;    dc.Transform(input,cout);        return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -