zoukankan      html  css  js  c++  java
  • 凝视转换(部分)

    #include <stdio.h>
    #include <errno.h>
    #include <assert.h>
    
    typedef enum STATE
    {
    	SUCCESS,	// 成功
    	FILE_ERROE,	// 文件错误
    	NO_MATCH,	// 不匹配
    	OTHER,		// 其它错误
    }STATE;
    
    typedef enum TAG
    {
    	TAG_BEGIN,		// 在C凝视段中
    	TAG_END,		// C凝视结束
    }TAG;
    
    #pragma warning(disable:4996)
    
    STATE AnnotationConvert(FILE* inFile, FILE* outFile)
    {
    	TAG tag = TAG_END;
    	char firstCh, secondCh;
    	assert(inFile);
    	assert(outFile);
    
    	do{
    		firstCh = fgetc(inFile);
    		switch (firstCh){
    		case '/':
    			secondCh = fgetc(inFile);
    			if (secondCh == '*'&& tag == TAG_END)
    			{
    				fputc('/', outFile);
    				fputc('/', outFile);
    
    				tag = TAG_BEGIN;
    			}
    		
    			else
    			{
    				fputc(firstCh, outFile);
    				fputc(secondCh, outFile);
    				if (secondCh == '/')
    				{
    					char next = fgetc(inFile);
    					while ((next != '
    ')&&(next!=EOF))
    					{
    						next = fgetc(inFile);
    						fputc(next, outFile);
    					} 
    				}
    			}
    			break;
    		case '
    ':
    			fputc('
    ', outFile);
    			if (tag == TAG_BEGIN)
    			{
    				fputc('/', outFile);
    				fputc('/', outFile);
    			}
    			break;
    		case '*':
    			secondCh = fgetc(inFile);
    			if (secondCh == '/'&&tag==TAG_BEGIN)
    			{
    				char next = fgetc(inFile);
    				if (next != '
    ' && next != EOF)
    				{
    					fseek(inFile, -1, SEEK_CUR);
    				}
    				if (next == EOF)
    				{
    					firstCh = EOF;
    				}
    
    				tag = TAG_END;
    			}
    			else
    			{
    				fputc(firstCh, outFile);
    				fseek(inFile, -1, SEEK_CUR);
    			}
    			fputc('
    ', outFile);
    			break;
    		default:
    			fputc(firstCh, outFile);
    			break;
    		}
    	} while (firstCh != EOF);
    
    	if (tag == TAG_END)
    	{
    		return SUCCESS;
    	}
    	else
    	{
    		return NO_MATCH;
    	}
    }
    
    int StartConvert()
    {
    	STATE s;
    	const char* inFileName = "input.c";
    	const char* outFileName = "output.c";
    
    	FILE* inFile = fopen(inFileName, "r");
    	FILE* outFile = fopen(outFileName, "w");
    
    	if (inFile == NULL)
    	{
    		return FILE_ERROE;
    	}
    
    	if (outFile == NULL)
    	{
    		fclose(inFile);
    		return FILE_ERROE;
    	}
    
    	s = AnnotationConvert(inFile, outFile);
    
    	fclose(inFile);
    	fclose(outFile);
    
    	return s;
    }
    
    int main()
    {
    	STATE ret = StartConvert();
    
    	if (ret == SUCCESS)
    	{
    		printf("转换成功
    ");
    	}
    	else if (ret == NO_MATCH)
    	{
    		printf("不匹配
    ");
    	}
    	else if (ret == FILE_ERROE)
    	{
    		printf("文件错误: %d
    ", errno);
    	}
    	else
    	{
    		printf("其它错误: %d
    ", errno);
    	}
    
    	return 0;
    }


    
       
    
  • 相关阅读:
    iconv 文件编码相互转换
    MySQL 字符编码
    MySQL there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause同时创建多个更新当前时间戳字段 解决方法
    PHP 输出日志到文件 DEMO
    Nginx http -> https 跳转后 POST 丢失
    SSH SCP 远程密钥登录配置和服务器间的文件传输
    Mac 安装 7zip
    git pull There is no tracking information for the current branch.
    MacOS 安装配置 Laravel
    Mac OS 安装 MySQL5.7
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7040795.html
Copyright © 2011-2022 走看看