Re: Is there a more elegant way to populate a linked list ?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 05/21/04
- Next message: Robert W Hand: "Re: Is there a more elegant way to populate a linked list ?"
- Previous message: Robert W Hand: "Re: find sub array"
- In reply to: Wayne McDermott: "Is there a more elegant way to populate a linked list ?"
- Next in thread: Robert W Hand: "Re: Is there a more elegant way to populate a linked list ?"
- Reply: Robert W Hand: "Re: Is there a more elegant way to populate a linked list ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 21 May 2004 09:06:40 -0400
On Fri, 21 May 2004 17:22:38 +0700, "Wayne McDermott"
<elhmbre@ozemail.com.au> wrote:
>Evening All,
>
>I have a class that represents a sequence of polynomial expressions. Each
>polynomial consists of a coefficient and a power and the expression is
>represented as a linked list of structures. One of my class constructors
>accepts polynomials as pairs of values from a text file and is included
>below. It works but I cannot help but feel there is a more elegant and
>generic way of populating structures without resorting to hardwired values
>such as "i" which identifies an integer as a coefficient or power depending
>on its place in the file. Any ideas ?
>
>/*
>Constructor that accepts a file.
>Pre : A correctly formatted file is passed to the constructor. The file
>consists of number pairs where the first number represents a polynomial
>coefficient and the second number represents the polynomial power.
>Post : An object of type Polynomial consisting of a linked list of nodes
>is created.
>*/
>Polynomial::Polynomial(ifstream& InputFile)
>{
>Polynomial p; //Create an object of type Polynomial
>node_t node;
>int value;
>int i = 1; //A counter used to determine if the value read from file is a
>coefficient or a power
>cout << "File Constructor has been called" << endl; //Just a test
>if (!InputFile)
>{
>cerr << "Unable to open file.\n";
>}
>while (InputFile >> value)
>{
>if (i == 1)
>{
>node.coefficient = value;
>cout << "coefficient is " << node.coefficient << endl;
>}
>else // i = 2 so value must be a power
>{
>node.power = value;
>cout << "power is " << node.power << endl;
>}
>if (i > 1)
>p.Insert(node);
>if (i == 1)
>i++;
>else
>i = 1; //We have inserted a node so reset the counter to 1
>}
>p.ToFirst();
>if (p.CurIsEmpty())
>cout << "File is empty or does not contain valid values" << endl;
>}
>
Off-hand, I don't see the reason for "i" at all.
If it were me, I'd format the input in some human-readable pair format,
such as:
(n,n) (n,n) (n,n)
or
n,n n,n n,n
or
(n n)(n n)
or anything at all that sets pairs apart from each other to avoid errors
such as
1 2 4 6 9 11 12 4 6 3 18
where you'd be hard pressed to know where the missing value was supposed to
go.
Then, I'd read the input /a pair at a time/, validate that a pair was read,
and repeat until EOF. Nowhere would I need a counter; I'd simply be reading
two values per loop iteration, into separate variables. Or, to make the
loop code even simpler, define an extractor for your node_t type and read
directly into node in the loop. That would collapse your loop down to:
while (InputFile >> node)
p.insert(node);
(I'm not sure what that business at the end of your loop is about, but you
can adjust accordingly if really necessary.)
HTH,
-leor
-- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
- Next message: Robert W Hand: "Re: Is there a more elegant way to populate a linked list ?"
- Previous message: Robert W Hand: "Re: find sub array"
- In reply to: Wayne McDermott: "Is there a more elegant way to populate a linked list ?"
- Next in thread: Robert W Hand: "Re: Is there a more elegant way to populate a linked list ?"
- Reply: Robert W Hand: "Re: Is there a more elegant way to populate a linked list ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|