天天看點

注釋轉換小項目(c注釋->到c++注釋)

首先歡迎閱讀本文,注釋轉換小項目涉及到c語言對檔案的操作,另外這個小項目還應該對各種情況都考慮到。将c語言注釋轉換為c++注釋

經過分析思考,我得到證據要的注釋轉換有以下幾類:我将之整理并放入一個檔案裡,起名input.c

//1.一般情況
/* int i=0;*/

//2.換行問題
/* int i=0;*/int j = 0;
/* int i=0 */
int j = 0;

//3.比對問題
/* int i=0;/*xxxxxxx*/

//4.多行注釋
/*
int i=0;
int j=0
int k=0;
*/int f = 0;

//5.連續注釋問題
/**//**/

//6.連續的**/問題
/***/

//7.C++注釋問題
//  /*xxxxxxxxxxxxxxx*/      

通過C語言代碼對其進行注釋轉換,并輸出到另一個檔案裡,我起名為output.c,

之是以起名字尾為.c 是為了打開友善,當然也可起名字尾為.txt,.doc等等

程式代碼如下:

頭檔案(函數定義):AnnotationConversion.h

#pragma once

#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<errno.h>

typedef enum Srate
{
	C_BEGIN,
	C_END,
}Srate;

void Convert(FILE *fIn, FILE *fOut);
void AnnotationConversion(const char* inputFile,
											const char* outputFile);      
#include"AnnotationConversion.h"

void Convert(FILE *fIn, FILE *fOut)  //打開檔案
{
	char first, second;
	Srate tag = C_END;
	assert(fIn);
	assert(fOut);
	do
	{
		first = fgetc(fIn);
		switch (first)
		{
		case '/':
			second = fgetc(fIn);
			if ('*' == second)
			{
				//3.比對問題
				if (tag == C_END)
				{
					fputc('/', fOut);
					fputc('/', fOut);
					tag = C_BEGIN;
				}
				else
				{
					fputc('/', fOut);
					fputc('*', fOut);
				}
			}
			else if ('/' == second)    //C++注釋問題
			{
				char next;
				fputc('/', fOut);
				fputc('/', fOut);
				do
				{
					next = fgetc(fIn);
					if (EOF == next)
					{
						break;
					}
					else
						fputc(next, fOut);
				} while (('\n' != next) && (EOF != next));

			}
			else
			{
				fputc(first, fOut);
				fputc(second, fOut);
			}
			break;
		case'\n':
			fputc('\n', fOut);
			if (tag == C_BEGIN)  	//4.多行注釋問題
			{
				fputc('/', fOut);
				fputc('/', fOut);
			}
			break;
		case '*':
			second = fgetc(fIn);
			if (second == '/')
			{
				// 2.換行問題
				char next = fgetc(fIn);
				if (EOF == next)
				{
					fseek(fIn, -1, SEEK_CUR);
				}
				//5.連續注釋問題
				else if ('/' == next)
				{
					fputc('\n', fOut);
					fseek(fIn, -1, SEEK_CUR);
				}
				else if (('\n' != next) && (EOF != next))
				{
					fputc('\n', fOut);
					fputc(next, fOut);
				}
				else
					fputc('\n', fOut);
				tag = C_END;
			}
			else if ('*' == second)     //6.連續的**/問題
			{
				fputc(first, fOut);
				fseek(fIn, -1, SEEK_CUR);
			}
			else
			{
				fputc(first, fOut);
				fputc(second, fOut);
			}
			break;
		default:
			if (EOF == first)
				break;
			fputc(first, fOut);
			break;
		}
	} while (first != EOF);

}

void AnnotationConversion(const char* inputFile,
	const char* outputFile)
{
	FILE * fOut, *fIn;
	fIn = fopen(inputFile, "r");
	if (fIn == NULL)
	{
		printf("打開檔案%s失敗!errno:%d\n", inputFile, errno);
		return;
	}
	fOut = fopen(outputFile, "w");
	if (fOut == NULL)
	{
		fclose(fIn);
		printf("打開檔案%s失敗!errno:%d\n", inputFile, errno);
		return;
	}
	Convert(fIn, fOut);
	fclose(fIn);
	fclose(fOut);
}

int main()
{
	AnnotationConversion("input.c","output.c");
	system("pause");
	return 0;
}      
//1.一般情況
// int i=0;

//2.換行問題
// int i=0;
int j = 0;
// int i=0 
int j = 0;

//3.比對問題
// int i=0;/*xxxxxxx

//4.多行注釋
//
//int i=0;
//int j=0
//int k=0;
//
int f = 0;

//5.連續注釋問題
//
//

//6.連續的**/問題
//*

//7.C++注釋問題
//  /*xxxxxxxxxxxxxxx*/      

繼續閱讀