⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c64.cpp

📁 《C++程序设计习题及解答》配套代码VC版
💻 CPP
字号:
// c64.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <fstream.h>

// --------------------------------------------------------------------------
void DoLine( istream& fin );
void DoBlock( istream& fin );
void DoStrChar( istream& fin, char ch );

/*
ONLY FOR TEST
  */

// --------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	if ( argc != 2 )
	{
		cout << "command: " << argv[0] << " filename.cpp\n";
		return -1;
	}

	ifstream fin( argv[1] );
	if ( !fin )
	{
		cout << "file name not found.\n";
		return -1;
	}

	char ch;
	while ( fin.get( ch ) )
	{
		if ( ch == '/' )
		{
			fin.get( ch );
			if ( !fin ) break;

			if ( ch == '/' ) DoLine( fin );
			else if ( ch == '*' ) DoBlock( fin );
			else 
			{
				cout.put( '/' );
				fin.putback( ch );
			}
		}
		else
		{
			cout.put( ch );
			// 字符串里边的原封不动的输出
			if ( ch == '\"' || ch == '\'' ) DoStrChar( fin, ch );
		}
	}

	return 0;
}

// --------------------------------------------------------------------------
void DoLine( istream& fin )
{
	char ch;
	while ( fin.get( ch ) )
	{
		if ( ch == '\n' )
		{
			cout << endl; // TO REPLACE THE COMMENT
			return;
		}
	}

	cout << "EOF found within line comment";
}

// --------------------------------------------------------------------------
void DoBlock( istream& fin )
{
	cout.put( ' ' );
	char ch;
	while ( fin.get( ch ) )
	{	
		if ( ch == '*' )
		{
			fin.get( ch );
			if ( !fin ) break;
			if ( ch == '/' ) return;
			fin.putback( ch );
		}
		else if ( ch == '\n' ) cout << endl;
	}

	cout << "EOF found within block comment";
}

// --------------------------------------------------------------------------
void DoStrChar( istream& fin, char startquote )
{
	char ch;
	while ( fin.get( ch ) )
	{
		cout.put( ch );
		if ( ch == startquote ) return;
		if ( ch == '\\' ) // 遇到转移字符,显示其后边的字符
		{
			fin.get( ch );
			if ( !fin ) break;
			cout.put( ch );
		}
	}
	
	cout << "EOF found with string or char constant";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -