(C)【项目】注释转换

项目 : 注释转换

  • 项目内容:将C语言注释(/* */)转换成为CPP注释(//)
  • 项目难点:在C语言注释换行操作时,要将换行后的内容也用CPP注释“//”标记,以及在C语言注释之后会有同一行的其他内容不需要注释,但在CPP注释中一注释就是一整行,所以需要换行

这里写图片描述

以下是代码区:

//comment_convert.h
//头文件
#pragma once

#define INPUT "input.c" 
#define OUTPUT "output.c"

enum STA
{
    NULLSTATUS,
    CPPSTATUS,
    CSTATUS,
    EOFSTATUS,
}status;//定义枚举类型,表示四种不同的状态

void comment_convert_main(FILE* ipf,FILE* opf);
//转换实现文件
//comment_convert.c

#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
#include <stdio.h>
#include "comment_convert.h"

enum STA status = NULLSTATUS;  //预设代码为平常状态

void do_full_status(FILE* ipf,FILE* opf)
{
    int f = fgetc(ipf);

    switch(f)
    {
    case '/':
        {
            int s = fgetc(ipf);
            switch(s)
            {
            case '*':
                fputc('/',opf);
                fputc('/',opf);
                status = CSTATUS;
                break;
            case '/':
                fputc('/',opf);
                fputc('/',opf);
                status = CPPSTATUS;
                break;
            case EOF:
                fputc(f,opf);
                status = EOFSTATUS;
                break;
            default:
                fputc(f,opf);
                ungetc(s,ipf);//不是‘*’‘/’和EOF就是其他字符,因为不能判断字符内容,所以不get,返回给"ipt"
                status = NULLSTATUS;
                break;
            }
        }
        break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(f,opf);
        status = NULLSTATUS;
        break;
    }
    return ;
}
//正常状态:遇到'/',可能是 “//”“/*   */”“a/b”,所以进行switch选择,来判断不同的状态

void do_c_status(FILE* ipf,FILE* opf)
{
    int f = fgetc(ipf);
    switch(f)
    {
    case'\n':
        fputc(f,opf);
        fputc('/',opf);
        fputc('/',opf);
        status = CSTATUS;
        break;
    case'*':
        {
            int s = fgetc(ipf);
            switch(s)
            {
            case'/'://防止同一行没有注释的内容,被注释掉,需要换行
                {
                    int n = fgetc(ipf);
                    if (n != '\n')
                    {
                        fputc('\n',opf);
                        ungetc(n,ipf);
                    }
                    else 
                        fputc('\n',opf);
                }
                status = NULLSTATUS;
                break;
            case EOF:
                status = EOFSTATUS;
                break;
            default:
                ungetc(s,ipf);
                fputc(f,opf);
                break;
            }
        }
        break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(f,opf);
        status = CSTATUS;
        break;
    }
    return ;
}

void do_cpp_status(FILE* ipf,FILE* opf)
{
    int f = fgetc(ipf);
    switch(f)
    {
    case'\n'://换行即为CPP注释结束
        fputc(f,opf);
        status = NULLSTATUS;
        break;
    case EOF:
        status = EOFSTATUS;
        break;
    default:
        fputc(f,opf);
        status = CPPSTATUS;
        break;
    }
    return ;
}


void comment_convert_main(FILE* ipf,FILE* opf)
{
    while(status != EOFSTATUS) //只要文件没结束一直进行判断和转换  
    {
        switch(status)
        {
        case NULLSTATUS:
            do_full_status(ipf,opf);
            break;
        case CSTATUS:
            do_c_status(ipf,opf);
            break;
        case CPPSTATUS:
            do_cpp_status(ipf,opf);
            break;
        case EOFSTATUS:
            status = EOFSTATUS;
            break;
        default:
            break;
        }
    }
    return ;
}
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>  
#include <stdlib.h>  
#include <Windows.h>  
//主函数文件
//test.c
#include "comment_convert.h"


int main ()
{ 
    FILE* ipf = fopen(INPUT,"r");
    FILE* opf = fopen(OUTPUT,"w");
    if (ipf == NULL || opf == NULL)  
    {  
        perror("Error opening file");  
        exit(1);  

    }  
    else
        comment_convert_main(ipf,opf);

    fclose(ipf);
    fclose(opf);

    return 0;
}
//测试代码
//input.c 



// this is cpp comment
/* int i = 0; */

/* int j = 10 */int k = 3;

int n = 20;

/*
int i = 0;
int j = 20;
int k = 250;
*/int q = 9527;

/***/

/* int z = 7748258; */ /*int b=94250;*/

// /*dsklfjdasl;fdsf;ldsfds*/
//测试结束代码
//output.c


// this is cpp comment
// int i = 0; 

// int j = 10 
int k = 3;

int n = 20;

//
//int i = 0;
//int j = 20;
//int k = 250;
//
int q = 9527;

//*

// int z = 7748258; 
 //int b=94250;

// /*dsklfjdasl;fdsf;ldsfds*/

猜你喜欢

转载自blog.csdn.net/giraffe_255/article/details/78709812