Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一直在想那个格式问题究竟该怎么破?

Posted by lijunyi at 2017-07-24 22:41:48 on Problem 2106
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include<string>
using namespace std;
int count = 1;

bool expression_value (string s);
bool term_value(string s);
int main()
{
	string s;
	getline(cin,s,'\n');
	char a[100]= {'\0'};int tot = 0;
 	for(int i = 0;i < s.length();++i)
	{
 		if (s[i] != ' ')
	 	{
	 		a[tot++] = s[i];
		}
	} 
 	s = string (a);
 	
 	//去掉那个最大的括号(如果有的话)
	if (s[0] == '(' && s[s.length() - 1] == ')')
 	{//还需要确认下 是不是左右括号匹配 
 		int m = 0,flag = 0;
 		for (;m < s.length();++m)
 		{
 			if (s[m] == '(')flag ++;
 			if (s[m] == ')')flag --;
 			if (flag == 0)break;
		}
		if (m == s.length() - 1)//说明需要去除那个括号 
		{
			string b = s.substr(1,s.length() - 2);//去除掉左边那个括号,左边的起始位置就应该是第1,长度为s.length() - 2;
			s = b;//完成赋值 
		}
	}
	cout << "Expression "<<count <<":"; 
	if (expression_value(s))cout <<'V';
	else cout << 'F';
	++count;
	
	
	while (getline(cin,s,'\n'))
	{//读入这个串	
		//将所有的空格都给删除掉
 		char a[100]= {'\0'};int tot = 0;
 		for(int i = 0;i < s.length();++i)
		{
	 		if (s[i] != ' ')
	 		{
	 			a[tot++] = s[i];
			}
		} 
 		s = string (a);
 		
 		//去掉那个最大的括号(如果有的话)
		if (s[0] == '(' && s[s.length() - 1] == ')')
 		{//还需要确认下 是不是左右括号匹配 
 			int m = 0,flag = 0;
 			for (;m < s.length();++m)
 			{
 				if (s[m] == '(')flag ++;
 				if (s[m] == ')')flag --;
 				if (flag == 0)break;
			}
			if (m == s.length() - 1)//说明需要去除那个括号 
			{
				string b = s.substr(1,s.length() - 2);//去除掉左边那个括号,左边的起始位置就应该是第1,长度为s.length() - 2;
				s = b;//完成赋值 
			}
		}
		cout <<endl<< "Expression "<<count <<":"; 
		if (expression_value(s))cout <<'V';
		else cout << 'F';
		++count;
	}
}

bool term_value(string s)
{
	if (s[0] == '('){
		return expression_value(s.substr(1,s.length() - 2));
	}
	if (s[0] == '!'){
		return !term_value(s.substr(1,s.length() - 1));
	}else if(s[0] == 'V')return true;
	else if (s[0] == 'F')return false; 
}

bool expression_value(string s)
{
	int k = 0,flag2 = 0;
	for (k = 0;k < s.length();++k){
		if (s[k] == '(')++flag2;
		else if (s[k] == ')')--flag2;
		if (flag2 == 0&& (s[k] =='&'|| s[k] == '|'))break;
	}
	bool result = term_value(s.substr(0,k));//如果是不含有 & 或 |运算式子就可以使得k = s.length那么就相当于完全开始了
	
	if (k == s.length())return result; //k就是这个运算符的位置
	
	int x = k + 1,len = 0;//一个记录左值 一个记录长度,用来使用substr(x,l);
	int oper;
	if (s[k] == '|')oper = 1;
	else oper = 0;

	int flag = 0;//flag == 1,表示这个等式的位置在里面
	for(int i = x;i < s.length();++i){
		if (s[i] == '(')++flag;
		else if (s[i] == ')')--flag;
		if (flag == 0&& (s[i] =='&'|| s[i] == '|')){//flag == 0
			bool value = term_value(s.substr(x,len));
			if (oper == 0){
				result &= value;
			}else result |= value;
			if (s[i] == '&')oper = 0;
			else oper = 1;
			x = i + 1;
			len = 0;
		}else ++len;
	}
	if (x <= s.length() - 1)
	{
		bool value = term_value(s.substr(x));
		if (oper == 0)
		{
			result &= value;
		}else result |= value;
	}
	return result;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator