Experiment "data structure" Shandong University Four: application stack

Experiment 4 stacks of applications

First, the purpose of the experiment

Master stack usage.

Second, the experimental content

1. Enter a mathematical expression (assuming that the legitimate expression input format), and outputs the calculation result of the expression.
2, a mathematical expression and a single digital operator "+", "-", "*", "/", "(,)" configuration, for example, 2 + 3 * (4 + 5) - 6/4.
3, variable output is an integer, rounded down.

Third, pay attention

This question tests must find a few test cases, especially with complex cases, edge cases. Otherwise, it is likely to cause crashes or does not return a result.

  假设输入的表达式正确,即每个左括号都对应一个右括号;
  假设不存在括号嵌套的情况;
  用栈LIFO的思想,借助数组完成中缀表达式的直接计算。 

code show as below:

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<string.h> 
using namespace std;
string expression;
int num[25],temp[10],i,t;
bool flag=true;//true时对num数组操作,false时对temp数组操作(对括号内进行运算)
int numi=-1,tempi=-1;
void calculate(const char exp,int next){
	if(exp=='+'){//'+':存入后一位的数
		if(flag) num[++numi]=next;
		else temp[++tempi]=next;
	}
	else if(exp=='-'){//'-':存入后一位的相反数
		if(flag) num[++numi]=-next;
		else temp[++tempi]=-next; 
	}
	else if(exp=='*'){//'*':存入前一位和后一位的乘积
		if(flag){
			t=num[numi];
			num[numi]=t*next;
		}
		else{
			t=temp[tempi];
			temp[tempi]=t*next;
		}
	}
	else if(exp=='/'){//'/':存入前一位和后一位的商
		if(flag){
			t=num[numi];
			num[numi]=t/next;
		}
		else{
			t=temp[tempi];
			temp[tempi]=t/next;
		}
	}
	return;	
}
int main(){
	char pair;//pair记录左括号前的运算符号
	cout<<"Input"<<endl;
	cin>>expression;
	cout<<"Output"<<endl;
	for(i=0;i<expression.length();i++){
		if(expression[i]>='0'&&expression[i]<='9'){//如果是数字就压栈
			if(flag)num[++numi]=expression[i]-'0';
			else temp[++tempi]=expression[i]-'0';
			continue;
		}
		else {
			if(expression[i]=='('){//遇到左括号时flag设为false
				flag=false;
			}
			else if(expression[i]==')'){//遇到右括号时flag重设为true并计算得到括号内计算的结果
				t=0;
				for(int j=tempi;j>=0;j--){t+=temp[j];}
				flag=true;
				if(pair!=0){calculate(pair,t);pair=0;}
				else num[++numi]=t;
				tempi=-1;
			}
			else{//遇到运算符,判断下一位是否是左括号
				if(expression[i+1]=='('){//是
					flag=false;
					pair=expression[i];
					i++;
				}
				else{//不是,正常计算
					calculate(expression[i],expression[i+1]-'0');
					i++;
				}
			}	
		}
	}

	int ans=0;//最终将num数组中所有的值加起来得到表达式的结果
	for(int j=numi;j>=0;j--){ans+=num[j];}
	cout<<ans<<endl;
	cout<<"End"<<endl;	
	return 0;
} 

Conclusion Analysis and experience:

老师在课上讲了后缀表达式的过程。直接用中缀表达式需要两个栈来实现不带括号嵌套的表达式计算,括号嵌套的可以用三个栈去实现。
以上代码是提交平台的代码,其实可以对运算部分(加减乘除)的方法进行优化,在主函数里判断flag后将数组作为参数传入。
Published 11 original articles · won praise 0 · Views 35

Guess you like

Origin blog.csdn.net/weixin_43959421/article/details/103973996