数据结构课程设计--polynomial

本文介绍了一种使用链表实现一元多项式的加法和减法的方法。通过定义链表节点结构并实现初始化、复制、输出、加法及减法等关键函数,实现了用户输入多项式并进行运算的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gif ExpandedBlockStart.gif Code
  1 #include <iostream.h>
  2 typedef struct lnode
  3 {
  4     float coef, exp;
  5     struct lnode *next;
  6 }Lnode, *Linklist;
  7 //初始化,使多项式按升幂排列
  8 void Initialization(Linklist &L)
  9 {
 10     int flag;
 11     L = (Linklist)malloc(sizeof(Lnode));
 12     L->next = NULL;
 13     Linklist p, r;
 14     cout << "请依次输入各项的系数和指数" << endl;
 15     p = (Linklist)malloc(sizeof(Lnode));
 16     while (cin >> p->coef >> p->exp && (p->coef != 0 || p->exp != 0))
 17     {
 18         if (p->coef == 0)
 19             continue;
 20         if (L->next == NULL)                //第一项
 21         {
 22             flag = 1;
 23             L->next = p;
 24             p->next = NULL;
 25         }
 26         else
 27         {
 28             for (r = L,flag = 0; r->next != NULL;r = r->next)        //查找
 29             {
 30                 if (r->next->exp == p->exp)                            //如果插入的数据与前面的数据的指数项相同
 31                 {
 32                     flag = 1;
 33                     r->next->coef += p->coef;
 34                     free(p);
 35                     break;
 36                 }
 37                 else if (r->next->exp < p->exp)                        //插入的元素大于多项式中的指数项
 38                     continue;
 39                 else                                                //小于时插入
 40                 {
 41                     flag = 1;
 42                     p->next = r->next;
 43                     r->next = p;
 44                     break;
 45                 }
 46 
 47             }
 48         }
 49         if (flag == 0)                                                //如果它比多项式中任何数据的指数项都大,则插到最后
 50         {
 51             p->next = r->next;
 52             r->next = p;
 53         }
 54         p = (Linklist)malloc(sizeof(Lnode));
 55     }
 56 }
 57 Linklist copy(Linklist a)                                            //复制数据,保护原数据
 58 {
 59     Linklist p = (Linklist)malloc(sizeof(Lnode));
 60     Linklist r, q = a->next, t;
 61     p->coef = a->coef;
 62     p->exp = a->exp;
 63     p->next = NULL;
 64     t = p;
 65     while(q != NULL)
 66     {
 67         r = (Linklist)malloc(sizeof(Lnode));
 68         r->coef = q->coef;
 69         r->exp = q->exp;
 70         r->next = t->next;
 71         t->next = r;
 72         t = r;
 73         q = q->next;
 74     }
 75     return p;
 76 }
 77 void output(Linklist L)
 78 {
 79     if (L->next == NULL)
 80         cout << "";
 81     else
 82     {
 83         L = L->next;
 84         cout << L->coef << "*" << "x^" << L->exp;
 85         while(L->next != NULL)
 86         {
 87             L = L->next;
 88             if (L->coef >= 0)
 89                 cout << "+" << L->coef << "*" << "x^" << L->exp;
 90             else
 91                 cout << L->coef << "*" << "x^" <<L->exp;
 92         }
 93     }
 94 }
 95 
 96 Linklist add(Linklist a, Linklist b)
 97 {
 98     Linklist p, q, r, t;
 99     p = a->next;
100     q = b->next;
101     r = a;
102     if(a->next == NULL)
103         return b;
104     if(b->next == NULL)
105         return a;
106     while (q != NULL && p != NULL)
107     {
108         if (p->exp < q->exp)                    //小于时
109         {
110             r = r->next;
111             p = p->next;
112         }
113         else if (p->exp > q->exp)                //大于时插入
114         {
115             t = (Linklist)malloc(sizeof(Lnode));
116             t->coef = q->coef;
117             t->exp = q->exp;
118             r->next = t;
119             t->next = p;
120             r = t;
121             q = q->next;
122         }
123         else                                    //相同时相加
124         {
125             p->coef += q->coef;
126             if (p->coef == 0)
127             {
128                 r->next = p->next;
129             }
130             q = q->next;
131             r = p;
132             p = p->next;
133         }
134     }
135     if (q)                                        //把第二个多项式没有插入的部分,插入到第一个多项式的最后
136         r->next = q;
137     return a;
138 }
139 
140 Linklist sub(Linklist a, Linklist b)
141 {
142     Linklist c = b->next;
143     while (c != NULL)
144     {
145         c->coef = 0 - c->coef;
146         c = c->next;
147     }
148     return add(a, b);
149 }
150 
ContractedBlock.gif ExpandedBlockStart.gif
ContractedBlock.gif ExpandedBlockStart.gif Code
 1#include<iostream.h>
 2#include<stdlib.h> 
 3
 4#include "polynomial.h"
 5
 6int main()
 7ExpandedBlockStart.gifContractedBlock.gif{
 8    Linklist pa, pb;
 9    char y = 'y';
10    while (y == 'Y' || y == 'y')
11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
12        cout << "*****一元多项式加减法*****" << endl;
13        cout << "建立一元多项式A(x)[以0 0 为结束符]" << endl;
14        Initialization(pa);
15        cout << "A(x) = ";
16        output(pa);
17        cout << endl;
18        cout << "建立一元多项式B(x)[以0 0 为结束符]" << endl;
19        Initialization(pb);
20        cout << "B(x) = ";
21        output(pb);
22        cout << endl;
23        cout << "两式相加 = " << endl;
24        output(add(copy(pa), copy(pb)));
25        cout<< endl;
26        cout << "两式相减 = " << endl;
27        output(sub(copy(pa), copy(pb)));
28        cout<< endl;
29        cout << "are you continue? y or n?" << endl;
30        cin >> y;
31        if (y != 'y' && y != 'Y')
32            break;
33    }

34    return 0;
35}
Code
#include <iostream.h>
typedef 
struct lnode
{
    
float coef, exp;
    
struct lnode *next;
}Lnode, 
*Linklist;
//初始化,使多项式按升幂排列
void Initialization(Linklist &L)
{
    
int flag;
    L 
= (Linklist)malloc(sizeof(Lnode));
    L
->next = NULL;
    Linklist p, r;
    cout 
<< "请依次输入各项的系数和指数" << endl;
    p 
= (Linklist)malloc(sizeof(Lnode));
    
while (cin >> p->coef >> p->exp && (p->coef != 0 || p->exp != 0))
    {
        
if (p->coef == 0)
            
continue;
        
if (L->next == NULL)                //第一项
        {
            flag 
= 1;
            L
->next = p;
            p
->next = NULL;
        }
        
else
        {
            
for (r = L,flag = 0; r->next != NULL;r = r->next)        //查找
            {
                
if (r->next->exp == p->exp)                            //如果插入的数据与前面的数据的指数项相同
                {
                    flag 
= 1;
                    r
->next->coef += p->coef;
                    free(p);
                    
break;
                }
                
else if (r->next->exp < p->exp)                        //插入的元素大于多项式中的指数项
                    continue;
                
else                                                //小于时插入
                {
                    flag 
= 1;
                    p
->next = r->next;
                    r
->next = p;
                    
break;
                }

            }
        }
        
if (flag == 0)                                                //如果它比多项式中任何数据的指数项都大,则插到最后
        {
            p
->next = r->next;
            r
->next = p;
        }
        p 
= (Linklist)malloc(sizeof(Lnode));
    }
}
Linklist copy(Linklist a)                                            
//复制数据,保护原数据
{
    Linklist p 
= (Linklist)malloc(sizeof(Lnode));
    Linklist r, q 
= a->next, t;
    p
->coef = a->coef;
    p
->exp = a->exp;
    p
->next = NULL;
    t 
= p;
    
while(q != NULL)
    {
        r 
= (Linklist)malloc(sizeof(Lnode));
        r
->coef = q->coef;
        r
->exp = q->exp;
        r
->next = t->next;
        t
->next = r;
        t 
= r;
        q 
= q->next;
    }
    
return p;
}
void output(Linklist L)
{
    
if (L->next == NULL)
        cout 
<< "";
    
else
    {
        L 
= L->next;
        cout 
<< L->coef << "*" << "x^" << L->exp;
        
while(L->next != NULL)
        {
            L 
= L->next;
            
if (L->coef >= 0)
                cout 
<< "+" << L->coef << "*" << "x^" << L->exp;
            
else
                cout 
<< L->coef << "*" << "x^" <<L->exp;
        }
    }
}

Linklist add(Linklist a, Linklist b)
{
    Linklist p, q, r, t;
    p 
= a->next;
    q 
= b->next;
    r 
= a;
    
if(a->next == NULL)
        
return b;
    
if(b->next == NULL)
        
return a;
    
while (q != NULL && p != NULL)
    {
        
if (p->exp < q->exp)                    //小于时
        {
            r 
= r->next;
            p 
= p->next;
        }
        
else if (p->exp > q->exp)                //大于时插入
        {
            t 
= (Linklist)malloc(sizeof(Lnode));
            t
->coef = q->coef;
            t
->exp = q->exp;
            r
->next = t;
            t
->next = p;
            r 
= t;
            q 
= q->next;
        }
        
else                                    //相同时相加
        {
            p
->coef += q->coef;
            
if (p->coef == 0)
            {
                r
->next = p->next;
            }
            q 
= q->next;
            r 
= p;
            p 
= p->next;
        }
    }
    
if (q)                                        //把第二个多项式没有插入的部分,插入到第一个多项式的最后
        r->next = q;
    
return a;
}

Linklist sub(Linklist a, Linklist b)
{
    Linklist c 
= b->next;
    
while (c != NULL)
    {
        c
->coef = 0 - c->coef;
        c 
= c->next;
    }
    
return add(a, b);
}
ContractedBlock.gif ExpandedBlockStart.gif Code
#include<iostream.h>
#include
<stdlib.h> 

#include 
"polynomial.h"

int main()
{
    Linklist pa, pb;
    
char y = 'y';
    
while (y == 'Y' || y == 'y')
    {
        cout 
<< "*****一元多项式加减法*****" << endl;
        cout 
<< "建立一元多项式A(x)[以0 0 为结束符]" << endl;
        Initialization(pa);
        cout 
<< "A(x) = ";
        output(pa);
        cout 
<< endl;
        cout 
<< "建立一元多项式B(x)[以0 0 为结束符]" << endl;
        Initialization(pb);
        cout 
<< "B(x) = ";
        output(pb);
        cout 
<< endl;
        cout 
<< "两式相加 = " << endl;
        output(add(copy(pa), copy(pb)));
        cout
<< endl;
        cout 
<< "两式相减 = " << endl;
        output(sub(copy(pa), copy(pb)));
        cout
<< endl;
        cout 
<< "are you continue? y or n?" << endl;
        cin 
>> y;
        
if (y != 'y' && y != 'Y')
            
break;
    }
    
return 0;
}

转载于:https://www.cnblogs.com/anderson0/archive/2009/09/05/1560710.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值