How to count the occurences of operands and operators of a program using C ??

This is a discussion on How to count the occurences of operands and operators of a program using C ?? within the C Programming forums, part of the General Programming Boards category; How to count the occurences of operands and operators of a program using C programming?? I'm trying to read a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Exclamation How to count the occurences of operands and operators of a program using C ??

    How to count the occurences of operands and operators of a program using C programming?? I'm trying to read a from a file with coding then to count the operands and operators.Can anybody please help me.... Thanks in advanced...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    What have you tried so far?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Exclamation No idea how to start the programming

    Accually i really don't have idea how to start the coding. Please guide me...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Well the first step is open the file, and print every character to the screen.
    This at least verifies that you can read the file correctly, because if you can't do this then the rest of the problem will be out of reach.

    Then try classifying the characters as you read them, say as a keyword, an identifier or an operator.
    Again, just print out the character, and what set you decided it should belong to.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Exclamation How do i compair the operands and operator?? Urgent!!!

    I have manage to read the coding from the file but how do i compair them with the operands and operator because i wanted to get the total number of operand. Please help.....

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Code:
    switch (readVal) {
       case '+':
       case '-':
       case '*':
       /* ...and so on */
           operatorNum++;
           newOperand = 1;
           break;
       default:    // Anything that isn't the operators you listed
           if(newOperand == 1) {
              operandNum++;
              newOperand = 0;
           }
           break;
    }
    Last edited by SlyMaelstrom; 02-27-2006 at 08:25 AM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Exclamation What if i wanted to list all in another notepad than compair?? Urgent!!!

    What if i wanted to list all in another notepad than compair?? How do i do this i put all the coding into a notepad name sampel.txtthen i put all the operands and operators in another notepad name operators.txt. How do i read the operators.txt and then compair the two to get the amount of operands and operators?? Can anybody cip in and give me some ideas??? Please guide me. Thanks alot...

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    What you said makes no sense at all.

    Are you familiar with the term "beating around the bush"? That's what you're doing, right now. You might as well come out and just ask someone to write your program for you. You haven't shown us a single line of code in the 4 posts you've had in this topic.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Exclamation I'm trying with this

    Code:
    # include <stdio.h>
    #include <stdlib.h>
    # include <string.h>
    
    void main (void)
    {
    	FILE *fin;
    	char no[100];
    	char i;
    	char b;
    	char a[10] = {"+"} ;
    
    
    	fin = fopen ("2.txt","r");
    	if (fin == NULL)
    	{
    		printf ("Error  \n");
    		exit(1);
    	}
    
    	while ((fgets(no,50, fin)) != NULL)
    	{
    		fscanf(fin, "%c\n", &no);
    		printf("%s", &no);
    		printf ("\n");
    	}
    
    	for (i=0;i<5;i++)
    	{
    		b = strcmp ("a","no[i]");
    
    		if (b==1)
    		{
    			printf ("Found Match");
    			printf ("\n");
    		}
    
    		else
    		{
    			printf ("No match");
    			printf ("\n");
    		}
    	}
    	
    	fclose(fin);
    
    }

    This is the code i'm try out but it only print out No match even though there is a match.

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Att: SlyMaelstrom

    If you don't want to help you can just ignore the posting. You need not be so rude. We can judge a person by the way they talk and write...

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    What rude? I was telling it how it is. You're here asking a question that doesn't make sense with code that you hadn't yet shown us and yet each time you reply you wonder why you haven't gotten a direct answer, yet.

    Anyway, the reason your code prints no match when there is a match is because that's what you're code says to do. strcmp() returns 0 when the two words match. Yes, I know that's a bit odd considering 0 is false, but that's how it is. Basically, it returns the difference in bytes between the two strings. If there is no difference, then they must be the same.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Tried that before

    Code:
    for (i=0;i<5;i++)
    	{
    		b = strcmp ("a","no[i]");
    
    		if (b==0)
    		{
    			printf ("Found Match");
    			printf ("\n");
    		}
    
    		else
    		{
    			printf ("No match");
    			printf ("\n");
    		}
    	}

    I have tried with 0 before and it print the same result so i try with 1. But both also having the same problem.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    Code:
    strcmp ("a","no[i]");
    Unless "no[i]" is a string literal it shouldn't be under quotes. I'm guessing that it's a variable. Now for the next problem. A string name with an index yields a character.

    This is a character: 'a'
    This is not a character: "a"

    See the difference? One is a character one is a string. strcmp() compares strings, not characters.

    What you want is the good ol' comparison operator:

    Code:
    if(no[i] == 'a')
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    9

    Thanks alot

    Thanks alot for alll your help. Have a nice day

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    9
    i'm now trying to do like this. Now it can found "int", and "else", but even the other operators/operands like if, main do appear in the C program file (try.txt), it's found unmatch.

    Code:
    void main ()
    {
    	FILE *fin;
    
    	fin=fopen("try.txt", "r");
    
    	if (fin == NULL)
    	{
    		printf("Error\n");
    		exit(1);
    	}
    
    	int i, j, c;
    	char z[10][10]={"if", "int", "else", "scanf", "include", "if"};
    	char hals[30];
    	char a;
    
    	while((fgets(hals,30,fin))!=NULL)
    
    	{
    		fscanf(fin, "%s", &hals);
    		for (j=0; j<4; j++)
    		{
    			c=strcmp(hals,z[j]);
    			if(c==0)
    				printf("Found %s\n", &z[j]);
    			else
    				printf("Cant find\n");
    		}	
    		
    	}
    
    	fclose(fin);
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Program to evaluate expressions
    By DMEngOba in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2009, 10:16 PM
  2. operators operands
    By verb in forum C Programming
    Replies: 6
    Last Post: 02-13-2002, 07:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21