so, we want our polynomials in the "proper" order (descending exponents) right? of course. or so my algebra 2 book says. *shrug*
anyway, to do that i have a dynamically alloced 2d array holding each of the terms of the polynomial as a character string. currently, im attempting this to sort them:
qsort((void *)TermArray, NumberOfTerms, sizeof(char *), compare);
...
int compare(const void *a, const void *b )
{
char *str1 = (char *) a, *str2 = (char *) b;
int len1 = strlen(str1), len2 = strlen(str2);
int i = 0, exp1 = 0, exp2 = 0;
for(i = 0; i < len1; i++)
{
if(str1 == '^')
exp1 = i;
}
for(i = 0; i < len2; i++)
{
if(str2 == '^')
exp2 = i;
}
len1 -= exp1;
len2 -= exp2;
exp1 = atoi(str1+len1);
exp2 = atoi(str2+len2);
if(exp1 < exp2)
return -1;
if(exp1 > exp2)
return 1;
else
return 0;
}
that _should_ scan until it hits the exponent, convert the exponent to an int, compare, and return properly (i think). but it doesnt. qsort isnt quite passing the correct values. anyone care to point out my silly error? (this is the first time ive tried sorting a 2d array, so i know itll be obvious to someone smarter than i)
žÅNžÅ
anyway, to do that i have a dynamically alloced 2d array holding each of the terms of the polynomial as a character string. currently, im attempting this to sort them:
qsort((void *)TermArray, NumberOfTerms, sizeof(char *), compare);
...
int compare(const void *a, const void *b )
{
char *str1 = (char *) a, *str2 = (char *) b;
int len1 = strlen(str1), len2 = strlen(str2);
int i = 0, exp1 = 0, exp2 = 0;
for(i = 0; i < len1; i++)
{
if(str1 == '^')
exp1 = i;
}
for(i = 0; i < len2; i++)
{
if(str2 == '^')
exp2 = i;
}
len1 -= exp1;
len2 -= exp2;
exp1 = atoi(str1+len1);
exp2 = atoi(str2+len2);
if(exp1 < exp2)
return -1;
if(exp1 > exp2)
return 1;
else
return 0;
}
that _should_ scan until it hits the exponent, convert the exponent to an int, compare, and return properly (i think). but it doesnt. qsort isnt quite passing the correct values. anyone care to point out my silly error? (this is the first time ive tried sorting a 2d array, so i know itll be obvious to someone smarter than i)
žÅNžÅ