GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-May-2007, 07:25
brink600 brink600 is offline
New Member
 
Join Date: May 2007
Location: Lebanon
Posts: 7
brink600 is on a distinguished road

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
CPP / C++ / C Code:
#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;
}
  #2  
Old 03-May-2007, 15:48
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help to reduce complexity of this CPP


Just a couple of small starters, how about some code reduction first, THEN it'll be easier to see where other improvements can be done...

1. (minor) I see that you have windows.h included... why not use the Sleep() function instead of delay() and its loop?

2. To reduce the heavy bracketed-indexing that you have, take a deeper look into the various functions provided by the string library, specifically strcmp(), strcpy(), etc [there are more]. There might be a 'learning curve' with their use, though, if pointer-indexing is a new idea to you.

Also, consider using a variable with the 'string' type from the STL as it provides functions such as replace(), erase(), append(), etc... but, if you're not ready to jump into the STL just yet [if 'iterators' is new to you], then just start with the other functions mentioned above.

Either way, examples for using the strings functions can be found across the web, and, if you use an IDE, they can usually be found through the IDE's help files. [might even find examples searching through our forums!]
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 03-May-2007, 16:06
brink600 brink600 is offline
New Member
 
Join Date: May 2007
Location: Lebanon
Posts: 7
brink600 is on a distinguished road

Thanks but..


thanks man

did u get the point of the program ... please compile!

please try 10 variables
and type the statement pORqORrORsORtORuOR......etc
then 11
see the difference

The whole BIG O is in this loop :
CPP / C++ / C Code:
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();
		}
  #4  
Old 04-May-2007, 11:48
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Thanks but..


Quote:
Originally Posted by brink600
did u get the point of the program ... please compile!

Yes, I get the point of the program, but "reduce the complexity" is ambiguous; therefore, I leaned towards code-overhead-complexity, NOT execution-speed-complexity...(though sometimes these are correlated)
Quote:
Originally Posted by brink600
please try 10 variables
and type the statement pORqORrORsORtORuOR......etc
then 11
Although I figured it out, it would have been nice to know, in advance, that a file is required that gives the variables to display. It would have been better if you actually had some error checking around those file handles to make it obvious.

So, I still advise to take a deeper look into the string lib function capabilities, and also especially look at the function strstr(). Unless you plan on including boolean reduction, the EVALUATE function is needlessly processing for operators that are not in the expression! For example, in the example expression you gave: 'pORqORrORsORtORuOR...' does NOT have AND, XOR, etc.

Also, just for a hint, if you decide to use strstr(): because XOR, and OR, both contain the string 'OR', it might be necessary to preprocess the string and change any XORs to XR. [or something else, to know the difference]

Overall, there are several++ things that can be done to your code to "reduce the complexity." -- WYCIWYG
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Complexity Analysis Jokky C++ Forum 5 03-Jul-2006 21:19
hashing help saiz66 C++ Forum 1 06-Jul-2004 07:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.