Posts Tagged C++
C++ operator precedence table
Posted by admin in C++ Examples on June 24th, 2009
The highest precedence operators appear first in the list. Operators in the same box each have equal precedence, and the order of application depends on the associativity labeled in the rightmost column.
| Operator | Type | Associativity |
| :: :: |
binary scope resolution unary scope resolution |
left to right |
| () [] . -> ++ – typeid dynamic_cast< type > static_cast reinterpret_cast const_cast |
parentheses array subscript member selection via object member selection via pointer unary postincrement unary postdecrement run-time type information run-time type-checked cast compile-time type-checked cast cast for non-standard conversions cast away const-ness |
|
| ++ – + - ! ~ ( type ) sizeof & * new new[] delete delete[] |
unary preincrement unary predecrement unary plus unary minus unary logical negation unary bitwise complement C-style unary cast determine size in bytes address dereference dynamic memory allocation dynamic array allocation dynamic memory deallocation dynamic array deallocation |
right to left |
| left to right | ||
| .* ->* |
pointer to member via object pointer to member via pointer |
|
| * / % |
multiplication division modulus |
|
| + - |
addition subtraction |
|
| << >> |
bitwise left shift bitwise right shift |
|
| < <= > >= |
relational less than relational less than or equal to relational greater than relational greater than or equal to |
|
| == != |
relational is equal to relational is not equal to |
|
| & | bitwise AND | |
| ^ | bitwise exclusive OR | |
| | | bitwise inclusive OR | |
| && | logical AND | |
| || | logical OR | |
| ?: | ternary conditional | right to left |
| = += -= *= /= %= &= ^= |= >>= <<= |
assignment addition assignment subtraction assignment multiplication assignment division assignment modulus assignment bitwise AND assignment bitwise exclusive OR assignment bitwise inclusive OR assignment bitwise left shift assignment bitwise right shift with assignment |
|
| , | comma | left to right |
PROGRAM TO COMPARE VARIOUS SEARCH TECHNIQUES.
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h> //HEADER FILES
#include<time.h>
#include<fstream.h>
#include<math.h>
fstream file;
int record[30000],key[20000],valid,found; //TO STORE DATA
long int comp=0,avgcomp;
float avgtime;
void seqsearch() //TO PERFORM SEQUENTIAL SEARCH
{
comp=found=0;
int i,j;
for(i=0;i<valid;i++)
{
for(j=0;j<30000;j++)
{
if(record[j]==key[i])
{
//cout<<"\nfound "<<key[i]<<" at "<<j;
found++;
break;
}
//else
comp++;
}
//if(j==30000)
//cout<<"\n not found**********";
}
}
void binsearch() //TO PERFORM BINARY SEARCH
{
comp=found=0;
int top,mid,bot,i;
for(i=0;i<valid;i++)
{
top=0;
bot=29999;
mid=(bot+top)/2;
//cout<<"\n"<<mid;
while(top<=bot)
{
//cout<<"\n"<<mid;
if(record[mid]==key[i])
{
found++;
comp++;
//cout<<"\nfound"<<key[i]<<" at "<<mid;
break;
}
//else
//{
comp++;
if(record[mid]>key[i])
bot=mid-1;
else
top=mid+1;
mid=(bot+top)/2;
//}
}
}
}
void intersearch() // TO PERFORM INTERPOLATION SEARCH
{
comp=found=0;
int top,mid,bot,i;
for(i=0;i<valid;i++)
{
top=0;
bot=29999;
if(top!=bot)
mid=top+(bot-top)*(key[i]-record[top])/(record[bot]-record[top]);
while(bot-top>0)
{
if(record[mid]==key[i])
{
//cout<<"\nfound"<<key[i];
comp++;
found++;
break;
}
else
{
comp++;
if(top==mid||bot==mid)
{
if(record[mid+1]==key[i])
{
found++;
//cout<<"\nfound"<<key[i];
}
//else
// cout<<"\n notfound*******";
break;
}
if(record[mid]>key[i]&&bot!=mid)
bot=mid;
else if(record[mid]<key[i]&&top!=mid)
top=mid;
if(top!=bot)
mid=top+(bot-top)*(key[i]-record[top])/(record[bot]-record[top]);
}
}
}
}
void robintersearch() //TO PERFORM ROBUST INTERPOLATION SEARCH
{
int top,bot,mid,min,max,position=0,k;
int gap,probe;
comp=found=0;
for(k=0;k<valid;k++)
{
bot=29999;
top=0;
gap=sqrt(bot-top+1);
while(top<bot)
{
probe=top+(bot-top)*((key[k]-record[top])/(record[bot]-record[top]));
max=(probe>(top+gap))?probe:(top+gap);
min=(bot-gap);
mid=(min<max)?min:max;
//cout<<"\n"<<probe<<" "<<mid<<" "<<min<<" "<<max<<" "<<top<<" "<<bot;
//comp=comp+2;
if(mid>bot||mid<top)
{
//cout<<"\n not found!!!!!!!!!!!!!!!!!!!!!!";
//comp++;
break;
}
//comp++;
if(key[k]==record[mid])
{
//cout<<"found "<<key[k]<<" at "<<mid<<endl;
found++;
comp++;
break;
}
if(key[k]==record[mid-1])
{
//cout<<"found "<<key[k]<<" at "<<mid-1<<endl;
found++;
break;
}
if(key[k]==record[mid+1])
{
//cout<<"found "<<key[k]<<" at "<<mid+1<<endl;
found++;
break;
}
if((mid-top)<(bot-mid))
{
comp++;
if(key[k]<record[mid])
{
bot=mid-1;
gap=sqrt(bot-top+1);
}
else
{
top=mid+1;
gap=2*gap;
if(gap>((bot-top)/2))
gap=(bot-top)/2;
} //end of if
}
else
{
comp++;
if(key[k]>record[mid])
{
top=mid+1;
gap=sqrt(bot-top+1);
}
else
{
gap=2*gap;
bot=mid-1;
if(gap>((bot-top)/2))
gap=(bot-top)/2;
} //end of if
} // end of if
}
}
}
void main() //EXECUTION STARTS HERE
{
int i,j,k;
cout<<" \n\n WAIT........................";
file.open("records.txt",ios::in);
for(i=0;i<30000;i++) //READING DATA
file>>record[i];
file.close();
file.open("keys.txt",ios::in);
for(i=0;i<20000;i++) //READING KEYS TO BE SEARCHED
file>>key[i];
file.close();
file.open("sequence.txt",ios::out); //SEQUENTIAL SEARCH
long p,q;
for(k=1;k<=200;k++)
{
valid=100*k;
p=clock();
seqsearch();
q=clock();
avgtime=(float)(q-p)/valid;
avgcomp=comp/valid;
file<<avgtime*1000<<"\t"<<avgcomp<<"\n";
}
file.close();
file.open("binary.txt",ios::out); //BINARY SEARCH
for(k=1;k<=200;k++)
{
valid=100*k;
p=clock();
for(j=0;j<100;j++)
binsearch();
q=clock();
avgtime=(float)(q-p)/valid/100;
avgcomp=comp/valid;
file<<avgtime*1000<<"\t"<<avgcomp<<"\n";
}
file.close();
file.open("inter.txt",ios::out); //INTERPOLATION SEARCH
for(k=1;k<=200;k++)
{
valid=100*k;
p=clock();
for(j=0;j<1000;j++)
intersearch();
q=clock();
avgtime=(float)(q-p)/valid/1000;
avgcomp=comp/valid;
file<<avgtime*1000<<"\t"<<avgcomp<<"\n";
}
file.close();
file.open("robinter.txt",ios::out); //ROBUST INTERPOLATION SEARCH
for(k=1;k<=200;k++)
{
valid=100*k;
p=clock();
for(j=0;j<100;j++)
robintersearch();
q=clock();
avgtime=(float)(q-p)/valid/100;
avgcomp=comp/valid;
file<<avgtime*1000<<"\t"<<avgcomp<<"\n";
}
file.close();
}
PROGRAM TO GENERATE 30000 RANDOM NUMBERS AND WRITE TO FILE RECORDS.TXT IN SORTED ORDER (USED INORDER TRAVERSAL FOR THIS).
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};
void inorder(bintree *node)
{
if(node->left!=NULL)
inorder(node->left);
file<<" "<<node->data;
if(node->right!=NULL)
inorder(node->right);
}
void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}
void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("records.txt",ios::out);
while(total<30000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
inorder(root);
dump(&root);
file.close();
}
PROGRAM TO GENERATE 20000 RANDOM KEYS AND WRITE TO FILE KEYS.TXT
Posted by admin in C++ Examples on June 14th, 2009
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int total=0;
fstream file;
struct bintree
{
int data;
bintree *left,*right;
};
void makenode(bintree **garb,int num)
{
(*garb)->data=num;
(*garb)->left=NULL;
(*garb)->right=NULL;
}
void makebintree(bintree *garb,bintree **base)
{
if((*base)==NULL)
{
*base=garb;
total++;
file<<garb->data<<" ";
return;
}
else if(garb->data==(*base)->data)
{
return;
}
else if(garb->data<(*base)->data)
makebintree(garb,&((*base)->left));
else
makebintree(garb,&((*base)->right));
}
void dump(bintree **node)
{
if(*node==NULL)
return;
if((*node)->left==NULL&&(*node)->right==NULL)
{
delete(*node);
*node=NULL;
}
else
{
dump(&((*node)->left));
dump(&((*node)->right));
dump(node);
}
}
void main()
{
bintree *garb,*root=NULL;
file.open("keys.txt",ios::out);
while(total<20000)
{
garb=new bintree;
makenode(&garb,rand());
makebintree(garb,&root);
}
dump(&root);
file.close();
}
PROGRAM TO TRAVERSE LINKED LIST IN REVERSE ORDER
Posted by admin in C++ Examples on May 15th, 2009
#include<iostream.h> //HEADER FILES
#include<ctype.h>
#include<stdio.h>
#include<process.h>
struct node //STRUCTUREFOR LIST
{
char data;
node *next;
};
node *head=NULL;
node *current=NULL;
node *temp,*previous;
void print(node *, node *); //FUNCTION TO PRINT LIST
void main() //MAIN() STARTS HERE
{
char cho;
do
{
temp=new(node);
cout<<"\n ENTER THE CHARACTER :";
cin>>temp->data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
current=temp;
}
else
{
current->next=temp;
current=temp;
}
cout<<"\n WANT TO ADD MORE? (Y/N)";
cin>>cho;
}while(tolower(cho)=='y');
cout<<"\n LIST IN THE REVERSE ORDER IS \n";
temp=head;
previous=NULL;
print(temp,previous);
}
void print(node *temp,node *previous) //FUNCTION DEFINITION
{
while(1)
{
if(temp->next==NULL)
{
printf("%c%s",(temp->data)," ");
if(previous!=NULL)
previous->next=NULL;
if(temp==head)
exit(0);
delete(temp);
temp=head;
}
else
print(temp->next,temp);
}
}
PROGRAM TO DEMONSTRATE RECURSION USING SOLUTION OF TOWER OF HANOI.
Posted by admin in C++ Examples on May 15th, 2009
#include<iostream.h>
int i;
unsigned long int fact(int n) //FOR FACTORIAL
{ //F(N)=N*F(N-1)
if(n<=1)
return 1;
else
return(n*fact(n-1));
}
long int fibb(int n) //FOR FIBONACCI SERIES
{
if (n<=1)
return 1;
else
return(fibb(n-1)+fibb(n-2));
}
void hanoi(int n,char source,char inter,char dest) //FOR TOWERS OF HANOI
{
if(n==1)
cout<<"\n MOVE "<<++i<<": MOVE DISK 1 FROM "<<source<<" TO "<<dest;
else
{
hanoi(n-1,source,dest,inter);
cout<<"\n MOVE "<<++i<<": MOVE DISK "<<n<<" FROM "<<source<<" TO "<<dest;
hanoi(n-1,inter,source,dest);
}
}
void main() //MAIN() FUNCTION
{
int n;
char ch;
do
{
cout<<"\n MAIN MENU"
<<"\n FIND FACTORIAL :: 1"
<<"\n FIND FIBONACCI VALUE :: 2"
<<"\n SOLVE TOWER OF HANOI :: 3"
<<"\n EXIT :: 4"
<<"\n ENTER UR CHOICE :: ";
cin>>ch;
switch(ch-48)
{
case 1: do
{
cout<<"\n ENTER THE POSITIVE NO. WHOSE FACTORIAL IS TO BE FOUND ::";
cin>>n;
}while(n<0);
cout<<" fact("<<n<<")="<<fact(n)<<"\n";
break;
case 2: do
{
cout<<"\n ENTER THE POSITIVE NO. WHOSE FIBONACCI VALUE IS TO BE FOUND ::";
cin>>n;
}while(n<0);
cout<<" fibb("<<n<<")="<<fibb(n)<<"\n";
break;
case 3: do
{
cout<<"\n ENTER THE NO. OF DISKS IN TOWER ::";
cin>>n;
}while(n<0);
i=0;
cout<<"\n SOURCE PEG: A , INTERMEDIATE PEG: B , DESTINATION PEG: C";
hanoi(n,'A','B','C');
cout<<"\n\n TOTAL NO. OF MOVES ="<<i;
break;
case 4: cout<<"\n PROGRAM OVER \n";
break;
default:cout<<"\n BAD CHOICE \n";
}
}while(ch!='4');
}
PROGRAM TO CONVERT INFIX TO POSTFIX EXPRESSION.
Posted by admin in C++ Examples on May 15th, 2009
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
#define SIZE 50
class stack
{
char arr[SIZE];
int top;
public:
void push(char);
int findtop();
char pop();
bool isempty();
bool isfull();
void display();
char gettop();
stack()
{
top=-1;
}
};
int stack::findtop()
{
return(top);
}
bool stack::isempty()
{
return(top==-1);
}
bool stack::isfull()
{
return(top==(SIZE-1));
}
void stack::push(char ch)
{
if(isfull())
{
cout<<"\n STACK OVERFLOW";
return;
}
arr[++top]=ch;
}
char stack::pop()
{
if(isempty())
return('?');
return(arr[top--]);
}
void stack::display()
{
cout<<endl;
if(isempty())
{
cout<<" STACK EMPTY";
return;
}
for(int i=0;i<=top;i++)
cout<<arr[i];
cout<<" "<<top;
}
char stack::gettop()
{
if(isempty())
return('?');
return(arr[top]);
}
void isvalid(char *exp)
{
int check=0,i,l;
l=strlen(exp);
if(exp[0]!='('&&!isalnum(exp[0]))
{
check=-1;
goto label;
}
if(exp[l-1]!=')'&&!isalnum(exp[l-1]))
{
check=-1;
goto label;
}
for(i=1;i<l-1;i++)
{
if(exp[i]=='(')
check++;
else if(exp[i]==')')
check--;
else if((exp[i]=='.')&&(!(isalnum(exp[i-1]))||!(isalnum(exp[i+1]))))
check=-1;
if(check<0)
{
cout<<"\n SYNTAX ERROR IN INFIX EXPRESSION"<<endl;
exit(0);
}
}
label:
if(check!=0)
{
cout<<"\n SYNTAX ERROR IN INFIX EXPRESSION"<<endl;
exit(0);
}
}
void main()
{
char infix[SIZE];
stack pfix,oper;
cout<<"\n ENTER THE INFIX EXPRESSION ::\n";
cin.getline(infix,SIZE);
isvalid(infix);
int len=strlen(infix);
for(int i=0;i<len;i++)
{
switch(infix[i])
{
case ' ': pfix.display();
oper.display();
break;
case '(':
case '$': oper.push(infix[i]);
pfix.display();
oper.display();
break;
case '/':
case '*':
case '%': while((oper.gettop()=='$')||(oper.gettop()=='/')||(oper.gettop()=='*')||(oper.gettop()=='%')&&(oper.gettop()!='?'))
{
pfix.push(oper.pop());
pfix.push(' ');
}
oper.push(infix[i]);
pfix.display();
oper.display();
break;
case '+':
case '-':
while(oper.gettop()!='('&&oper.gettop()!='?')
{
pfix.push(oper.pop());
pfix.push(' ');
}
oper.push(infix[i]);
pfix.display();
oper.display();
break;
case ')': while(oper.gettop()!='('&&!oper.isempty())
{
pfix.push(oper.pop());
pfix.push(' ');
}
oper.pop();
pfix.display();
oper.display();
break;
case '.': pfix.push(infix[i]);
break;
default: if(!isalnum(infix[i]))
{
cout<<"\n UNDEFINED SYMBOL "<<infix[i]<<endl;
exit(0);
}
else
{
for(int j=i;isalnum(infix[j]);j++)
pfix.push(infix[j]);
i=j-1;
if(infix[j]!='.')
pfix.push(' ');
}
pfix.display();
oper.display();
}
}
while(oper.gettop()!='?'&&!oper.isempty())
{
pfix.push(oper.pop());
pfix.push(' ');
}
pfix.display();
oper.display();
cout<<endl;
}
PROGRAM TO EVALUATE POSTFIX EXPRESSIONS.
Posted by admin in C++ Examples on May 15th, 2009
#include<iostream.h> //HEADER FILES
#include<string.h>
#include<ctype.h>
#include<process.h>
#include<math.h>
#define SIZE 50
class stack //STACK DEFINITION
{
float arr[SIZE];
int top;
public: //FUNCTIONS OF STACK CLASS
bool isempty();
bool isfull();
void push(float);
float pop();
void display();
void convert(char *);
stack()
{
top=-1;
}
};
bool stack::isempty() // FUNCTION DEFINITION
{
return(top==-1);
}
bool stack::isfull()
{
return(top==(SIZE-1));
}
void stack::push(float ch) // FUNCTION DEFINATION TO INSERT AN ITEM INTO STACK
{
if(isfull())
{
cout<<"\n STACK OVERFLOW";
return;
}
arr[++top]=ch;
}
float stack::pop() // FUNCTION DEFINITION TO DELETE AN ITEM FORM STACK
{
if(isempty())
return('?');
return(arr[top--]);
}
void stack::display() // FUNCTION DEFINATION TO DESPLAY STACK(STCK ITEMS)
{
cout<<endl;
if(isempty())
{
cout<<" STACK EMPTY";
return;
}
for(int i=0;i<=top;i++)
cout<<arr[i];
}
void stack::convert(char* array) // FUNCTION DEFNATION TO CONVERT INTO POSTFIX
{
int i,j,l;//,index=-1;
float a,b,num,dec;
l=strlen(array);
for(i=0;i<l;i++)
{
num=0;
dec=1;
if(array[i]==' ')
continue;
if(isdigit(array[i]))
{
for(j=i;array[j]!='.'&&array[j]!=' ';j++)
num=num*10+(array[j]-48);
if(array[j]=='.')
for(j++;array[j]!=' ';j++)
{
dec/=10;
num=num+dec*(array[j]-48);
}
i=j;
push(num);
continue;
}
if((i<l)&&(!isdigit(array[i])))
if(top<1)
{
cout<<"\n SYNTAX ERROR EXTRA OPERATOR ENCOUNTERED";
exit(0);
}
b=pop();
a=pop();
switch(array[i])
{
case '+': a+=b;
push(a);
break;
case '-': a-=b;
push(a);
break;
case '*': a*=b;
push(a);
break;
case '/': if(b==0)
{
cout<<"\n MATH ERROR DIVISION BY ZERO ENCOUNTERED";
exit(0);
}
else
{
a/=b;
push(a);
}
break;
case '%': if(b==0)
cout<<"\n MATH ERROR DIVISION BY ZERO ENCOUNTERED";
else
{
a=(int)a%(int)b;
push(a);
}
break;
case '$': a=pow((int)a,(int)b);
push(a);
break;
}
}
cout<<"\n result ::"<<pop()<<endl;
}
void main()
{
stack postfix;
char exp[SIZE];
cout<<"\n ENTER THE POSTFIX EXPRESSION"
<<"\n USE SPACE BETWEEN OPERATOR AND OPERANDS:\n";
cin.getline(exp,SIZE);
postfix.convert(exp);
}


Recent Comments