最近做了一個(gè)注釋轉(zhuǎn)換的項(xiàng)目,將C語(yǔ)言注釋轉(zhuǎn)化為C++格式。
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,是專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
也許你剛看到這個(gè)題目不知道如何下手,那么恭喜你看了我的博客,我一張簡(jiǎn)單的圖就能告訴你怎樣簡(jiǎn)單處理,看下圖。
注釋轉(zhuǎn)換的問(wèn)題
1.一般問(wèn)題
/*int a = 10; */
2.換行問(wèn)題
/*int a = 10; */int j = 0;
/*int a = 10; */
int j = 0
3.匹配問(wèn)題
/*int i = 10;/* XXXX*/
4.多行注釋問(wèn)題
/*
int i = 0;
int j = 0;
int k = 0;
*/int f = 0;
5.連續(xù)注釋
/**//**/
6.連續(xù)的**/問(wèn)題
/***/
7.c++注釋問(wèn)題
// /*aaaa*/
如果你還是不明白,就往下看我的代碼,會(huì)簡(jiǎn)單分析怎么轉(zhuǎn)化的。我這里用的是文件的方式寫的
頭文件“CommentConvert.h”
#ifndef __COMMENTCONVERT_H__ #define __COMMENTCONVERT_H__ #include <stdio.h> #include <stdlib.h> #define INTFILE "intfile.c" #define OUTFILE "outfile.c" enum STATE //用枚舉定義讀取文本現(xiàn)在的狀態(tài) { NUL_STATE, //無(wú)狀態(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);//將多讀的一個(gè)字符還給緩沖區(qū),處理/****/的注釋問(wèn)題 //third = fgetc(pfRead); //if (third == '/' && second == '*') //{ // fputc('\n',pfWrite); // state = NUL_STATE; //} //else //{ // fputc(second,pfWrite); // fputc(third,pfWrite); //} } break; //多行注釋問(wèn)題 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) { }
測(cè)試文件“test.c”
#include "CommentConvert.h" int main() { FILE *pfRead = NULL; FILE *pfWrite = NULL; printf("轉(zhuǎn)換開(kāi)始\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("轉(zhuǎn)換結(jié)束\n"); system("pause"); return 0; }
分享標(biāo)題:一幅圖告訴你"C->C++"注釋轉(zhuǎn)換有多簡(jiǎn)單
URL地址:http://jinyejixie.com/article10/gpgcgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、App設(shè)計(jì)、搜索引擎優(yōu)化、云服務(wù)器、網(wǎng)站策劃、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)