計算機:接尾辞へのインフィックスと接尾辞の計算

サフィックスをサフィックスに変換し、サフィックスで計算するための2つのソースファイルを作成しました。後で、それらを接続して計算機を作成することを考えました。2つのスタックの宣言を組み合わせてクラステンプレートにすることができますが、コピーアンドペーストが便利なため、変更はありません。そして、そのようなものは配列スタックであり、もう一方はリンクされたリストスタックであり、これも良いことです。
インフィックス変換スタックの関数を作成するときに、主に開始括弧の処理でいくつかの問題が発生しました。原理をよりよく理解できるように、テスト用にいくつかのインフィックス式を記述したい場合があります。
簡単に言えば、2つのモジュールは次のアイデアにグループ化できます。(Luo Guのソリューションを参照してください)
ここに写真の説明を挿入

#include<iostream>
#include<string>
#define MAX_N 10000
using namespace std;
class node{
    
    
	public:
		int data;
		node *next;
};

//中缀转换栈 
class Stack1{
    
    
	public:
		Stack1(){
    
    
			top=-1;
			ope= new char[MAX_N];
		}
		
		~Stack1(){
    
    
			delete ope;
		}
		
		void Push(int data){
    
    
			top= top+1;
			ope[top]= data;
		}
		
		char Pop(){
    
    
			int temp= top;
			top= top-1;
			return ope[temp];
		}
		
		char Top(){
    
    
			return ope[top];
		}
		
		bool isEmpty(){
    
    
			return top==-1;
		}
		
		
	private:
		int top;
		char *ope;
	
}; 
//后缀计算栈 

class Stack2{
    
    
	public:
		Stack2(){
    
    
			top= NULL;
		}
		
		~Stack2(){
    
    
			while(top!=NULL){
    
    
				node *ptr= top;
				top= top->next;
				delete ptr;
			}
		}
		
		void Push(int data){
    
    
			node *ptr= new node;
			ptr->next= top;
			ptr->data= data;
			top= ptr;
		}
		
		int Pop(){
    
    
			if(isEmpty()){
    
    
				cout <<"Error Pop:STACK EMPTY" << endl; 
				return -1;
			}
			else{
    
    
				int tempdata= top->data;
				node *ptr= top;
				top= top->next;
				delete ptr;
				return tempdata;
		}
		
			}	
		
		int Top(){
    
    
			if(isEmpty()){
    
    
				cout <<"Error Top:STACK EMPTY" << endl; 
				return -1;
			}
			else
			return top->data;
		}
		
		bool isEmpty(){
    
    
			return top== NULL;
		}
			
	private:
		node *top;
		
}; 

//优先级判断函数 
int judge(char temp){
    
    
	if(temp=='+'||temp=='-'){
    
    
		return 1;
	}
	
	if(temp=='*'||temp=='/'){
    
    
		return 2;
	}
	
	if(temp=='(') return 0; 
}
//中缀转后缀函数
string midfixToPostfix(string midfix){
    
    
	string postfix;
	Stack1 stk;
	char curr;

	for(int i=0; i<midfix.length(); i++){
    
    
		curr= midfix[i]; 
		//数字直接输出 
		if(curr>='0'&&curr<='9'){
    
    
			postfix= postfix+ curr;
		}
		
		//左括号直接入栈 
		if(curr=='(') {
    
    
			stk.Push('('); 
		}
		//右括号弹出元素至左括号
		if(curr==')'){
    
    
			while(stk.Top()!='('){
    
    
				postfix= postfix+ stk.Pop();
			} 
			stk.Pop();
		} 
		//其他符号比较栈顶优先级 
		if((curr<'0'||curr>'9')&&curr!='(' &&curr!=')'){
    
    
			while(!stk.isEmpty()&&judge(curr)<=judge(stk.Top())){
    
    
			 	postfix= postfix+ stk.Pop();
			}
			stk.Push(curr);
		} 
	}
	
	while(!stk.isEmpty()){
    
    
		postfix= postfix+ stk.Pop();
	}
	
	return postfix;
}
//后缀计算函数
int calPostfix(string postfix){
    
    
	Stack2 sta;

	int i=0;
	while(i<postfix.length()){
    
    
		char curr=postfix[i]; 
		
		if(curr>='0'&& curr<='9'){
    
    
			sta.Push(curr-'0');
		}
		
		if(curr<'0'||curr>'9'){
    
    
			int result= 0;
			int numA= sta.Pop();
			int numB= sta.Pop();
			switch(curr){
    
    
				case '+':  
					result= numA+numB;
					break;
				case '-':  
					result= numB-numA;
					break;
				case '*':  
					result= numA*numB;
					break;
				case '/':  
					result= numB/numA;
					break;
				default:
					break;
				}
			sta.Push(result);	
		}
		i=i+1;
	}
	return sta.Top();

} 


//函数接口
int calculate(string midfix){
    
    
	string postfix;
	int result;
	postfix= midfixToPostfix(midfix);
	result= calPostfix(postfix);
	
	return result;
}



string input;

int main(){
    
    
	cin >> input;
	//cout << midfixToPostfix(input)<< endl;
	cout << calculate(input)<< endl;
	return 0;
} 



おすすめ

転載: blog.csdn.net/roswellnotfound/article/details/108622298