C的小项目:注释转换

来源:互联网 发布:java生成二维数组 编辑:程序博客网 时间:2024/06/06 03:25

emmm。。。。。
第一次发博客,欢迎大家批评指点哈。

  1. 头文件:convert_comment.h
#ifndef __convert_comment__H__#define __convert_comment_H__#define INPUTFILE "input.c"#define OUTPUTFILE "output.c"enum {    CSTATUS,    //C注释状态    NULLSTATUS, //普通状态    CPPSTATUS,  //C++注释状态    EOFSTATUS   //结束状态};void convert_main();#endif
  1. 源文件,放函数
    convert_comment.c
#include <stdio.h>#include <stdlib.h>#include "convert_comment.h"int status = NULLSTATUS;void do_null_status(FILE *ifp, FILE *ofp){    int ch = fgetc(ifp);    switch (ch)    {    case '/':    {                int c = fgetc(ifp);                switch (c)                {                case '/':                    fputc(ch, ofp);                    fputc(c, ofp);                    status = CPPSTATUS;                    break;                case '*':                {                       fputc(ch, ofp);                    fputc('/', ofp);                    status = CSTATUS;                    break;                }                    break;                case EOF:status = EOFSTATUS;                    break;                default: status = NULLSTATUS;                    break;                }    }        break;    case '*':    {                int c = fgetc(ifp);                switch (c)                {                case '/':                    fputc('\n', ofp);                    break;                default:                    fputc(ch, ofp);                    ungetc(c, ofp);                    status = NULLSTATUS;                    break;                }    }        break;    case EOF:status = EOFSTATUS;        break;    default:         fputc(ch, ofp);        status = NULLSTATUS;        break;    }}void do_cpp_status(FILE *ifp, FILE *ofp){    int ch = fgetc(ifp);    switch (ch)    {    case '\n':status = NULLSTATUS;        break;    case EOF:status = EOFSTATUS;        break;    default:        fputc(ch, ofp);        status = NULLSTATUS;        break;    }}void do_c_status(FILE *ifp, FILE *ofp){    int ch = fgetc(ifp);    switch (ch)    {    case '*':    {                int c = fgetc(ifp);                switch (c)                {                case '/':                    fputc('\n', ofp);                    status = NULLSTATUS;                    break;                default:                            fputc(ch, ofp);                    fputc(c, ofp);                    status = CSTATUS;                    break;                }    }        break;    case EOF:        status = EOFSTATUS;        break;    case '\n':        fputc('\n', ofp);        fputc('/', ofp);        fputc('/', ofp);        break;    default:                fputc(ch, ofp);        status = CSTATUS;        break;    }}static void convert_work(FILE* ifp, FILE *ofp){    while (status != EOFSTATUS)    {        switch (status)        {        case NULLSTATUS:do_null_status(ifp, ofp);            break;        case CPPSTATUS:do_cpp_status(ifp, ofp);            break;        case CSTATUS:do_c_status(ifp, ofp);                break;        default:            break;        }    }}void convert_main(){    FILE * ifp = fopen(INPUTFILE, "r");    FILE * ofp = fopen(OUTPUTFILE,"w");    if (ifp == NULL || ofp == NULL)    {        printf("...............");        return;    }    convert_work(ifp, ofp);    fclose(ifp);    fclose(ofp);}

3.源文件,放主函数
test.c

#include <stdio.h>#include <Windows.h>#include "convert_comment.h"int main(){    convert_main();    system("pause");    return 0;}

3.进行测试
这里写图片描述

阅读全文
0 0
原创粉丝点击