Documente Academic
Documente Profesional
Documente Cultură
Assignment
#include<iostream>
#include<vector>
using namespace std;
bool checkEquivalence(vector<pair<int,int>> &relation,vector<int> &domain){
vector<vector<pair<int,int>>> sum_hash(10);
bool reflexive=true;
bool symmetric=true;
bool transitive=true;
int refcount=0;
for(int i=0;i<relation.size();i++){
if(relation[i].first==relation[i].second) refcount++;
else sum_hash[(relation[i].first+relation[i].second)%10].push_back(relation[i]);
}
if(refcount!=domain.size()) reflexive=false;
for(int i=0;i<10;i++){
if(sum_hash[i].size()%2==1) symmetric=false;
}
if(!reflexive&&symmetric) transitive=false;
else{
for(int i=0;i<relation.size();i++){
if(relation[i].first==relation[i].second) continue;
else{
for(int j=0;j<relation.size();j++){
if(relation[i]==relation[j]) continue;
else if(relation[i].second==relation[j].first){
bool flag=false;
for(int k=0;k<relation.size();k++){
if(relation[k]==make_pair(relation[i].first,relation[j].second)) {
flag=true;
break;
}
}
if(!flag){
transitive=false;
break;
}
}
}
if(!transitive) break;
}
}
}
return reflexive&&symmetric&&transitive;
}
#include<iostream>
#include<stack>
using namespace std;
#define AND(a,b) a&&b
#define OR(a,b) a||b
#define NOT(a) !a
#define IMPLIES(a,b) (!a)||b
#define DOUBLEIMPLIES(a,b) ((!a)||b)&&((!b)||a)
bool evaluate(string expr,bool p,bool q){
stack<bool> operands;
stack<string> operators;
int i=0;
while(i<expr.length()){
if(expr[i]=='p' || expr[i]=='q' || expr[i]=='T' || expr[i]=='F'){
if(expr[i]=='p'){
operands.push(p);
}
else if (expr[i]=='q'){
operands.push(q);
}
else if (expr[i]=='T'){
operands.push(true);
}
else{
operands.push(false);
}
}
else if(expr[i]=='('){
int bracketcount=1;
int j=i+1;
while(bracketcount){
if(expr[j]==')') bracketcount--;
else if(expr[j]=='(') bracketcount++;
j++;
}
operands.push(evaluate(expr.substr(i+1,j-i-2),p,q));
i=j-1;
}
else{
if(expr[i]=='<'){
operators.push("<>");
i++;
}
else{
operators.push(expr.substr(i,1));
}
}
i++;
}
bool result;
while(!operators.empty()){
string op=operators.top();
operators.pop();
if(op=="~"){
bool a1=operands.top();
operands.pop();
result=(NOT(a1));
}
else{
bool a1=operands.top();
operands.pop();
bool a2=operands.top();
operands.pop();
if(op=="^"){
result=(AND(a2,a1));
}
else if(op=="V"){
result=(OR(a2,a1));
}
else if(op==">"){
result=(IMPLIES(a2,a1));
}
else if(op=="<>"){
result=(DOUBLEIMPLIES(a2,a1));
}
}
operands.push(result);
}
return operands.top();
}
void checkLogicalExpression(string expr){
bool truthtable[4];
int k=0;
bool tautology=true,contradiction=true;
for(int i=0;i<=1;i++){
for(int j=0;j<=1;j++){
truthtable[k]=evaluate(expr,i,j);
if(truthtable[k]==true){
contradiction=false;
}
else{
tautology=false;
}
k++;
}
}
if(tautology){
cout<<"it's a tautology"<<endl;
}
else if(contradiction){
cout<<"it's a contradiction"<<endl;
}
else{
cout<<"it's a contingency"<<endl;
}
}
#include <iostream>
#include <vector>
using namespace std;
vector<int> UnionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>& domain){
//where domain is the same for both mutisets
vector<int> result;
int count1=0,count2=0,k,l;
for(int i=0;i<domain.size();i++){
int j=domain[i];
count1=0;
count2=0;
for(k=0;k<Mset1.size();k++){
if(Mset1[k]==j) count1++;
}
for(k=0;k<Mset2.size();k++){
if(Mset2[k]==j) count2++;
}
l=max(count1,count2);
for(k=0;k<l;k++){
result.push_back(j);
}
}
return result;
}
vector<int> IntersectionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>&
domain){
//where domain is the same for both mutisets
vector<int> result;
int count1=0,count2=0,k,l;
for(int i=0;i<domain.size();i++){
int j=domain[i];
count1=0;
count2=0;
for(k=0;k<Mset1.size();k++){
if(Mset1[k]==j) count1++;
}
for(k=0;k<Mset2.size();k++){
if(Mset2[k]==j) count2++;
}
l=min(count1,count2);
for(k=0;k<l;k++){
result.push_back(j);
}
}
return result;
}
vector<int> AdditionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>& domain){
//where domain is the same for both mutisets
vector<int> result;
int count1=0,count2=0,k,l;
for(int i=0;i<domain.size();i++){
int j=domain[i];
count1=0;
count2=0;
for(k=0;k<Mset1.size();k++){
if(Mset1[k]==j) count1++;
}
for(k=0;k<Mset2.size();k++){
if(Mset2[k]==j) count2++;
}
l=count1+count2;
for(k=0;k<l;k++){
result.push_back(j);
}
}
return result;
}
vector<int> SubtractionMultiSet(vector<int>& Mset1,vector<int>& Mset2,vector<int>&
domain){
//where domain is the same for both mutisets
vector<int> result;
int count1=0,count2=0,k,l;
for(int i=0;i<domain.size();i++){
int j=domain[i];
count1=0;
count2=0;
for(k=0;k<Mset1.size();k++){
if(Mset1[k]==j) count1++;
}
for(k=0;k<Mset2.size();k++){
if(Mset2[k]==j) count2++;
}
l=count1-count2;
for(k=0;k<l;k++){
result.push_back(j);
}
}
return result;
}