c注释风格转化到c++注释风格

来源:互联网 发布:剑灵秦捏脸数据 编辑:程序博客网 时间:2024/05/19 02:43


input.c

// 1.一般情况int num = 0;/* 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*/

CommentConvert.h

#pragma once#include <stdio.h>#include <stdlib.h>typedef enum Convert_State//定义状态{NULL_STATE,C_STATE,CPP_STATE,END_STATE}StateType;void CommentConvert();void DoConvertWork(FILE *read, FILE *write);void Null_Convert(FILE *read, FILE *write);void C_Convert(FILE *read, FILE *write);void Cpp_Convert(FILE *read, FILE *write);

CommentConvert.c

#define _CRT_SECURE_NO_WARNINGS 1#include "CommentConvert.h"StateType state;//state定义为全局变量void CommentConvert(){FILE *pwrite = NULL;FILE *pread = fopen("input.c", "r");if(pread == NULL){perror("open file for read");exit(EXIT_FAILURE);}pwrite = fopen("output.c", "w");if(pwrite == NULL){fclose(pread);perror("open file for write");exit(EXIT_FAILURE);}DoConvertWork(pread, pwrite);fclose(pread);fclose(pwrite);}void DoConvertWork(FILE *read, FILE *write){state = NULL_STATE;//初值赋为无状态while(state != END_STATE){switch(state){case NULL_STATE:Null_Convert(read, write);break;case C_STATE:C_Convert(read, write);break;case CPP_STATE:Cpp_Convert(read, write);break;default:break;}}}void Null_Convert(FILE *read, FILE *write){int first = fgetc(read);int second = 0;switch(first){case '/':{second = fgetc(read);if(second == '*'){fputc(first, write);fputc('/', write);state = C_STATE;}else if(second == '/'){fputc(first, write);fputc(second, write);state = CPP_STATE;}else{fputc(first, write);fputc(second, write);}}break;case EOF:fputc(first, write);state = END_STATE;break;default:fputc(first, write);break;}}void C_Convert(FILE *read, FILE *write){int first = fgetc(read);int second = 0;int third = 0;switch(first){case '\n':fputc('\n', write);fputc('/', write);fputc('/', write);break;case '*':{second = fgetc(read);if(second == '/'){third = fgetc(read);if(third == '\n'){fputc('\n', write);}else{fputc('\n', write);ungetc(third, read);//在c注释风格最后判断为非\n要}state = NULL_STATE;}else if(second == '*'){fputc(first, write);ungetc(second, read);//把读取到的'*'字符退回到输入流中}else{fputc(first, write);}}break;default:fputc(first, write);break;}}void Cpp_Convert(FILE *read, FILE *write){int first = fgetc(read);int second = 0;switch(first){case '\n':fputc(first, write);state = NULL_STATE;break;case EOF:fputc(first, write);state = END_STATE;break;default:fputc(first, write);break;}}

test.c

#include "CommentConvert.h"int main(){CommentConvert();system("pause");return 0;}

output.c

// 1.一般情况int num = 0;// int i = 0; // 2.换行问题// int i = 0; int j = 0;// int i = 0; int j = 0;// 3.匹配问题//int i = 0;/*xxxx// 4.多行注释问题////int i=0;//int j = 0;//int k = 0;//int k = 0;// 5.连续注释问题////// 6.连续的**/问题//*// 7.C++注释问题// /*xxxxxxxxxxxx*/

原创粉丝点击