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

📄 wordany.cpp

📁 第一次写的
💻 CPP
字号:
#ifndef __WORDANY_CPP
#define __WORDANY_CPP

#include "WordAny.h"

string ImToken[TOTAL]={"program","const","var","procedure","begin","if","while","call","read","write","end","then","else","do","odd"};

void GetChar(char &ch,int &i,string passage)
{
	if(i<passage.length())
		ch=passage[i];
	else
		ch=0;
	i++;
}

void GetBC(char &ch,int &i,string passage)
{
	while(ch==' ')
		GetChar(ch,i,passage);
}

void Concat(string &strTokenFile,char ch)
{
	strTokenFile+=ch;
}

bool IsLetter(char ch)
{
	if(ch<='z' && ch>='a')
		return true;
	else if(ch<='Z' && ch>='A')
		return true;
	else
		return false;
}

bool IsDigit(char ch)
{
	if(ch<='9' && ch>='0')
		return true;
	else
		return false;
}

int Reserve(const string strTokenFile)
{
	for(int i=0;i<14;i++)
		if(strTokenFile.compare(ImToken[i])==0)
			return i+1;
	return 0;
}

void Retract(int &i,char &ch)
{
	i--;
	ch=' ';
}

void WordAnaly(string passage,int &i,int &j,Word *Token,const int row)
{
	string strTokenFile;
	char ch;
	int code;

	strTokenFile="";
	GetChar(ch,i,passage);
	GetBC(ch,i,passage);
	if(IsLetter(ch))
	{
		while(IsLetter(ch) || IsDigit(ch))
		{
			Concat(strTokenFile,ch);
			GetChar(ch,i,passage);
		}
		Retract(i,ch);
		code=Reserve(strTokenFile);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanXY(i,row);
		if(code==0)			
			Token[j].ChanWType($id);
		else
			Token[j].ChanWType(code);
		j++;
	}
	else if(IsDigit(ch))
	{
		while(IsDigit(ch))
		{		
			Concat(strTokenFile,ch);
			GetChar(ch,i,passage);
		}
		if(IsLetter(ch))
		{
			cout<<"错误的标识符,第"<<row<<"行"<<endl;
			exit(0);
		}
		Retract(i,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($integer);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch=='=')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($lop);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch=='+' || ch=='-')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($aop);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch=='*' || ch=='/')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($mop);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch=='<')
	{
		Concat(strTokenFile,ch);
		GetChar(ch,i,passage);
		if(ch=='>' || ch=='=')
			Concat(strTokenFile,ch);
		else 
		Retract(i,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($lop);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch=='>')
	{
		Concat(strTokenFile,ch);
		GetChar(ch,i,passage);
		if(ch=='=')
			Concat(strTokenFile,ch);
		else
		Retract(i,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($lop);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch==';')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch==':')
	{
		Concat(strTokenFile,ch);
		GetChar(ch,i,passage);
		if(ch=='=')
			Concat(strTokenFile,ch);
		else
		{
			cout<<"错误的输入!!"<<":"<<"后应该为"<<"\"=\",第"<<row<<"行"<<endl;
			exit(0);
		}

		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($equal);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch==',')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($and);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch=='(')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($leftbrack);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch==')')
	{
		Concat(strTokenFile,ch);
		Token[j].ChanCont(strTokenFile);
		Token[j].ChanWType($rightbrack);
		Token[j].ChanXY(i,row);
		j++;
	}
	else if(ch==0)
		ch=' ';
	else
	{
		cout<<"Wrong Input!!,第"<<row<<"行"<<endl;
		exit(0);
	}
}

#endif

⌨️ 快捷键说明

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