424,955 Members | 1,756 Online Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

How to input the complete polynomial at once and arrange it's terms in a linked form?

 P: 62 The program is of addition of the polynomials. Implementing it through linked lists. Every node of the list has three fields: coefficient, exponent and the next pointer field pointing to the next node of the polynomial expression. The next pointer field of the last node contains NULL. 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;         } where: -*node, *temp1 and *start are the POLYS type pointers Expand|Select|Wrap|Line Numbers struct POLYS{             int coef;             int exp;             struct POLYS *next;         }; -start points to the first node of the expression 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? Jan 14 '18 #1
6 Replies

 P: 62 I can't edit the title. That's "arrange *its terms in a linked form?" Jan 14 '18 #2

 Expert Mod 5K+ P: 9,197 You are using cin. While you enter data, your program is not running. Instead your program is suspended while the input system runs. Your program will not resume until you press the enter key. So if you enter: 1 2 3 4 Your program first cin will process 1 (pause on whitespace ...more of your code executes.... You second cin will process the 2 (pause on whitespace) ...more of your code executes... etc... Is this what you mean by "linked form"? So you should be able to enter: 3x^4 + 5x^5 + 8x^6 at one time and have the cins process it correctly. Jan 14 '18 #3

 P: 62 Thanks for the reply. By "arranging in a linked form", I meant to create a node and dynamically allocate the memory for it. Just to clear up: If the user enters: 3x^4 + 5x^5 + 8x^6 - The program can detect the coefficient and exponent of the first term (at first) of the expression. - Create a node and set its "coef" and "exp" fields same as entered by the user. - Add the node to the linked list. - Similarly, the program can detect the coefficient and exponent of the second term of the expression. - Create a node and set its "coef" and "exp" fields same as entered by the user. - Add the node to the linked list (i.e. the next pointer field of the first node will point to this node). etc.. So after this, there will be a linked list having 3 nodes. _____________________________ "1 2 3 4 Your program first cin will process 1 (pause on whitespace ...more of your code executes.... You second cin will process the 2 (pause on whitespace) ...more of your code executes... etc... So you should be able to enter: 3x^4 + 5x^5 + 8x^6 at one time and have the cins process it correctly." Can you provide a code snippet of that for the explanation of the last one? I understand the working of cins in the simple input format like in "1 2 3 4". But in the "3x^4 + 5x^5 + 8x^6" or in any other expression, some changes may be required like: since as the number of terms present in the expression depends on the user, therefore the code has to manually set the number of times cin will perform its action. Also, the requirement is only of the exponent and the coefficient. So, how to just extract the useful info from the whole input provided by the user like the values 3, 4 and 8, 6 from 3x^4 + 8x^6 with the help of cins? Jan 14 '18 #4

 Expert Mod 5K+ P: 9,197 Maybe I wasn't explaining clearly. In the case of: 1 2 3 4 Those, in fact could be your coefficients and exponents. 1x^2 3x^4 It just how you interpret what was entered. cin returns when what it scans, in this case, is not a digit. So you pick off the 1 Next, you cin.ignore() to bypass the non digit. You now pick off the 2. I am assuming the user will enter exactly what is required, in the correct order, for the correct number of elements. If you try o create a flexible general purpose data entry mechanism, you will be coding for a long, long time. cin/cout are just provided by C++ as a starting point. Commercial applications generally don't use them. Nothing in Windows apps use them. I would provide only what the program needs in the form that it needs it and not do anything fancy. Since this appears not to be what you want, let me ask why you need this user flexibility? Jan 15 '18 #5

 P: 62 Actually, the flexible data entry mechanism may be beneficial in many cases: -There are several operations like addition, subtraction, multiplication, division etc. which need to be performed with them. Therefore, the program needs debugging and testing again and again. So, entering the coefficients and exponents of multiple polynomials again and again will be time consuming. -Right now the things are being tested on a terminal but maybe a GUI at a later point. So, I think it's just a standard way of inputting polynomials, integral equations, trigonometric equations etc. as a whole and treat them accordingly unlike the unprofessional way of inputting the components separately. By inputting them as whole, it may be parsed as a string and can be passed to different modules. Say, parsing stuff can be put in a PolyCommand.h class and Poly.h can have the poly manipulation methods and stuff like addition, multiplication etc. Jan 15 '18 #6

 Expert 100+ P: 2,396 If your goal is to speed up debugging/testing then I suggest you modify the front-end to obtain the coefficients from a text file. Each test case could be a different line in the file. Once you have confidence the back-end is working, then you can restore the front-end and merely test that the input subsystem works properly. Jan 18 '18 #7 