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 zihuacs at 2010-06-08 17:22:28 on Problem 2106
#include<iostream>
#include<string>
using namespace std;
char ch;               
char temp_str[500];
char str[500];
int k;
char Getch()
{
	return str[k++];   
}
void Putch()
{
	k--;
}

bool expression(void);

bool factor(void)
{
	bool re;
	ch=Getch();
	switch(ch)
	{
	case 'V':
		re=true;
		break;
	case 'F':
		re=false;
		break;
	case '(':
		re=expression();
		break;
	case '!':
		ch=Getch();
		if(ch=='(')
		{
			Putch();
			re=expression();
			Putch();
			re=!re;
		}
		else
		{
			Putch();
			re=factor();
			Putch();
			re=!re;
		}
		break;
	default:
		break;
	}
	ch=Getch();
	return re;
}

bool term(void)
{
	bool re=factor();
	while(true)
	{
		switch(ch)
		{
		case '&':
			re  = re & factor();
			break;
		default:
			return re;
		}
	}
	
}

bool expression(void)
{
	bool re=term();

	while(true)
	{
		switch(ch)
		{
		case '|':
			re =re | term();
			break;
		default:
			return re;
		}
	}
}

int main(void)
{
	int i,j,t;
	t=1;
	while(gets(temp_str)!=NULL)
	{
		j=0;
		for(i=0;temp_str[i]!='\0';i++)
		{
			if(temp_str[i]!=' ')
				str[j++]=temp_str[i];
		}
		str[j]='=';   // 结束标志
		str[j+1]='\0';
//				puts(str);
		
		k=0;
		printf("Expression %d: ",t);
		if(expression())
			printf("V\n");
		else
			printf("F\n");

		t++;
	}
	return 0;
}

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