I am recently trying to write a code in which the program will show the derivative of a function using the product rule. The function can be either x^n, sin(ax) or cos(ax). I have made local functions, such as
void derivcc (){
double a, b;
fscanf(f,"cos(%lf*x)*cos(%lf*x)",&a, &b);
if (a<0 && b<0)
printf("%lfsin(%lf*x)*cos(%lf*x)+%lfsin(%lf*x)*cos(%lf*x)\n", -a,a,b,-b,b,a);
else if (a<0 && b>0)
printf("%lfsin(%lf*x)*cos(%lf*x)-%lfsin(%lf*x)*cos(%lf*x)\n", -a,a,b,b,b,a);
else if (a>0 && b<0)
printf("-%lfsin(%lf*x)*cos(%lf*x)+%lfsin(%lf*x)*cos(%lf*x)\n", a,a,b,-b,b,a);
else printf("-%lfsin(%lf*x)*cos(%lf*x)-%lfsin(%lf*x)*cos(%lf*x)\n",
a,a,b,b,b,a);}
However, the issue that I am having is in the main function. I want to know how should I address the while loop, so the local functions can work. Here is the main:
int main(void)
{
FILE * f;
char lines[50];
f=fopen("function.txt", "r");
if (f == NULL)
{
printf("Error with opening the file!/n");
exit (1);
}
int c;
fgets(lines, sizeof(lines), f);
char sin[]="sin";
char cos[]="cos";
char x[]="x^";
char *checkc, *checks, *checkx;
checkc = strstr(lines,cos);
checks = strstr(lines,sin);
checkx = strstr(lines, x);
double a,b;
while((c = getc(f)) != EOF)
{
if (checks == NULL)
{
if (checkc == NULL)
{
derivxx();
}
else
{
if (checkx==NULL) derivcc();
else
{
if (lines[0] == cos[0]) derivcx();
else derivxc();
}
}
}
else
{
if (checkc == NULL && checkx == NULL)
{
derivss();
}
else
{
if (checkc == NULL && checkx != NULL)/
{
if (lines[0]==sin[0]) derivsx();
else derivxs();
}
else if (lines[0]==sin[0]) derivsc();
else derivcs();
}
}
}
fclose(f);
Aucun commentaire:
Enregistrer un commentaire