注释转换(C->C++)

转换原理图解:

基于上图原理,可以写出代码:

 

//主函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "标头.h"
int main()
{
	convert();
	system("pause");
	return 0;
}

 

//具体实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "标头.h"

void convert()
{
	FILE *fop = fopen("c注释.c", "r");
	FILE *fop1 = fopen("C++注释.cpp", "w");
	if (fop == NULL||fop1==NULL)
		perror("The following error occurred");
	else
	{
		ele = Null_case;
		while (ele != eof_case)
		{
			int ch=getc(fop);
			switch (ele)
			{
			case C_case:
				do_c(ch,fop,fop1);
				break;
			case Cpp_case:
				do_cpp(ch, fop, fop1);
				break;
			case Null_case:
				do_Null(ch, fop, fop1);
				break;
			case eof_case:
				ele = eof_case;
				break;
			default:
				break;
			}
		}
	}
	fclose(fop);
	fclose(fop1);
	return 0;
}
void do_Null(int ch, FILE* fop, FILE *fop1)
{
	switch (ch)
	{
	case '/':
	{
				int ch = getc(fop);
				switch (ch)
				{
				case'/':
				{
						   putc('/', fop1);
						   putc('/', fop1);
						   ele = Cpp_case;
						   break;
				}
				case '*':
				{
							putc('/', fop1);
							putc('/', fop1);
							ele = C_case;
							break;
				}
				case EOF:
				{
							putc('/', fop1);
							ele = eof_case;
							break;
				}
				default:
				{
						   putc('/', fop1);
						   ch = ungetc(ch, fop);
						   ele = Null_case;
						   break;
				}
                }
				break;
	}
	case EOF:
		ele = eof_case;
		break;
	default:
		putc(ch, fop1);
		break;
	}
	return;
}
void do_c(int ch, FILE* fop, FILE *fop1)
{
	switch (ch)
	{
	case '*':
	{
				int ch = getc(fop);
				switch (ch)
				{
				case '/':
				{
							ele = Null_case;
							break;
				}
				case EOF:
					ele = eof_case;
					break;
				default:
				{
						   putc('*', fop1);
						   ch = ungetc(ch, fop);
						   ele = C_case;
						   break;
				}
				}
				break;
	}
	case '\n':
	{
				 putc(ch, fop1);
				 putc('/', fop1);
				 putc('/', fop1);
				 ele = C_case;
				 break;
	}
	case EOF:
		ele = eof_case;
		break;
	default:
		putc(ch, fop1);
		break;
	}
	return;
}
void do_cpp(int ch, FILE* fop, FILE *fop1)
{
	switch (ch)
	{
	case'\n':
	{
		putc(ch, fop1);
		ele = Null_case;
		break;
	}
	case EOF:
		ele = eof_case;
		break;
	default:
		putc(ch, fop1);
		break;
	}
	return;
}

 

 

//头文件
#pragma once
#include<windows.h>
enum
{
	C_case,
	Cpp_case,
	Null_case,
	eof_case
}ele;
void convert();
void do_c(int ch, FILE* fop, FILE *fop1);
void do_cpp(int ch, FILE* fop, FILE *fop1);
void do_Null(int ch, FILE* fop, FILE *fop1);


测试结果:


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
给下面代码每一行给上注释并说明这段代码的意思#include "pch.h" #include "FinBudgetSupport.h" #include <map> time_t StringToDateTime(char* str) { tm tm_; int year, month, day, hour, min, sec; afxDump << str << "\n\n\n\n"; sscanf_s(str, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec); tm_.tm_year = year - 1900; tm_.tm_mon = month - 1; tm_.tm_mday = day; tm_.tm_hour = hour; tm_.tm_min = min; tm_.tm_sec = sec; tm_.tm_isdst = -1; time_t t_ = mktime(&tm_); return t_; } CString DateTimeToString(time_t _time) { tm *_tm = new tm(); gmtime_s(_tm ,&_time); CString t_str; t_str.Format(_T("%d-%d-%d %d:%d:%d"), _tm->tm_year+1900, 1+_tm->tm_mon, _tm->tm_mday, _tm->tm_hour , _tm->tm_min, _tm->tm_sec); delete _tm; return t_str; } CString _toCString(double _value) { CString t_str; t_str.Format(_T("%lf"), _value); return t_str; } CString _toCString(int _value) { CString t_str; t_str.Format(_T("%d"), _value); return t_str; } double _toDouble(CString _str) { return _ttof(_str); } char* CStringToCharArray(CString str) { int str_len = WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), NULL, 0, NULL, NULL);//计算字节数 char* CharArray = new char[str_len + 1]; WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), CharArray, str_len, NULL, NULL); CharArray[str_len] = '\0'; return CharArray; } void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c) { std::string::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while (std::string::npos != pos2) { v.push_back(s.substr(pos1, pos2 - pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if (pos1 != s.length()) v.push_back(s.substr(pos1)); }
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值