$ 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=0i<100i++)variables[i]=false
numVar=0
}
formulaBase::formulaBase(const formulaBase&rhs)
{
sourceFormula=rhs.sourceFormula
for(int i=0i<100i++)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=2i<=ni*=2)tempA*=tempA
i/=2
if(i==n)return tempA
i=n-i
for(int j=2j<=ij*=2)tempB*=tempB
for(j/=2j<ij++)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=0sourceFormula[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=0i<ni++)
{
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=0i<ni++)
{
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=0i<=numVari++)cout<<"___"
cout<<endl
//for( i=0i<numVari++)cout<<'|'<<vctofVar[i]
//cout<<"|F|" <<endl
for( i=0i<=numVari++)cout<<"___"
cout<<endl
for(i=0i<counti++ )
{
int temp=i
for(int j=0j<numVarj++)
{
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=0k<=numVark++)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
}
常用的方法有两种,等值演算法和真值表法,等值演算法,就是按照步骤推导公式,最终得到主合取范式或者主析取范式咐绝。
检查主合取埋简盯范式中遗漏的4个主项p∨q∨¬r,p∨¬q∨¬r,¬p∨q∨¬r,¬p∨¬q∨r可以反推出它的主析取范式⇔(¬p∧¬q∧r)∨(¬p∧q∧r)∨(p∧¬q∧r)∨(p∧q∧¬r)得到主析取范式。
主析取范式
是大学数学里一门名叫离散数学(Discrete mathematics)的课程中的内容,在离散数学的数理逻辑一节中,利用真值表和等值演算法可以化简或推证一些命题,但是当命题的变元的数目较多时,上述方法都显得不方便,所以需弯和要给出把命题公式规范的方法,即把命题公式化成主合取范式和主析取范式的方法。
析取范式和合取范式如下:
约束条件可以是由独立的表达式构成的,也可枣亮以是由多个表达式构成的合取范式或析取范式,其中独立的表达式需要根据统计信息计算选择率,合取范式和析取范式则借助计算概率的方法获得选择率。
合取范式:P(A and B) = P(A) + P(B) – P(AB)
析取范式:P(AB) = P(A) × P(B)
假设要对约束条件“A>5andB<3”计算选择率,那么首先需要对 A>5和B<3分别计算选择率,由于已经有了A 列和B 列的统计信息,因此可以根据统计信息计算出A 列中值大于5的数据比例。
数学的背后:
SQL是丰富多镇岩扰样的,应用非常灵活,不同的开发人员依据不同的经验,编写的SQL语句也是各式各样,SQL语句还可以通过工具自动生成。
SQL是一种描述性语言,数据库的使用者只是描述了想要的结果,而不关心数据的具体获取方式。输入数御旦据库的SQL语句很难做到以最优形式表示,往往隐含了冗余信息,这些信息可以被挖掘以生成更加高效的SQL 语句。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)