Problem with expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cool17
    New Member
    • Oct 2006
    • 24

    Problem with expression

    Code:
    #include "expression.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    xContainer splitTerm(string,int,int);
    
    
    
    expContainer createExpression()
    {
    	expContainer exp;
    	
    	/*allocate memory*/
    	exp = (expContainer)malloc(sizeof(struct expressions));
    	if(exp != NULL)
    	{
    	  /*set inital terms to zero*/
    		exp->numberOfTerms = 0;
    		return exp; /*return pointer*/
    	}
    	else
    		printf("Out Of Heap!!!\n");
    		return NULL;
    
    }
    
    void string2Expression(string inString,expContainer* inExp)
    {
    	string tempString;
    	int length;
    	int i=0, j=0,sign=0;
    	
    	tempString = (string)calloc(1,sizeof(char));
    	/*get string length*/
    	length = strlen(inString);
    	
    	/*determine first term sign*/
    	
    	if(inString[0] == '-')
    		sign = 0;
    
    	else if(inString[0] == '+')
    		sign = 1;
    
    	else if(inString[0] == '*')
    		sign = 2;
    	else
    		sign = 3;
    
    	/*The operation down there is to splite the whole string into separate term*/
    	/*the term is split by detecting the sign*/
    	
    	for(i=0; i < length; i++)
    	{
    	  /*pass if char not '+' and not '-' and current pos is not the end of the string*/
    		if(inString[i] != '+' && inString[i] != '-' && inString[i] != '*' && inString[i] != '/' && i != length-1)
    		{
    	
    			tempString = (char*)realloc(tempString,sizeof(char)*j+1);
    			tempString[j] = inString[i];
    			j++;
    			continue;
    		}
    		else
    		{	
    		  /*to add the last value before the end of the string*/
    			if(i == length-1)
    			{
    					tempString = (string)realloc(tempString,sizeof(char)*j+1);
    					tempString[j] = inString[i];
    					j++;
    			}
    
    			if(j != 0)
    			{
    			  /*save the term into expression format*/
    				if((*inExp)->numberOfTerms == 0)
    					(*inExp)->exp = (xContainer*)calloc(3,sizeof(xContainer));
    				else
    					(*inExp)->exp = (xContainer*)realloc((*inExp)->exp,sizeof(xContainer)*((*inExp)->numberOfTerms+1));
    
    				
    				(*inExp)->exp[(*inExp)->numberOfTerms] = splitTerm(tempString,sign,j);
    				(*inExp)->numberOfTerms++;
    
    				if(inString[i] == '-')
    					sign = 0;
    
    				else if(inString[i] == '+')
    					sign = 1;
    
    				else if(inString[i] == '*')
    					sign = 2;
    
    				else
    					sign = 3;
    
    			}
    			j = 0;
    			tempString = (string)realloc(tempString,sizeof(char)*0);
    		}
    	}
    }
    
    /*function to handle term*/
    xContainer splitTerm(string inTerm,int inSign, int length)
    {
    	string tempString;
    	xContainer part;
    	int xFlag=0,powerFlag=0;
    	int i,j=0;
    
    	part = (xContainer)malloc(sizeof(struct terms));
    	part->KnowX = 0;
    	part->positive = inSign;
    	part->isX = 0;
    	part->x_power = 1;
    	part->x_times = 1;
    	part->x_value = 0;
    
    	tempString = (string)calloc(1,sizeof(char));
    
    	/*function below is to determine x and square root*/
    	for(i=0; i < length; i++)
    	{
    		if(inTerm[i] != 'x' && inTerm[i] != '^')
    		{
    			tempString = (string)realloc(tempString,sizeof(char)*j+1);
    			tempString[j] = inTerm[i];
    			j++;
    
    			if(i == length-1 && !xFlag && !powerFlag)
    			{
    				part->x_value = atof(tempString);
    				part->KnowX = 1;
    			}
    			continue;
    		}
    		else
    		{
    			if(inTerm[i] == 'x' && strlen(tempString) > 0)
    			{
    				part->x_times = atof(tempString);
    				xFlag = 1;			
    			}
    			else if(inTerm[i] == 'x')
    				xFlag = 1;
    			
    			if(inTerm[i] == '^')
    			{
    				powerFlag = 1;
    				if(!xFlag)
    					part->x_value = atof(tempString);
    			}
    
    			j = 0;
    			tempString = (string)realloc(tempString,sizeof(char)*0);
    
    		}	
    	}
    
    	if(xFlag)
    	{
    		part->isX = 1;
    		part->KnowX = 0;
    	}
    	
    	if(powerFlag)
    		part->x_power = atof(tempString);
    
    	switch(inSign)
    		
    		case 0:
    		case 1:
    		case 2:
    		case 3:
    	
    
    	return part;
    
    
    }

    With this code i am able to capture expression like 3x+9x^2-2/7..something like that...So i am wondering if i want to have some like (((x-3)+(x+9))(x+6)) or (x+3)(3x-9)(4-x)....The figure can be anything up to the user... So how am i goin to write the code?
  • cool17
    New Member
    • Oct 2006
    • 24

    #2
    can someone help with it? I am trying to get my code going... thank thank

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Give a more precise description of your problem. Why will your code capture the first expression successfully but not the second?

      Comment

      Working...