2

请阅读下面的评论

这是具体的错误:

  • 错误 2 错误 LNK2019:函数 _main 中引用的未解析的外部符号“void __cdecl totalPolynomial(class LinkedList *,class LinkedList *,class LinkedList *)”(?totalPolynomial@@YAXPAV?$LinkedList@VTerm@@@@00@Z)

  • 警告 1 警告 C4805:“==”:操作 38 中“无符号整数”类型和“布尔”类型的不安全混合

我认为问题出在 functions.cpp/.h 中的 sumPolynomial 中,特别是它的声明/实现。

头文件.h

#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;

#include "linkedList.h"
#include "term.h"

void loadPolynomial(string expression, LinkedList<Term> *theList);
void totalPolynomial(LinkedList<Term> *x, LinkedList<Term> *y, LinkedList<Term> *sum);

函数.cpp

#include "header.h"



void loadPolynomial(string expression, LinkedList<Term> *theList)
{
    Term tempPolynomial;

    string varDelimiter = "x";
    string posDelimiter = "+";
    string negDelimiter = "-";
    string token = "";

    double coefficient;
    double exponent;

    /*bool isNeg;*/
    bool removedCoefficient = false;

    while(expression.length() > 0)
    {
        /*isNeg = false;*/
        coefficient = 1.0;
        exponent = 1.0;
        removedCoefficient = false;

        if(expression.substr(0, 1) == "+")
        {
            expression.erase(0, 1);
        }

        /*else if(expression.substr(0, 1) == "-")
        {
            isNeg = true;
            expression.erase(0, 1);
        }*/

        if( expression.length() > 1 && expression.find(varDelimiter) == true)
        {
            //Get the coefficient
            token = expression.substr(0, expression.find(varDelimiter));
            //Remove the coefficient and variable from the string leaving only the exponent
            expression.erase(0, expression.find(varDelimiter) + varDelimiter.length());
            //Convert and put token's coeficient into a double
            coefficient = atof(token.c_str());

            /*if(isNeg == true)
            {
                coefficient = coefficient * -1;
            }*/

            //Put the coefficient value into the tempPolynomial
            tempPolynomial.setCoefficient(coefficient);
            removedCoefficient = true;
        }

        if(expression.find(posDelimiter) > 0 && expression.length() > 0 && removedCoefficient == true)
        {
            //Get the exponent
            token = expression.substr(0, expression.find(posDelimiter));
            //Remove the exponent but leave the + 
            expression.erase(0, expression.find(posDelimiter) + posDelimiter.length());
            //Convert and put token's coeficient into a double
            exponent = atof(token.c_str());

            //Put the exponent value into the tempPolynomial
            tempPolynomial.setExponent(exponent);
        }

        if(removedCoefficient == false)
        {
            //Get the trailing number
            token = expression.substr(0);
            coefficient = atof(token.c_str());
            //Set it as the coefficient
            tempPolynomial.setCoefficient(coefficient);
            //Set the exponent as 0 so we know it doesn't include a variable
            tempPolynomial.setExponent(0.0);
            //Remove it from the string
            expression.erase(0, token.length());
        }

        //Intert the first term into the linked list
        theList->insert(tempPolynomial);
    }
}



void sumPolynomial(LinkedList<Term> *x, LinkedList<Term> *y, LinkedList<Term> *sum)
{
    LinkedList<Term> *tempNodeX;
    LinkedList<Term> *tempNodeY;
    Term tempTermX;
    Term tempTermY;
    Term newTerm;

    tempNodeX = x->getNext();
    tempNodeY = y->getNext();


    while(tempNodeX != NULL)
    {
        tempTermX = tempNodeX->getData();

        while(tempNodeY !=NULL)
        {
            tempTermY = tempNodeY->getData();

            if(tempTermX.getExponent() == tempTermY.getExponent())
            {
                newTerm.setCoefficient(tempTermX.getCoefficient() + tempTermY.getCoefficient());
                newTerm.setExponent(tempTermY.getExponent());

                sum->insert(newTerm);
            }

            tempNodeX = tempNodeY->getNext();
        }

        tempNodeX = tempNodeX->getNext();
    }
}

术语.h

#ifndef TERM_H
#define TERM_H

#include<iostream>
using namespace std;

class Term
{
private:
    double mCoefficient;
    double mExponent; 

public:
    Term();
    //Term(double coefficient);
    //Term(double exponent);
    Term(double coefficient, double exponent);
    ~Term();

    double getCoefficient();
    double getExponent();
    void setCoefficient(double coefficient);
    void setExponent(double exponent);

    friend ostream& operator<<(ostream &output, Term object);

    bool operator==(Term right);
    //friend bool operator==(Term left, Term right);
    bool operator!=(Term right);
    //friend bool operator!=(Term left, Term right);
    bool operator>(Term right);
    //friend bool operator>(Term left, Term right);
    bool operator<(Term right);
    //friend bool operator<(Term left, Term right);
};

#endif

术语.cpp

#include "term.h"

Term::Term()
{
    mCoefficient = 1;
    mExponent = 1;
}


//
//Term::Term(double coefficient)
//{
//  mCoefficient = coefficient;
//  mExponent = 1.0;
//}
//
//
//Term::Term(double exponent)
//{
//  mCoefficient = 1.0;
//  mExponent = exponent;
//}



Term::Term(double coefficient, double exponent)
{
    mCoefficient = coefficient;
    mExponent = exponent;
}



Term::~Term(){}



double Term::getCoefficient()
{
    return mCoefficient;
}



double Term::getExponent()
{
    return mExponent;
}



void Term::setCoefficient(double coefficient)
{
    mCoefficient = coefficient;
}



void Term::setExponent(double exponent)
{
    mExponent = exponent;
}



ostream& operator<<(ostream &output, Term object)
{
    if(object.mExponent > 0)
    {
        output << object.mCoefficient << "x" << object.mExponent;
    }

    else
    {
        output << object.mCoefficient;
    }
    return output;
}


bool Term::operator==(Term right)
{
    if(mExponent == right.mExponent && mCoefficient == right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }
}



bool Term::operator!=(Term right)
{
    if(mExponent != right.mExponent)
    {
        return true;
    }

    else if(mCoefficient != right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }
}


bool Term::operator>(Term right)
{
    if(mExponent > right.mExponent)
    {
        return true;
    }

    else if(mExponent == right.mExponent && mCoefficient > right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }

}



bool Term::operator<(Term right)
{
    if(mExponent < right.mExponent)
    {
        return true;
    }

    else if(mExponent == right.mExponent && mCoefficient < right.mCoefficient)
    {
        return true;
    }

    else
    {
        return false;
    }
}

链接列表.h

#ifndef LINKED_LIST_H
#define LINKED_LIST_H

#include <iostream>
#include <fstream>
using namespace std;

template <class T>
class LinkedList
{
private:
    T mData;
    LinkedList<T> *mNext;

public:
    LinkedList();
    LinkedList(T data);
    ~LinkedList();

    T getData();
    LinkedList<T> *getNext();

    void setData(T data);

    void display();
    void insert(T data);
    bool isExist(T data);
    void remove(T data);

    friend ostream& operator<<(ostream &output, LinkedList<T> object);

    bool operator==(T right);
    friend bool operator==(T left, LinkedList<T> right);

    bool operator!=(T right);
    friend bool operator!=(T left, LinkedList<T> right);

    bool operator>(T right);
    friend bool operator>(T left, LinkedList<T> right);

    bool operator<(T right);
    friend bool operator<(T left, LinkedList<T> right);
};



template <class T>
LinkedList<T>::LinkedList()
{
    mNext = NULL;
    mData = T();
}



template <class T>
LinkedList<T>::LinkedList(T data)
{
    mNext = NULL;
    mData = data;
}



template <class T>
LinkedList<T>::~LinkedList()
{
    LinkedList<T> *tempNode;

    tempNode = mNext;

    while(tempNode != NULL)
    {
        mNext = tempNode->mNext;
        tempNode->mNext = NULL;

        delete tempNode;

        tempNode = mNext;
    }
}



template <class T>
T LinkedList<T>::getData()
{
    return mData;
}



template <class T>
LinkedList<T> *LinkedList<T>::getNext()
{
    return mNext;
}



template <class T>
void LinkedList<T>::setData(T data)
{
    mData = data;
}



template <class T>
void LinkedList<T>::display()
{
    LinkedList<T> *tempNode;

    tempNode = mNext;

    while(tempNode != NULL)
    {
        cout << tempNode->mData << endl;

        tempNode = tempNode->mNext;
    }
}



template <class T>
void LinkedList<T>::insert(T data)
{
    LinkedList<T> *previousNode;
    LinkedList<T> *tempNode;
    LinkedList<T> *newNode;

    newNode = new LinkedList(data);

    if(mNext == NULL)
    {
        mNext = newNode;
    }

    else
    {
        previousNode = mNext;
        tempNode = mNext;

        while(tempNode != NULL && tempNode->mData > data)
        {
            previousNode = tempNode;
            tempNode = tempNode->mNext;
        }

        if(tempNode == mNext)
        {
            newNode->mNext = mNext;
            mNext = newNode;
        }

        else
        {
            previousNode->mNext = newNode;
            newNode->mNext = tempNode;
        }
    }
}



template <class T>
bool LinkedList<T>::isExist(T data)
{
    LinkedList<T> *tempNode;
    bool exist = false;

    tempNode = mNext;

    while(tempNode != NULL)
    {
        if(tempNode->mData == data)
        {
            exist = true;

            break;
        }

        tempNode = tempNode->mNext;
    }

    return exist;
}



template <class T>
void LinkedList<T>::remove(T data)
{
    LinkedList<T> *tempNode;
    LinkedList<T> *previousNode;

    if(isExist(data) == false)
    {
        return;
    }

    tempNode = mNext;
    previousNode = mNext;

    while(tempNode->mData != data)
    {
        previousNode = tempNode;
        tempNode = tempNode->mNext;
    }

    if(tempNode == mNext)
    {
        mNext = tempNode->mNext;
        tempNode->mNext = NULL;
    }

    else
    {
        if(tempNode->mNext == NULL)
        {
            previousNode->mNext = NULL;
        }

        else
        {
            previousNode->mNext = tempNode->mNext;
            tempNode->mNext = NULL;
        }
    }

    delete tempNode;
}



template <class T>
ostream& operator<<(ostream &output, LinkedList<T> object)
{
    output << object.mData;

    return output;
}



template <class T>
bool LinkedList<T>::operator==(T right)
{
    return mData == right;
}



template <class T>
bool operator==(T left, LinkedList<T> right)
{
    return left == right.mData;
}



template <class T>
bool LinkedList<T>::operator!=(T right)
{
    return mData != right;
}



template <class T>
bool operator!=(T left, LinkedList<T> right)
{
    return left != right.mData;
}



template <class T>
bool LinkedList<T>::operator>(T right)
{
    return mData > right;
}



template <class T>
bool operator>(T left, LinkedList<T> right)
{
    return left > right.mData;
}



template <class T>
bool LinkedList<T>::operator<(T right)
{
    return mData < right;
}



template <class T>
bool operator<(T left, LinkedList<T> right)
{
    return left < right.mData;
}

#endif
4

1 回答 1

0

这是现在可以工作的修改后的循环:

    while(tempNodeX != NULL)
    {
        tempTermX = tempNodeX->getData();

        while(tempNodeY != NULL)
        {
            tempTermY = tempNodeY->getData();

            if(tempTermX.getExponent() == tempTermY.getExponent())
            {
                newTerm.setCoefficient(tempTermX.getCoefficient() + tempTermY.getCoefficient());
                newTerm.setExponent(tempTermY.getExponent());

                //cout << tempTermX << " " << tempTermY << " " << newTerm << endl;
                sum->insert(newTerm);

                tempNodeY = y->getNext();
                tempNodeY->remove(tempTermY);
                break;
            }

            tempNodeY = tempNodeY->getNext();
        }

        tempNodeX = tempNodeX->getNext();
        tempNodeY = y->getNext();
    }
于 2013-09-16T07:08:44.850 回答