checkexpression.h

来自「VC6.0环境下的多项式计算」· C头文件 代码 · 共 41 行

H
41
字号
#ifndef CHECKEXPRESSION_H_
#define CHECKEXPRESSION_H_

/*
   判断表达式是否正确
   方法:根据转成后缀式之后 栈中符号和数字的 个数是否相等
   如表达式 : 3 + 5 =  ->  3 5 + = 符号的个数 和 数字的个数是相等的 
*/
bool  checkExpression(stackList<float> &stackList2)

{
	int count1 ,count2 ;
	stackList<float> stackList1 ;

	while(stackList2.getLength() != 0)
	{
		// 栈中符号的个数
		if(stackList2.getType() == 0)
			count1 += 1 ;
		// 栈中数字的个数
		else
			count2 += 1 ;
		stackList1.push(stackList2.getTop(),stackList2.getType()) ;
		stackList2.pop() ;
	}

	// 把表达式重新压回栈 stackList2 中
	while(stackList1.getLength() != 0)
	{
		
		stackList2.push(stackList1.getTop(),stackList1.getType()) ;
		stackList1.pop() ;
	}
	// 比较长度
	if(count1 != count2)
		return false ;
	else
		return true ;
}

#endif

⌨️ 快捷键说明

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