国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 2106 Boolean Expressions(遞歸算法)

2019-11-11 01:10:16
字體:
來源:轉載
供稿:網友

1:Boolean ExPRessions

總時間限制: 1000ms 內存限制: 65536kB描述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: FExpression 2: VExpression 3: V看到網上的大多數都是用棧的方法解決該題的,而我則采用不同的方法,使用遞歸的思路來解決這道題布爾表達式理解成由項和因子組成,因子可由表達式組成,而表達式由項組成,而項由因子組成,即形成了遞歸的定義.
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char s[100001]={0};int my_cnt=0;bool expression_bool();bool term_bool();bool factor_bool();bool expression_bool(){//求一個表達式的值	bool result=term_bool();//求第一項的值	bool more=true;	while(more){		char op=s[my_cnt];//看一個字符,不取走		if(op=='&'||op=='|'){			my_cnt++;//從數組中取走一個字符			bool value=term_bool();			if(op=='&') result=result&value;			else result=result|value;		}else{			more=false;		}	}	return result;}bool term_bool(){//因為!的運算優先級更高,所以把!xxx也當成一個項	bool result;	char op=s[my_cnt];	if(op=='!'){		my_cnt++;		result=!factor_bool();	}else{		result=factor_bool();	}	return result;}bool factor_bool(){//求一個因子的值	bool result;	char c=s[my_cnt];	if(c=='('){//如果該因子是由括號括起來的表達式組成的話		my_cnt++;		result=expression_bool();		my_cnt++;	}else if(c=='V'){		result=true;		my_cnt++;	}else if(c=='F'){		result=false;		my_cnt++;	}else if(c=='!'){//出現了!,說明讀取到了一個因子		result=term_bool();	}	return result;}void oj_3_1(){	int k=0;	while(cin.getline(s,100000)){		char t[100001]={0};		int len=strlen(s);		for(int i=0,k=0;i<len;++i){			if(s[i]!=' '){				t[k++]=s[i];			}		}		len=strlen(t);		for(int i=0;i<len;++i){			s[i]=t[i];		}		s[len]='/0';				//到這里輸入中的空格已經被去除干凈了		cout<<"Expression "<<++k<<": "<<(expression_bool()?"V":"F")<<endl;		//初始化工作		my_cnt=0;		memset(s,0,100000);	}}int main(){	oj_3_1();	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南雄市| 鄂州市| 大石桥市| 两当县| 封开县| 高陵县| 万山特区| 岳阳县| 旌德县| 株洲市| 正阳县| 荔波县| 屏南县| 濮阳市| 潍坊市| 泰兴市| 闻喜县| 三原县| 民县| 扎赉特旗| 肥西县| 曲松县| 田阳县| 曲阳县| 阿荣旗| 广河县| 永昌县| 西昌市| 英吉沙县| 六安市| 溧阳市| 济阳县| 潜江市| 友谊县| 临朐县| 皮山县| 桦南县| 阿拉善左旗| 东兰县| 民权县| 永年县|