利用棧做的中綴計算器雛形

#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack> 
using namespace std;
stack<double>num;//有關數字的棧 
stack<char>op;//有關運算符的棧 
bool Is_op_character(char ch)//判斷是否是運算符 
{
	return ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='||ch==' '||ch=='('||ch==')';
}
int operation(char ch)//對數字的操作 
{
	double a,b;
	switch (ch)
			{
				case '+':
					{
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						a=num.top();
						num.pop();
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						b=num.top();
						num.pop();
						num.push(a+b);
					}break;
				case '-':
					{
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						a=num.top();
						num.pop();
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						b=num.top();
						num.pop();
						num.push(b-a);
					}break;
				case '*':
					{
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						a=num.top();
						num.pop();
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						b=num.top();
						num.pop();
						num.push(a*b);
					}break;
				case '/':
					{
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						a=num.top();
						num.pop();
						if(num.empty())
						{
							cout<<"表達式不規範\n";
							return 0;
						}
						b=num.top();
						num.pop();
						if(a==0)
						{
							cout<<"除數爲零"<<endl;
							return 0;
						}
						else 
						num.push(b/a);
					}break;	
				default: return 1;
			}
			return 1;
}
int do_in_operation(char ch)//有關運算符的操作 
{
	bool right=1;
	switch (ch)
			{
				case '+':
					{
						while(!op.empty()&&op.top()!='(')
						{
							ch=op.top();
							op.pop();
							right=operation(ch);
						}
						op.push('+');
					}break;
				case '-':
					{
						while(!op.empty()&&op.top()!='(')
						{
							ch=op.top();
							op.pop();
							right=operation(ch);
						}
						op.push('-');
					}break;
				case '*':
					{
						while(!op.empty()&&op.top()!='+'&&op.top()!='-'&&op.top()!='(')
						{
							ch=op.top();
							op.pop();
							right=operation(ch);
						}
						op.push('*');
					}break;
				case '/':
					{
						while(!op.empty()&&op.top()!='+'&&op.top()!='-'&&op.top()!='(')
						{
							ch=op.top();
							op.pop();
							right=operation(ch);
						}
						op.push('/');
					}break;	
				case ')':
					{
						while(!op.empty()&&op.top()!='(')
						{
							ch=op.top();
							op.pop();
							right=operation(ch);
						}
						if(!op.empty()) 
						op.pop();
						else 
						{
							cout<<"表達式不規範\n";
							return 1;
						} 
					}break;
				case '(':
					{
						op.push('(');
					}break;
				case '=':
					{
						while(!op.empty())
						{
							ch=op.top();
							op.pop();
							right=operation(ch);
						}
						cout<<num.top()<<endl;
					}break;
			}
		return right;
}
int main()
{
	char ch;
	char number[15];
	cout<<"計算器1.3------------------------------------\n支持四則運算,支持小數點後十五位的小數,支持括號,不支持負數T.T\n"; 
	cout<<"使用方法:輸入運算式,輸入等號並回車後輸出結果,按'#'結束程序 \n";
	int i=0;
	double a,b;
	cin>>ch;
	while(ch!='#')
	{
		while(ch>='0'&&ch<='9'||ch=='.')//對小數的支持 
		{
			number[i]=ch;
			i++;
			number[i]='\0';
			if(i>=10)
			{
				cout<<"輸入單個數據精度過大"<<endl;
				return -1;
			}
			cin>>ch;
			if(Is_op_character(ch))
			{
				a=atof(number);
				num.push(a);
				i=0;
				break;
			}
		}
		if(do_in_operation(ch))
		cin>>ch;
		else break;
	}
	cout<<"程序即將退出\n" ;
	system("pause");
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章