Thread: Polynomial program, Unknown Error.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Unhappy Polynomial program, Unknown Error.

    This program compiles correctly but When i need to input data, it crashes saying "string subscript out of range", I dont know what it means.

    I would really appreciate if someone can run the program and see if anything is missing.

    I wrote this program with my class mate who knows more about this than I do. unfortunately I cant get in touch with him.

    program is separated in 3 files. I really need to run some test with this thing. thank you.

    Class
    Code:
    #ifndef POLYNOMIAL_H
    #define POLYNOMIAL_H
    
    #include <iostream>
    #include <string>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    class Polynomial 
    {
    	friend ostream &operator << ( ostream &, const Polynomial & );
    	friend istream &operator >> ( istream &, Polynomial & );
    
    public:
    	Polynomial(); // default constructor
    	Polynomial( int );
    	Polynomial( string );
    	Polynomial( const char * );
    	Polynomial( const int *, int=0 ); // regular constructor
    	Polynomial( const Polynomial & ); // copy constructor
    	~Polynomial(); // destructor
    
    	void setPolynomial( int );
    	void setPolynomial( string );
    	void setDegree( int = 0, int = 0 );
    	void setDegree( const char *, int = 0 );
    	int getDegree( int = 0 );
            long long int eval( int = 0 );
    
    	Polynomial operator+( const Polynomial & ) const;
    	Polynomial operator+( const char * ) const;
    	Polynomial operator+( int ) const;
    
    	Polynomial operator-( const Polynomial & ) const;
    	Polynomial operator-( const char * ) const;
    	Polynomial operator-( int ) const;
    
    	Polynomial operator*( const Polynomial & ) const;
    	Polynomial operator*( const char * ) const;
    	Polynomial operator*( int ) const;
    
    private:
    	int *ptr;
    	int size;
    };
    
    #endif
    Definition
    Code:
    #include "Poly.h"
    
    #include <iomanip>
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    
    
    Polynomial::Polynomial() 
    {
    	setPolynomial(0);
    }
    
    Polynomial::Polynomial( int input ) 
    {
    	setPolynomial( input );
    }
    
    Polynomial::Polynomial( string temp ) 
    {
    	setPolynomial( temp );
    }
    
    Polynomial::Polynomial( const char * temp ) 
    {
    	string tempstring = temp;
    	setPolynomial( tempstring );
    }
    
    Polynomial::Polynomial( const int * coeff, int max ) 
    {
    	size = max;
    	ptr = new int[size];
    	for ( int i = 0; i < size; i++) 
    		setDegree(i, coeff[i]);
    }
    
    Polynomial::Polynomial( const Polynomial &polyToCopy ) 
    {
    	size = polyToCopy.size;
    	ptr = new int[size];
    	for ( int i = 0; i < size; i++)
    		setDegree( i, polyToCopy.ptr[i] );
    }
    
    Polynomial::~Polynomial() 
    {
    	delete [] ptr;
    }
    
    void Polynomial::setPolynomial(int input) 
    {
    	size = 1;
    	ptr = new int[size];
    	ptr[0] = input;
    }
    
    void Polynomial::setPolynomial( string temp ) 
    {
    	string polyonly;
    	char poly[100];
    	int pos = 0, j = 0;
    
    	for ( unsigned int i = 0; i <= temp.length(); i++) 
    	{
        	if ( ( isdigit( temp[i] ) ) || ( temp[i] == '+' ) 
    		|| ( temp[i] == '-' ) || ( temp[i] == 'x' ) 
    		|| ( temp[i] == 'X' ) || ( temp[i] == '^' ) 
    		|| ( i == temp.length() ) ) 
    		{
    			polyonly[j] = temp[i];
    			j++;
    		}
    	}
    
    	for ( int i = 0; i <= j; i++ ) 
    	{
    		if ( (i == 0) || ( polyonly[i] == '+' ) || ( polyonly[i] == '-') ) 
    			pos = 0;
    		if ( ( isdigit(polyonly[i] ) ) || ( polyonly[i] == '-' ) 
    		|| ( polyonly[i] == 'x' ) || ( polyonly[i] == 'X' ) 
    		|| ( polyonly[i] == '^') ) 
    		{
    			poly[pos] = polyonly[i];
    			pos++;
    		}
    
    		if ((i == j) || ((i<j) && ((polyonly[i+1] == '-') || (polyonly[i+1] == '+')))) 
    		{
    			poly[pos] = '\0';
    			setDegree(poly,pos);
    		}
    	}
    }
    
    void Polynomial::setDegree( int degree, int coeff ) 
    {
    	if (degree >= size) 
    	{
    		int oldsize = size;
    		Polynomial temp(*this);
    		size = degree+1;
    		delete [] ptr;
    		ptr = new int[size];
    
    		for ( int i = 0; i < size; i++ ) 
    		{
    			if ( i < oldsize )
    				ptr[i] = temp.ptr[i];
    			else if ( i >= oldsize )
    				ptr[i] = 0;
    		}
    	}
    
    	if ( degree >= 0 ) 
    		ptr[degree] = coeff;
    }
    
    void Polynomial::setDegree( const char *input, int max ) 
    {
    	int isNegative = 0;
    	int foundX = 0;
    	int coeff = 0;
    	int degree = 0;
    	int i;
    
    	if ( input[0] == '-' )
    		isNegative = 1;
    
    	for ( i = 0; i < max; i++ ) 
    	{
    		if ( ( input[i] != '+' ) && ( input[i] != '-' ) ) 
    		{
    			if ( ( input[i] == 'x' ) || ( input[i] == 'X' ) ) 
    			{
    				foundX = 1;
    				if ( coeff == 0 )
    					coeff = 1;
    
    				if ( i == max - 1 ) 
    					degree = 1;
    			}
    
    			if ( isdigit( input[i] ) ) 
    			{
    				if ( foundX == 0 ) 
    				{
    					coeff *= 10;
    					coeff += input[i] - '0';
    				} 
    				else 
    				{
    					degree *= 10;
    					degree += input[i] - '0';
    				}
    			}
    		}
    	}
    	if ( isNegative == 1 )	
    		coeff *= -1;	
    
    	setDegree( degree,coeff );
    }
    
    int Polynomial::getDegree( int degree ) 
    {
    	if ( size > degree )
    		return ptr[degree];
    	else
    		return 0;
    }
    
    long long int Polynomial::eval( int value ) 
    {
    	long long int total = 0;
    
    	for ( int i = 0; i < size; i++ )
    		total += ( long long int )ptr[i]*( ( long long int ) pow( ( float ) value,( float ) i ) );
    		return total;
    }
    
    Polynomial Polynomial::operator+( const Polynomial &right ) const 
    {
    	Polynomial temp;
    	int max_size, value;
    	max_size = ( size > right.size ) ? size : right.size;
    
    	for ( int i = 0; i < max_size; i++ ) 
    	{
    		value = 0;
    
    		if ( i < size )	
    			value += ptr[i];
    
    		if ( i < right.size)
    			value += right.ptr[i];
    
    		temp.setDegree(i,value);
    	}
    	return temp;
    }
    
    Polynomial Polynomial::operator+(const char *input) const 
    {
    	string stringinput = input;
    	Polynomial temp;
    	temp.setPolynomial( stringinput );
    	return *this + temp;
    }
    
    Polynomial Polynomial::operator+( int input ) const 
    {
    	return *this + Polynomial( input );
    }
    
    Polynomial Polynomial::operator-( const Polynomial &right ) const 
    {
    	Polynomial temp( right );
    	for ( int i = 0; i < temp.size; i++ )
            temp.ptr[i] *= -1;
    	return *this + temp;
    }
    
    Polynomial Polynomial::operator-( const char *input ) const 
    {
    	string stringinput = input;
    	Polynomial temp;
    	temp.setPolynomial( stringinput );
    	return *this - temp;
    }
    
    Polynomial Polynomial::operator-( int input ) const 
    {
     	return *this - Polynomial( input );
    }
    
    Polynomial Polynomial::operator*( const Polynomial &right ) const 
    {
    	Polynomial temp;
    	int i, j;
    
    	for ( i = 0; i < size; i++ )
            for ( j = 0; j < right.size; j++ )
    		temp.setDegree(i+j,temp.getDegree(i+j)+ptr[i]*right.ptr[j]);
    
    	return temp;
    }
    Polynomial Polynomial::operator*( const char *input ) const 
    {
    	string stringinput = input;
    	Polynomial temp;
    	temp.setPolynomial( stringinput );
    	return *this * temp;
    }
    Polynomial Polynomial::operator*( int input ) const 
    {
    	return *this * Polynomial(input);
    }
    
    ostream &operator<<( ostream &output, const Polynomial &right ) 
    {
    	int didOut = 0;
    	for ( int i = right.size - 1; i >= 0; i-- ) 
    	{
    		if ( right.ptr[i] != 0 ) 
    		{
    			if ( right.ptr[i] > 0 ) 
    			{
    				if ( i != right.size - 1 ) 
    					output << " + ";
    				else 
    				{
    					if ( i != right.size - 1) 
    						output << " - ";
    					else	
    						output << "-";
    				}
    				if ( ( fabs ((double)right.ptr[i])!= 1.0 ) || ( i == 0 ) ) 
    				{
    					output << fabs((double)right.ptr[i] );
    					didOut = 1;
    				}
    			}
    		if ( i > 1 ) 
    			output << "x^" << i;
    		else if ( i == 1 )
    			output << "x";
    		}
    	}
    
    	if ( didOut == 0)
    		output << 0;
    	return output;
    }
    
    istream &operator>>(istream &input, Polynomial &right ) 
    {
    	string temp;
    	getline( input, temp );
    	right.setPolynomial( temp );
    	return input;
    }
    test program
    Code:
    #include "Poly.h"
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    using namespace std;
    
    int main() 
    {
    	const int f_size = 4;
    	int f_arr[f_size] = { 1, 1, 1, 1 };
    	Polynomial f( f_arr,f_size );
    
    	Polynomial g;
    	cout << "Enter a polynomial (like:x^15+x^3-x-1) for 'g' (expecting x-1): ";
    	cin >> g;
    
    	Polynomial F;
    	F.setDegree( 0, 1 );
    	F.setDegree( 10, 1 );
    
    	Polynomial G( F );
    	G.setDegree( 10, -1 );
    	G.setDegree( 15, 1 );
    
    	cout << "Values of variables:" << endl;
    	cout << "f = " << f << endl;
    	cout << "g = " << g << endl;
    	cout << "F = " << F << endl;
    	cout << "G = " << G << endl;
    	cout << endl;
    
    	cout << "Evaluating f & F:" << endl;
    	cout << " f( 0 ) = " << f.eval( 0 ) << endl;
    	cout << " f( 1 ) = " << f.eval( 1 ) << endl;
    	cout << "f( -2 ) = " << f.eval( -2 ) << endl;
    	cout << " F( 0 ) = " << F.eval( 0 ) << endl;
    	cout << " F( 1 ) = " << F.eval( 1 ) << endl;
    	cout << "F( -2 ) = " << F.eval( -2 ) << endl;
    	cout << endl;
    
    	cout << "Testing overloaded operations:" << endl;
    	cout << "       f + g: (" << f << ") + (" << g << ")" << endl;
    	cout << "            = " << f + g << endl << endl;
    	cout << "     f + 288: (" << f << ") + ( 288 ) " << endl;
    	cout << "            = " << f + 288 << endl << endl;
    	cout << "  f + ( 3x+1 ): ( " << f << " ) + ( 3x + 1 )" << endl;
    	cout << "            = " << f + "3x + 1" << endl << endl;
    	cout << "       f - g: (" << f << ") - (" << g << ")" << endl;
    	cout << "            = " << f - g << endl << endl;
    	cout << "       f * g: (" << f << ") * (" << g << ")" << endl;
    	cout << "            = " << f * g << endl << endl;
    	cout << "       F + G: (" << F << ") + (" << G << ")" << endl;
    	cout << "            = " << F + G << endl << endl;
    	cout << "       F - G: (" << F << ") - (" << G << ")" << endl;
    	cout << "            = " << F - G << endl << endl;
    	cout << "       F * G: (" << F << ") * (" << G << ")" << endl;
    	cout << "            = " << F * G << endl << endl;
    	cout << endl;
    
    	cout << endl;
    	system( "PAUSE" );
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    i <= temp.length();
    So don't walk off the end of your string if you don't want "you walked off the end of your string" error messages.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    yea i dont get it...lol

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know how much clearer I can be. You are always and forever trying to process one more character than is present in your strings. Don't do that.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If the length of temp is N, then valid indexes are 0...N-1.
    You are trying to iterate through 0...N.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'll break it down.
    You appear to understand how arrays are zero-based because you got these loops right:
    Code:
    	for ( int i = 0; i < size; i++)
    		for ( int i = 0; i < size; i++ ) 
    	for ( i = 0; i < max; i++ ) 
    	for ( int i = 0; i < max_size; i++ ) 
    	for ( i = 0; i < size; i++ )
            for ( j = 0; j < right.size; j++ )
    But you got this loop wrong, and were just as perplexed after the problem part was pointed out:
    Code:
    	for ( unsigned int i = 0; i <= temp.length(); i++)
    One could easily conclude that you did not write the whole program yourself.
    Do you or do you not understand how arrays are indexed?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with Horner's polynomial c program
    By swebdev in forum C Programming
    Replies: 6
    Last Post: 05-24-2011, 12:13 PM
  2. Is it just me, or is the polynomial program popular?
    By CaptainMorgan in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-05-2006, 08:10 PM
  3. Newbie - cubic polynomial program - help!
    By jaffa in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 05:52 AM
  4. Cubic polynomial root solver program...
    By hayai32 in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 07:58 AM
  5. unknown error
    By pktcperlc++java in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2004, 06:08 PM