| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 341 人关注过本帖
标题:大家来找茬吧,很多错误,请教您,谢谢
只看楼主 加入收藏
changyilin
Rank: 2
等 级:论坛游民
帖 子:18
专家分:20
注 册:2009-9-4
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
大家来找茬吧,很多错误,请教您,谢谢
一个多项式加法问题,我先赋值,然后使用函数Padd求的和,
  1 #include<stdio.h>
  2 #define MAX_SIZE 100
  3
  4 void main()
  5 {
  6
  7 struct polynomail{
  8         float coef;/*值*/
  9         int expon;//指数
 10 };
 11
 12 int i = 0;
 13 struct polynomail *avail;
 14 int padd(int starta,int finisha,int startb,int finishb,struct polynomail *avail,struct polymail *terms);
 15
 16 struct polynomail terms[MAX_SIZE];
 17
 18 int starta = 0,finisha = 1;
 19 int startb = 2,finishb = 5;
 20 terms[starta] = {2,1000};
 21 terms[finisha] = {1,0};
 22 terms[startb] = {1,4};
 23 terms[startb + 1] = {10,3};
 24 terms[startb + 2] = {3,2};
 25 terms[startb + 3] = {1,0};
 26
 27 for(i=0;i<=startb;i++)
 28 {      
 29         printf("[%f,%d] ",terms[i].coef,terms[i].expon);
 30         printf("\n");
 31 }
 32
 33  avail = &terms[finishb+1];
 34  padd(starta,finisha,startb,finishb,avail,terms);
 35
36 }
 37
 38 int padd(int starta,int finisha,int startb,int finishb,struct polynomail *avail,struct polynomail *terms)
 39 {
 40         struct polynomail *print=avail;
 41         while( starta <= finishb && startb <= finishb && avail<=terms+MAX_SIZE)
 42         {
 43         switch (COMPARE(terms[starta].expon,terms[startb].expon))
 44         {       case 1 :
 45                         avail->coef=terms[starta].coef;
 46                         avail->expon=terms[starta].expon;
 47                         starta++;
 48                         break;
 49                 case 0 :
 50                         avail->coef=terms[starta].coef+terms[startb].coef;
 51                         avail->expom=terms[starta].expon;
 52                         starta++;
 53                         startb++;
 54                         break;;
 55                 case -1:
 56
 57                         avail->coef=terms[startb].coef;
 58                         avail->expon=terms[startb].expon;
 59                         startb++;
 60                         break;
 61         }
 62         avail++;
 63         }
 64         for (;print<avail;print++)
 65         {
 66                 printf("[%f,%d]",print->coef,print->expon);
 67                 printf("\n");
 68         }
 69 }
搜索更多相关主题的帖子: 多项式 
2010-04-22 10:57
z__change
Rank: 2
等 级:论坛游民
帖 子:7
专家分:13
注 册:2010-4-19
收藏
得分:10 
我刚好也写这个作业,给你看看吧!!!
#include<stdio.h>
#include<stdlib.h>
typedef struct ployno{
    float coef;//系数
    int exon;//指数
    struct ployno *next;
}ployno,*point;
point p,q,r;//全局变量定义
point initlist(point i);//函数申明初始化
void creat(point j);//函数申明构造
void add(point,point);//函数申明相加

int main()
{ point a,b,c;
  a=initlist(a);
  b=initlist(b);
  c=initlist(c);
  creat(a);

  creat(b);

  r=c;
  add(a,b);
  free(a);//释放头节点
  free(b);//释放头节点
  r=c->next;
while(r){
  printf("%f    %d\n",r->coef,r->exon);
  r=r->next;
 }
 free(c);
 r=NULL;
 return 0;
}

//构造一个空的线性表
point initlist(point i)
{ i=(point)malloc(sizeof(ployno));
   if(i){
     i->coef=0.0;
     i->exon=0;
     i->next=NULL;
   }
   else
       exit(0);
   return(i);
}
//在空表的基础上进行继续申请并且初始化一个多项式
void creat(point j)
{ p=j;q=NULL;
  int temp;
  while(1){
      q=(point)malloc(sizeof(ployno));
    if(q)
        p->next=q;
    else
        exit(0);
    p=p->next;
    printf("enter:");
    scanf("%f%d",&p->coef,&p->exon);
    printf("enter 0 break");
    scanf("%d",&temp);
    if(temp==0){
        p->next=NULL;
        q=NULL;
        break;
    }
  }
}
//比较指数得到三种不同的情况
int compar(point m,point n)
{ if(m->exon==n->exon)
     return 1;
  else if(m->exon>=n->exon)
     return 2;
  else
     return 3;
}
//重新构造一个链表存放和式
void recreat(float xs,point zs)
{  point e;
   e=(point)malloc(sizeof(ployno));
   if(e)
       r->next=e;
   else
       exit(0);
   r=e;
   r->coef=xs;
   r->exon=zs->exon;
}
//将两个多项式进行相加
void add(point k,point l)
{  p=k->next;
   q=l->next;
   while(p&&q){
   switch(compar(p,q)){//调用比较函数
     case 1:
         if(p->coef+q->coef)
          recreat(p->coef+q->coef,p);//调用recreat存放结果
         else
          ;
         q=q->next;
         p=p->next;
         break;
     case 2:
         recreat(q->coef,q);//调用recreat存放结果
         q=q->next;
         break;
     case 3:
         recreat(p->coef,p);//调用recreat存放结果
         p=p->next;
         break;
         }
   }
   if(p)
       while(p){
           recreat(p->coef,p);//调用recreat存放结果
           p=p->next;
       }
   else
       while(q){
           recreat(q->coef,q);//调用recreat存放结果
           q=q->next;
       }
   r->next=NULL;

}
2010-04-22 11:49
无忧草
Rank: 2
等 级:论坛游民
帖 子:12
专家分:14
注 册:2010-4-22
收藏
得分:10 
结构体定义错误。
2010-04-25 17:47
快速回复:大家来找茬吧,很多错误,请教您,谢谢
数据加载中...
 
   



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

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