| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2236 人关注过本帖
标题:求解程序问题一员多项式加法函数(c写的)
只看楼主 加入收藏
虚空行走
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-4-11
收藏
 问题点数:0 回复次数:0 
求解程序问题一员多项式加法函数(c写的)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>      //多项式链表结点类型定义
typedef struct{
    float coef;       //系数
    int   expn;       //指数
}DataType;
typedef struct node{
    DataType  data;
    struct node *next;
}ListNode,*LinkList;   //结点的位置查找
int q ;               //定义一个全局量
int LocateNode(LinkList L,DataType e)
{
    ListNode*p=L->next;
    q=0;              //纪录结点位置序号
    while(p && e.expn<p->data.expn)
    {  p=p->next;
       q++;
    }                        
    //printf("%f,%d\n",e.expn!=p->data.expn);
if(p==NULL||e.expn!=p->data.expn)
  return 0;
else
  return 1;
}                          
//有序链表结点的插入
LinkList InsertNode(LinkList L ,DataType e)
{
    ListNode *s,*p;
    int i=0;
    p=L;
    while(p->next &&i<q)
    { p=p->next;
      i++;
    }
    s=(ListNode*)malloc(sizeof(ListNode));
    s->data.coef=e.coef;
    s->data.expn=e.expn;
    s->next=p->next;
    p->next=s;
    return L;
}                          //多项式链表的建立
LinkList CreatPolyn(int n)
{
    LinkList pa;
    int i;
    DataType e;
    pa=(ListNode*)malloc(sizeof(ListNode));//生成链表头结点
    pa->next=NULL;
    for(i=1;i<=n;i++)
    {
        scanf("%f,%d",&e.coef,&e.expn);
        if(!LocateNode(pa,e))             //当前链表中不存在该指数项
            pa=InsertNode(pa,e);
    }
    return pa;
}                                         //多项式链表的输出
void PrintList(LinkList L)
{
    ListNode *p;
    p=L->next;
    while(p)
    {
        printf("%c %fx^%d",(p->data.coef>0 ? '+' : ' '),p->data.coef,p->data.expn);
        p=p->next;
    }
    printf("\n");
}                                          //多项式的相加
LinkList AddPolyn(LinkList La,LinkList Lb)
{  //两个有序链表La和Lb表示多项式相加
    ListNode *pa,*pb,*pc,*s;
    LinkList Lc;
    float sum;
    pa=La->next;                           //pa和pb分别指向两个链表的开始结点
    pb=Lb->next;                           //用La的头结点作为Lc的头结点
    Lc=pc=La;
    while(pa&&pb){
        if(pa->data.expn>pb->data.expn){
            pc->next=pa;pc=pa;pa=pa->next;
        }
        else if(pa->data.expn<pb->data.expn)
        { pc->next=pb;pc=pb;pb=pb->next;}
        else{
            sum=pa->data.coef+pb->data.coef;
            if(fabs(sum)>1e-6){//系数和不为零
                pa->data.coef=sum;
                pc->next=pa;pc=pa;pa=pa->next;
                s=pb;pb=pb->next;free(s);
            }
            else {
                s=pa;pa=pa->next;free(s);
                s=pb;pb=pb->next;free(s);
            }
        }
    }
    pc->next=pa ? pa : pb;                 //插入链表剩余部分
    free(Lb);                              //释放Lb的头结点
    return Lc;
}
//主控函数
void main()
{
    LinkList La,Lb,Lc;
    int n;
    printf("输入第一个多项式的项数: ");
    scanf("%d",&n);
    printf("输入第一个多项式的每一项的系数,指数: \n");
    La=CreatPolyn(La,n);
    printf("第一个多项式为: ");
    printList(La);
    printf("输入第二个多项式的项数: ");
    scanf("%d",&n);
    printf("输入第二个多项式的每一项的系数,指数: \n");
    Lb=CreatPolyn(Lb,n);
    printf("第二个多项式为: ");
    printList(Lb);
    Lc=AddPolyn(La,Lb,Lc);
    printf("\n相加后的为: ");
    printList(Lc);
}
程序警告错误,不能运行,显示结果如下:
C:\Users\Administrator\Desktop\c1.c(109) : warning C4047: 'function' : 'int ' differs in levels of indirection from 'struct node *'
C:\Users\Administrator\Desktop\c1.c(109) : warning C4024: 'CreatPolyn' : different types for formal and actual parameter 1
C:\Users\Administrator\Desktop\c1.c(109) : warning C4020: 'CreatPolyn' : too many actual parameters
C:\Users\Administrator\Desktop\c1.c(111) : warning C4013: 'printList' undefined; assuming extern returning int
C:\Users\Administrator\Desktop\c1.c(115) : warning C4047: 'function' : 'int ' differs in levels of indirection from 'struct node *'
C:\Users\Administrator\Desktop\c1.c(115) : warning C4024: 'CreatPolyn' : different types for formal and actual parameter 1
C:\Users\Administrator\Desktop\c1.c(115) : warning C4020: 'CreatPolyn' : too many actual parameters
C:\Users\Administrator\Desktop\c1.c(118) : warning C4020: 'AddPolyn' : too many actual parameters
C:\Users\Administrator\Desktop\c1.c(109) : warning C4700: local variable 'La' used without having been initialized
C:\Users\Administrator\Desktop\c1.c(115) : warning C4700: local variable 'Lb' used without having been initialized
C:\Users\Administrator\Desktop\c1.c(118) : warning C4700: local variable 'Lc' used without having been initialized

c1.obj - 0 error(s), 0 warning(s)
求解释错处,并修改程序以能运行,新手请谅解
搜索更多相关主题的帖子: include 多项式 
2015-04-11 01:11
快速回复:求解程序问题一员多项式加法函数(c写的)
数据加载中...
 
   



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

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