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