稀疏运算器
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LEN sizeof(Poly)
typedef struct term{
float xs;
int zs;
struct term * next;
} Poly, * Link;
int LocateElem ( Link p, Link s, Link & q) ;
void CreatePolyn ( Link & p, int m) ;
void PrintPolyn ( Link p) ;
int cmp ( Link a, Link b) ;
Link AddPolyn ( Link pa, Link pb) ;
Link SubPolyn ( Link pa, Link pb) ;
Link Reverse ( Link p) ;
int main ( )
{
Link P1, P2, P3;
int L1, L2;
char ch1;
printf ( "输入第一个多项式的项数:" ) ;
scanf ( "%d" , & L1) ;
CreatePolyn ( P1, L1) ;
printf ( "第一个多项式为:" ) ;
printf ( "P1(X)=" ) ;
PrintPolyn ( P1) ;
printf ( "输入第二个多项式的项数:" ) ;
scanf ( "%d" , & L2) ;
CreatePolyn ( P2, L2) ;
printf ( "第二个多项式为:" ) ;
printf ( "P2(X)=" ) ;
PrintPolyn ( P2) ;
printf ( "请输入要选择的运算(+ , -): " ) ;
getchar ( ) ;
scanf ( "%c" , & ch1) ;
getchar ( ) ;
switch ( ch1)
{
case '+' :
{
printf ( "P1(X)+P2(X)=" ) ;
P3= AddPolyn ( P1, P2) ;
PrintPolyn ( P3) ;
} break ;
case '-' :
{
printf ( "P1(X)-P2(X)=" ) ;
P3= SubPolyn ( P1, P2) ;
PrintPolyn ( P3) ;
} break ;
}
return 0 ;
}
int LocateElem ( Link p, Link s, Link & q)
{
Link p1 = p-> next;
Link p2 = p;
while ( p1)
{
if ( s-> zs > p1-> zs)
{
p1 = p1-> next;
p2 = p2-> next;
}
else if ( s-> zs == p1-> zs)
{
q = p1;
return 1 ;
}
else
{
q = p2;
return 0 ;
}
}
if ( ! p1)
{
q = p2;
return 0 ;
}
}
void CreatePolyn ( Link & p, int m)
{
Link s, q;
int i;
p= ( Link) malloc ( LEN) ;
p-> next= NULL ;
for ( i= 0 ; i< m; i++ )
{
s= ( Link) malloc ( LEN) ;
printf ( "输入系数和指数(以空格隔开):" ) ;
scanf ( "%f %d" , & s-> xs, & s-> zs) ;
if ( ! LocateElem ( p, s, q) )
{
s-> next = q-> next;
q-> next = s;
}
else q-> xs+ = s-> xs;
}
}
void PrintPolyn ( Link p)
{
Link s;
s = p-> next;
while ( s)
{
printf ( " %.2f X^%d" , s-> xs, s-> zs) ;
s = s-> next;
if ( s!= NULL )
if ( s-> xs>= 0 )
printf ( " +" ) ;
}
printf ( "\n" ) ;
}
int cmp ( Link a, Link b)
{
if ( a-> zs< b-> zs) return - 1 ;
else if ( a-> zs == b-> zs) return 0 ;
else return 1 ;
}
Link AddPolyn ( Link pa, Link pb)
{
Link newp, p, q, s, pc;
float sum;
p = pa-> next;
q = pb-> next;
newp= ( Link) malloc ( LEN) ;
pc = newp;
while ( p&& q)
{
switch ( cmp ( p, q) )
{
case - 1 : / /
{
s = ( Link) malloc ( LEN) ;
s-> xs = p-> xs;
s-> zs = p-> zs;
pc-> next = s;
pc = s;
p = p-> next;
} break ;
case 0 : / / 若比较两项的指数相等,则将两项系数相加后得到的项放入头结点为newp的链表中 ,且p, q同时向后遍历
{
sum = p-> xs+ q-> xs;
if ( sum!= 0.0 )
{
s = ( Link) malloc ( LEN) ;
s-> xs = sum;
s-> zs = p-> zs;
pc-> next = s;
pc = s;
}
p = p-> next;
q = q-> next;
} break ;
case 1 : / / 若指数:q< p, 则将q所指结点链入头结点为newp的链表中,且q向后遍历
{
s = ( Link) malloc ( LEN) ;
s-> xs = q-> xs;
s-> zs = q-> zs;
pc-> next = s;
pc = s;
q = q-> next;
} break ;
}
}
pc-> next= p? p: q;
return newp;
}
Link SubPolyn ( Link pa, Link pb)
{
Link newp, p, q, s, pc;
float sum;
newp= ( Link) malloc ( LEN) ;
pc = newp;
p = pa-> next;
q = pb-> next;
while ( q)
{
q-> xs= 0 - q-> xs;
q= q-> next;
}
q= pb-> next;
while ( p&& q)
{
switch ( cmp ( p, q) )
{
case - 1 :
{
s = ( Link) malloc ( LEN) ;
s-> xs = p-> xs;
s-> zs = p-> zs;
pc-> next = s;
pc = s;
p = p-> next;
} break ;
case 0 :
{
sum = p-> xs- q-> xs;
if ( sum!= 0.0 )
{
s = ( Link) malloc ( LEN) ;
s-> xs = sum;
s-> zs = p-> zs;
pc-> next = s;
pc = s;
}
p = p-> next;
q = q-> next;
} break ;
case 1 :
{
s = ( Link) malloc ( LEN) ;
s-> xs= q-> xs;
s-> zs = q-> zs;
pc-> next = s;
pc = s;
q = q-> next;
} break ;
}
}
pc-> next= p? p: q;
return newp;
}
Link Reverse ( Link p)
{
Link head= p;
Link q1, q2;
q2= head-> next;
head-> next= NULL ;
while ( q2)
{
q1= q2;
q2= q2-> next;
q1-> next= head-> next;
head-> next= q1;
}
return head;
}