473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

dev7060
636 Expert 512MB
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
  1.     node=(struct POLYS *)malloc(sizeof(struct POLYS));
  2.     cout<<"Enter coef: ";
  3.     cin>>node->coef;
  4.     cout<<"Enter exponent:";
  5.     cin>>node->exp;
  6.     node->next=NULL;
  7.     temp1=start;
  8.     if(start==NULL){
  9.         start=node;
  10.         }
  11.     else{
  12.         while(temp1->next!=NULL){
  13.             temp1=temp1->next;
  14.             }
  15.         temp1->next=node;
  16.         }
where:

-*node, *temp1 and *start are the POLYS type pointers
Expand|Select|Wrap|Line Numbers
  1. struct POLYS{
  2.             int coef;
  3.             int exp;
  4.             struct POLYS *next;
  5.         };
-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 2416
dev7060
636 Expert 512MB
I can't edit the title. That's "arrange *its terms in a linked form?"
Jan 14 '18 #2
weaknessforcats
9,208 Expert Mod 8TB
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 <enter>

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 <enter>

at one time and have the cins process it correctly.
Jan 14 '18 #3
dev7060
636 Expert 512MB
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 <enter>

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 <enter>

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
weaknessforcats
9,208 Expert Mod 8TB
Maybe I wasn't explaining clearly.

In the case of:

1 2 3 4 <enter>

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
dev7060
636 Expert 512MB
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
donbock
2,426 Expert 2GB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Keiron Waites | last post by:
I have the following code: <input type="text" name="search" class="search_top"> <a href="" onclick="window.location='search.inc.php'+document..search. value; return false;"...
3
by: Rahul Agarwal | last post by:
Hi In our web page we use a combination of HTML and server side controls and some of them have a custom attribute based on which we need to find and replace the values once the HTML is ready. ...
0
by: weezles | last post by:
hi I'm having trouble with using a linked form instead of a sub form on a 1 to many relationship. Then linked form can be accessed from various other forms. When it opens the linked form, if...
2
by: monnomiznogoud | last post by:
Ok, my problem is the following: I have very complicated Access 97 databases that link through ODBC to Sybase databases. Now in some of the forms controls I had queries that used as "where...
3
by: Michiel Rapati-Kekkonen | last post by:
hi, once upon a time I read that the communication with the back end would be faster if one table there was kept open. For that purpose you were supposed to make an otherwise useless table in...
0
by: colin | last post by:
Hi, I have an IDE style editor based on the WeifenLuo.WinFormsUI.Docking code, wich I must say seems realy good. Im trying to activate drop down menus and such from various special command...
3
by: gina farrow | last post by:
I have forms that I need four people to look at and be able to put a check on the form that they have seen it once check cannot be unchecked. the form goes to the next person to check it. when it has...
6
by: Alby22 | last post by:
I have a linked table between two databases(A & B). The data is entered by multiple users in to A once all the data is entered I review the data in B and then append that data to my main table in B....
2
by: rab26 | last post by:
Hi, It has been a while since I have used Access and even then it was basic use. I think I am thus a little in over my head! Some advice would therefore be much appreciated. The data input...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.