Thread: function to assigning index in front

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    20

    function to assigning index in front

    i am getting in trouble which to write a function to assign or delete the extra index in my loop~

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    
    int main()
    {
    	char txt[200];
    	char temp[50][50];
    	char opt[200];
    	float num[200];
    	int i=0, j=0, k=0, n=0, m=0;
    	
    	printf("?\n");
    	gets(txt);
    	
    	i = strlen(txt);
    	
    	for(j=0;j<i;j++)
    	{
    		if(!(txt[j]=='+' || txt[j]=='-' || txt[j]=='*' || txt[j]=='/'))
    		{
    			 temp[m][k] = txt[j]; k++;
    		}
    		else
    		{			
                opt[n] = txt[j]; n++;
    			num[m] = atof(temp[m]); m++;
    			k=0;
                			
    		}
    	}
    	num[m] = atof(temp[m]); m++;
    	
    {
    	int yee = 0;
    	float ans = 0;
    	float tempnum[200]; 
    	int asd = 0;
    	
    	asd = strlen(opt);
    	
    	for(j=0;j<asd;j++)
    	{
    		if(opt[j]=='*')
    		{
    			num[j+1] = num[j] * num[j+1];
    			num[j] = num[j+1];
                ans = num[j+1];          			
    		}
    		if(opt[j]=='/')
    		{
    			num[j+1] = num[j] / num[j+1];
    			num[j] = num[j+1];
                ans = num[j+1];			
    		}
    	}
    	
    	
    	
    	
    	for(j=0;j<asd;j++)
    	{
    		if(opt[j]=='+')
    		{ 
                if(j==0)
                {ans = ans + num[j];}
                else
                {ans = ans + num[j+1];}                   			
    		}
    		if(opt[j]=='-')
    		{
                if(j==0)
                {ans = ans - num[j];}
                else
                {ans = ans - num[j+1];}         			
    		}
    	}
    	
        
        	
    
    	printf("%f",ans);
    }
    	getchar();
    }
    tis is my code n my idea is as folo~

    num = {1, 2, 3, 4}
    opt = "+*+"

    num = {1, 6, 4}
    opt = "++"

    num = {7, 4}
    opt = "+"

    so how can i delete the extra index or assign the index in front? please help me~~

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want to get rid of the end of string char at the end of "txt" array?

    As you can see, I'm unclear what you want to do. You can't "get rid of an extra index", since indeces are built into the array, (via pointer design), and can't be removed from it.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    ok~ let me show you~
    example my expression is 1+2*3+4
    first i save all numbers in num
    num = 1 2 3 4

    then when i found the operator is *,it wil perform num[1] multiply num[2],the the index bcome as below..
    num = 1 6 6 4

    then is + operator, it wil perform num[0]+num[1], the index now bcome as follow.. as you can see, there is repetation of same answer in different index,
    num = 7 7 6 4

    last it wil perform num[3]+num[4],the problem is same as the above, there is repetation too..
    7 7 10 10

    so i hope to delete the extra answer of index~how should i do that?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Code:
    for(j=0;j<asd;j++)
    	if(opt[j]=='*')
    		{
    			num[j+1] = num[j] + num[j+1];
    			num[j] = num[j+1];
                ans = num[j+1];          			
    		}
    		if(opt[j]=='/')
    		{
    			num[j+1] = num[j] - num[j+1];
    			num[j] = num[j+1];
                ans = num[j+1];			
    		}
    	}
    sorry~ there are correction for the + and - operation
    this should be the loop for it

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you reduce a binary expression like 2 * 3, into one number (6 in this case), then you need to move the higher index numbers (all of them from that point on), of the array, down one index, and "cover up" the extra 6.

    Make sense?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    yup.. thats the idea.. but how to write it in a function so that i can call it in every mathematical expression?

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Last edited by Bayint Naung; 08-31-2010 at 07:27 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So in your code:
    Code:
    for(j=0;j<asd;j++)
       if(opt[j]=='*')
       {
    	num[j+1] = num[j] + num[j+1];
    	num[j] = num[j+1];
            ans = num[j+1];          			
            /* boogie everything above j, one element to the left */
            for(k=j; num[k]<asd-1;k++)
               num[k] = num[k+1];
       }
       if(opt[j]=='/')
       {
          num[j+1] = num[j] - num[j+1];
          num[j] = num[j+1];
          ans = num[j+1];			
       }
    }
    making the "boogie" code into it's own function and then just calling it, seems good, as does using flags to set if it's needed, and then having the code outside all the if() statements.

    Code:
    int boogie=0;
    
    if(opt[j]=='*') {
      //other code here
      boogie=1;
    }
    if(opt[j]=='/') {
      //other code here
      boogie=1;
    }
    
    /*other if statements here, with their own code 
    setting boogie to 1, if an operator is present, and 
    a reduction is made in the expression. Then:
    */
    if(boogie)  {
       /* boogie everything above j, one element to the left */
       for(k=j; num[k]<asd-1;k++)
          num[k] = num[k+1];
       boogie=0;  /* reset for the next loop */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 03:31 PM
  2. Longest Index
    By megazord in forum C Programming
    Replies: 3
    Last Post: 12-12-2009, 08:32 AM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM