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

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

POJ 2106 Boolean Expressions(遞歸算法)

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

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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘谷县| 尖扎县| 镇赉县| 辉南县| 边坝县| 绥德县| 孝义市| 陆丰市| 万盛区| 二手房| 通州区| 库尔勒市| 阿拉善右旗| 永福县| 文成县| 内丘县| 凯里市| 奉贤区| 宕昌县| 舟山市| 邓州市| 广灵县| 吴川市| 班戈县| 土默特右旗| 建阳市| 南雄市| 合阳县| 容城县| 鄂伦春自治旗| 邮箱| 合阳县| 谷城县| 全州县| 安吉县| 富源县| 灯塔市| 都兰县| 永定县| 新郑市| 贡嘎县|