005:Boolean Expressions

代码很挫,绝对原创!

描述

The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: 

Expression: ( V | V ) & F & ( F | V )


where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed. 

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file. 

输入

The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown. 

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below. 

输出

For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line. 

Use the same format as that shown in the sample output shown below. 

样例输入

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

样例输出

Expression 1: F
Expression 2: V
Expression 3: V
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

string Fun1(string strexp);
string Fun2(string strexp);
string Fun3(string strexp);

string Fun3(string strexp)
{
	if(strexp.length()>1)
	{
		for(int i=0;i<strexp.length();i++)
		{
			if(strexp[i]=='!')
			{
				if(strexp[i+1]=='V')
				{
					strexp=strexp.replace(i,2,"F");
					if(strexp.length()==1)
					{
						break;
					}
					i=-1;
					continue;
				}
				else if(strexp[i+1]=='F')
				{
					strexp=strexp.replace(i,2,"V");
					if(strexp.length()==1)
					{
						break;
					}
					i=-1;
					continue;
				}
			}
		}
	}
	
	return strexp;
}

string Fun1(string strexp)
{
	if(strexp.length()>1)
	{
		strexp=Fun3(strexp);
		for(int i=0;i<strexp.length();i++)
		{
			if(strexp[i+1]=='&')
			{
				if((strexp[i]=='V')&&(strexp[i+2]=='V'))
				{
					strexp=strexp.replace(i,3,"V");
					if(strexp.length()==1)
					{
						break;
					}
					i=-1;
					continue;
				}
				else
				{
					strexp=strexp.replace(i,3,"F");
					if(strexp.length()==1)
					{
						break;
					}
					i=-1;
					continue;
				}
			}
			else if(strexp[i+1]=='|')
			{
				if((strexp[i]=='V')||(strexp[i+2]=='V'))
				{
					strexp=strexp.replace(i,3,"V");
					if(strexp.length()==1)
					{
						break;
					}
					i=-1;
					continue;
				}
				else
				{
					strexp=strexp.replace(i,3,"F");
					if(strexp.length()==1)
					{
						break;
					}
					i=-1;
					continue;
				}
			}
		}
	}
	
	return strexp;
}

string Fun2(string strexp)
{
	//找括号
	if(strexp.length()>1)
	{
		for(int i=0;i<strexp.length();i++)
		{
			if(strexp[i]==')')
			{
				int iposbeg=strexp.rfind('(',i);
				string strtemp=strexp.substr(iposbeg+1,i-iposbeg-1);
				strexp.replace(iposbeg,i-iposbeg+1,Fun1(strtemp));

				if(strexp.length()==1)
				{
					break;
				}
				i=-1;
				continue;
			}
		}
	}
	
	return strexp;
}

 void trim(string &s)
 {
      
      if( !s.empty() )
      {
          s.erase(0,s.find_first_not_of(" "));
          s.erase(s.find_last_not_of(" ") + 1);
      }
      
     int index = 0;
     if( !s.empty())
     {
         while( (index = s.find(' ',index)) != string::npos)
         {
             s.erase(index,1);
         }
     }
 
 }


int main()
{
	vector<string> vecstralls;
	string strexp;
	int iflag=0;
	while(getline(cin,strexp))
	{
		iflag++;
		trim(strexp);
		string str1,str2,str3;
		str2=Fun2(strexp);
		str3=Fun3(str2);
		str1=Fun1(str3);
		stringstream ss;
		ss<<iflag;
		vecstralls.push_back("Expression "+ss.str()+": "+str1);
		//cout<<str1<<endl;
	}
//	string strexp;
//	cin>>strexp;
	for(int j=0;j<vecstralls.size();j++)
	{
		cout<<vecstralls[j]<<endl;
	}

	return 0;
}
发布了161 篇原创文章 · 获赞 17 · 访问量 73万+

猜你喜欢

转载自blog.csdn.net/nanfeiyannan/article/details/104250370