help needed

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

    help needed

    hi guys below are the code for user to key in the epression like x^3+9x+8, i am only able to do + n - sign so i am wondering how can i add on to do expression like x^7*9x+9 or even (5x+3)(9/78+9)???? I really need help on this. Thank you

    #include "expression .h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>


    express chkterm(char*,i nt,int);

    myexp createExpressio n()
    {
    myexp exp;

    /*allocate memory*/
    exp=(myexp)mall oc(sizeof(struc t expressions));
    if(exp!=NULL)
    {
    exp->numberOfTerms= 0; /*set inital terms to zero*/
    return exp; /*return pointer*/
    }
    else
    printf("Out of range!!!\n");
    return NULL;
    }
    void string2Expressi on(char* inString,myexp* inExp)
    {
    char* tempString;
    int length;
    int i=0;
    int j=0;
    int sign=0;

    tempString=(cha r*)calloc(1,siz eof(char));

    length=strlen(i nString);
    /*determine the sign*/
    if(inString[0]=='+')
    sign = 1;
    else
    sign = 0;

    for(i=0;i<lengt h;i++)
    {
    /*checl for the position of the string*/
    if(inString[i]!='+'&&inString[i]!='-'&&i!=length-1)
    {
    tempString = (char*)realloc( tempString,size of(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 = (char*)realloc( tempString,size of(char)*j+1);
    tempString[j]=inString[i];
    j++;
    }

    if(j != 0)
    {
    /*save the term into expression format*/
    if((*inExp)->numberOfTerm s == 0)
    (*inExp)->exp = (express*)callo c(3,sizeof(expr ess));
    else
    (*inExp)->exp = (express*)reall oc((*inExp)->exp,sizeof(exp ress)*((*inExp)->numberOfTerms+ 1));


    (*inExp)->exp[(*inExp)->numberOfTerm s] = chkterm(tempStr ing,sign,j);
    (*inExp)->numberOfTerms+ +;

    if(inString[i] == '+')
    sign = 1;

    else
    sign = 0;

    }
    j = 0;
    tempString = (char*)realloc( tempString,size of(char)*0);
    }
    }
    }

    /*function to handle term*/
    express chkterm(char* inTerm,int inSign, int length)
    {
    char* tempString;
    express part;
    int xFlag=0,powerFl ag=0;
    int i,j=0;

    part = (express)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 = (char*)calloc(1 ,sizeof(char));

    /*function to check if x and power were given*/
    for(i=0; i < length; i++)
    {
    if(inTerm[i] != 'x' && inTerm[i] != '^')
    {
    tempString = (char*)realloc( tempString,size of(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(tempStri ng) > 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 = (char*)realloc( tempString,size of(char)*0);

    }
    }

    if(xFlag)
    {
    part->isX = 1;
    part->KnowX = 0;
    }

    if(powerFlag)
    part->x_power = atof(tempString );

    if(inSign)
    part->positive = 1;
    else
    part->positive = 0;

    return part;


    }
    }
    }
  • vninja
    New Member
    • Oct 2006
    • 40

    #2
    Ok really simple and not hard at all using namespace std and iostream.


    double ex(double x, double pwr);

    void main()
    {
    double power,
    power = ex(2,2);
    cout << power;
    }

    double ex(double x, double pwr)
    {
    for(int i=1; i<=pwr; i++)
    x*=x;
    return x;
    }


    there you go this can do many numbers to many powers just stay within range of the double.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by vninja
      double ex(double x, double pwr);

      void main()
      {
      double power,
      power = ex(2,2);
      cout << power;
      }

      double ex(double x, double pwr)
      {
      for(int i=1; i<=pwr; i++)
      x*=x;
      return x;
      }
      Have you tried this code?

      a. it doesn't compile because you have a , after double power but if fixed then

      b. It outputs 16 which is the wrong answer

      c. It will never work for negative powers but it goes ahead and tries anyway

      d. Although power is a double it only works for integer values

      e. basing any sort of comparison on a double is a dodgy move (although at least in this case the variable is not changed)

      f. since you use a int for the loop control variable having pwr as double is pointless because values of pwr outside the range an int will just cause it to go into an infiniate loop (probably, actually it invokes undefined behaviour because adding 1 to a signed int with a value of MAX_INT produces this).


      Solutions would be accept that you can't do negative or non integer powers and rewite the function with a unsigned long type for pwr

      Code:
      double ex(double x, unsigned long pwr)
      {
          double result = 1.0;
          for(unsigned long i=0; i<pwr; i++)
          {
              result*=x;
          }
          return result;
      }
      however you will find that even this is slow for large values of pwr, it can be speeded up as

      Code:
      double ex(double x, unsigned long pwr)
      {
          double result = 1.0;
          double multi = x;
          unsigned long powers = 1;
          unsigned long increment;
      
          for(unsigned long i=0; i<pwr; i+=increment)
          {
              if (powers > pwr-i)
              {
                  if (powers/2 >= pwr-i)
                  {
                      multi = x;
                      powers = 1;
                  }
                  else
                  {
                      while (powers > pwr-i)
                      {
                          multi /= x;
                          powers--;
                      }
                  }
              }
      
              increment = powers;
      
              result*=multi;
      
              if ((i+increment) <= pwr/2)
              {
                  multi=result;
                  powers=i+increment;
              }
          }
          return result;
      }
      Which gets the result more quickly by using squaring where possible

      but probably what you want to do is use the math.h function

      double pow(double, double);

      that does it all for you.

      Comment

      • cool17
        New Member
        • Oct 2006
        • 24

        #4
        hi guys

        But i need to write my code in C not in C++. That is why i am facing the problem...hopef ully u guys will be able to help me again...thank

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          The ex function is compileable C, it is just the cout in the example that isn't so just replace it with a printf.

          Comment

          Working...