c++

Showing posts with label c++. Show all posts
Showing posts with label c++. Show all posts

Utopian Tree



Problem Statement
The Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. 
Now, a new Utopian Tree sapling is planted at the onset of spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

SOURCE CODE:

 #include<stdio.h>  
 void main()  
 {  
   int t,test,total;  
   scanf("%d",&test);  
   while(test--){  
     scanf("%d",&t);  
     total=1;  
     int flag=1;  
     while(t--)  
     {  
       if(flag){  
         if(total==1){  
           total+=1;  
         }else{  
         total*=2;  
         }  
       flag--;  
       }  
       else{  
         total+=1;  
         flag++;  
       }  
     }  
     printf("%d\n",total);  
   }  
 }   

Operator Precedence


Simple program for operator precedence parser in C.
It takes the expression as input and calculates the precedence of the operators.

Subscribe to the blog to get instant updates via E-Mail.

PROGRAM CODE:


 #include<stdio.h>  
 #include<string.h>  
 void main()  
 {  
   int i,j,cnt=1;  
   char operators[] ="*/%+-",input[100];  
   printf("Enter the statement : ");  
   gets(input);  
   for(i=0;i<strlen(operators);i++)  
   {  
     for(j=0;j<strlen(input);j++)  
     {  
       if(input[j]==operators[i])  
       {  
         printf("%d %c=%c%c%c\n",cnt++,input[j-1],input[j-1],input[j],input[j+1]);  
         input[j+1]=input[j-1];  
       }  
     }  
   }  
 }  

OUTPUT:



Subscribe to the blog to get instant updates via E-Mail.

Notify the Suggestions about the program through comments ... :)

Lexical Analyzer


Simple Lexical Analyzer program. It takes the filename in the same directory as the input and analyzes the program for variables operators and special characters. and prints the output for each values separately.

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 void main()  
 {  
      FILE *fp;  
      int i=0;  
      char input[100],filename[20];  
      clrscr();  
      printf("Enter Filename : ");  
      scanf("%s",filename);  
      fp=fopen(filename,"r");  
      while(!feof(fp))  
      {  
           input[i]=fgetc(fp);  
           i++;  
      }  
      input[--i]='\0';  
      for(i=0;i<strlen(input);i++)  
      {  
           if(isalpha(input[i]))  
           {  
                printf("%c is a Character.\n",input[i]);  
           }  
           else if(input[i]=='='||input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/'||input[i]=='%')  
           {  
                printf("%c is an Operator.\n",input[i]);  
           }  
           else if(isspace(input[i])||iscntrl(input[i]))  
           {  
           }  
           else if(isdigit(input[i]))  
           {  
                printf("%c is a number.\n",input[i]);  
           }  
           else  
           {  
                printf("%c is a Special character.\n",input[i]);  
           }  
      }  
      getch();  
 }  
Notify the Suggestions about the program through comments ... :)

Text analyzer


The following Text analyzer program is implemented in c program. It program analyzes the given text from the user and calculate the number of words and total number of characters in the line of text. It determines the numbers of vowels, consonants, white space characters and other characters for each line. And finally determines average number of vowels, consonants per line.

Concepts used:

Array concept is used to store the input text which is to be analysed. The input text is entered during run time. The text contains space hence gets function is used to get the input text from the user.
Local variables and global variables are used. For loops and while loops are used to calculate the number of characters and words. Function is used to check whether the given character matches with the available character.

Source code:

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 int stc(char *,char);  
 void main()  
 {  
 int ch,ln=0,space,vow,con,other,err,tch=0,twrd=0,tvow=0,tcon=0;  
 char txt[500],*l, v[]="aeiouAEIOU";  
 char c[]="bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";  
 printf("\t\t\t\tTEXT ANALYZER");  
 printf("\nEnter the text to be analyzed :\n");  
 gets(txt);  
 for(l=strtok(txt,".");l!=NULL;l=strtok(NULL,"."))  
 {  
 ch=1,err=0,vow=0,space=0,other=1,con=0;  
 ln++;  
 if(*l==' ')  
 {  
 err++;  
 }  
 while(*l!='\0')  
 {  
 if(stc(v,*l)==1)  
 vow++;  
 else if(stc(c,*l)==1)  
 con++;  
 else if(*l==' ')  
 space++;  
 else  
 other++;  
 l++,ch++;  
 }  
 if(*(l-1)==' ')  
 {  
 err++;  
 }  
 printf("\nnumber of vowels in line %d : %d" ,ln,vow);  
 printf("\nnumber of consonants in line %d : %d" ,ln,con);  
 printf("\nnumber of white spaces in line %d : %d" ,ln,space);  
 printf("\nnumber of other characters in line %d : %d" ,ln,other);  
 printf("\nnumber of words in line %d : %d" ,ln,space+1-err);  
 printf("\nnumber of characters in line %d : %d\n" ,ln,ch);  
 tvow+=vow;  
 tcon+=con;  
 tch+=ch;  
 twrd+=space+1-err;  
 }  
 printf("\nAverage number of vowels per line : %d",tvow/ln);  
 printf("\nAverage number of consonants per line : %d",tcon/ln);  
 printf("\nTotal number of words : %d",twrd);  
 printf("\nTotal number of characters : %d",tch);  
 getch();  
 }  
 int stc(char *str, char c)  
 {  
 while(*str!='\0')  
 {  
 if(*str==c)  
 return 1;  
 str++;  
 }  
 return 0;  
 }  

OUTPUT:

Text analyser sample output
Text analyzer

text analyzer sample output
Text analyzer