简体   繁体   English

使用链表C ++添加2个多项式表达式

[英]Adding 2 Polynomial Expressions Using Linked List C++

Below is my source code which is to generate 2 polynomials which are given by the user input. 下面是我的源代码,它将生成由用户输入指定的2个多项式。 Now, I'm stuck on how to add both polynomials and display the result. 现在,我停留在如何将两个多项式相加并显示结果上。 I have the idea how to evaluate which is we check the exponent in the list. 我有一个想法如何评估我们将检查列表中的指数。 If same, the coefficient will add. 如果相同,系数将相加。 If not, the current node will proceed to the next node and compare to the other node.Is that right? 如果不是,当前节点将前进到下一个节点并与另一个节点进行比较,对吗? Actually I'm quite confused about linked list. 其实我对链接列表很困惑。

 #include<iostream>
using namespace std;
class Node{
      public :
             int coef;
             int exp;
             Node *next;
      private:
      };

class List{
      public:
             List(){
                    head = NULL;
             }
             void insert(int x, int y){ //inserting node
                  Node *newNode = new Node;
                  newNode->coef = x;
                  newNode->exp = y;
                  newNode->next = NULL;

                  if(head==NULL){
                                 head = newNode;
                  }
                  else{
                       Node *currNode = head;
                       while(currNode->next!=0){
                             currNode = currNode->next;
                       }
                       currNode->next = newNode;
                  }
             }
             void display(){ //display the expression
                  Node *currNode = head;
                  cout<<"\n\n\t";
                  while(currNode->next!=0){
                        //cout<<"Coef: "<<currNode->coef<<"\t"<<"Expo: "<<currNode->exp<<endl;
                        cout<<currNode->coef<<"X^"<<currNode->exp<< "+";
                        currNode = currNode->next;
                  }
                  cout<<currNode->coef<<"X^"<<currNode->exp<<endl;
                  //cout<<"Coef: "<<currNode->coef<<"\t"<<"Expo: "<<currNode->exp<<endl;
             }
      private:
              Node *head;              
      };

int main(){

    List seq1,seq2;

    int x,y,a,b;
    cout<<"Enter your expression 1: "<<endl;

    while(cin>>x>>y,x!=0&&y!=-1){
            seq1.insert(x,y);
    }
    seq1.display();
    cout<<endl;
    cout<<"Enter your expression 2: "<<endl;

    while(cin>>a>>b,a!=0&&b!=-1){
            seq2.insert(a,b);
    }
    seq2.display();

    return 0;
}

As you guessed, you'll need to traverse each list and compare the exponent. 如您所料,您需要遍历每个列表并比较指数。 If the exponents match, you add the coefficients. 如果指数匹配,则将系数相加。 What you want to look at more carefully is the way you use your linked list. 您想要更仔细地研究的是链接列表的使用方式。 Currently, you are adding each new entry of the polynomial at the end regardless of the value of the exponent. 当前,无论指数的值如何,都将在末尾添加多项式的每个新条目。 Also, if the user enters the same exponent twice you add a second node. 同样,如果用户两次输入相同的指数,则添加第二个节点。

If you keep the expressions in the linked list ordered by exponent, it will make it much easier to add polynomials together and much more efficient too. 如果将表达式保留在按指数排序的链表中,将使多项式加在一起变得更加容易,效率也将大大提高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM