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

📄 express.cpp

📁 此程序实现的是一个运算器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			nod->right=NULL;
			nod->operatie=159;
			SkipSpaces();
			return nod;
		}
		///////////////////////////
         if (id =="INT")		// Functia sinus CString
		{
			nod2 = factor();
			nod = new NOD;
			nod->left = nod2;
			nod->right=NULL;
			nod->operatie=160;
			SkipSpaces();
			return nod;
		}
         if (id =="ABS")		// Functia sinus CString
		{
			nod2 = factor();
			nod = new NOD;
			nod->left = nod2;
			nod->right=NULL;
			nod->operatie=161;
			SkipSpaces();
			return nod;
		}
         if (id =="MIN")		// Functia sinus CString
		{
			nod2 = factor();
			nod = new NOD;
			nod->left = nod2;
			pozitie--;
			nod2 = factor();
			nod->right = nod2;
			nod->operatie=162;
			SkipSpaces();
			return nod;
		}
		 //////////////////////////////////
		 if (id =="MAX")		// Functia sinus CString
		{
			nod2 = factor();
			nod = new NOD;
			nod->left = nod2;
			pozitie--;
			nod2 = factor();
			nod->right = nod2;
			nod->operatie=168;
			SkipSpaces();
			return nod;
		}
		 /////////////////////////////////////////
      if (id =="IF")		// Functia sinus CString
		{
			nod2 = factor();
			nod = new NOD;
			nod->left = nod2;
			pozitie--;
	 	    nod2 = factor();
			pozitie--;
			nod1 = new NOD;
			nod1->left = nod2;
	    	nod2 = factor();
			nod1->right = nod2;
	        nod->right=nod1;
			nod->operatie=169;
			SkipSpaces();
			return nod;
		}
		 //////////////////////////////////////////
       if (id =="LOG")		// Functia sinus CString
		{
			nod2 = factor();
			nod = new NOD;
			nod->left = nod2;
			nod->right=NULL;
			nod->operatie=167;
			SkipSpaces();
			return nod;
		}
		/////////////////////////////////////////
		/////////////////////////
		CValue *valoare;
		if (m_pvariabile->Lookup(nid,valoare))
		{
			nod = new NOD;
			nod -> left = NULL;
			nod -> right = NULL;
			nod -> operatie = '`';
			nod -> valoarestr = new CString(nid); 
			result = nod;
		}
		else 
			result = NULL;
	}
	SkipSpaces();
	return result;
        
}

int CExpression::ChangeExpression(CString & expresie)
{
	m_definitie = expresie + '\0' + '\0';
	return UpdateArbore();
}


// Error code var
int code;


int CExpression::Value(double& valoare)
{
  code=0;
  if (m_Arbore == NULL) return -1;
  valoare=vexp(m_Arbore);
  return (code);
}


double CExpression::vexp ( arbore a )
{
double v;
if (a->operatie==NULL) {code=10;return 0;}
switch(a->operatie) 
 {
 case '+' : return( vexp(a->left)+vexp(a->right) );
 case '-' : return( vexp(a->left)-vexp(a->right) );
 case '*' : return( vexp(a->left)*vexp(a->right) );
 case '/' : v=vexp(a->right) ;
			 if (v==0)
			 {code=DIVISION_BY_0;return -vexp(a->left)/0.001;}
			 else
				return(vexp(a->left)/v);
 case 150 : return(sin(vexp(a->left)));
 case 151 : return(cos(vexp(a->left)));
 case 152 : return(exp(vexp(a->left)));
 case 153 : v=vexp(a->left) ;
			 if (v<0) {code=INVALID_DOMAIN;return 0;}
			 else return(sqrt(v));
 case 154 : v=vexp(a->left) ;
			 if (v<=0) {code=INVALID_DOMAIN;return 0;}
			 else return(log(v));
 case 155 : return (tan (vexp(a->left)));
 case 156 : return (1 / tan (vexp(a->left)));
 case 157 : return (asin (vexp(a->left)));
 case 158 : return (acos (vexp(a->left)));
 case 159 : return (atan (vexp(a->left)));
	 ///////////////////////////////////
 case 160: return (int(vexp(a->left)));
 case 161: return (fabs(vexp(a->left)));
 case 162: 
	 //return (vexp(a->left)<=vexp(a->right)?vexp(a->left):vexp(a->right));
	    if(vexp(a->left)<=vexp(a->right))
			return (vexp(a->left));
		else  return (vexp(a->right));
 case 163: return (vexp(a->left)<=vexp(a->right));
 case 164: return (vexp(a->left)>=vexp(a->right));
 case 165: return (vexp(a->left)!=vexp(a->right));
 case 166: return (vexp(a->left)==vexp(a->right));
 case 167 : v=vexp(a->left) ;
			 if (v<=0) {code=INVALID_DOMAIN;return 0;}
			 else return(log(v)/log(2));
 case 168:	if(vexp(a->left)>=vexp(a->right))
			return (vexp(a->left));
		  else  return (vexp(a->right));
 case 169: return(vexp(a->left)?vexp(a->right->left):vexp(a->right->right));
	 ///////////////////////////////////
 case '|' : return(fabs(vexp(a->left)));
 case '^' : return(pow(vexp(a->left),vexp(a->right)));
 case '@' : return (a->valoare);
 //logical operations evaluation
 case '<' : return( vexp(a->left)<vexp(a->right) );
 case '>' : return( vexp(a->left)>vexp(a->right) );
 //case '=' : return( vexp(a->left)==vexp(a->right) );
 case '!' : return(!vexp(a->right)) ;
 case '&' : return(vexp(a->left)&&vexp(a->right));
 case '#' : return(vexp(a->left)||vexp(a->right));	 
	 
 default  : {
				if (m_pvariabile==NULL) 
				{
					code=UNDEFINED_VARIABLE;
					return 0;
				}
				CValue *valoare;
				if (!m_pvariabile->Lookup(*a->valoarestr,valoare))
				{
					code=UNDEFINED_VARIABLE;
					return 0;
				}
				else 
				return valoare->GetValue();
			}
 }
}


CExpression::arbore CExpression::GetArbore()
{
	return m_Arbore;
}


CExpression::CExpression(CExpression & expresie)
{
	*this = expresie;
}




CExpression::arbore CExpression::CloneTree()
{
	return clone(m_Arbore);	
}

void CExpression::AtachVariables(CMapVariabile * vars)
{
	m_pvariabile = vars;
}

CExpression::arbore CExpression::clone(arbore arb)
{
	if (arb == NULL)
		return NULL;
	arbore clonArb = new NOD;
	*clonArb = *arb;
	clonArb->left = clone(arb->left);
	clonArb->right = clone(arb->right);
	return clonArb;
}

CExpression& CExpression::operator=(CExpression &expr)
{
	m_definitie = expr.m_definitie;
	m_pvariabile = expr.m_pvariabile;
	pozitie = 0;
	m_Arbore = expr.CloneTree();
	return *this;
}

void CExpression::SkipSpaces()
{
	while (m_definitie[pozitie]==' ' && m_definitie[pozitie]!='\0')
		pozitie ++;
}


///////////////////////////////////////////////////////////////////////
// the new inserted operations
// logical operations on the first level of priority
// warning: for operations with more than one character you nedd to modify sligthly the code
// See also the new inserted operations in the vexp function


CExpression::arbore CExpression::logicalOp()
{
	arbore nod;
	arbore arb1 = sgOp();
	arbore arb2;
	if  (arb1 == NULL) return NULL;  // In caz de eroare terminate

	while ((m_definitie[pozitie]=='<') || (m_definitie[pozitie]=='>')
		||(m_definitie[pozitie]=='=')
		||(m_definitie[pozitie]=='&')
		||(m_definitie[pozitie]=='#')/* || another same priority operations*/) 
	{ 
	///////////////////////////////
		if((m_definitie[pozitie]=='<')&&(m_definitie[pozitie+1]=='='))
		{	
		nod=new NOD;
		nod->left=arb1;
		nod->operatie=163;
		pozitie++;
		pozitie++;
		arb2 = sgOp();
		nod->right=arb2;
		if  (arb2 == NULL) 
		{
			elibmem(nod);
			return NULL;  // In caz de eroare terminate
		}

		arb1 = nod;
		}
	////////////////////////////////////////////////////////
    else if((m_definitie[pozitie]=='>')&&(m_definitie[pozitie+1]=='='))
		{	
		nod=new NOD;
		nod->left=arb1;
		nod->operatie=164;
		pozitie++;
		pozitie++;
		arb2 = sgOp();
		nod->right=arb2;
		if  (arb2 == NULL) 
		{
			elibmem(nod);
			return NULL;  // In caz de eroare terminate
		}

		arb1 = nod;
		}
	/////////////////////////////////////////////////
	else if((m_definitie[pozitie]=='<')&&(m_definitie[pozitie+1]=='>'))
		{	
		nod=new NOD;
		nod->left=arb1;
		nod->operatie=165;
		pozitie++;
		pozitie++;
		arb2 = sgOp();
		nod->right=arb2;
		if  (arb2 == NULL) 
		{
			elibmem(nod);
			return NULL;  // In caz de eroare terminate
		}

		arb1 = nod;
		}
	/////////////////////////////////////////////////
	else if((m_definitie[pozitie]=='=')&&(m_definitie[pozitie+1]=='='))
		{	
		nod=new NOD;
		nod->left=arb1;
		nod->operatie=166;
		pozitie++;
		pozitie++;
		arb2 = sgOp();
		nod->right=arb2;
		if  (arb2 == NULL) 
		{
			elibmem(nod);
			return NULL;  // In caz de eroare terminate
		}

		arb1 = nod;
		}
	///////////////////////////////
	else{
		nod=new NOD;
		nod->left=arb1;
		nod->operatie=m_definitie[pozitie];
		pozitie++;
		arb2 = sgOp();
		nod->right=arb2;
		if  (arb2 == NULL) 
		{
			elibmem(nod);
			return NULL;  // In caz de eroare terminate
		}

		arb1 = nod;
		}
	}
	return arb1;
}


CExpression::arbore CExpression::sgOp()
{
	arbore nod = NULL;
	arbore arb2;
	if ((m_definitie[pozitie]=='!') /* || another same priority operations*/) 
	{
		nod=new NOD;
		nod->left=NULL;
		nod->operatie=m_definitie[pozitie];
		pozitie++;
		arb2 = sgOp();
		nod->right=arb2;
		if  (arb2 == NULL) 
		{
			elibmem(nod);
			return NULL;  // In caz de eroare terminate
		}
	}
	else 
		nod = factor();
	return nod;
}

⌨️ 快捷键说明

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