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

首頁 > 學院 > 開發(fā)設計 > 正文

POJ 2106 Boolean Expressions(遞歸算法)

2019-11-10 23:10:04
字體:
供稿:網(wǎng)友

1:Boolean ExPRessions

總時間限制: 1000ms 內(nèi)存限制: 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看到網(wǎng)上的大多數(shù)都是用棧的方法解決該題的,而我則采用不同的方法,使用遞歸的思路來解決這道題布爾表達式理解成由項和因子組成,因子可由表達式組成,而表達式由項組成,而項由因子組成,即形成了遞歸的定義.
#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++;//從數(shù)組中取走一個字符			bool value=term_bool();			if(op=='&') result=result&value;			else result=result|value;		}else{			more=false;		}	}	return result;}bool term_bool(){//因為!的運算優(yōu)先級更高,所以把!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=='!'){//出現(xiàn)了!,說明讀取到了一個因子		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';				//到這里輸入中的空格已經(jīng)被去除干凈了		cout<<"Expression "<<++k<<": "<<(expression_bool()?"V":"F")<<endl;		//初始化工作		my_cnt=0;		memset(s,0,100000);	}}int main(){	oj_3_1();	return 0;}
上一篇:97. Interleaving String

下一篇:1057. Stack (30)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖江市| 房产| 信宜市| 礼泉县| 连平县| 滦平县| 巧家县| 乌兰浩特市| 江孜县| 辽中县| 罗江县| 祁阳县| 安义县| 伊春市| 双峰县| 滦平县| 巩义市| 务川| 乳山市| 东明县| 米脂县| 游戏| 舒城县| 息烽县| 衡阳县| 皮山县| 龙江县| 扎赉特旗| 鱼台县| 肇州县| 紫阳县| 旬阳县| 公安县| 张北县| 乌兰浩特市| 闽侯县| 渑池县| 岳池县| 黑山县| 石泉县| 进贤县|