码迷,mamicode.com
首页 > 其他好文 > 详细

注释转换

时间:2016-05-01 01:17:14      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:注释转换

在我们写代码是有时要注释,所以很多时候,我们都是ctrl+k+c.既然常常用到,我们不妨写写看看它是怎么转换的。一方面提高编程能力,另一方面,也可以提高自身的的思考和处理问题的能力。

 首先,我们来思考注释转换一个有多少问题。

 1.一般情况

/* int i = 0; */

// 2.换行问题

/* int i = 0; */int j = 0;

/* int i = 0; */

int j = 0;

// 3.匹配问题

/*int i = 0;/*xxxxx*/

// 4.多行注释问题

/*

int i=0;  

int j = 0;

int k = 0;

*/int k = 0;


// 5.连续注释问题

/**//**/


// 6.连续的**/问题

/***/


// 7.C++注释问题

// /*xxxxxxxxxxxx*/


// 8.C注释本身不匹配

/* int i = 0;

大概,就这些吧。一看八个问题,可能有些头大。这么多问题怎么才能解决呢?

我们可以一个一个的解决,分布解决

.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<errno.h>
.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"AnnotationConver.h"
typedef enum State
{
	C_BEGIN,
	C_END,
}State;

void Conver(FILE* put, FILE* Out)
{
	char frist, second;
	State tag = C_BEGIN;
	assert(put);
	assert(Out);
	do
	{
		frist = fgetc(put);
		switch (frist)
		{
		case ‘/‘:
			second =  fgetc(put);
				if(second == ‘*‘ && tag == C_BEGIN)     // 3.匹配问题
				{
               fputc(‘/‘,Out);
			   fputc(‘/‘,Out);
			   tag = C_END;
				}
		
			else
			{
				fputc(frist,Out);
				fputc(second,Out);
				if(second == ‘/‘)
			{
                char nextch;
                do
              {   nextch = fgetc(put);
                  fputc(nextch, Out);
              } while (nextch != ‘\n‘ && nextch != EOF);
			}
				}
			break;
		case‘*‘:
			second =  fgetc(put);
			if(second == ‘/‘)
			{
				/*if(tag == C_BEGIN)
			   {
				fputc(‘\n‘,Out);
				tag = C_BEGIN;
				}
				else if (tag == C_DD)
				{
					fputc(frist,Out);
					fputc(second,Out);
				}*/
				 char nextch ;
				 fputc(‘\n‘, Out);
                    tag = C_BEGIN;
					nextch = fgetc(put);
                   
                    if (nextch == ‘/‘)
                    {
                        fseek(put, -1, SEEK_CUR);
                    }
                    else if (nextch != ‘\n‘)
                    {
						fputc(nextch, Out);
                    }
			}
			else                                   //连续的**/问题
			{
				fputc(frist,Out);
				fseek(put,-1,SEEK_CUR);
				
			}
			break;
		case ‘\n‘:                                       // 4.多行注释问题
			/*second =  fgetc(put);
				if(second == EOF)
			{
				break;
			}*/
			if(tag == C_END)
			{
			 fputc(‘\n‘,Out);
			 fputc(‘/‘,Out);
			 fputc(‘/‘,Out);
           
			}
			/*else if(tag == C_BEGIN)
			{
				fputc(frist,Out);
			}*/
			break;
		default:
			fputc(frist,Out);
			break;
		}
	}while(frist != EOF );
}
void Annotationconver(const char* putfile, const char* Outfile)
{
	FILE* put, *Out;
	put = fopen(putfile,"r");
	if(put == NULL)
	{
		printf("打开文件%s失败,errno:%d\n",putfile,errno);
	}
     Out = fopen(Outfile,"w");
	if(Out == NULL)
	{
		printf("打开文件%s失败,errno:%d\n",Outfile,errno);
	}
	Conver(put, Out);
	fclose(put);
	fclose(Out);
}
int main()
{
	Annotationconver("input.c","output.c");
	system("pause");
	return 0;
}

以上便是,本人自己写出来的,在我们一个接一个解决问题的时候,有时候,在解决第三个问题时,第四个问题也得以解决,或者解决后一个问题时,影响到后一个问题。所以我们要学会思考和调试。从而方便与我们解决问题和实现功能。

注释转换

标签:注释转换

原文地址:http://10741706.blog.51cto.com/10731706/1769257

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!