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

📄 main.cpp

📁 银行收支管理系统,用VC实现,竭诚欢迎大家提供建议.
💻 CPP
字号:
#include "stack.h"
#include <string>
#include <vector>
using namespace std;

void RPN(string,char *);
int procecn( char);
void Count( char *);

int main()
{
	string exp;
	char answerexp[255];
	cout<< "NOTE: Enter # for infix expression to stop.\n";
	for(;;)
	{
		cout<< "\nInfix Expression? ";
		getline(cin,exp,'\n');
		if( exp == "#") break;
		RPN(exp,answerexp);
		cout<< "RPN Expression is " << answerexp <<endl;
		Count(answerexp);

	}
}

int procecn(vector<char> ch)
{
	
	switch(ch[0])
	{
	case '+':case '-':
		return 2;
	case '*':case '/':
		return 3; 
	case '<':case '>':
		if(*(ch.end()-1) == '<'||*(ch.end()-1) == '>')
		return 1;
	default:
		return 0;
	}
}


void RPN(string exp,char * answerexp)
{
   
	Stack<char,128> s;
	int j=0,i=0;
    vector<char> ch,temp;
	s.push('@');

	 while(i< exp.length())
	{ 
		ch.push_back(exp[i]);
		if( ch[0] == ' ')
			i++;
		else if ( ch[0] == '(')
		{
			s.push( ch[0] );
			i++;
		}
		else if ( ch[0] ==  ')')
		{
			while( s.gettop() != '(')
			{
				answerexp[j++] = s.gettop();
				s.pop();
			}
			s.pop();
			i++;
		}
		else if( ch[0] == '*'|| ch[0] =='/' || 
			   ch[0] == '-' || ch[0] == '+'|| ch[0] == '<'|| ch[0] == '>')
		{
			if(exp[i+1] == '<')
			{
				i++;
				ch.push_back('<');
			}
			else if(exp[i+1] == '>')
			{
				i++;
				ch.push_back('>');
			}
			temp.push_back(s.gettop());
			if( procecn(temp) >= procecn( ch) )
			{

				if(s.gettop() == '<')
				{
					while(s.gettop() == '<')
					{
						answerexp[j++] = s.gettop();
						s.pop();
					}	
				}
				else if( s.gettop() =='>')
				{
					while(s.gettop() == '>')
					{
						answerexp[j++] = s.gettop();
						s.pop();
					}	
				}
			
				else{
				answerexp[ j++ ] = s.gettop();
				s.pop();
				}
			}
			temp.clear();
			vector<char>::iterator it = ch.begin();
			for(; it != ch.end(); ++it)
			s.push(*it);

			i++;
		}
		else{
			while( exp[i] >= '0' && exp[i] <= '9')
			{   
				answerexp[j++] = exp[i];
				i++;			
			}
		}
	  answerexp[j++] = ' ';
	  ch.clear();
	}
	
	while(s.gettop()!= '@')
	{
		if( s.gettop() == '(')
		{
			cout << " Error in infix expression \n";
			exit(-1);
		}
		else{
			answerexp[j++] = s.gettop();
			s.pop();
		}
	}
	answerexp[j] = '\0';
	
	s.pop();
}
			
void Count(char *answerexp)
{

   int sum = 0,num1,num2,i = 0;
   char ch;
   Stack<int,128> s;
   while ( answerexp[i] != '\0')
   {
	   ch = answerexp[i];
	   switch(ch)
	   {
	   case '+':
		   num1 = s.gettop();
		   s.pop();
		   num2 = s.gettop();
		   s.pop();
		   s.push( num1+num2);
		   i++;
		   break;
	   case '-':
		   num1 = s.gettop();
		   s.pop();
		   num2 = s.gettop();
		   s.pop();
		   s.push( num2-num1);
		   i++;
		   break;
	   case '*':
		   num1 = s.gettop();
		   s.pop();
		   num2 = s.gettop();
		   s.pop();
		   s.push( num1*num2);
		   i++;
		   break;
	   case '/':
           num1 = s.gettop();
		   s.pop();
		   num2 = s.gettop();
		   s.pop();
		   s.push( num2/num1);
		   i++;
		   break;
	   case '<':
		   if( answerexp[i+1] == '<')
		   {
			   i++;
		       num1 = s.gettop();
		       s.pop();
		       num2 = s.gettop();
		       s.pop();
		       s.push( (num2<<num1));
		   }
		   i++;
		   break;
	   case '>':
		   if( answerexp[i+1] =='>')
		   {
			   i++;
               num1 = s.gettop();
		       s.pop();
		       num2 = s.gettop();
		       s.pop();
		       s.push( (num2>>num1) );
		   }
		   i++;
		   break;
	   case ' ': i++; break;
	   default:
		   while(ch >= '0' && ch <= '9')
		   {
		     sum+= sum*10+(ch-'0');
			 i++;
			 ch = answerexp[i];
		   }
		   s.push(sum);
		   sum = 0;
           break;
	   }
   }
   cout<<"the result is "<<s.gettop()<<endl;
   s.pop();
}


⌨️ 快捷键说明

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