小程序-去除单行注释
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小程序-去除单行注释相关的知识,希望对你有一定的参考价值。
数据结构的文档一行代码要附上两三行注释呢……比如这样:
T& back(); // return the element at the rear of the vector. // Precondition: the vector is not empty. if vector // is empty, throws the underflowError exception
讲道理作为注释它有帮助的……不过读起来不方便,在清楚函数的含义的时候可以考虑清除它们。
小程序功能有限,将源代码复制到sorce.txt,会生成一个new.txt。
或许有一天会回来扩展呢:
图形化界面,打开任意文件,输出到剪贴板等。
#include <iostream> #include <fstream> #include <string> using namespace std; const char* filePath = "sorce.txt"; //本注释会被删除 bool isNote(const string &); //本注释不会被删除 int main(){ ifstream infile(filePath, ios::in); if(!infile){ cout << "转换失败,请将文件重命名为 sorce.txt" <<endl; } else{ ofstream outfile("new.txt", ios::out); string line; while(!infile.eof()){ getline(infile, line); if(!isNote(line)){ //这一行是注释行吗? outfile << line << endl; } } outfile.close(); cout << "转换成功!" << endl; } infile.close(); return 0; } bool isNote(const string & s){ for(int i = 0; i < s.size(); i++){ if(s[i] != ‘ ‘){ if(s[i] == ‘/‘){ return true; } else{ return false; } } } return false; }
以上是关于小程序-去除单行注释的主要内容,如果未能解决你的问题,请参考以下文章