Velocity Reviews > symbolic computation in c language !!!

# symbolic computation in c language !!!

xhunga
Guest
Posts: n/a

 03-07-2007

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;
}

user923005
Guest
Posts: n/a

 03-07-2007
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-
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;
}

Jens Thoms Toerring
Guest
Posts: n/a

 03-07-2007
user923005 <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de

Keith Thompson
Guest
Posts: n/a

 03-07-2007
(E-Mail Removed) (Jens Thoms Toerring) writes:
> user923005 <(E-Mail Removed)> 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) (E-Mail Removed) <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"

Malcolm McLean
Guest
Posts: n/a

 03-07-2007

"user923005" <(E-Mail Removed)> 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

xhunga
Guest
Posts: n/a

 03-08-2007

Thank for you help,

I have correct my code,
and I have read the faq.

http://www.ibiblio.org/pub/packages/...func-1.3.tar.Z

Thank.

xhunga
Guest
Posts: n/a

 03-08-2007

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 ___ (E-Mail Removed)
> \__________________________ http://toerring.de

Thank for you help,

Pearheaps

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

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

...

it is better.

Thank

xhunga
Guest
Posts: n/a

 03-08-2007
Thank for you help,

I go to try you work.

Thank

Jens Thoms Toerring
Guest
Posts: n/a

 03-08-2007
xhunga <(E-Mail Removed)> 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 )
> {
> ...

> 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 ___ (E-Mail Removed)
\__________________________ http://toerring.de

Kenneth Brody
Guest
Posts: n/a

 03-08-2007
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: <(E-Mail Removed)>