Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > symbolic computation in c language !!!

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
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-
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;
}

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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"
 
Reply With Quote
 
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


 
Reply With Quote
 
xhunga
Guest
Posts: n/a
 
      03-08-2007

Thank for you help,

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

I have download the file :defunc-1.3.tar.Z

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

Thank.

 
Reply With Quote
 
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 )
{
...

Instead of

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

...

it is better.

Thank

 
Reply With Quote
 
xhunga
Guest
Posts: n/a
 
      03-08-2007
Thank for you help,

I go to try you work.

Thank

 
Reply With Quote
 
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 )
> {
> ...


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
symbolic algebra computation with list xhunga C Programming 1 04-27-2007 11:52 PM
Symbolic computation with the language c : The Derivative step by step. xhunga C Programming 0 03-16-2007 10:30 PM
Symbolic computation : the derivative of a quotient. xhunga C Programming 0 03-10-2007 06:00 PM
Symbolic computation : the derivative of a product. xhunga C Programming 3 03-10-2007 02:10 PM
"Symbolic computation" Jesper Sahner Java 3 12-06-2004 10:41 AM



Advertisments