多项式计算器代码


#include "stdafx.h"
#include "stdlib.h"
typedef struct node
{
float con;
int exp;
struct node * next;
}listnode;
listnode * head;
listnode * p;
listnode * q;
listnode * r;
listnode * head1;
listnode * head2;
int head_num = 0;
listnode * point[2];
void input()
{
int j = 0;
int exp;
float con;
head = (listnode *) malloc (sizeof(listnode));
head ->next = NULL;
start: scanf("%f %d",&con,&exp);
if (exp == 0 && con == 0)
goto next;
p = (listnode *) malloc (sizeof (listnode));
j++;
p ->con = con;
p ->exp = exp;
p ->next =head ->next;
head ->next = p;
goto start;
next: head ->exp =j;
point [head_num] = head;
head_num ++;
}
int bubble_sort(listnode * myhead)
{
int i,j;
listnode * swap;
listnode * real_myhead;
real_myhead = myhead;
int num = myhead ->exp;
//myhead = myhead ->next;
if (num == 1 && num == 0 )
goto next;
for (i = 0; i < num-1; i ++)
{
myhead = real_myhead;
for (j = 0;j < num-1; j ++)
{
if ((myhead ->next ->exp ) < (myhead->next ->next->exp ))
{
swap = (listnode *)malloc (sizeof(listnode));
swap ->exp = myhead ->next ->exp;
swap ->con = myhead ->next ->con;
myhead ->next ->exp = (myhead->next ->next)->exp
myhead ->next ->con = (myhead->next ->next)->con
myhead ->next ->next ->exp = swap ->exp
myhead ->next ->next ->con = swap ->con
free (swap);
}
myhead =myhead ->next;
}
}
myhead = real_myhead;
if (myhead ->exp == 0)
return 0;
p = myhead ->next;
for (;p ->next != NULL;p = p ->next )
{
if ((p ->exp) ==(p->next->exp ) && (p ->next ->next != NULL))
{
p ->con = p ->con + p -> next ->con
swap = (listnode *)malloc (sizeof(listnode));
swap = p ->next
p ->next = p ->next ->next
free (swap);
real_myhead ->exp -= 1;
//p =p ->next
}
if ((p ->exp) ==(p->next->exp ) && (p ->next ->next == NULL))
{
p ->con = p ->con + p -> next ->con
swap = (listnode *)malloc (sizeof(listnode));
swap = p ->next
p ->next =NULL;
real_myhead ->exp -= 1;
//free(swap);
//goto next;
return 0;
}
}
next:return 0;
}
void myprintf(listnode * node)
{
listnode * mynode; mynode = (listnode *)malloc (sizeof(listnode));
mynode ->con = node ->con;
mynode ->exp = node ->exp;
if ((mynode ->con == 1 ) && (mynode ->exp == 1))
printf("x");
else if ((mynode ->con == 1) && (mynode ->exp != 1) && (mynode ->exp != 0) )
printf("x^%d",mynode ->exp );
else if ((mynode ->con == 0)) ;
else if ((mynode ->con != 1) && (mynode ->exp == 1))
printf("%.2f x",mynode ->con );
else if ((mynode ->con != 0) && (mynode ->exp == 0))
printf("%.2f",mynode ->con );
//else if (mynode ->exp == 0)
//printf("%f", mynode ->con);
else

printf("%.2f x^%d",mynode ->con ,mynode ->exp );
free(mynode);
}
void operplus()
{
head1 = point[0];
head2 = point[1];
head = (listnode *)malloc(sizeof(listnode));
head ->exp = head1 ->exp + head2 ->exp
head ->next = NULL; if (head1 ->exp == 0)
goto first_over; p = head1 ->next;
for (;p ->next != NULL;p = p ->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
first_over: if (head ->exp == 0)
goto second_over;
p = head2 ->next;
for (;p ->next != NULL;p = p ->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
second_over:
bubble_sort(head);
bubble_sort(head);
}
void operminus()
{
head1 = point[0];
head2 = point[1];
head = (listnode *)malloc(sizeof(listnode));
head ->exp = head1 ->exp + head2 ->exp
head ->next = NULL;
if (head1 ->exp == 0)
goto first_over;
p = head1 ->next;
for (;p ->next != NULL;p = p ->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = p ->con
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
first_over: if (head ->exp == 0)
goto second_over;
p = head2 ->next;
for (;p ->next != NULL;p = p ->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = -(p ->con)
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = -(p ->con );
r ->exp = p ->exp
r ->next = head ->next
head ->next = r;
second_over: bubble_sort(head);
bubble_sort(head);
}
int opermul()
{
head1 = point[0]; head2 = point[1];
head = (listnode *)malloc(sizeof(listnode));
head ->next = NULL;
head ->exp =(head1 ->exp ) * (head2 ->exp );
if (head ->exp == 0)
return 0;
for (p = head1 ->next p ->next !=NULL p = p ->next )
{
for(q = head2 ->next q ->next != NULL; q = q ->next )
{
r = (listnode *)malloc (sizeof(listnode));
r ->con = (q ->con ) * (p ->con );
r ->exp = (q ->exp ) + (p ->exp );
r ->next =head ->next;
head ->next =r;
}
r = (listnode *)malloc (sizeof(listnode));
r ->con = (q ->con ) * (p ->con );
r ->exp = (q ->exp ) + (p ->exp );
r ->next =head ->next; head ->next =r;
}
r = (listnode *)malloc (sizeof(listnode));
r ->con = (q ->con ) * (p ->con );
r ->exp = (q -

>exp ) + (p ->exp );
r ->next =head ->next;
head ->next =r;
bubble_sort(head);
bubble_sort(head);
}
int output(listnode * output_head)
{
if (output_head ->exp == 0)
{
printf("0\n");
return 0;
}
p = output_head ->next;
if (p ->con > 0)
{
myprintf(p);
p = p ->next;
}
for(;p ->next != NULL; p = p->next )
{
if (p ->con > 0) printf("+");
//else if (p ->con < 0)
//printf("-");
myprintf(p);
}
if (p ->con > 0)
printf("+");
//else if (p ->con < 0)
//printf("-");
myprintf(p);
printf("\n");
return 0;
}
listnode * operdy(listnode * p)
{
listnode * operdy_head;
operdy_head = (listnode *)malloc (sizeof(listnode));
operdy_head ->exp = 0;
operdy_head ->next = NULL;
if (p ->exp == 0)
return operdy_head;
for (p = p ->next p ->next != NULL p = p->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = (p ->con) * (p ->exp );
r ->exp =((p ->exp ) - 1);
r ->next = operdy_head ->next
operdy_head ->next = r;
operdy_head ->exp ++;
}
r = (listnode *) malloc (sizeof(listnode));
r ->con = (p ->con) * (p ->exp );
r ->exp =((p ->exp ) - 1);
r ->next = operdy_head ->next
operdy_head ->next = r;
operdy_head ->exp ++;
bubble_sort(operdy_head);
bubble_sort(operdy_head);
return operdy_head;
}
listnode * operjifen(listnode * p)
{
listnode * operdy_head;
operdy_head = (listnode *)malloc (sizeof(listnode));
operdy_head ->exp = 0;
operdy_head ->next = NULL;
if (p ->exp == 0) return operdy_head;
for (p = p ->next p ->next != NULL p = p->next )
{
r = (listnode *) malloc (sizeof(listnode));
r ->con = (p ->con) / (p ->exp + 1);
r ->exp =((p ->exp ) + 1);
r ->next = operdy_head ->next operdy_head ->next = r;
operdy_head ->exp ++;
}

r = (listnode *) malloc (sizeof(listnode));
r ->con = (p ->con) / (p ->exp + 1 );
r ->exp =((p ->exp ) + 1);
r ->next = operdy_head ->next operdy_head ->next = r;
operdy_head ->exp ++;
bubble_sort(operdy_head);
bubble_sort(operdy_head);
return operdy_head;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf("请依次输入第1个多项式的各个元素:(例:-9x^3 应该输入 -9 3)\n");
input();
printf("请依次输入第2个多项式的各个元素:(例:-9x^3 应该输入 -9 3)\n");
input();
bubble_sort(point[0]);
bubble_sort(point[1]);
bubble_sort(point[0]);
bubble_sort(point[1]);
//整理顺序 printf("第1个多项式为:\n");
output(point [0]);
printf("第2个多项式为:\n");
output(point [1]);
printf("多项式1+多项式2 =:\n");
operplus();
output(head);
printf("多项式1-多项式2 =:\n");
operminus();
output(head);
printf("多项式1*多项式2 =:\n");
opermul();
output(head);
printf("多项式1关于x求导得:\n");
head = operdy(head1)

; output(head);
printf("多项式2关于求导得:\n");
head = operdy(head2);
output(head);
printf("多项式1关于y的积分\n");
head = operjifen(head1);
output(head);
printf("多项式2关于y的积分\n");
head = operjifen(head2);
output(head);
printf("Press any key to exit");
getchar();
getchar();
return 0; }

相关文档
最新文档