您好,登錄后才能下訂單哦!
最近做了一個注釋轉換的項目,將C語言注釋轉化為C++格式。
也許你剛看到這個題目不知道如何下手,那么恭喜你看了我的博客,我一張簡單的圖就能告訴你怎樣簡單處理,看下圖。
注釋轉換的問題
1.一般問題
/*int a = 10; */
2.換行問題
/*int a = 10; */int j = 0;
/*int a = 10; */
int j = 0
3.匹配問題
/*int i = 10;/* XXXX*/
4.多行注釋問題
/*
int i = 0;
int j = 0;
int k = 0;
*/int f = 0;
5.連續(xù)注釋
/**//**/
6.連續(xù)的**/問題
/***/
7.c++注釋問題
// /*aaaa*/
如果你還是不明白,就往下看我的代碼,會簡單分析怎么轉化的。我這里用的是文件的方式寫的
頭文件“CommentConvert.h”
#ifndef __COMMENTCONVERT_H__ #define __COMMENTCONVERT_H__ #include <stdio.h> #include <stdlib.h> #define INTFILE "intfile.c" #define OUTFILE "outfile.c" enum STATE //用枚舉定義讀取文本現在的狀態(tài) { NUL_STATE, //無狀態(tài) C_STATE, //C狀態(tài) CPP_STATE, //C++狀態(tài) END_STATE //空文件狀態(tài) }; void CommentConvert(FILE* pfRead,FILE* pfWrite); void DO_NUL_STATE(FILE* pfRead,FILE* pfWrite); void DO_C_STATE(FILE* pfRead,FILE* pfWrite); void DO_CPP_STATE(FILE* pfRead,FILE* pfWrite); void DO_END_STATE(FILE* pfRead,FILE* pfWrite); #endif//__COMMENTCONVERT_H__
注釋文件“CommentConvert.c”
#define _CRT_SECURE_NO_WARNINGS 1 #include "CommentConvert.h" enum STATE state = NUL_STATE; void CommentConvert(FILE* pfRead,FILE* pfWrite) { while (state != END_STATE) { switch(state) { case NUL_STATE: DO_NUL_STATE(pfRead,pfWrite); break; case C_STATE: DO_C_STATE(pfRead,pfWrite); break; case CPP_STATE: DO_CPP_STATE(pfRead,pfWrite); break; case END_STATE: DO_END_STATE(pfRead,pfWrite); break; } } } void DO_NUL_STATE(FILE* pfRead,FILE* pfWrite) { char first = 0; char second = 0; first = fgetc(pfRead); switch(first) { case '/': second = fgetc(pfRead); if (second == '*') { fputc('/',pfWrite); fputc('/',pfWrite); state = C_STATE; } else if(second == '/') { fputc(first,pfWrite); fputc(second,pfWrite); state = CPP_STATE; } else { fputc(first,pfWrite); fputc(second,pfWrite); } break; case EOF: state = END_STATE; break; default: fputc(first,pfWrite); break; } } void DO_C_STATE(FILE* pfRead,FILE* pfWrite) { char first = 0; char second = 0; char third = 0; first = fgetc(pfRead); switch(first) { case '*': second = fgetc(pfRead); if (second == '/') { //fputc('\n',pfWrite); //欠缺考慮 third = fgetc(pfRead); if (third == '\n') { fputc(third,pfWrite); } else { fputc('\n',pfWrite); ungetc(third,pfRead); } state = NUL_STATE; } else { fputc(first,pfWrite); ungetc(second,pfRead);//將多讀的一個字符還給緩沖區(qū),處理/****/的注釋問題 //third = fgetc(pfRead); //if (third == '/' && second == '*') //{ // fputc('\n',pfWrite); // state = NUL_STATE; //} //else //{ // fputc(second,pfWrite); // fputc(third,pfWrite); //} } break; //多行注釋問題 case '\n': fputc(first,pfWrite); fputc('/',pfWrite); fputc('/',pfWrite); break; case EOF: state = END_STATE; break; default: fputc(first,pfWrite); break; } } void DO_CPP_STATE(FILE* pfRead,FILE* pfWrite) { char first = 0; char second = 0; first = fgetc(pfRead); switch(first) { case '\n': fputc(first,pfWrite); state = NUL_STATE; break; case EOF: state = END_STATE; break; default: fputc(first,pfWrite); break; } } void DO_END_STATE(FILE* pfRead,FILE* pfWrite) { }
測試文件“test.c”
#include "CommentConvert.h" int main() { FILE *pfRead = NULL; FILE *pfWrite = NULL; printf("轉換開始\n"); pfRead = fopen(INTFILE,"r"); if (NULL == pfRead) { perror("open file for read\n"); exit(EXIT_FAILURE); } pfWrite = fopen(OUTFILE,"w"); if(NULL == OUTFILE) { fclose(pfRead); perror("open file for write\n"); exit(EXIT_FAILURE); } CommentConvert(pfRead,pfWrite); printf("轉換結束\n"); system("pause"); return 0; }
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。