西工大機の試験問題--命題の公式の真の値の表を計算します
命題式の真値テーブルを出力
質問説明:まず正の整数n(nが10以下)を入力し、n個の命題変数を共有することを表し、逆ポーランド式に類似する文字列を入力して命題式を表し、この文字列の中で1桁の10進数で1つの命題変数を表し、a、o、n、i、eでそれぞれ表し、または、非、含む、等値を表すことを約束する.逆ポーランド式のような文字列で表される命題式の真値表ポーランド式(すなわち、2つのオペランドが前、演算子が後、1つのオペランドが前、演算子が後)です.入力は、10以下の正の整数nを入力し、文字列を入力します.文字列が表す命題式の真値テーブルを出力します.ヒント:この3つの命題変数をP、Q、Rでそれぞれ表すと、入力データ01 a 2 iで表される命題式は、(((P∧Q)→R)入力データ012 iaで表される命題式は、(P∧(Q→R))入力データ0 nで表される命題式は、(P∧(Q→R)入力データ0 nで表される命題式は、[P入力サンプル3 01 a 2 i出力サンプル0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
コード:
質問説明:まず正の整数n(nが10以下)を入力し、n個の命題変数を共有することを表し、逆ポーランド式に類似する文字列を入力して命題式を表し、この文字列の中で1桁の10進数で1つの命題変数を表し、a、o、n、i、eでそれぞれ表し、または、非、含む、等値を表すことを約束する.逆ポーランド式のような文字列で表される命題式の真値表ポーランド式(すなわち、2つのオペランドが前、演算子が後、1つのオペランドが前、演算子が後)です.入力は、10以下の正の整数nを入力し、文字列を入力します.文字列が表す命題式の真値テーブルを出力します.ヒント:この3つの命題変数をP、Q、Rでそれぞれ表すと、入力データ01 a 2 iで表される命題式は、(((P∧Q)→R)入力データ012 iaで表される命題式は、(P∧(Q→R))入力データ0 nで表される命題式は、(P∧(Q→R)入力データ0 nで表される命題式は、[P入力サンプル3 01 a 2 i出力サンプル0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
コード:
#include
#include
//#include
#include
using namespace std;
//char str[20];
string str;
int ans[20];
stack<char>s;
int len;
int n;
// , 。
void run()
{
while(!s.empty()){
s.pop();
}
char s1,s2,s3;
int top=1;
for(int i=0;i<len;i++){
if(str[i]=='a'){
s1=s.top();
s.pop();
s2=s.top();
s.pop();
if(s1=='0'||s2=='0'){
//s3='0';
s.push('0');
}
else{
s.push('1');
}
}
else if(str[i]=='o'){
s1=s.top();
s.pop();
s2=s.top();
s.pop();
if(s1=='0'&&s2=='0'){
//3='1';
s.push('0');
}
else{
//s3='0';
s.push('1');
}
}
else if(str[i]==i){
s1=s.top();
s.pop();
s2=s.top();
s.pop();
if(s2=='1'&&s1=='0'){
//s3='0';
s.push('0');
}
else{
//s3='1';
s.push('1');
}
}
else if(str[i]=='e'){
s1=s.top();
s.pop();
s2=s.top();
s.pop();
if(s1==s2){
s3='1';
s.push('1');
}
else{
s3='0';
s.push('0');
}
}
else if(str[i]=='n'){
s1=s.top();
if(s1=='0'){
//s3='1';
s.push('1');
}
else{
//s3='0';
s.push('0');
}
}
else{
s3=ans[top++]+'0';
s.push(s3);
}
}
}
void print_info(){
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
//printf("%d ",ans[i]);
}
cout<<s.top()<<endl;
//printf("%c",s.top());
}
// ,
void dfs(int r){
if(r>n){
run();print_info();return;}
else{
for(int i=0;i<2;i++){
ans[r]=i;
dfs(r+1);// r++, r=r+1, dfs() , , !
}
}
}
int main(){
cin>>n;
cin>>str;
//scanf("%s",str);
//len=strlen(str);//strlen() , ,
len=str.length();// length() , 。
dfs(1);
return 0;
}