[데이터 구조] dengyuxin 의 procject 1.2

2782 단어 데이터 구조
#ifndef POLYON_H_INCLUDED
#define POLYON_H_INCLUDED
//polyon.h
#include 
using namespace std;
class node
{
    public:
    int pa;
    int times;
    node *next;
    node(int p = 0,int t = 0,node* n=NULL)
    {
        pa = p;
        times = t;
        next = n;
    }
};
class polynomial
{
    private:

    void swap(node *n1,node *n2)
    {
        int n,m;
        n=n1->pa;n1->pa=n2->pa;n2->pa=n;
        m=n2->times;n2->times=n1->times;n1->times=m;
    }
    public:
    node *head;
    node *curr;
    int size;
    polynomial()
    {
        head = curr =new node();
        size =0;
    }
    polynomial(polynomial &p1,polynomial &p2)
    {
        p1.curr = p1.head;
        while(p1.curr->next!=NULL)
           p1.curr=p1.curr->next;
        p1.curr->next = p2.head->next;
        delete p2.head;
        head = p1.head;
        curr = p1.head;
        size = p1.size+p2.size;

    }
    void insert(int p,int t)
    {
        curr =new node(p,t,head->next);
        head->next = curr;
        size++;
    }
    void sort()
    {
        for(int i =0;inext;curr->next!=NULL;curr=curr->next)
        {
            if(curr->times < curr->next->times)
                swap(curr,curr->next);
        }}
    }
    void merge()
    {
        node *tmp;
        for(curr =head->next;curr!=NULL;curr=curr->next)
        { //  cout<next!=NULL)
            {   //cout<next->times==curr->times)
                {
                    curr->pa += tmp->next->pa;
                    node *t=tmp->next;
                    tmp->next=tmp->next->next;
                    delete t;
                }
                else{
                    tmp=tmp->next;}
            }
        }
    }
    void print()
    {
         sort();
        merge();
       // cout<next;
        while(curr!=NULL)
        {
            if(curr->pa!=0&&curr->next!=NULL)
                cout<pa<times<next==NULL)
                cout<pa<times;
            curr=curr->next;
        }
        cout<

다항식 저장 에 사용 할 함 수 를 정의 한 것 이다.
//main function
//author: byshen
#include 
#include "polyon.h"
using namespace std;

int main()
{
    int a,b;
    char c;
    polynomial y1,y2;
    do
    {
        cout<>a>>b;
        y1.insert(a,b);
        cout<>c;
    } while(c!='n');
    cout<>a>>b;
        y2.insert(a,b);
        cout<>c;
    } while(c!='n');

    cout<

하루 더 버 텨 줘 서 고마워!
화 이 팅!

좋은 웹페이지 즐겨찾기