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

📄 main.cpp

📁 原理同简单的编译器分析
💻 CPP
字号:
/*
*程序功能: 从一个文件中读出指令
           对另一个文件操作
*
*接口要求:  main函数传入三个参数分别为:
             argv[1]: 指令文件
			 argv[2]: 被操作文件
			 argv[3]: -s ( 在本程序中将不起作用,但为程序升级提供便利 ) 
*
*
*/
///////////////////////////////////////////////////////////////////////
////////main.cpp //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
#include "csascii.h"
#include "parse.h"

#include <fstream>
#include <string>
#include <sstream>

#include <stdlib.h>
#include <conio.h>
using namespace std;
////////////////////////////////////////////////////////////////////////

//常量字符  退出键
const char ESC = char( 27 );

//接口要求:argv[1], argv[2]为文件名
//          argv[3]要以-s开头
int main( int argc, char** argv )
{
	int screen = 0;
	if ( argc < 3 )
	{
		cerr << "usage: tawk tawkfile datafile\n"
			 << "trailing -s pages output to screen";
		exit( 1 );
	}

	if ( argc == 4 )
	{
		if ( argv[3][0] != '-' )
		{
			cerr << "must use '-' before trailing flag\n";
			exit( 1 );
		}
		
		//argv[3]的第一个字符是 '-'
		else
		{
			if ( argv[3][1] != 's' )
			{
				cerr << " 's' is only trailing flag allowed";
				exit( 1 );
			}
			
			//argv[3]的第二个字符是 's' 
			else 
			{
				screen++;
			}
		}
	}

	ifstream tawkfile( argv[1] );
	if ( !tawkfile )
	{
		cerr << "couldn't open " << argv[1];
		exit( 1 );
	}
    
	//read file "argv[1]" to "preamble"
	parse_array Apreamble( tawkfile );

    //read file "argv[1]" from "main" to "conclusion"
	parse_array Amain( tawkfile );

    //read file "argv[1]" from "conclusion" to "end"
	parse_array Aconclusion( tawkfile );

	//对argv[2]操作
	csascii datafile( argv[2] );
	

    //Apreamble.size()    return end_
	for ( int i = 0; i < Apreamble.size(); i++ )
	{
		//print " @main " 以前的字符
		cout << Apreamble[i];

	}

	if ( screen )
	{
		cout << ESC << "[7m" << "press any key" << ESC << "[0m";

		//按任意键继续
		getch();
	}


	//
	do
	{
		if ( screen )
		{
			cout << ESC << "[2J" << endl;
		}

		//int size()    return end_     end_ = token_count
		for ( int i = 0; i < Amain.size(); i++ )
		{
			//int token_type()   return ttype;
			switch ( Amain[i].token_type() )
			{
			
			//if the letters in "argv[1]" is @(x)   x is a variable
			case fieldnumber:
				
				/*
				field_number()   return fieldnum
				print argv[2] 中的部分字符
				Amain[i].field_number 为 get_value() 返回值
				即文件argv[1]中@(x)中值
				*/
				cout << datafile[Amain[i].field_number()];
				break;

			case string__:
				
				//print argv[1] main字符
				cout << Amain[i];
				break;
			
			//if the letter in "argv[1]" is @?X@:          x is a variable
			case if_:
				{
				    //fn = x
					int fn = Amain[i].field_number();
	                
					//the word of file "argv[2]" is empty?
					if ( datafile[fn].size() == 0 )
					{
						//return if_level; 
						int level = Amain[i].nesting_level();
                        
						//exit the loop until the '@~' appears in "argv[1]"
						//and passed the letters between @?X@ and @~
						while ( !( Amain[i].token_type() == else_ 
							&& Amain[i].nesting_level() == level ) )
						{
							i++;
						}
					}
					break;
				}

			//if the letter in "argv[1]" is @~
			case else_:
				{
					
					//return if_level; 
					int level = Amain[i].nesting_level();
                        
					//exit the loop until the '@.' appears in "argv[1]"
					//and pass the words between @~ and @.
					while ( !( Amain[i].token_type() == endif_ 
						&& Amain[i].nesting_level() == level ) )
					{
						i++;
					}

					break;
				} 
            
			//when the file "argv[1]" appears '@.'
			case endif_:
				break;

			default:
				cerr << "unknown statement encountered at run time\n";
				exit( 1 );
			}

		}

		if ( screen )
		{
			cout << ESC << "[7m" 
				 << "pess a key ( ESC quits ) "
				 << ESC << "[0m";
			if ( getch() == ESC )
			{
				break;
			}
		}
	}
	while ( datafile.next() );

	//print the word from 'comclusion' to 'end' in the file "argv[1]"
	for ( i = 0; i < Aconclusion.size(); i++ )
	{
		cout << Aconclusion[i];
	}

	cout << "Main Terminate" << endl;
	return 0;
}

⌨️ 快捷键说明

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