symbolic computation in c language !!!

Discussion in 'C Programming' started by xhunga, Mar 7, 2007.

  1. xhunga

    xhunga Guest

    Hello,

    Into a text file,
    I have an equation.

    Examples:

    eq01.txt:

    cos(2*x+3*x^2)
    +b
    +cos(2*a+3*a^2)
    =

    Or

    eq02.txt:

    cos(2*x+
    3*x^2)
    -b*x
    -cos(2*a+3*x^2) *c os(2*a+3*x^2)
    =

    I want eliminate all the constant part.


    See below my work.

    Have I do some mistakes.
    The result seem correct.

    Thank.

    /* ------------------------------------ */
    /* ------------------------------------ */
    #include <stdio.h>
    #include <stdlib.h>
    /* ------------------------------------ */
    #define FILENAME 11
    #define CHARMAX 1000
    #define CHAR1 2
    /* ------------------------------------ */
    void noconst(
    char *FP,
    char *eq,
    char *x
    )
    {
    /* ------------------------------------ */
    FILE *fp;
    /* ------------------------------------ */
    char t[CHARMAX];
    char c[CHAR1];
    /* ------------------------------------ */
    int p;
    int y;
    /* ------------------------------------ */
    strcpy( t,"");
    strcpy( c,"");
    strcpy(eq,"");
    /* ------------------------------------ */
    p=0;
    y=0;
    /* ------------------------------------ */

    fp = fopen(FP,"r");

    if (fp==NULL) printf("File doesn't exist\n");
    else
    {
    while(c[0]!=EOF)
    {
    c[0] = getc(fp);

    if( !(strcmp(c,"(")) ) p++;
    if( !(strcmp(c,")")) ) p--;
    if( !(strcmp(c,x )) ) y=1;
    if( !(strcmp(c,"\n"))||
    !(strcmp(c," " )) )strcpy(c,"");

    if( (!strcmp(c,"+")||
    !strcmp(c,"-")||
    !strcmp(c,"="))&&
    !p )/* p==0 true */
    {
    if(y==1){/* add a new eq */
    strcat(eq,"@[");
    strcat(eq,t);
    strcat(eq,"]");
    strcpy(t,"");
    strcat(t,c);
    y=0;
    }
    else{
    strcpy(t,"");/* constant */
    }
    }
    else strcat(t,c);
    }
    }

    if(fp!=NULL)fclose(fp);
    }
    /* ------------------------------------ */
    int main(void)
    {
    char FP[FILENAME];
    char eq[CHARMAX];
    char x[CHAR1];

    strcpy(FP,"eq04.txt");
    strcpy(x,"x");

    noconst(FP,eq,x);

    printf(" eq: %s \n",eq);

    getchar();
    return 0;
    }
     
    xhunga, Mar 7, 2007
    #1
    1. Advertising

  2. xhunga

    user923005 Guest

    Other than a missing prototype, a narrow filename allowance and an
    annoying capitalized name for the file (FP looks like a macro) I don't
    see anything outrageously wrong with the code.

    Now, as to what it is supposed to be doing and what it is actually
    doing that is another matter.

    It appears that you are trying to write an equation parser/
    interpreter.
    There is a FAQ for that.

    18.14: I need code to parse and evaluate expressions.

    A: Two available packages are "defunc," posted to comp.sources.misc
    in December, 1993 (V41 i32,33), to alt.sources in January, 1994,
    and available from sunsite.unc.edu in
    pub/packages/development/libraries/defunc-1.3.tar.Z, and
    "parse," at lamont.ldgo.columbia.edu. Other options include the
    S-Lang interpreter, available via anonymous ftp from
    amy.tch.harvard.edu in pub/slang, and the shareware Cmm ("C-
    minus-minus" or "C minus the hard stuff"). See also questions
    18.16 and 20.6.

    There is also some parsing/evaluation code in _Software
    Solutions in C_ (chapter 12, pp. 235-55).

    /* ------------------------------------*/
    /* ------------------------------------ */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* ------------------------------------ */
    #define CHARMAX 1000
    #define CHAR1 2
    /* ------------------------------------ */
    void noconst( char *filename, char *eq, char *x )
    {
    FILE *f;
    char t[CHARMAX];
    char c[CHAR1];
    int p;
    int y;

    strcpy(t, "");
    strcpy(c, "");
    strcpy(eq, "");
    p = 0;
    y = 0;

    f = fopen(filename, "r");

    if (f == NULL)
    printf("File doesn't exist\n");
    else {
    while (c[0] != EOF) {
    c[0] = getc(f);

    if (!(strcmp(c, "(")))
    p++;
    if (!(strcmp(c, ")")))
    p--;
    if (!(strcmp(c, x)))
    y = 1;
    if (!(strcmp(c, "\n")) ||
    !(strcmp(c, " ")))
    strcpy(c, "");

    if ((!strcmp(c, "+") ||
    !strcmp(c, "-") ||
    !strcmp(c, "=")) &&
    !p) { /* p==0 true */
    if (y == 1) { /* add a new eq */
    strcat(eq, "@[");
    strcat(eq, t);
    strcat(eq, "]");
    strcpy(t, "");
    strcat(t, c);
    y = 0;
    } else {
    strcpy(t, ""); /* constant */
    }
    } else
    strcat(t, c);
    }
    }

    if (f != NULL)
    fclose(f);
    }

    /* ------------------------------------ */
    int main(void)
    {
    char filename[FILENAME_MAX];
    char eq[CHARMAX];
    char x[CHAR1];

    strcpy(filename, "eq04.txt");
    strcpy(x, "x");

    noconst(filename, eq, x);

    printf(" eq: %s \n", eq);

    getchar();
    return 0;
    }
     
    user923005, Mar 7, 2007
    #2
    1. Advertising

  3. user923005 <> wrote:
    > Other than a missing prototype, a narrow filename allowance and an
    > annoying capitalized name for the file (FP looks like a macro) I don't
    > see anything outrageously wrong with the code.


    Well, except the problem with reading with fgetc() into a char and
    then comparing to EOF, which never will be true even if the end of
    the file as been reached.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 7, 2007
    #3
  4. (Jens Thoms Toerring) writes:
    > user923005 <> wrote:
    >> Other than a missing prototype, a narrow filename allowance and an
    >> annoying capitalized name for the file (FP looks like a macro) I don't
    >> see anything outrageously wrong with the code.

    >
    > Well, except the problem with reading with fgetc() into a char and
    > then comparing to EOF, which never will be true even if the end of
    > the file as been reached.


    Or, if plain char is signed, will be true on reading a character with
    the value (char)-1.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 7, 2007
    #4
  5. "user923005" <> wrote in message
    > 18.14: I need code to parse and evaluate expressions.
    >
    > There is also some parsing/evaluation code in _Software
    > Solutions in C_ (chapter 12, pp. 235-55).
    >

    Or download "MiniBasic - how to write a script interpreter" by me
    http://www.lulu.com/bgy1mm

    If $2.50 breaks the bank, the code is freely avialable on my website


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Mar 7, 2007
    #5
  6. xhunga

    xhunga Guest

    xhunga, Mar 8, 2007
    #6
  7. xhunga

    xhunga Guest

    Jens Thoms Toerring a écrit :
    ....
    > Well, except the problem with reading with fgetc() into a char and
    > then comparing to EOF, which never will be true even if the end of
    > the file as been reached.
    > Regards, Jens
    > --
    > \ Jens Thoms Toerring ___
    > \__________________________ http://toerring.de


    Thank for you help,

    Pearheaps

    while( (c[0]=getc(f)) != EOF )
    {
    ...

    Instead of

    while(c[0]!=EOF)
    {
    c[0] = getc(f);

    ...

    it is better.

    Thank
     
    xhunga, Mar 8, 2007
    #7
  8. xhunga

    xhunga Guest

    Thank for you help,

    I go to try you work.

    Thank
     
    xhunga, Mar 8, 2007
    #8
  9. xhunga <> wrote:
    > Jens Thoms Toerring a ?crit :
    > ...
    > > Well, except the problem with reading with fgetc() into a char and
    > > then comparing to EOF

    >
    > Pearheaps


    > while( (c[0]=getc(f)) != EOF )
    > {
    > ...


    > Instead of


    > while(c[0]!=EOF)
    > {
    > c[0] = getc(f);


    > it is better.


    Not really, you must store the result of fgetc() in an integer (that's
    the return type of fgetc()) and compare that to EOF - EOF is a value
    that cannot be stored in a char. But if you try to store it in a char
    variable its value gets truncated (the resulting value depending on
    if you assign EOF to a signed or unsigned char) that first of all
    coincides with one of the possible values a char read from the file
    may have, so making the two indistinguishable, and second may lead to
    the comparison with the "real" value of EOF to fail, thus making
    detection of the end of file impossible.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 8, 2007
    #9
  10. EOF and char (was Re: symbolic computation in c language !!!)

    Jens Thoms Toerring wrote:
    [...]
    > Not really, you must store the result of fgetc() in an integer (that's
    > the return type of fgetc()) and compare that to EOF - EOF is a value
    > that cannot be stored in a char.

    [...]

    Is EOF really something that _cannot_ be stored in a char? Or is it
    something that doesn't represent any "valid" char? What happens on
    those systems where sizeof(int)==1?

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Mar 8, 2007
    #10
  11. Re: EOF and char (was Re: symbolic computation in c language !!!)

    In article <>,
    Kenneth Brody <> wrote:
    >Jens Thoms Toerring wrote:
    >[...]
    >> Not really, you must store the result of fgetc() in an integer (that's
    >> the return type of fgetc()) and compare that to EOF - EOF is a value
    >> that cannot be stored in a char.

    >[...]
    >
    >Is EOF really something that _cannot_ be stored in a char? Or is it
    >something that doesn't represent any "valid" char? What happens on
    >those systems where sizeof(int)==1?


    -1 is a perfectly valid value for EOF, and on systems where char is
    signed will fit in a char.

    I believe that the consensus of the language lawyers about systems on
    which sizeof(int)==1 is that not being able to store EOF in an int is
    likely to get lost in Other Problems.


    dave

    --
    Dave Vandervies
    Oh, and when you insert whitespace in the middle of a function identifier,
    it stops working.
    --Ryan Hennesy in comp.lang.c
     
    Dave Vandervies, Mar 8, 2007
    #11
  12. xhunga

    xhunga Guest

    Jens Thoms Toerring a écrit :
    > xhunga <> wrote:
    > > Jens Thoms Toerring a ?crit :
    > > ...
    > > > Well, except the problem with reading with fgetc() into a char and
    > > > then comparing to EOF

    > >


    Perhaps (II)

    char c[2];
    int c_int;

    ...
    while( (c_int=getc(f)) != EOF )
    {
    c[0]=c_int;
    ...

    Instead of

    while(c[0]!=EOF)
    {
    c[0] = getc(f);

    is better?

    thank.
     
    xhunga, Mar 9, 2007
    #12
  13. Re: EOF and char (was Re: symbolic computation in c language !!!)

    Dave Vandervies wrote:
    >
    > In article <>,
    > Kenneth Brody <> wrote:
    > >Jens Thoms Toerring wrote:
    > >[...]
    > >> Not really, you must store the result of fgetc() in an integer (that's
    > >> the return type of fgetc()) and compare that to EOF - EOF is a value
    > >> that cannot be stored in a char.

    > >[...]
    > >
    > >Is EOF really something that _cannot_ be stored in a char? Or is it
    > >something that doesn't represent any "valid" char? What happens on
    > >those systems where sizeof(int)==1?

    >
    > -1 is a perfectly valid value for EOF, and on systems where char is
    > signed will fit in a char.
    >
    > I believe that the consensus of the language lawyers about systems on
    > which sizeof(int)==1 is that not being able to store EOF in an int is
    > likely to get lost in Other Problems.


    Just remember that "sizeof(int)==1" doesn't necessarily mean that
    ints are 8 bits.

    Long before I learned C (so I can't comment on how a C compiler
    would be implemented on the system), I programmed on a DEC KL-10
    computer. This system's smallest addressable unit was 36 bits
    wide. I can see a C compiler on such a system using CHAR_BITS==36.

    Now, there were machine-level opcodes to do things like "get the
    next N bits from this pointer, store it in this register, and
    increment the pointer to the next N-bit value", which could pack
    four 8-bit values into a single 36-bit "word" with 4 wasted bits.
    (It could also store six 6-bit characters into the 36-bit word,
    with no waste, which is how filenames were stored.) I have no
    idea how such a construct would affect CHAR_BITS and sizeof(int).

    The FORTRAN compiler on that system stored string literals as
    five 7-bit values per 36-bit word. However, I understand that C
    requires a minimum of 8 bits per char.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Mar 9, 2007
    #13
  14. xhunga

    Ben Pfaff Guest

    Re: EOF and char

    Kenneth Brody <> writes:

    > Just remember that "sizeof(int)==1" doesn't necessarily mean that
    > ints are 8 bits.


    It *never* means that ints are 8 bits, in a standard C
    implementation. An int has to take up at least 16 bits to
    conform.
    --
    "What is appropriate for the master is not appropriate for the novice.
    You must understand the Tao before transcending structure."
    --The Tao of Programming
     
    Ben Pfaff, Mar 9, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesper Sahner

    "Symbolic computation"

    Jesper Sahner, Oct 23, 2004, in forum: Java
    Replies:
    3
    Views:
    453
  2. xhunga
    Replies:
    3
    Views:
    279
    xhunga
    Mar 10, 2007
  3. xhunga
    Replies:
    0
    Views:
    320
    xhunga
    Mar 10, 2007
  4. xhunga
    Replies:
    0
    Views:
    313
    xhunga
    Mar 16, 2007
  5. xhunga

    symbolic algebra computation with list

    xhunga, Apr 27, 2007, in forum: C Programming
    Replies:
    1
    Views:
    321
Loading...

Share This Page