C/C++小项目之注释转换
2017-12-05 19:29
295 查看
描述:打开一个文档,将里面所有用C注释的改成C++注释。
思路:设置一个状态机,分为四种状态,每次从文件里读到一个字符之后判断是何种状态,在进行对应的处理就好了。
图示:
CommentConvert.h
CommentConvert.cpp
Test.cpp
创建测试文件input.cpp
输出文件out.cpp
思路:设置一个状态机,分为四种状态,每次从文件里读到一个字符之后判断是何种状态,在进行对应的处理就好了。
图示:
CommentConvert.h
# pragma once #include<iostream> using namespace std; void _StartConvert(FILE* fin, FILE* fout); void OnNullState(FILE* fin, FILE* fout); void OnCState(FILE* fin, FILE* fout); void OnCppState(FILE* fin, FILE* fout);
CommentConvert.cpp
#include<iostream> #include <assert.h> #include"CommentConvert.h" using namespace std; enum State { NULL_STATE,//空状态 C_STATE,//C状态 CPP_STATE,//C++状态 END_STATE,//结束 }; enum State state = NULL_STATE; void OnNullState(FILE* fin, FILE* fout) // 找/* // -> { char first = fgetc(fin); if (first == '/') { char second = fgetc(fin); if (second == '*') { state = C_STATE; } else if (second = '/') { state = CPP_STATE; } fputs("//", fout); } else if (first == EOF)//如果遇到EOF,则文件结束, { fputc(first, fout); //写入读到的字符 state = END_STATE;//并将此时的状态值为结束状态 } else { fputc(first, fout); } } void OnCState(FILE* fin, FILE* fout) // 找*/ -> NULL_STATE { char first = getc(fin); if (first == '*') { char second = getc(fin); if (second == '/') //读取第二个字符进行判断 { char third = fgetc(fin); state = NULL_STATE; if (third != '\n') //再读取第三个字符 { fputc('\n', fout); ungetc(third, fin);//由于无法知道下一个字符是什么,因此只能将次字符退回 } else { fputc(third, fout); } } else { fputc(first, fout); ungetc(second, fin); } } else if (first == '\n') { fputc(first, fout); fputs("//", fout); } else if (first == EOF) { fputc(first, fout); state = END_STATE; } else { fputc(first, fout); } } void OnCppState(FILE* fin, FILE* fout) // 找 \n -> NULL_STATE { char first = fgetc(fin); if (first == '\n') { fputc(first, fout); state = NULL_STATE; } else if (first == EOF) { fputc(first, fout); state = END_STATE; } else { fputc(first, fout); } } void _StartConvert(FILE* fin, FILE* fout) { while (state != END_STATE) { switch (state) { case NULL_STATE: OnNullState(fin, fout); break; case C_STATE: OnCState(fin, fout); break; case CPP_STATE: OnCppState(fin, fout); break; case END_STATE: break; default: assert(false); break; } } }
Test.cpp
#include<iostream> #include<assert.h> #include"CommentConvert.h" using namespace std; int main() { //以读的方式打开一个文件名为input.cpp的文件 FILE *pRead = fopen("input.cpp", "r"); if (pRead == NULL) { perror("open file for read"); exit(EXIT_FAILURE); 9fa5 } //写入新创建的"output.c"文件; FILE *pWrite = fopen("output.cpp", "w"); if (pWrite == NULL) { fclose(pRead); perror("open file for write"); exit(EXIT_FAILURE); } //调用操作选项函数; _StartConvert(pRead, pWrite); //关闭已经打开的文件; fclose(pRead); fclose(pWrite); return 0; }
创建测试文件input.cpp
// 1.一般情况 /* int i = 0; */ // 2.换行问题 /* int i = 0; */int j = 0; /* int i = 0; */ //int j = 0; // 3.匹配问题 /*int i = 0;/*xxxxx*/ // 4.多行注释问题 /* int i=0; int j = 0; int k = 0; */int k = 0; // 5.连续注释问题 /**//**/ // 6.连续的**/问题 /***/ // 7.C++注释问题 // /*xxxxxxxxxxxx*/
输出文件out.cpp
// 1.一般情况 // int i = 0; // 2.换行问题 // int i = 0; int j = 0; // int i = 0; //int j = 0; // 3.匹配问题 //int i = 0;/*xxxxx // 4.多行注释问题 // //int i=0; //int j = 0; //int k = 0; // int k = 0; // 5.连续注释问题 // // // 6.连续的**/问题 //* // 7.C++注释问题 // /*xxxxxxxxxxxx*/
OVER
相关文章推荐
- 注释转换(C++小项目)
- C语言小项目—注释转换( C注释转换为C++注释 )
- c语言项目—注释转换(c——c++)
- c语言项目 注释转换 (c———c++)
- VS2008 C++ 项目怎样添加“依赖”、“库目录”和“包含目录”
- C++第14周项目模板【项目2-两个成员的类模板】 !!!!!!!
- C++小项目-本校科协管理系统
- C++项目中的extern "C" {}
- C++第9周(春)项目5 - 一元一次方程类
- 2014秋C++第10周项目6参考-贪财的富翁
- C++实验5-项目1-数组分离
- 第一周项目 C/C++语言中函数参数传递的三种方式
- 第二周项目1-c/c++语言中函数参数传递的三种方式
- C++实验4-项目7穷举法解决组合问题-百钱百鸡问题
- 值得学习的C/C++语言开源项目,以及好多资源整理
- c++实验4-项目6
- UE4新手编程之创建C++项目
- 注释转换 (将C注释转换为C++注释)
- C++开源实用项目之我见(待续20100517修改)
- 怎样去平衡C++项目的设计?