数据结构问题没有语法错误就是搞啦好久哪位大神帮帮忙

liuzhiqi 发布于 2012/10/24 22:48
阅读 214
收藏 0

/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{   int    coef, exp;
    struct node  *next;
} NODE;

void multiplication( NODE *, NODE * , NODE * );
void input( NODE * );
void output( NODE * );

void input( NODE * head )
{   int flag, sign, sum, x;
 char c;

    NODE * p = head;

 while ( (c=getchar()) !='\n' )
 {
  if ( c == '<' )
     {    sum = 0;
          sign = 1;
          flag = 1;
        }
  else if ( c =='-' )
             sign = -1;
  else if( c >='0'&& c <='9' )
  {    sum = sum*10 + c - '0';
        }
  else if ( c == ',' )
        {    if ( flag == 1 )
             {    x = sign * sum;
                  sum = 0;
                  flag = 2;
    sign = 1;
             }
        }
  else if ( c == '>' )
        {    p->next = ( NODE * ) malloc( sizeof(NODE) );
             p->next->coef = x;
             p->next->exp  = sign * sum;
             p = p->next;
             p->next = NULL;
             flag = 0;
        }
    }
}

void output( NODE * head )
{
    while ( head->next != NULL )
    {   head = head->next;
        printf("<%d,%d>,", head->coef, head->exp );
    }
    printf("\n");
}
void multiplication( NODE *h1, NODE *h2 , NODE *h3 )
{
  NODE *p,*q,*r, *r1,*s;
  int Max;

  p = h1->next;
  s = NULL;
  r1 = NULL;
 
  
  while(p ->next != NULL){
  q = h2 -> next;
  Max = p->exp;
 while(q ->next != NULL)
 {
 r->coef = p->coef * q->coef;
 r->exp = p->exp + q->exp;
    r = r->next;
 if(r->exp > Max )    {
     Max = r->exp;
 }
 }
 if(p->next == NULL&&q->next == NULL)
  { free(p);
             free(q);
    }
  if(r->coef == 0)
  {
            free(r);
   break;
   }
  for(int i = Max; i >= 0 ; i++){
        s->next = (NODE*)malloc(sizeof(NODE));
  s->coef = r->coef;
  s->exp = r->exp;
  r = r->next;
   }
  while( s)
  {

  NODE *p2 , *q2, *r1 = h3;
  p2 = r1 -> next;
  q2 = s;
  r1->next = NULL;
  while (p2 != NULL)
  {
   q2 = p2->next;
   p2->next = r1->next;
   r1-> next = p2;
   p2 = q2;
  }
  free(s);
  }
  }
}


int main()
{   NODE * head1, * head2, * head3;

    head1 = ( NODE * ) malloc( sizeof(NODE) );
    input( head1 );

    head2 = ( NODE * ) malloc( sizeof(NODE) );
    input( head2 );

    head3 = ( NODE * ) malloc( sizeof(NODE) );
    head3->next = NULL;
    multiplication( head1, head2, head3 );

 output( head3 );

 return 0;
}

/* PRESET CODE END - NEVER TOUCH CODE ABOVE */

加载中
0
优游幻世
优游幻世
是要实现长整数的乘法?搞得太复杂了吧。。。
l
liuzhiqi
是的,就是刚开始学完c就用数据结构的c版编啦,所以编的怎么复杂,不过就是不能出结果烦啊
0
水牛叔叔
水牛叔叔
注释也不给点
l
liuzhiqi
那该怎么搞啊
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部