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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

POJ 2106 Boolean Expressions(遞歸算法)

2019-11-10 22:02:39
字體:
供稿:網(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ù)都是用棧的方法解決該題的,而我則采用不同的方法,使用遞歸的思路來解決這道題布爾表達(dá)式理解成由項和因子組成,因子可由表達(dá)式組成,而表達(dá)式由項組成,而項由因子組成,即形成了遞歸的定義.
#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(){//求一個表達(dá)式的值	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也當(dāng)成一個項	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=='('){//如果該因子是由括號括起來的表達(dá)式組成的話		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;}
上一篇:除法

下一篇:poj1328

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 临城县| 红原县| 崇文区| 平顶山市| 定陶县| 迭部县| 拜城县| 乐清市| 永胜县| 靖江市| 沅江市| 易门县| 盖州市| 凭祥市| 东丰县| 禹州市| 兴化市| 新营市| 喀什市| 伊宁市| 辉南县| 汕头市| 民丰县| 新昌县| 长沙县| 新兴县| 鹿泉市| 绥宁县| 旬邑县| 安溪县| 澄迈县| 宁远县| 嘉祥县| 旬阳县| 南澳县| 绍兴县| 奈曼旗| 读书| 龙里县| 巴里| 崇左市|