Right now, the coefficients and exponents are to be entered separately by the user. The coefficient and exponent are then added to the list by adding them to node's fields and then adding the node at the end of the expression.
Like:
(Output screen)
Enter coefficient: 5
Enter exponent: 2
Expression now is: 5x^2
Press 1 to continue insertion ...
The code that assigns these values to node's fields and add then to the expression is:
Expand|Select|Wrap|Line Numbers
- node=(struct POLYS *)malloc(sizeof(struct POLYS));
- cout<<"Enter coef: ";
- cin>>node->coef;
- cout<<"Enter exponent:";
- cin>>node->exp;
- node->next=NULL;
- temp1=start;
- if(start==NULL){
- start=node;
- }
- else{
- while(temp1->next!=NULL){
- temp1=temp1->next;
- }
- temp1->next=node;
- }
-*node, *temp1 and *start are the POLYS type pointers
Expand|Select|Wrap|Line Numbers
- struct POLYS{
- int coef;
- int exp;
- struct POLYS *next;
- };
The above method works efficiently but is there any way to input the complete expression at once?
Like:
(Output screen)
Enter expression: 3x^4 + 5x^5 + 8x^6
And then the program can automatically arrange them in a linked form?
Maybe treating the input as a string and then separating the coefficient and exponent of each term of the expression and then adding the values to the node's fields?