|
Need help to reduce complexity of this CPP
Please i wish you can find me a way to reduce the complexity of this peice of code
It may look big
Just compile it and you'll understand
#include<iostream>
#include<cstring>
#include<fstream>
#include<cmath>
#include<conio.h>
#include<windows.h>
#include<string>
#include<iomanip>
using namespace std;
const double UNI=1.3; //UNI: speed of letters appearance
/*//////////////////////////////////////////////////////////////////////////////////////////*/
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/*//////////////////////////////////////////////////////////////////////////////////////////*/
void delay(double time)
{
for(int c=0;c<time*10000000;c++)
{}
}
/*function despace deletes the spaces in the cstring
Done by Sherif and Abdulla*/
void despace(char A[], int& n)
{
for( int i=0;i<n;i++ )
{
if(A[i]==' ' && A[i+1]!=' ')
{
A[i]=A[i+1];
A[i+1]=' ';
i=i-2;
}
}
n=strlen(A);
}
void TorF(char A[],int n)
{
for(int i=0;i<n;i++)
{
if (A[i]=='F')
A[i]=0;
if (A[i]=='T')
A[i]=1;
}
}
void simpleNOT(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='N') && (A[i+1]=='O') && (A[i+2]=='T') && (A[i+3]=='1'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
i+=3;
}
else if ((A[i]=='N') && (A[i+1]=='O') && (A[i+2]=='T') && (A[i+3]=='0'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
i+=3;
}
}
despace(A,n);
}
void AND(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='1') && (A[i+1]=='A') && (A[i+2]=='N') && (A[i+3]=='D') && (A[i+4]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
else if ((A[i]=='1') && (A[i+1]=='A') && (A[i+2]=='N') && (A[i+3]=='D') && (A[i+4]=='0'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
else if ((A[i]=='0') && (A[i+1]=='A') && (A[i+2]=='N') && (A[i+3]=='D') && (A[i+4]=='1'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
else if ((A[i]=='0') && (A[i+1]=='A') && (A[i+2]=='N') && (A[i+3]=='D') && (A[i+4]=='0'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
}
despace(A,n);
}
void XOR(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='1') && (A[i+1]=='X') && (A[i+2]=='O') && (A[i+3]=='R') && (A[i+4]=='1'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
else if ((A[i]=='1') && (A[i+1]=='X') && (A[i+2]=='O') && (A[i+3]=='R') && (A[i+4]=='0'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
else if ((A[i]=='0') && (A[i+1]=='X') && (A[i+2]=='O') && (A[i+3]=='R') && (A[i+4]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
else if ((A[i]=='0') && (A[i+1]=='X') && (A[i+2]=='O') && (A[i+3]=='R') && (A[i+4]=='0'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
i+=4;
}
}
despace(A,n);
}
void IMPLIES(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='1') && (A[i+1]=='I') && (A[i+2]=='M') && (A[i+3]=='P') && (A[i+4]=='L')&& (A[i+5]=='I') && (A[i+6]=='E') && (A[i+7]=='S') && (A[i+8]=='0'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
A[i+6]=' ';
A[i+7]=' ';
A[i+8]=' ';
i+=8;
}
else if ((A[i]=='1') && (A[i+1]=='I') && (A[i+2]=='M') && (A[i+3]=='P') && (A[i+4]=='L')&& (A[i+5]=='I') && (A[i+6]=='E') && (A[i+7]=='S') && (A[i+8]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
A[i+6]=' ';
A[i+7]=' ';
A[i+8]=' ';
i+=8;
}
else if ((A[i]=='0') && (A[i+1]=='I') && (A[i+2]=='M') && (A[i+3]=='P') && (A[i+4]=='L')&& (A[i+5]=='I') && (A[i+6]=='E') && (A[i+7]=='S') && (A[i+8]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
A[i+6]=' ';
A[i+7]=' ';
A[i+8]=' ';
i+=8;
}
else if ((A[i]=='0') && (A[i+1]=='I') && (A[i+2]=='M') && (A[i+3]=='P') && (A[i+4]=='L')&& (A[i+5]=='I') && (A[i+6]=='E') && (A[i+7]=='S') && (A[i+8]=='0'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
A[i+6]=' ';
A[i+7]=' ';
A[i+8]=' ';
i+=8;
}
}
despace(A,n);
}
void EQUI(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='1') && (A[i+1]=='E') && (A[i+2]=='Q') && (A[i+3]=='U') && (A[i+4]=='I') && (A[i+5]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
i+=5;
}
else if ((A[i]=='1') && (A[i+1]=='E') && (A[i+2]=='Q') && (A[i+3]=='U') && (A[i+4]=='I') && (A[i+5]=='0'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
i+=5;
}
else if ((A[i]=='0') && (A[i+1]=='E') && (A[i+2]=='Q') && (A[i+3]=='U') && (A[i+4]=='I') && (A[i+5]=='0'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
i+=5;
}
else if ((A[i]=='0') && (A[i+1]=='E') && (A[i+2]=='Q') && (A[i+3]=='U') && (A[i+4]=='I') && (A[i+5]=='1'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
i+=5;
}
}
despace(A,n);
}
void OR(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='1') && (A[i+1]=='O') && (A[i+2]=='R') && (A[i+3]=='0'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
i+=3;
}
else if ((A[i]=='1') && (A[i+1]=='O') && (A[i+2]=='R') && (A[i+3]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
i+=3;
}
else if ((A[i]=='0') && (A[i+1]=='O') && (A[i+2]=='R') && (A[i+3]=='0'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
i+=3;
}
else if ((A[i]=='0') && (A[i+1]=='O') && (A[i+2]=='R') && (A[i+3]=='1'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
i+=3;
}
}
despace(A,n);
}
void reduce(char A[],int& n)
{
for (int i=0;i<n;i++)
{
if ((A[i]=='(') && (A[i+1]=='1') && (A[i+2]==')'))
{
A[i]='1';
A[i+1]=' ';
A[i+2]=' ';
}
if ((A[i]=='(') && (A[i+1]=='0') && (A[i+2]==')'))
{
A[i]='0';
A[i+1]=' ';
A[i+2]=' ';
}
}
despace(A,n);
}
/*function Evaluate calls the functions that of the operators and these
functions work to simplify the expression
Done by Sherif and Abdulla*/
void EVALUATE(char A[], int& n)
{
simpleNOT(A,n);
AND(A,n);
OR(A,n);
XOR(A,n);
EQUI(A,n);
IMPLIES(A,n);
reduce(A,n);
}
void NEWSubstitute(int i,char A[],int n,int NUM,ifstream& in)
{
int r=i+1;
char *s;
s = new char[(pow(2,NUM))+1];
for (int num=0;num<NUM;num++)
{
in >>s;
for(int c=0;c<n+1;c++)
{
if (A[c]==s[0])
A[c]=s[r];
}
}
}
/*function Substitute substitutes for p,q,and r the corresponding
value either 1 or 0 (True or False)
Done by Sherif and Abdulla*/
void Substitute(int i,char A[],int n)
{
char temp;
string P="11110000";
string Q="11001100";
string R="10101010";
for(int c=0;c<n+1;c++)
{
if (A[c]=='p')
{
temp=P[i];
A[c]=temp;
}
else if (A[c]=='q')
{
temp=Q[i];
A[c]=temp;
}
else if (A[c]=='r')
{
temp=R[i];
A[c]=temp;
}
}
}
/*function Prof (Professional) Done by SHERIF*/
void Prof(string line,int speed)
{
for (int i=0;i<line.length();i++)
{ for (int c=0;c<speed*10000000;c++){}
cout << line[i];}
}
/*function NewCheckOperation checks whether the operation is valid
Clearly it is new because we had an old one
This new one indicates the number of the error
Done by Sherif and Abdulla*/
bool NewCheckOperation(char B[],int& n,int NUM,int& NofError)
{
bool *l;
l=new bool[NUM];
for (int y=0;y<NUM;y++)
l[y]=(false);
char A[100];
strcpy(A,B);
char *p;
p=new char[NUM];
ifstream infile;
infile.open("Var.txt");
for (int i=0;i<NUM;i++)
{
infile >> p[i];
for (int y=0;y<n;y++)
{
if (A[y]==p[i])
{
l[i]=true;
A[y]='*';
}
}
}
for (y=0;y<NUM;y++)
{
if (l[y]==false)
{
NofError=6;
return false;
}
}
int count=0;
for(i=0;i<n;i++)
{
if (((A[i]=='*')&&(A[i+1]=='('))||((A[i]==')')&&(A[i+1]=='*'))||((A[i]=='*')&&(A[i+1]=='*')))
{
NofError=1;
return false;
}
}
for(i=0;i<n;i++)
{
if ((A[i]=='A')&&(A[i+1]=='N')&&(A[i+2]=='D'))
{
A[i]='$';
A[i+1]=' ';
A[i+2]=' ';
despace(A,n);
}
if ((A[i]=='X')&&(A[i+1]=='O')&&(A[i+2]=='R'))
{
A[i]='$';
A[i+1]=' ';
A[i+2]=' ';
despace(A,n);
}
if ((A[i]=='O')&&(A[i+1]=='R'))
{
A[i]='$';
A[i+1]=' ';
despace(A,n);
}
if ((A[i]=='N')&&(A[i+1]=='O')&&(A[i+2]=='T'))
{
A[i]='-';
A[i+1]=' ';
A[i+2]=' ';
despace(A,n);
}
if ((A[i]=='E')&&(A[i+1]=='Q')&&(A[i+2]=='U')&&(A[i+3]=='I'))
{
A[i]='$';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
despace(A,n);
}
if ((A[i]=='I')&&(A[i+1]=='M')&&(A[i+2]=='P')&&(A[i+3]=='L')&&(A[i+4]=='I')&&(A[i+5]=='E')&&(A[i+6]=='S'))
{
A[i]='$';
A[i+1]=' ';
A[i+2]=' ';
A[i+3]=' ';
A[i+4]=' ';
A[i+5]=' ';
A[i+6]=' ';
despace(A,n);
}
}
for(i=0;i<n;i++)
{
if (((A[i]=='$')&&((A[i+1]!='*')&&(A[i+1]!='(')&&(A[i+1]!='-'))))
{
NofError=2;
return false;
}
if ((((A[i]!='(')&&(A[i]!='$'))&&(A[i+1]=='-'))||((A[i]=='-')&&(A[i+1]==')')))
{
NofError=3;
return false;
}
if (A[i]=='(')
count++;
if (A[i]==')')
count--;
if ((A[i]=='(')&&(A[i+1]==')'))
{
NofError=5;
return false;
}
if(count<0)
{
NofError=4;
return false;
}
}
if (count==0)
return true;
else
{
NofError=4;
return false;
}
}
/*function printR prints the results in a form of a truth table
Done by Sherif and Abdulla*/
void printR(char P[],char Q[],char R[],char A[],char AnswerCHAR[],int NUM)
{
cout << "\n--------------------------------------------------------------------------\n";
cout << "\t\tTABLE OF TRUTH"<<endl;;
cout << "--------------------------------------------------------------------------\n";
ifstream in;
in.open("Var.txt");
char *s;
s = new char[NUM];
for (int y=0;y<NUM;y++)
{
in >> s[y];
}
cout << setw(5) << s[0];
for (y=1;y<NUM;y++)
cout << setw(7)<<s[y];
cout << " ";
if (strlen(A)<36)
cout << left << setw(36) <<A;
else
cout << "Compound Statement A";
cout <<endl;
for (int h=0;h<pow(2,NUM);h++)
{
for (int o=0;o<NUM;o++)
{
int r=h+1;
char *s;
s = new char[(pow(2,NUM))+1];
ifstream inFILE;
inFILE.open("Chars.txt");
for (int num=0;num<=o;num++)
{
inFILE >>s;
}
inFILE.close();
inFILE.clear();
if (s[r]=='1')
cout << right << setw(7) << "true";
else if (s[r]=='0')
cout << right << setw(7) << "false";
}
if (AnswerCHAR[h]=='1')
cout <<setw(10) << "TRUE"<<endl;
else
cout<<setw(10) << "FALSE"<<endl;
}
}
void printRfile(char P[],char Q[],char R[],char A[],char AnswerCHAR[],int NUM)
{
Prof("Calculation Done...Printing results \n\n",UNI);
ofstream out;
out.open("Table.txt");
if (strlen(A)>=36)
out << "Compund Statement: " <<A <<endl <<endl;
out << "\n--------------------------------------------------------------------------\n";
out << "\t\tTABLE OF TRUTH"<<endl;;
out << "--------------------------------------------------------------------------\n";
ifstream in;
in.open("Var.txt");
char *s;
s = new char[NUM];
for (int y=0;y<NUM;y++)
{
in >> s[y];
}
out << setw(5) << s[0];
for (y=1;y<NUM;y++)
out << setw(7)<<s[y];
out << " ";
if (strlen(A)<36)
out << left << setw(36) <<A;
else
out << "Compound Statement";
out <<endl;
for (int h=0;h<pow(2,NUM);h++)
{
for (int o=0;o<NUM;o++)
{
int r=h+1;
char *s;
s = new char[(pow(2,NUM))+1];
ifstream inFILE;
inFILE.open("Chars.txt");
for (int num=0;num<=o;num++)
{
inFILE >>s;
}
inFILE.close();
inFILE.clear();
if (s[r]=='1')
out << right << setw(7) << "true";
else if (s[r]=='0')
out << right << setw(7) << "false";
}
if (AnswerCHAR[h]=='1')
out <<setw(10) << "TRUE"<<endl;
else
out<<setw(10) << "FALSE"<<endl;
}
out.close();
out.clear();
Prof("\nThe table has been succussfully printed in the file\n\t\t\t\t\t\t ",UNI);
Prof("Table.txt\n\n",UNI);
}
/*function ifTAUT checks whether the statement is a tautology
Done by Sherif and Abdulla*/
bool ifTAUT(char A[],int NUM)
{
for (int i=0;i<pow(2,NUM);i++)
{
if (A[i]!='1')
return false;
}
return true;
}
/*function ifCONT checks whether the statement is a contradiction
Done by Sherif and Abdulla*/
bool ifCONT(char A[],int NUM)
{
for (int i=0;i<NUM;i++)
{
if (A[i]!='0')
return false;
}
return true;
}
void Generate(ifstream& in,ofstream& out,int NUM)
{
int distance = pow (2,NUM)/2;
int F=pow(2,NUM);
char Var;
int count;
Prof ("\nYou can use the following variables:\n\t",UNI);
for (int i=0; i<NUM;i++)
{
in.get(Var);
cout << Var;
if (i!=(NUM-1))
cout << " ; ";
count=0;
out << Var;
for (int r=0;r<F;r++)
{
if (count<distance)
out <<"1";
else
out <<"0";
count++;
if (count==2*distance)
count=0;
}
distance=distance/2;
out << endl;
}
}
int main()
{
int NUM;
char P[]="11110000";
char Q[]="11001100";
char R[]="10101010";
Prof("Please enter the number of prepostions you wanna use (up to 26) >>",UNI);
cin >> NUM;
if (NUM>8)
{
Prof("This is not recommended at all",UNI);
Prof("\n\nBut ANYWAY\n",UNI+1);
delay(5);
}
ofstream out;
out.open("Chars.txt");
ifstream in;
in.open("Var.txt");
Generate(in,out,NUM);
out.close();
in.close();
out.clear();
in.clear();
cout <<endl;
bool firsttime=true;
int third=0;
char A[100];
for(;;)
{
third++;
if (third==4)
{
Prof("\n\nAre you trying to waste my time !! Sorry I have work to do\n",UNI);
Prof("\n\tBYE\n",UNI*3);
break;
}
if (firsttime==true)
{
Prof("Please Enter the statement\n>>",UNI);
cin.ignore(1,'a');
}
else Prof("Ok Try another statement\n>>",UNI);
cin.getline(A,1000);
int n=strlen(A);
int NofError;
if (NewCheckOperation(A,n,NUM,NofError)==false)
{
if (NofError==1)
{
Prof("There is a mistake in your use of variables!!\n\n",UNI);
delay(5);
firsttime=false;
continue;
}
else if (NofError==2)
{
Prof("There is an error in the use of Operators!!!\n\n",UNI);
delay(5);
firsttime=false;
continue;
}
else if (NofError==3)
{
Prof("There is a wrong NOT in your Equation\n\n",UNI);
delay(5);
firsttime=false;
continue;
}
else if (NofError==4)
{
Prof("The number of Parentheses ( and ) you entered do not match\n\n",UNI);
delay(5);
firsttime=false;
continue;
}
else if (NofError==5)
{
Prof("You have inputed Parentheses in this form () !!!\n\n",UNI);
delay(5);
firsttime=false;
continue;
}
else if (NofError==6)
{
Prof("You did'nt use all the variables you requested!!\nPlease dont ask for more than you need!!\n\n",UNI);
delay(5);
firsttime=false;
continue;
}
}
char *s;
s = new char[pow(2,NUM)];
char B[100];
ifstream inFILE;
for (int h=0;h<pow(2,NUM);h++)
{
inFILE.open("Chars.txt");
strcpy(B,A);
n=strlen(B);
despace(B,n);
NEWSubstitute(h,B,n,NUM,inFILE);
for (int i=0;i<n;i++)
{
EVALUATE(B,n);
}
s[h]=B[0];
inFILE.close();
inFILE.clear();
}
cout <<endl;
if ((ifTAUT(s,NUM)!=true) && (ifCONT(s,NUM)!=true))
{
if (NUM==3)
printR(P,Q,R,A,s,3);
else if (NUM==2)
printR(P,Q,R,A,s,2);
else if (NUM==1)
printR(P,Q,R,A,s,1);
else
printRfile(P,Q,R,A,s,NUM);
}
else if ((ifTAUT(s,NUM)==true) && (ifCONT(s,NUM)!=true))
Prof("The compound statement you entered is a TAUTOLOGY\n\n",UNI);
else if ((ifTAUT(s,NUM)!=true) && (ifCONT(s,NUM)==true))
Prof("The compound statement you entered is a CONTRADICTION\n\n",UNI);
return 1;
}
return 0;
}
|