| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4501 人关注过本帖
标题:本人的一元多项式加减乘
只看楼主 加入收藏
旷野乡人
Rank: 1
等 级:新手上路
威 望:1
帖 子:111
专家分:0
注 册:2006-12-18
收藏
得分:0 
谢谢楼主的分享,昨天看了一下,写得不错,一个错误都没有
2006-12-19 10:04
dhcyx1988
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-10-14
收藏
得分:0 
哪位帮偶调下这个程序
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>

typedef struct node
{
    float coef;
    int expn;
    struct node * next;
} PolyNode;

PolyNode * Create_Poly(char ch)                      //输入多项式
{
    PolyNode * p, *s,*r;
    float x; int y;
    p=(PolyNode *)malloc(sizeof(PolyNode));
    p->next=NULL;
    printf("请输入一元多项式%c:(格式:系数 指数,指数递增,以0 0结束.)\n",ch);
    scanf("%f %d",&x,&y);
    while(x!=0)
    {
        s=(PolyNode *)malloc(sizeof(PolyNode));
        s->coef=x;
        s->expn=y;
        s->next=NULL;
        if(p->next==NULL)
        {
            p->next=s;
            r=s;
        }
        else
        {
            r->next=s;
            r=s;
        }
        scanf("%f %d",&x,&y);
    }
    return p;
}

PolyNode * Add_Poly(PolyNode * f,PolyNode * g)    //多项式相加
{
    PolyNode * fg;
    PolyNode *t,*q,*s,*r;
    float m;
    t=f->next;
    q=g->next;
    fg=r=(PolyNode*)malloc(sizeof(PolyNode));
    fg->next=NULL;
    while(t&&q)
    {
        if(t->expn==q->expn)                //指数相等时系数相加
        {
            m=t->coef+q->coef;
            if(m!=0)                        //系数为不0时加到结果中去
            {
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=m;
                s->expn=t->expn;
                s->next=NULL;                  
            }
            t=t->next;
            q=q->next;
        }
        else                                //指数小的加到结果中去再后移
            if(t->expn<q->expn)                
            {                
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=t->coef;
                s->expn=t->expn;
                s->next=NULL;
                t=t->next;
            }
            else
            {
                s=(PolyNode *)malloc(sizeof(PolyNode));
                s->coef=q->coef;
                s->expn=q->expn;
                s->next=NULL;
                q=q->next;
            }

        if(fg->next==NULL)
        {
            fg->next=s;
            r=s;
        }
        else
        {
            r->next=s;
            r=s;
        }
    }//while
    r->next=t?t:q;                            //把没加完的接上
    return fg;
}


void Out_Poly(PolyNode * f)                    //输入多项式
{
    PolyNode *t;
    t=f->next;
    if(!f->next){
        printf("0\n");  return;
    }
    while(t)
    {   if(t->coef>0&&f->next!=t) printf("+");
        if(t->expn==0)
            printf("%f",t->coef);
        else
            printf("%f*X^%d",t->coef,t->expn);
        t=t->next;
    }
   printf("\n");
}



PolyNode * Mul_Poly(PolyNode *f,PolyNode *g)//多项式相乘
{
    PolyNode *h;
    PolyNode *t,*q,*s,*r;
    h=(PolyNode *)malloc(sizeof(PolyNode));
    h->next=NULL;
    r=(PolyNode *)malloc(sizeof(PolyNode));
    r->next=NULL;
    for(t=f->next;t;t=t->next)            //相乘时把第一项多项式的每一项
    {                                //与第二个多项式中的每一项相乘
        for(q=g->next;q;q=q->next)        //用双重循环实现
        {
            s=(PolyNode *)malloc(sizeof(PolyNode));
            r->next=s;
            s->coef=q->coef*t->coef;
            s->expn=q->expn+t->expn;
            s->next=NULL;
            h=Add_Poly(r,h);            //把每项相乘结果加起来
        }

    }
    return h;
}

void Open()                            //用户选择界面
{
    printf("*****************************\n");
    printf("    一元多项式相加与相乘\n");
    printf("    中山大学 03信管 叶伟鑫\n");
    printf("*****************************\n");
    printf("请选择操作:\n");
    printf("0.退出\n");
    printf("1.两个一元多项式相加\n");
    printf("2.两个多项式相乘\n");
    printf("3.用户手册\n");
}


void Readme()
{
    printf("**********用户手册***********\n");
    printf("1.输入时只输入多项式的系数与指数,以0 0结束.\n");
    printf("2.请按指数升幂形式输入.\n");
    printf("3.例如输入 \"1 1 2 2 0 0\" 表示 \"1*X^1+2*X^2\"\n");
    printf("4.感谢你的使用!哈哈^_^\n");
}


void main()
{
    Open();
    PolyNode * f,* g,* fg;
    int i=-1;                                //设置标志
    while(i!=0)
    {
        scanf("%d",&i);
        getchar();
        switch(i)
        {    
        case 0:
            return;                        //退出
        case 1:  
            //clrscr();                        //清屏
            printf("你选择的操作是多项式相加:\n");
            f=Create_Poly('A');                //输入多项式A
            printf("A=");
            Out_Poly(f);
            g=Create_Poly('B');                //输入多项式B
            printf("B=");
            Out_Poly(g);
            printf("A+B=");
            fg=Add_Poly(f,g);
            Out_Poly(fg);
            i=-1;                            //复位标志
            //clrscr();                        //清屏
            Open();                        //回复用户选择界面
            break;
        case 2:
            //clrscr();                        //清屏
            printf("你选择的操作是多项式相乘:\n");
            f=Create_Poly('A');                //输入多项式A
            printf("A=");
            Out_Poly(f);
            g=Create_Poly('B');                //输入多项式B
            printf("B=");
            Out_Poly(g);
            printf("A*B=");
            fg=Mul_Poly(f,g);
            Out_Poly(fg);
            i=-1;                            //复位标志
            //clrscr();                        //清屏
            Open();                                //回复用户选择界面
            break;
        case 3:
            //clrscr();                            //清屏
            Readme();
            i=-1;                                //复位标志
            //clrscr();                            //清屏
            Open();                                //回复用户选择界面
            break;
        default:
            //clrscr();                            //清屏
            printf("输入有误!请重新选择操作!\n");//选择错误,返回选择界面
            Open();
        }
    }
2008-10-14 18:49
yingwufenghua
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-10-25
收藏
得分:0 
哎呦,都不错哦!
2008-10-25 01:02
快速回复:本人的一元多项式加减乘
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026162 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved