用C或C++编写程序,要求: 输入命题公式,给出它的主合取范式和主析取范式。
用C或C++编写程序,要求:输入命题公式,给出它的主合取范式和主析取范式。例如运行时,从键盘输入:p∧q∨r程序能根据这个输入求出主析取范式:(!p∧!q∧r)∨(!p∧...
用C或C++编写程序,要求:
输入命题公式,给出它的主合取范式和主析取范式。
例如运行时,从键盘输入:
p∧q∨r
程序能根据这个输入求出主析取范式:
(!p∧!q∧r) ∨(!p∧q∧r) ∨(p∧!q∧r) ∧p∧q∧!r) ∨(p∧q∧r)
同样得到主合取范式,∨可用or或&代替,∧可以用and 或&&代替
输入:(p->q)->q
得到主析取范式:(p∧!q)∨(!p∧q)∨(p∧q)
p∨q∨r (主合取范式)
以下是原题:
1、设计并开发一个能帮助学习数理逻辑的小软件。
2、结合离散数学的相关知识,掌握VC等集成开发环境,开发一个能辅助教学的小软件。
1、输入命题公式,该软件能判断公式的类型,并列出真值表。
2、输入命题公式,给出它的主合取范式和主析取范式。
1、使用VC等集成开发环境设计并开发出一个辅助教学的小软件。
2、能够对命题公式的类型做出判断,能列出真值表,写出主范式。
3、有能力者可扩充数理逻辑的其他内容,譬如推理系统的构造和证明,求谓词公式的前束范式等。
谢谢了 展开
输入命题公式,给出它的主合取范式和主析取范式。
例如运行时,从键盘输入:
p∧q∨r
程序能根据这个输入求出主析取范式:
(!p∧!q∧r) ∨(!p∧q∧r) ∨(p∧!q∧r) ∧p∧q∧!r) ∨(p∧q∧r)
同样得到主合取范式,∨可用or或&代替,∧可以用and 或&&代替
输入:(p->q)->q
得到主析取范式:(p∧!q)∨(!p∧q)∨(p∧q)
p∨q∨r (主合取范式)
以下是原题:
1、设计并开发一个能帮助学习数理逻辑的小软件。
2、结合离散数学的相关知识,掌握VC等集成开发环境,开发一个能辅助教学的小软件。
1、输入命题公式,该软件能判断公式的类型,并列出真值表。
2、输入命题公式,给出它的主合取范式和主析取范式。
1、使用VC等集成开发环境设计并开发出一个辅助教学的小软件。
2、能够对命题公式的类型做出判断,能列出真值表,写出主范式。
3、有能力者可扩充数理逻辑的其他内容,譬如推理系统的构造和证明,求谓词公式的前束范式等。
谢谢了 展开
4个回答
展开全部
A-Z + is OR * is AND _ is → # is⊕(圆圈里加个+) @ is ⊙
$ is ↑ 命题的"与非" 运算( "与非门" )
% is ↓ 命题的"或非"运算( "或非门" )
Input the source formula:
A*!S+R
Here!
8countTerms
NORMALc: (A*!S*!R)+(!A*!S*R)+(A*!S*R)+(!A*S*R)+(A*S*R)
NORMALd (A+S+R)*(A+!S+R)*(!A+!S+R)
!A+S*!R
Input the source formula:
(!A+B)_R
Here!
8countTerms
NORMALc: (!A*!B*!R)+(A*!B*!R)+(!A*B*!R)+(A*B*!R)+(!A*!B*R)+(!A*B*R)+(A*B*R)
NORMALd (!A+B+!R)
Error!
Input the source formula:
A#B
Here!
4countTerms
NORMALc: (A*!B)+(!A*B)
NORMALd (A+B)*(!A+!B)
Error!
Input the source formula:
A@B
Here!
4countTerms
NORMALc: (!A*!B)+(A*B)
NORMALd (!A+B)*(A+!B)
Error!
#include <string>
#include <stack>
#include <vector>
#include<iostream>
using namespace std;
class formulaBase
{
private:
int numVar;//The number of the variables in the formula
bool variables[100];//To store the value of the variables
string sourceFormula;
string normalCFormula;
string normalDFormula;
string dualFormula;
vector<char> vctofVar;
vector<char> vctofPoland;
stack<char> stk;
bool isVar(char ch)const;
void addMin(int minterm);
void addMax(int maxterm);
bool compute(int minterm);
void getInversePoland();
int countTerms(int n);
void assign(int minterm);
stack<bool> boolStk;
public:
formulaBase();
formulaBase(const formulaBase& rhs);
~formulaBase();
void getSource();
string generateNormalC();
string generateNormalD();
string getDual();
void printSource()const{cout<<sourceFormula<<endl;}
void printDNormal()const{cout<<normalDFormula<<endl;}
void printCNormal()const{cout<<normalCFormula<<endl;}
void printDual()const{cout<<dualFormula<<endl;}
//void printTruthTable();
};
formulaBase::formulaBase()
{
for(int i=0;i<100;i++)variables[i]=false;
numVar=0;
}
formulaBase::formulaBase(const formulaBase& rhs)
{
sourceFormula=rhs.sourceFormula;
for(int i=0;i<100;i++)variables[i]=false;
numVar=0;
}
formulaBase::~formulaBase()
{
while(!stk.empty())stk.pop();
vctofVar.clear();
vctofPoland.clear();
}
int formulaBase::countTerms(int n)
{
if(n==0)
{
cout<<"invalid input!"<<endl;
exit(0);
}
switch(n)
{
case 1:return 2;
case 2:return 4;
default:
{
int tempA=2,tempB=2;
for(int i=2;i<=n;i*=2)tempA*=tempA;
i/=2;
if(i==n)return tempA;
i=n-i;
for(int j=2;j<=i;j*=2)tempB*=tempB;
for(j/=2;j<i;j++)tempB*=2;
tempB*=tempA;
return tempB;
}
}
}
bool formulaBase::isVar(char ch)const
{
if (ch>='A'&&ch<='Z')
return true;
return false;
}
void formulaBase::getSource()
{
cout<<"Input the source formula:"<<endl;
cin>>sourceFormula;
/*if(!isValid(sourceFormula))
cout<<"Invalid input !"
"Operate again:"<<endl;
cin>>sourceFormula;*/
}
void formulaBase::getInversePoland()
{
char temp,temp1;
for(int i=0;sourceFormula[i]!='\0';i++)
{
temp=sourceFormula[i];
if(isVar(temp))
{
if(!variables[temp])
{
numVar++;
vctofVar.push_back(temp);
variables[temp]=true;
}
vctofPoland.push_back(temp);
}
else
switch(temp)
{
case'_':case'$': //
case'%':case'#':
case'@':
while(!stk.empty())
{
if(stk.top()==temp)
{
vctofPoland.push_back(temp);
stk.pop();
}
else break;
}
stk.push(temp);
break;
case '(':case '!':
stk.push(temp);
break;
case '+':
while (!stk.empty())
{
if(stk.top()!='(')
{
temp1=stk.top();
vctofPoland.push_back(temp1);
stk.pop();
}
else break;
}
stk.push(temp);
break;
case '*':
while (!stk.empty())
{
temp1=stk.top();
if(stk.top()=='*'||stk.top()=='!')
{
vctofPoland.push_back(temp1);
stk.pop();
}
else
break;
}
stk.push(temp);
break;
case ')':
while (!stk.empty())
{
if(stk.top()!='(')
{
temp1=stk.top();
vctofPoland.push_back(temp1);
stk.pop();
}
else break;
}
if(stk.empty())exit(0);
stk.pop();//pop the operator '('
break;
}
}
while(!stk.empty())
{
temp1=stk.top();
vctofPoland.push_back(temp1);
stk.pop();
}
}
void formulaBase::assign(int minterm)
{
int temp=minterm;
vector<char>::const_iterator itr=vctofVar.begin();
for(;itr!=vctofVar.end();itr++)
{
variables[*itr]=bool(temp&1);
temp=temp>>1;
}
}
bool formulaBase::compute(int minterm)
{
assign(minterm);
char temp;
bool valueA,valueB;
vector<char>::const_iterator itr=vctofPoland.begin();
while (itr!=vctofPoland.end())
{
temp=*itr;
if(isVar(temp))boolStk.push(variables[temp]);
else
switch(temp)
{
case '+':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=valueA||valueB;
boolStk.push(valueA);
}
break;
case '*':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=valueA&&valueB;
boolStk.push(valueA);
}
break;
case '!':
{
if(boolStk.empty())exit(0);
valueA=!(boolStk.top());
boolStk.pop();
boolStk.push(valueA);
}
break;
case'_':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=(!valueA)||valueB;
boolStk.push(valueA);
}
break;
case'$':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=!(valueA&&valueB);
boolStk.push(valueA);
}
break;
case'%':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=!(valueA||valueB);
boolStk.push(valueA);
}
break;
case'#':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=(!valueA&&valueB)||(valueA&&!valueB);
boolStk.push(valueA);
}
break;
case'@':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=(valueA&&valueB)||(!valueA&&!valueB);
boolStk.push(valueA);
}
break;
}
itr++;
}
if(boolStk.size()!=1)
{
cout<<"Error in computing the value of minterm"<<endl;
exit(0);
}
valueA=boolStk.top();
boolStk.pop();
return valueA;
}
void formulaBase::addMin(int minterm)
{
int temp=minterm;
vector<char>::const_iterator itr=vctofVar.begin();
normalCFormula+='(';
while (itr!=vctofVar.end())
{
if(!variables[*itr])
normalCFormula+='!';
normalCFormula+=*itr;
normalCFormula+='*';
itr++;
}
normalCFormula+="\b)+";
}
void formulaBase::addMax(int maxterm)
{
int temp=maxterm;
vector<char>::const_iterator itr=vctofVar.begin();
normalDFormula+='(';
while (itr!=vctofVar.end())
{
if( variables[*itr])
normalDFormula+='!';
normalDFormula+=*itr;
normalDFormula+='+';
itr++;
}
normalDFormula+="\b)*";
}
string formulaBase::generateNormalC()
{
if(vctofPoland.size()==0)//This oeration has not been done yet!
getInversePoland();
cout<<"Here!"<<endl;
int n=countTerms(numVar);
cout<<n<<"countTerms"<<endl;
normalCFormula=' ';
for(int i=0;i<n;i++)
{
if(compute(i))addMin(i);
}
normalCFormula+="\b ";
return normalCFormula;
}
string formulaBase::generateNormalD()
{
if(vctofPoland.size()==0)//This operation has not been done yet!!
getInversePoland();
int n=countTerms(numVar);
normalDFormula=' ';
for(int i=0;i<n;i++)
{
if(!compute(i))addMax(i);
}
normalDFormula+="\b ";
return normalDFormula;
}
string formulaBase::getDual()
{
int i=0;
char temp;
dualFormula=' ';
while ((temp=sourceFormula[i])!='\0')
{
switch(temp)
{
case '!':
{
i++;
dualFormula+=sourceFormula[i];
break;
}
case '+':dualFormula+='*';break;
case '*':dualFormula+='+';break;
case'(':case ')':dualFormula+=sourceFormula[i];
break;
default:
if (isVar(temp))
{
dualFormula+='!';
dualFormula+=temp;
}
else
{
cout<<"Error!"<<endl;
exit(0);
}
}
i++;
}
return dualFormula;
}
/*void formulaBase::printTruthTable()//A const function is unable to call a nonconst function!!!
{
int i=0;
int count=countTerms(numVar);
cout<<" TRUTH TABLE \n";
for( i=0;i<=numVar;i++)cout<<"___";
cout<<endl;
//for( i=0;i<numVar;i++)cout<<'|'<<vctofVar[i];
//cout<<"|F|" <<endl;
for( i=0;i<=numVar;i++)cout<<"___";
cout<<endl;
for(i=0;i<count;i++ )
{
int temp=i;
for(int j=0;j<numVar;j++)
{
if(bool(temp&1))cout<<"|1";
else cout<<"|0";
temp=temp>>1;
}
if(this->compute(i))cout<<"|1";
else cout<<"|0";
cout<<'|'<<endl;
for( int k=0;k<=numVar;k++)cout<<"___";
cout<<endl;
}
}
*/
int main()
{
string str;
formulaBase f;
f.getSource();
str=f.generateNormalC();
cout<<"NORMALc:"<<str<<endl;
str=f.generateNormalD();
cout<<"NORMALd"<<str<<endl;
// cout<<"Coming here"<<endl;
//f.printTruthTable();
str=f.getDual();
f.printDual();
return 0;
}
$ is ↑ 命题的"与非" 运算( "与非门" )
% is ↓ 命题的"或非"运算( "或非门" )
Input the source formula:
A*!S+R
Here!
8countTerms
NORMALc: (A*!S*!R)+(!A*!S*R)+(A*!S*R)+(!A*S*R)+(A*S*R)
NORMALd (A+S+R)*(A+!S+R)*(!A+!S+R)
!A+S*!R
Input the source formula:
(!A+B)_R
Here!
8countTerms
NORMALc: (!A*!B*!R)+(A*!B*!R)+(!A*B*!R)+(A*B*!R)+(!A*!B*R)+(!A*B*R)+(A*B*R)
NORMALd (!A+B+!R)
Error!
Input the source formula:
A#B
Here!
4countTerms
NORMALc: (A*!B)+(!A*B)
NORMALd (A+B)*(!A+!B)
Error!
Input the source formula:
A@B
Here!
4countTerms
NORMALc: (!A*!B)+(A*B)
NORMALd (!A+B)*(A+!B)
Error!
#include <string>
#include <stack>
#include <vector>
#include<iostream>
using namespace std;
class formulaBase
{
private:
int numVar;//The number of the variables in the formula
bool variables[100];//To store the value of the variables
string sourceFormula;
string normalCFormula;
string normalDFormula;
string dualFormula;
vector<char> vctofVar;
vector<char> vctofPoland;
stack<char> stk;
bool isVar(char ch)const;
void addMin(int minterm);
void addMax(int maxterm);
bool compute(int minterm);
void getInversePoland();
int countTerms(int n);
void assign(int minterm);
stack<bool> boolStk;
public:
formulaBase();
formulaBase(const formulaBase& rhs);
~formulaBase();
void getSource();
string generateNormalC();
string generateNormalD();
string getDual();
void printSource()const{cout<<sourceFormula<<endl;}
void printDNormal()const{cout<<normalDFormula<<endl;}
void printCNormal()const{cout<<normalCFormula<<endl;}
void printDual()const{cout<<dualFormula<<endl;}
//void printTruthTable();
};
formulaBase::formulaBase()
{
for(int i=0;i<100;i++)variables[i]=false;
numVar=0;
}
formulaBase::formulaBase(const formulaBase& rhs)
{
sourceFormula=rhs.sourceFormula;
for(int i=0;i<100;i++)variables[i]=false;
numVar=0;
}
formulaBase::~formulaBase()
{
while(!stk.empty())stk.pop();
vctofVar.clear();
vctofPoland.clear();
}
int formulaBase::countTerms(int n)
{
if(n==0)
{
cout<<"invalid input!"<<endl;
exit(0);
}
switch(n)
{
case 1:return 2;
case 2:return 4;
default:
{
int tempA=2,tempB=2;
for(int i=2;i<=n;i*=2)tempA*=tempA;
i/=2;
if(i==n)return tempA;
i=n-i;
for(int j=2;j<=i;j*=2)tempB*=tempB;
for(j/=2;j<i;j++)tempB*=2;
tempB*=tempA;
return tempB;
}
}
}
bool formulaBase::isVar(char ch)const
{
if (ch>='A'&&ch<='Z')
return true;
return false;
}
void formulaBase::getSource()
{
cout<<"Input the source formula:"<<endl;
cin>>sourceFormula;
/*if(!isValid(sourceFormula))
cout<<"Invalid input !"
"Operate again:"<<endl;
cin>>sourceFormula;*/
}
void formulaBase::getInversePoland()
{
char temp,temp1;
for(int i=0;sourceFormula[i]!='\0';i++)
{
temp=sourceFormula[i];
if(isVar(temp))
{
if(!variables[temp])
{
numVar++;
vctofVar.push_back(temp);
variables[temp]=true;
}
vctofPoland.push_back(temp);
}
else
switch(temp)
{
case'_':case'$': //
case'%':case'#':
case'@':
while(!stk.empty())
{
if(stk.top()==temp)
{
vctofPoland.push_back(temp);
stk.pop();
}
else break;
}
stk.push(temp);
break;
case '(':case '!':
stk.push(temp);
break;
case '+':
while (!stk.empty())
{
if(stk.top()!='(')
{
temp1=stk.top();
vctofPoland.push_back(temp1);
stk.pop();
}
else break;
}
stk.push(temp);
break;
case '*':
while (!stk.empty())
{
temp1=stk.top();
if(stk.top()=='*'||stk.top()=='!')
{
vctofPoland.push_back(temp1);
stk.pop();
}
else
break;
}
stk.push(temp);
break;
case ')':
while (!stk.empty())
{
if(stk.top()!='(')
{
temp1=stk.top();
vctofPoland.push_back(temp1);
stk.pop();
}
else break;
}
if(stk.empty())exit(0);
stk.pop();//pop the operator '('
break;
}
}
while(!stk.empty())
{
temp1=stk.top();
vctofPoland.push_back(temp1);
stk.pop();
}
}
void formulaBase::assign(int minterm)
{
int temp=minterm;
vector<char>::const_iterator itr=vctofVar.begin();
for(;itr!=vctofVar.end();itr++)
{
variables[*itr]=bool(temp&1);
temp=temp>>1;
}
}
bool formulaBase::compute(int minterm)
{
assign(minterm);
char temp;
bool valueA,valueB;
vector<char>::const_iterator itr=vctofPoland.begin();
while (itr!=vctofPoland.end())
{
temp=*itr;
if(isVar(temp))boolStk.push(variables[temp]);
else
switch(temp)
{
case '+':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=valueA||valueB;
boolStk.push(valueA);
}
break;
case '*':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=valueA&&valueB;
boolStk.push(valueA);
}
break;
case '!':
{
if(boolStk.empty())exit(0);
valueA=!(boolStk.top());
boolStk.pop();
boolStk.push(valueA);
}
break;
case'_':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=(!valueA)||valueB;
boolStk.push(valueA);
}
break;
case'$':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=!(valueA&&valueB);
boolStk.push(valueA);
}
break;
case'%':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=!(valueA||valueB);
boolStk.push(valueA);
}
break;
case'#':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=(!valueA&&valueB)||(valueA&&!valueB);
boolStk.push(valueA);
}
break;
case'@':
{
if(boolStk.size()<2)exit(0);
valueA=boolStk.top();
boolStk.pop();
valueB=boolStk.top();
boolStk.pop();
valueA=(valueA&&valueB)||(!valueA&&!valueB);
boolStk.push(valueA);
}
break;
}
itr++;
}
if(boolStk.size()!=1)
{
cout<<"Error in computing the value of minterm"<<endl;
exit(0);
}
valueA=boolStk.top();
boolStk.pop();
return valueA;
}
void formulaBase::addMin(int minterm)
{
int temp=minterm;
vector<char>::const_iterator itr=vctofVar.begin();
normalCFormula+='(';
while (itr!=vctofVar.end())
{
if(!variables[*itr])
normalCFormula+='!';
normalCFormula+=*itr;
normalCFormula+='*';
itr++;
}
normalCFormula+="\b)+";
}
void formulaBase::addMax(int maxterm)
{
int temp=maxterm;
vector<char>::const_iterator itr=vctofVar.begin();
normalDFormula+='(';
while (itr!=vctofVar.end())
{
if( variables[*itr])
normalDFormula+='!';
normalDFormula+=*itr;
normalDFormula+='+';
itr++;
}
normalDFormula+="\b)*";
}
string formulaBase::generateNormalC()
{
if(vctofPoland.size()==0)//This oeration has not been done yet!
getInversePoland();
cout<<"Here!"<<endl;
int n=countTerms(numVar);
cout<<n<<"countTerms"<<endl;
normalCFormula=' ';
for(int i=0;i<n;i++)
{
if(compute(i))addMin(i);
}
normalCFormula+="\b ";
return normalCFormula;
}
string formulaBase::generateNormalD()
{
if(vctofPoland.size()==0)//This operation has not been done yet!!
getInversePoland();
int n=countTerms(numVar);
normalDFormula=' ';
for(int i=0;i<n;i++)
{
if(!compute(i))addMax(i);
}
normalDFormula+="\b ";
return normalDFormula;
}
string formulaBase::getDual()
{
int i=0;
char temp;
dualFormula=' ';
while ((temp=sourceFormula[i])!='\0')
{
switch(temp)
{
case '!':
{
i++;
dualFormula+=sourceFormula[i];
break;
}
case '+':dualFormula+='*';break;
case '*':dualFormula+='+';break;
case'(':case ')':dualFormula+=sourceFormula[i];
break;
default:
if (isVar(temp))
{
dualFormula+='!';
dualFormula+=temp;
}
else
{
cout<<"Error!"<<endl;
exit(0);
}
}
i++;
}
return dualFormula;
}
/*void formulaBase::printTruthTable()//A const function is unable to call a nonconst function!!!
{
int i=0;
int count=countTerms(numVar);
cout<<" TRUTH TABLE \n";
for( i=0;i<=numVar;i++)cout<<"___";
cout<<endl;
//for( i=0;i<numVar;i++)cout<<'|'<<vctofVar[i];
//cout<<"|F|" <<endl;
for( i=0;i<=numVar;i++)cout<<"___";
cout<<endl;
for(i=0;i<count;i++ )
{
int temp=i;
for(int j=0;j<numVar;j++)
{
if(bool(temp&1))cout<<"|1";
else cout<<"|0";
temp=temp>>1;
}
if(this->compute(i))cout<<"|1";
else cout<<"|0";
cout<<'|'<<endl;
for( int k=0;k<=numVar;k++)cout<<"___";
cout<<endl;
}
}
*/
int main()
{
string str;
formulaBase f;
f.getSource();
str=f.generateNormalC();
cout<<"NORMALc:"<<str<<endl;
str=f.generateNormalD();
cout<<"NORMALd"<<str<<endl;
// cout<<"Coming here"<<endl;
//f.printTruthTable();
str=f.getDual();
f.printDual();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
输入格式? 例子?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ds
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
题目真的不清晰的。详细一点好吗?把原题拿来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询