0

Look, I'm sorry I didn't go back and re-read your posts before preaching to the choir. All you have to say is "I understand ^ and ** and polys."

What you are failing to grasp is sprawled all over pages 1 through 4 of this thread. It is fine if you want to jump in late but if you are then told "you've missed something" then you have probably missed something.

The purpose of using a programming language is to deal with abstractions. There is nothing magical about it. The last three or four posts OP and I made dealt explicitly with the "special function" of turning adjacent like terms into a single term, for every term in the list. Notation has absolutely no effect on the operation.

I can just give the OP code. But I won't. I want him to write it himself. (He can do it just fine and he'll be happier with the result.) What he is struggling with is how to arrange his data to do the reduction. This requires abstract thought over what is happening to the terms of the polynomial.

What you are struggling with is already a solved point. (Check out the operator>> and the Polynomial class's data structure.)

Sorry if I upset you.

0

Its so easier said than done JRM, i understand where u coming from. But i was greatly aided with this assignment and was able to systematically approach it.

Thanks Duoas, i now know how to compare elements within a vector.

Am almost done with this, the only thing thats stopping me is that it won't let me edit my input overload as to enable it to recognize and input negative integers into the vector without giving me that " can't convert to this, its illegal!" error

Basically i tried multiplying the input by -1 after peeking for the "-" sign

0

Just these simple lines of code

if (stream.peek() == '-')
stream >> t.co*(-1);
stream >> ws;

But i get this error: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) Srry for the late reply, in and out of lectures

0

this doesn't give me an error but it doesn't work

if (stream.peek() == '-')
{
      stream >> t.co;
	if (t.co >= 1)
	{
		t.co * -1;
	}
}
0

That's a nice catch. (I thought I tested for that...)

I fixed it by using a temporary. I replaced the line

if (stream.peek() != 'x') stream >> t.co;

with

if (stream.peek() != 'x')
{
	int sign = t.co; // save the current sign (1 or -1)
	stream >> t.co;  // get the coefficent absolute value
	t.co *= sign;    // restore the sign
}

Hope this helps.

0

Omg! thats so brilliant! sigh... i wish i thought of it

Ok so it recognizes negative integers now but lets say we have the polynomial 1 = 4x^2+3x+4 and polynomial 2 = -3x+5 The sum ends up being 4x^2+0x^1+9 I tried solving this problem by declaring another iterator and having it go through the result and if it comes across a coefficient that is = 0, then erase that element. But once again, this doesn't go without an error. Here is what i have:

vector<term>::iterator p5Current = result.poly.begin();
	for(; p5Current != result.poly.end(); ++p5Current)
	{
		if(p5Current->co == 0)
		result.poly.erase(p5Current);

	}

Its the climax!

0

Do you get an error message from the compiler or a run time error, as indicated by unexpected output?

If it's a run time error, then it may be something to do with how you keep track of the polynomial internally or how you display it rather than how you erase a value from a vector. The code posted in #67 looks okay (unless co is a double, in which case there are concerns with comparing doubles using the == operator).

0

Here is the error am getting:

Debug Assertion Failed!
.....
Expression: ("this->_Has_container()",0)
....

Oh and co is an int

0

The problem is essentially this:

vector<term>::iterator p5Current = result.poly.begin();
result.poly.erase( p5Current );
p5Current++;  // <-- this throws an error

Once you erase an element, you cannot operate on it. Since *p5Current no longer exists, it makes no sense to increment to the next one...

You'll have to think a little bit about how to solve this. (If you feel comfortable with the <algorithm> template functions, you might want to take a look at remove_if. However, you can easily do it without using the algorithms remove_if() template function...)

Hope this helps.

0

Thanks for letting me know that algorithm exists. Here's my take on it..... though it keeps saying isZero is undeclared

Poly.h

bool isZero (int);

Poly.cpp

bool Polynomial::isZero (int i)
{
	return i == 0;
}

vector<term>::const_iterator p5Current; 
p5Current = remove_if(result.poly.begin(), result.poly.end(), isZero); // gives me error here

//create new vector from orginal using previous iterator
result2.poly.push_back(result.poly.begin(), p5Current); // apparently push_back function does not take 2 arguments so how do i resolve this?

return result2; //returns sum of the polynomial p1 and p2
0

I want to thank each and everyone of you for coming through and being nothing but helpful. Much respect to Duoas for being there with me from the beginning and teaching me a lot. I have no doubt in my mind that getting to this point would not have happened if it wasn't for each and every one of you. Am glad to have come across such a helpful and patient group of programming intellects and individuals.

Marking this as Solved.

1

So wait, did you get the "undeclared" error fixed (by typing isZero as) bool isZero( term t ) ?

Glad to have been of help.

Votes + Comments
Thanks a lot Duoas! Good looking out
0

Thnx for pointing that out. the code if a little bit more refined now but no it still declares isZero unidentified. Heres how everything is placed....oh and at this point i already turned in the assignment so. Well anyways here it is:

Poly.h

struct term
{
	int co;
	int expo;

	bool operator() (const term &first, const term &other_term) const;
	bool isZero (term);
};

Poly.cpp

bool term::isZero ( term t )
{
	return t.co == 0;
}

vector<term>::const_iterator p5Current; 
p5Current = remove_if(result.poly.begin(), result.poly.end(), isZero);

vector<term>::const_iterator p6Current = result.poly.begin();
		
//create new vector from orginal using previous iterator
for (; p6Current != p5Current; p6Current++)
{
	result2.poly.push_back(*p6Current);
}

return result2; //returns sum of the polynomial p1 and p2
0

That's because of the way the STL works. The Predicate object must either be a pointer to a static function or an object with an appropriate operator() overload.

Hence, you can do it one of three ways (well, two, but it looks like three):

#include <algorithm>
#include <iostream>
#include <vector>

struct fooey {
  int x;
  fooey(): x( 0 ) { }
  fooey( int _x ): x( _x ) { }

  // method 1: class operator overload
  bool operator () ( fooey x ) { return x.x == 0; }

  // method 2: static function (version 1)
  static bool pred( fooey x ) { return x.x == 1; }
  };

// method 2: static function (version 2)
bool pred( fooey x ) { return x.x == 2; }

int main() {
  using namespace std;

  // Fill our vector with 0 1 2 3 0 1 2 3 0 1 2 3 ...
  vector<fooey> xs;
  for (int i = 0; i < 20; i++) xs.push_back( fooey( i %4 ) );

  // Display the vector
  for (int i = 0; i < xs.size(); i++) cout << xs[i].x << ' ';
  cout << endl;

  // Remove all zeroes and display
  xs.erase(
    remove_if( xs.begin(), xs.end(), fooey() ),
    xs.end()
    );
  for (int i = 0; i < xs.size(); i++) cout << xs[i].x << ' ';
  cout << endl;

  // Remove all ones and display
  xs.erase(
    remove_if( xs.begin(), xs.end(), fooey::pred ),
    xs.end()
    );
  for (int i = 0; i < xs.size(); i++) cout << xs[i].x << ' ';
  cout << endl;

  // Remove all twos and display
  xs.erase(
    remove_if( xs.begin(), xs.end(), pred ),
    xs.end()
    );
  for (int i = 0; i < xs.size(); i++) cout << xs[i].x << ' ';
  cout << endl;

  return EXIT_SUCCESS;
  }

Hope this helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.