溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C語言實(shí)現(xiàn)注釋轉(zhuǎn)換

發(fā)布時間:2020-08-11 03:56:13 來源:網(wǎng)絡(luò) 閱讀:400 作者:2013221 欄目:編程語言
  • 將C語言注釋轉(zhuǎn)換成C++注釋


#pragma once

#pragma warning (disable:4996)
typedef enum ConvertState
{
	SUCCESS,//轉(zhuǎn)換成功
	FILE_ERROR,//打開文件失敗
	NO_MATCH,//轉(zhuǎn)換失敗
} ConvertState;


//轉(zhuǎn)換的狀態(tài)
typedef enum State
{
	C_BEGIN,//C語言注釋開始
	C_END,//C語言注釋結(jié)束
	CPP_BEGIN,//C++注釋開始
	CPP_END,//C++注釋結(jié)束
}State;
ConvertState Convert(FILE *fin, FILE *fout)
{
	ConvertState  ret = SUCCESS;
	char first, second;
	State 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 == '/')
			{
				char next;
				fputc('/', fout);
				fputc('/', fout);
				do{
					next = fgetc(fin);
					fputc(next, fout);
					if (next == EOF)
						return ret;
				} while (next != '\n');
			}
			else
			{
				fputc(first, fout);
				fputc(second, fout);
			}
			break;
		case '\n':
			//4.多行注釋問題
			fputc('\n', fout);

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

	} while (first != EOF);
	if (tag != C_END)
	{
		ret = NO_MATCH;
	}
	return ret;
}
ConvertState AnnotationConvert(const char*inputfile, const char* outputfile)
{
	ConvertState ret;
	FILE* fout, *fin;
	fin = fopen(inputfile, "r");
	if (fin == NULL)
	{
		printf("打開文件%s失敗,errno:%d\n", inputfile, errno);
		return FILE_ERROR;
	}
	fout = fopen(outputfile, "w");
	if (fout == NULL)
	{
		fclose(fin);
		printf("打開文件%s失敗,errno:%d\n", outputfile, errno);
		return FILE_ERROR;
	}
	ret = Convert(fin, fout);
	fclose(fin);
	fclose(fout);
	return ret;
};

void test()
{
	char *inputfile = "C";
	char *outputfile = "C++";
	ConvertState flag=AnnotationConvert(inputfile, outputfile);
	if (flag == SUCCESS)
	{
		printf("轉(zhuǎn)換成功,請查看");
	}
	else if (flag == FILE_ERROR)
	{
		printf("打開文件失敗");
	}
	else
	{
		printf("轉(zhuǎn)換失敗=_=||");
	}
}



#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<errno.h>
#include"AnnotationConvert.h"

int main()
{
	test();
	system("pause");
	return 0;
}

運(yùn)行結(jié)果:


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI