c语言注释转换项目

来源:互联网 发布:数据挖掘就业方向 编辑:程序博客网 时间:2024/06/09 03:02

Annootation conversion.h

#ifndef __ANNONTATION_CONVERSION_H__#define __ANNONTATION_CONVERSION_H__#define INPUTFILE "input.c"#define OUTPUTFILE "output.c" void Annotation_conversion(FILE *ifp, FILE *ofp);enum{    COMMONSTATE, //普通状态     CSTATE,          //c风格状态    CPPSTATE,        //c++风格状态    EOFSTATE,        //文件结束状态};#endif//__ANNONTATION_CONVERSION_H__

Annotation conversion.c

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h>#include"Annotation conversion.h"int state = COMMONSTATE;//普通状态void common_state(FILE *ifp,FILE *ofp)     {    int ch = fgetc(ifp);    switch(ch)    {        case '/':            {                int a = fgetc(ifp);                switch(a)                {                    case '*':                        fputc('/',ofp);                        fputc('/',ofp);                        state = CSTATE;                        break;                    case '/':                        fputc('/',ofp);                        fputc('/',ofp);                        state = CPPSTATE;                        break;                    case EOF:                        fputc(ch,ofp);  //                        state = EOFSTATE;                        break;                    default :                        fputc(ch,ofp);                        //fputc(a,ofp);   //                        ungetc(a,ifp);  //                        state = COMMONSTATE;                        break;                                      }            }            break;        case EOF:            state = EOFSTATE;            break;        default:            fputc(ch,ofp);            state = COMMONSTATE;            break;    }}//c风格状态void c_state(FILE *ifp,FILE *ofp){    int ch = fgetc(ifp);    switch(ch)    {        case '*':            {                int a = fgetc(ifp);                switch(a)                {                    case '/':                        //                        fputc('\n',ofp);                        state = COMMONSTATE;                        break;                    case EOF:                        fputc(ch,ofp);  //                        state = EOFSTATE;                        break;                    default:                        fputc(ch,ofp); //                        fputc(a,ofp);  //                        ungetc(a,ifp); //                        state = CSTATE;                        break;                }            }            break;        case '\n':            fputc('\n',ofp);            fputc('/',ofp);            fputc('/',ofp);            state = CSTATE; //            break;        case EOF:            state = EOFSTATE;            break;        //        default:            fputc(ch,ofp);            state = CSTATE;            break;    }}//c++风格状态void cpp_state(FILE *ifp,FILE *ofp){    int ch = fgetc(ifp);    switch(ch)    {        case EOF:            state = EOFSTATE;            break;   //        case '\n':            fputc('\n',ofp);            state = COMMONSTATE;            break;  //        default:            fputc(ch,ofp);            state = CPPSTATE;            break;    }}int eof_state(FILE *ifp, FILE *ofp){    return 0;}//注释转换void Annotation_conversion(FILE *ifp, FILE *ofp){    while(state != EOFSTATE)    {        switch(state)        {            case COMMONSTATE:                common_state(ifp,ofp);                break;            case CSTATE:                c_state(ifp,ofp);                break;            case CPPSTATE:                cpp_state(ifp,ofp);                break;            case EOFSTATE:                eof_state(ifp,ofp);                break;            default:                break;        }    }}

test.c

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h>#include"Annotation conversion.h"int main(){    FILE *ifp = fopen(INPUTFILE,"r");       FILE *ofp = fopen(OUTPUTFILE,"w");    if((ifp == NULL) || (ofp == NULL))    {        printf("不能打开文件\n");        return -1;    }    Annotation_conversion(ifp,ofp);    fclose(ifp);    fclose(ofp);    return 0;}

input.c

int main(){    int c = 20; /* 创建c变量*/    char ch = 'a'; //创建ch    return 0;}//1.一般情况 int num = 0;/* int i = 0;*///2.换行问题/* int i = 0;*/ int j = 0;/*int i = 0;*/int z = 0;//3.匹配问题/*int i = 0;/*hehehehehehe*///4.多行注释问题/*int i = 0;int j = 0;int k = 0;*/int v = 0;//5.连续注释问题/*int a = 0;*//*int b = 0;*///6.连续的***/问题/****///7.c++注释问题// /*哈哈哈哈*/

这里写图片描述
这里写图片描述