Tuesday, November 22

5 PROCESSES TO CHECK ARMSTRONG AND REVERSE OF NUMBER~ Unix Program

Hi reader,
  Here our task is :

  • Create 5 processes.
  • Find whether a number is Armstong and Reverse the same using these processes.



A :Accept number.
C:Find whether it is armstrong.
D:Print Result.
B:Calculate Reverse of the Number.
E:Display Reverse.



Here is the complete Program.


#include<stdio.h>
#include<unistd.h>
main()
{             //initially in A

int num,f1,f2,f3,f4,p1[2],p2[2],p3[2],p4[2],arm,rev,flag=0;
pipe(p1);
pipe(p2);


f1=fork();  //A created B
if(f1>0)    //A is working ..
{
f2=fork();  //A created C

if(f2>0)     //A is working...
{
printf("Enter a number   :");
scanf("%d",&num);


close(p1[0]);
close(p2[0]);
write(p1[1],&num,sizeof(num));
write(p2[1],&num,sizeof(num));
}
if(f2==0)   //C is working..
{


pipe(p3);
f3=fork(); //C created D

if(f3>0)   //C is working..
{
close(p1[1]);
read(p1[0],&num,sizeof(num));
flag=0;

for(arm=0;num>0;num/=10)
arm+=(num%10)*(num%10)*(num%10);

if(num==arm)flag=1;
close(p3[0]);
write(p3[1],&flag,sizeof(flag));
}
else if (f3==0)  //D is working..
{

close(p3[1]);
read(p3[0], &flag, sizeof(flag));
(flag)?printf("It is Armstrong Number"):("It is Not Armstrong Number");

}
}
}
if(f1==0)   //B is working..

{
sleep(2);
pipe(p4);
f4=fork(); //B created E

if(f4>0)   //B is working..{
close(p2[1]);
read(p2[0],&num,sizeof(num));
for(rev=0;num>0;num/=10)
rev=10*rev+num%10;
close(p4[0]);
write(p4[1],&rev,sizeof(rev));
}
if(f4==0)   //E is working..

{
close(p4[1]);
read(p4[0],&rev,sizeof(rev));
printf("Reverse of Number is : %d\n",rev);
}
}
}





Output
Enter a number: 153
It is Armstrong Number
Reverse of Number is :351

Monday, November 21

Lex program ~ Read and copy file content


This lex program will read an input file and copy the content with line number to another file.






%{
#include<stdio.h>
#include<string.h>
char line[20];
int count=0,i=0;
FILE *out;
%}
%%
['\n'] { fprintf(out,"%d %s\n",++count,line);}
(.*) {
strcpy(line,yytext);
}
%%
main()
{
yyin=fopen("in.c","r");
out=fopen("out.c","w");
yylex();
getch();
}

int yywrap()
{
return 1;
}








input
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}


output file


1 main()
2 {
3 int i=9, j=900,k=115;
4 printf("%d,i+j+k);
5 }
6 }

Read a file and copy content to other file~ C code




This C program will read an input file and copy the content to another file with Line numbers..!!


#include<stdio.h>
main()
{
      FILE *f1,*f2;
      int t=0;
      char line[20];
      f1=fopen("in.c","r");
      f2=fopen("out.c","w");
   
      while(!feof(f1)){
      fgets(line,20,f1);
      t++;
      fprintf(f2,"%d  %s",t,line);
      }
     getch();
      }

Lex- Collect Tokens and sort

Hello friend,
  Here I am with an interesting Lex program which sorts all the nubers from input file and sort them. If you are well-familiarised with lex paradigm, it is very easy task.

Things to remember:
  • The definition [0-9]+ can detect all the numbers from input
  • The number detected at present will be pointed by yytext and have length yyleng
  • Whenever a new token (here a number) is detected, yytext will be replaced with new value. 
Logic
  1. Read the input
  2. If a number is detected, store it in an array (we can use yytext and yyleng for this). Read each digit --> multiply with 10 --> add next digit -->continue upto last digit.
  for eg, to read 123
initialise num=0;
  • read 1--> num=num*10+1-->num=0+1-->num=1
  • read 2--> num=num*10+2-->num=10+2-->num=12
  • read 3--> num=num*10+3-->num=120+3-->num=123
store num in an array.
3. After reading all the numbers, sort the number within the array




%{
#include<stdio.h>
int t, a[20],i,j,n,num;
%}
%%
[0-9]+ {num=0;
 for(i=0;i<yyleng;i++)
num=num*10+(yytext[i]-'0');

a[n++]=num;
}
. ;
%%
main()
{
yyin=fopen("file.c","r");
yylex();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);

}

int yywrap()
{
return 1;
}





sample input (in file.c)
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}




output
9
115
900

Sunday, November 20

Quadruple ~ C code

#include<stdio.h>
#include<string.h>


main()
{

 char line[20];
int s[20];
int t=1;
     
int i=0;     
printf("Enter string..  :");
gets(line);
for(i=0;i<20;i++)s[i]=0;

printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}


for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);




getch();
}





Output




Enter string..  :a=b*c-b*c


op      a1      a2      res
:=      c                t1
*       b       t1       t2
:=      c                t3
*       b       t3       t4
-       t2      t4       t5
:=      t5              a

Friday, November 18

Lex and Yacc in Windows 7 - Compile and Run


Hi Friend,
I was in search for a long time to get a platform in windows-7 to run lex and Yacc ,for academic purposes. Many of my friends also had the same problem and often forced to choose Linux only for merely executing Lex & Yacc programs..!!!

At last, I found one solution and its my pleasure to share it with all of you.!!  :)

Its very simple.You need to download two very small files.
You have to download two executable( i.e.,   .exe files). In total,its size may have about 370KB.

To download them click the links below.

Link 1:   Flex

Link 2:  Bison

Save both files in same place. ( I did in D: --> Flex)
Keep the lex program to be compiled also in same place.


Now open command prompt--> reach your directory through command prompt.

To compile, type command 
                flex <filename> (eg:- flex myflex.l)

If your program is error-free, lex.yy.c will be generated.


We can compile this C file with any C compiler available (eg: Turbo C)

and generate yy.lex.exe

Now just run yy.lex.exe. Its your lexical analyser..!!!




I hope this post was useful for you.!!
Please comment your feedback here.!!
Thank You.!!  :)








Wednesday, November 16

1. Copy this line. 2. Paste it as a comment. 3.Delete "+" sign 4.press enter See the magic..!!! :) @+[178952746....:0]


Hi friend ..
 This time I would like to share a trick to make a nice 'magic' comment in FB.
For example..the post will be like this:


1. Copy this line.
2. Paste it as a comment.
3.Delete "+" sign
4.press enter
See the magic..!!! :)

@+[258129740903222:0]



On doing  as this post, the number will automatically vanished and replaced by
"LoVe U my dear friend - by vipin" :):):)



wow.. Cool na??

You too can make your own magic comment (with your name, to ur dear ones)!!
Only 5 steps are needed.

Make your own Facebook page.
 You can find the option for page creation at the bottom side of the page as this:

#Start to make a FB page: (You can select any category. I chose the last one.)

# Then you have to enter the title for your page. Listen, the content of your magic comment should be placed here (See, I put  LoVe U my dear friend - by vipin).
It can be any comment like

I love you..!!!
~U r my best friend..!!~
Let U b ma Best frnd~ FOREVER!!!
Its wonderful na..??
I love my Nation :)




# Then several steps will come to add picture, web address,etc,etc. You can do or skip these steps. It doesn't matter.

# Finally you will get yor page like this.

#Look at the URL of the page. You can find the ID for your page.

#Uff.. The task is over.!! Now you can make your amgic comment as:

@+[page id:0]

eg:
@+[258129740903222:0]


After deleting the "+" sign put it in your comment area. FB will recognize that it is Page ID and replace the number with page title.!! :)

1. Copy this line.
2. Paste it as a comment.
3.Delete "+" sign
4.press enter
See the magic..!!! :)

@+[258129740903222:0]

ENJOY!!!!!

How is the trick?? Is it cool?? How did u feel on trying this??
 Please comment here... !!