Page 1 of 1 [ 9 posts ] 

jread
Snowy Owl
Snowy Owl

User avatar

Joined: 9 Nov 2005
Age: 45
Gender: Male
Posts: 138
Location: Austin, TX

05 Nov 2007, 2:27 pm

DISCLAIMER: I am in no way responsible if blood starts shooting from your eyes after looking at this!

So, this is what happens when you stay up all night writing a program that's due the next day because you're a procrastinating bastard. I needed a function that read a polynomial from a linked list (each node had data elements for coefficient and power), then printed them to the screen in the correct format. I wrote this one on the first pass and while it's horrid, it did the job and I didn't have enough time to refine it.

Code:
void printList()
      {
         Node *Walker;
         Walker = pHead;
         while (Walker != NULL)
         {
            if (Walker->power != 0)
            {
               if (Walker->coeff > 0)
               {
                  if (Walker == pHead)
                     cout << Walker->coeff << "x" << "^" << Walker->power;
                  else
                  {
                     if (Walker->power > 1)
                        cout << "+" << Walker->coeff << "x" << "^" << Walker->power;
                     else
                        cout << "+" << Walker->coeff << "x";
                  }
               }
               else
               {
                  if (Walker->power > 1)
                     cout << Walker->coeff << "x" << "^" << Walker->power;
                  else
                     cout << "+" << Walker->coeff << "x";
               }
            }
            else
            {
               if (Walker->coeff > 0)
                  cout << "+" << Walker->coeff;
               else
                  cout << Walker->coeff;
            }
            Walker = Walker->next;         
         }
      }     
};



Fuzzy
Veteran
Veteran

User avatar

Joined: 30 Mar 2006
Age: 52
Gender: Male
Posts: 5,223
Location: Alberta Canada

06 Nov 2007, 2:27 am

Spagetti much?

Its not too bad!



ahayes
Veteran
Veteran

User avatar

Joined: 2 Dec 2006
Gender: Male
Posts: 9,506

06 Nov 2007, 11:59 am

I've written worse until I figured out switch-case in C.



TheZach
Deinonychus
Deinonychus

User avatar

Joined: 10 Aug 2007
Gender: Male
Posts: 392
Location: Michigan, USA

06 Nov 2007, 2:26 pm

Hey, s**t happens. Look at windows.


_________________
TheZach

<a rel="nofollow" href="http://www.thezach.net/blog">My Blog</a>


TheBladeRoden
Veteran
Veteran

User avatar

Joined: 10 Feb 2005
Age: 41
Gender: Male
Posts: 1,208
Location: Wisconsin

06 Nov 2007, 5:37 pm

What does it do? It seems to involve some guy named Walker a lot.


_________________
"I reject your reality, and substitute my own" -Adam Savage


lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 76
Gender: Male
Posts: 9,795
Location: Somerset UK

06 Nov 2007, 8:39 pm

Changing whitespace and replacing "Walker" with "W", as a long variable name is merely clutter in a short piece of code...

Code:
void printList() {
  Node *W;
  W = pHead;
  while (W != NULL) {
    if (W->power != 0) {
      if (W->coeff > 0) {
        if      (W == pHead)   cout <<        W->coeff << "x" << "^" << W->power;
        else if (W->power > 1) cout << "+" << W->coeff << "x" << "^" << W->power;
        else                   cout << "+" << W->coeff << "x";     
      } else {
        if (W->power > 1)      cout <<        W->coeff << "x" << "^" << W->power;
        else                   cout << "+" << W->coeff << "x";
      }
    } else {
      if (W->coeff > 0)        cout << "+" << W->coeff;
      else                     cout <<        W->coeff;
    }
    W = W->next;         
  }     
}
};

There's the extra close brace, which might be a copy/paste slip.

There's a question... although x^0 is unity (when x is not zero), is this really part of the requirement, to drop "x^0" from the expression? In which case, you should also test for "W == pHead" in the second section, to also avoid the spurious prefixing of a "+" sign.

Also, the "+" is wrong in the fifth "cout" line.

From your description, I assume that "power" is unsigned. Even so, I would be tempted to use "W->power" != 1", just so negative powers would not be dropped. It will suffice, whether the field is signed or unsigned.

Anyhow, my pet hate is the current trend of using a code layout that completely obscures everything.

An alternative version would be...
Code:
void printList() {
  Node *W;
  char *p = ""; // don't need infix "+" at the start
  for (W = pHead; W != NULL; W = W->next) {
    if (W->coeff > 0) cout << p;
    p = "+"; // from now on, may need to force an infix "+"
    cout << W->coeff;
    if (W->power != 0) {
      cout << "x";
      if (W->power != 1) cout << W->power;
    }
  }
}


_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer


lonelyLady
Snowy Owl
Snowy Owl

User avatar

Joined: 19 Sep 2007
Age: 38
Gender: Female
Posts: 166
Location: behind a very old computer

13 Nov 2007, 8:46 pm

a dumb question: what does '->' do? I took a c++ programming course but I've never seen this one. Thanks!


_________________
"To be stupid, selfish, and have a good health are the three requirements for happiness, though if stupidity is lacking, all is lost."
-Gustave Flaubert


lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 76
Gender: Male
Posts: 9,795
Location: Somerset UK

13 Nov 2007, 8:58 pm

lonelyLady wrote:
a dumb question: what does '->' do? I took a c++ programming course but I've never seen this one. Thanks!

"a->b" is an alternative for "(*a).b"
where "a" is a pointer to an instance of a structure with a member named "b".
In the same vein as "a[b]" is equivalent to "(*(a+b)))".


_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer


Last edited by lau on 14 Nov 2007, 4:58 pm, edited 1 time in total.

MysteryFan3
Veteran
Veteran

User avatar

Joined: 8 Jun 2007
Age: 68
Gender: Male
Posts: 1,156
Location: Indiana

13 Nov 2007, 10:35 pm

For a last-minute jam session, that's pretty good code. I've seen really old unreformatted COBOL code that would curl anyone's hair.

I wrote a program in Unisys BPL once to try out some stuff. I didn't bother with formatting since it wasn't written for production. It was pretty ugly. It became a utility program so I ended up having to reformat and document it. I was more careful after that. :D


_________________
To eliminate poverty, you have to eliminate at least three things: time, the bell curve and the Pauli Exclusion Principle. Have fun.