symbolic computation in c language !!!

X

xhunga

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

user923005

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

Jens Thoms Toerring

user923005 said:
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
 
K

Keith Thompson

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.
 
M

Malcolm McLean

user923005 said:
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
 
X

xhunga

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

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
 
J

Jens Thoms Toerring

xhunga said:
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
 
K

Kenneth Brody

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:[email protected]>
 
D

Dave Vandervies

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
 
X

xhunga

Jens Thoms Toerring a écrit :
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.
 
K

Kenneth Brody

Dave said:
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:[email protected]>
 
B

Ben Pfaff

Kenneth Brody said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top