I'm working on an algebra application, here's the code
struct quotient
{
int numerator;
int denominator;
};
struct term
{
struct quotient coefficient;
char varname;
struct quotient power;
};
struct function
{
struct term* terms;
char* operators;
struct quotient coefficient;
struct quotient power;
};
//Constructor Functions
struct quotient NewQuotient()
{
struct quotient temp;
printf("Enter the numerator\n");
scanf("%d", &temp.numerator);
printf("Enter the denominator\n");
scanf("%d", &temp.denominator);
return temp;
}
char NewVarname()
{
char temp;
printf("Enter the variable letter: \n");
scanf("%c", &temp);
return temp;
}
struct term NewTerm()
{
//broken, won't let you enter a variable name, sets it to x by default until that's resolved
struct term temp;
printf("Enter the coefficient: ");
temp.coefficient = NewQuotient();
printf("Enter the variable name: \n");
temp.varname = NewVarname();
temp.varname = 'x';
printf("Enter the power: ");
temp.power = NewQuotient();
return temp;
}
void NewFunction(struct function* func, int size)
{
//so far so good
unsigned i;
func->terms = (struct term*)calloc(size, sizeof(struct term));
//loop to initialize each term
for(i = 0; i < size; i++)
{
func->terms[i] = NewTerm();
}
return;
}
int main(){
struct function fofx;
NewFunction(&fofx, 2);
DisplayFunction(&fofx, 2);
DeleteFunction(&fofx);
return 0;
}
Here is the output:
Enter the numerator:
1
Enter the denominator:
2
Enter the numerator:
3
Enter the denominator:
4
....
etc, until the end of the loop.
Half of the statements in NewTerm don't seem to be executing at all, but the program seems to successfully allocate and initialize a new function. Any help is very much appreciated, I am very confused about this. I didn't include the display and delete function, they work fine, but if they'd be helpful I can add them on here.