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

📄 job.cpp

📁 是关于算术表达式的翻译程序
💻 CPP
字号:
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
struct	wordtableitem
{
	int num;//编号
	char idvalue[40];//id最多为40位
	int type;//该项为常数还是表达式;
};
int num=0;//自动生成编号
inline bool __isdigital(char value)
{
	return value<='9'&&value>='0';
}
inline bool __isalpha(char value)
{
	return (value>='a'&&value<='z')||(value>='A'&&value<='Z');
}
vector<wordtableitem> symboltable;//符号表;
int findfromtable(char *symbol)
{
	vector<wordtableitem>::iterator it;
	vector<wordtableitem>::value_type vt;
	for(it=symboltable.begin();it!=symboltable.end();it++)
	{
		vt=*it;
		if(strcmp(vt.idvalue,symbol)==0)
			return vt.num;
	}
	return -1;
}
vector<wordtableitem>::iterator findfromtable(int ch)
{
	vector<wordtableitem>::iterator it;
	vector<wordtableitem>::value_type vt;
	for(it=symboltable.begin();it!=symboltable.end();it++)
	{
		vt=*it;
		if(vt.num==ch)
		{
			return it;
		}
	}
	return it;
}
char input[1024];//原始表达式;
char output[1024];//符号表达式;
char variable[40];//临时变量
 int induce_first;//推导的开始位置
 int induce_last;//推导的结束位置
 char newsymbol[10];//临时符号
 int newnum=0;//临时符号索引
 bool bgeneratetemp=false;
void generate_newsymbol()
{
	memset(newsymbol,0,10);
	itoa(newnum++,newsymbol+1,9);
	newsymbol[0]='T';
	return;
}
int item_induce();
int factor_induce();
int Express_induce()
{
	bool neg=false;
	char temp[40];
	char temp2[40];
	char opt;
	if(output[induce_first]=='+')
		induce_first++;
	else if(output[induce_first]=='-')
	{
		induce_first++;
		neg=true;	
	}
	int pos=item_induce();
	if(neg==true)
	{
		if(bgeneratetemp)
		{
			cout<<'-'<<'\t'<<newsymbol<<'\t';
			generate_newsymbol();
			cout<<'\t'<<newsymbol<<'\n';
		}
		else 
		{
			memset(temp,0,40);
			vector<wordtableitem>::iterator it=findfromtable(output[pos]);
			memcpy(temp,it->idvalue,40);
			generate_newsymbol();
			cout<<'-'<<'\t'<<temp<<'\t'<<'\t'<<newsymbol<<endl;

		}
		bgeneratetemp=true;
	}
	while(induce_first!=induce_last&&(output[induce_first]=='+'||output[induce_first]=='-'))
	{
		opt=output[induce_first];
		memset(temp,0,40);
		memset(temp2,0,40);
		if(bgeneratetemp)
		{
			memcpy(temp,newsymbol,10);
		}
		else
		{
			vector<wordtableitem>::iterator it=findfromtable(output[pos]);
			memcpy(temp,it->idvalue,40);
		}
		induce_first++;
		pos=item_induce();
		if(bgeneratetemp)
		{
			memcpy(temp2,newsymbol,10);
		}
		else
		{
			vector<wordtableitem>::iterator it=findfromtable(output[pos]);
			memcpy(temp2,it->idvalue,40);
		}
		generate_newsymbol();
		bgeneratetemp=true;
		cout<<opt<<'\t'<<temp<<'\t'<<temp2<<'\t'<<newsymbol<<endl;
		
	}
	return 0;	
}
int item_induce()
{
	char temp[40];
	char temp2[40];
	char opt;
	int pos=factor_induce();
	while(induce_first<induce_last&&(output[induce_first]=='*'||output[induce_first]=='/'))
	{
		opt=output[induce_first];
		memset(temp,0,40);
		memset(temp2,0,40);
		if(bgeneratetemp)
		{
			memcpy(temp,newsymbol,10);
		}
		else
		{
			vector<wordtableitem>::iterator it=findfromtable(output[pos]);
			memcpy(temp,it->idvalue,40);
		}
		induce_first++;
		pos=factor_induce();
		if(bgeneratetemp)
		{
			memcpy(temp2,newsymbol,10);
		}
		else
		{
			vector<wordtableitem>::iterator it=findfromtable(output[pos]);
			memcpy(temp2,it->idvalue,40);
		}
		bgeneratetemp=true;
		generate_newsymbol();
		cout<<opt<<'\t'<<temp<<'\t'<<temp2<<'\t'<<newsymbol<<endl;
	}
	return pos;
}
int factor_induce()
{
	if(output[induce_first]=='(')
	{
		induce_first++;
		int pos=Express_induce();
		if(output[induce_first]!=')')
		{
			cout<<"缺少')'在"<<induce_first<<endl;
			exit(0);
			
		}
		else
		{
			induce_first++;
			return pos;
		}
	}
	else
	{
		bgeneratetemp=false;
		vector<wordtableitem>::iterator it=findfromtable(output[induce_first]);
		if(it==symboltable.end())
		{
			cout<<"错误的表达式在"<<induce_first<<endl;
			exit(0);
		}
		induce_first++;
		return induce_first-1;
	}
	
}
int main()
{
	memset(input,0,sizeof(input));
	int firstindex,nextindex;
	firstindex=nextindex=0;
	int oindex=0;
	char temp;
	for(cin>>temp;;nextindex++)
	{
		input[nextindex]=temp;
		if(__isdigital(temp)==false&&__isalpha(temp)==false)
		{
			if(firstindex==nextindex)
			{
				if(temp!=' '&&temp!='#')
					output[oindex++]=temp;
				if(temp=='#')
					break;
				firstindex++;
				cin>>temp;
				
				continue;
			}
			memset(variable,0,40);
			memcpy(variable,input+firstindex,nextindex-firstindex);
			int numtemp=findfromtable(variable);
			
			if(numtemp==-1)
			{
				wordtableitem	vt;
				memset(vt.idvalue,0,40);
				vt.num=num++;
				memcpy(vt.idvalue,variable,40);
				if(__isdigital(input[firstindex]))
					vt.type=1;
				else if(__isalpha(input[firstindex]))
					vt.type=2;
				symboltable.push_back(vt);
				output[oindex++]=vt.num;
			}
			else 
			{
				output[oindex++]=numtemp;
			}
			if(temp=='#')
				break;
			output[oindex++]=temp;
			firstindex=nextindex+1;
			
		}
		else if(__isalpha(temp))
		{
			if(firstindex!=nextindex&&!__isalpha(input[firstindex]))
			{
				cout<<"error express"<<endl;
				return 1;
			}
		}
		cin>>temp;
		
	}
	cout<<output<<endl;
	vector<wordtableitem>::iterator it;
	vector<wordtableitem>::value_type vt;
	for(it=symboltable.begin();it!=symboltable.end();it++)
	{
		vt=*it;
		cout<<'@'<<vt.num<<':'<<vt.idvalue<<'@'<<endl;
	}
	induce_first=0;
	induce_last=oindex-1;
	Express_induce();
	cin>>temp;
	return 0;
}

⌨️ 快捷键说明

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