Used to calculate some mathematical operations of polynomial of one indeterminate.
Achievint simple calculation, multiplication and division addition and subtraction also derivation functions.
0.0
Only support valid input . orz…
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#define ERROR -1
#define OK 1
#define INF 0x7777777
#define status 0 //status:0add ,status:1 substract
int flag = status;//
typedef struct{
float coef;
int expn;
}ElemType;
typedef struct polynomail{
ElemType data;
struct polynomail *next;
}*Link,polynomail;
typedef struct{
Link head, tail;
int len;
}LinkList;
//POLYNOMAIL
//ADD¡¢MUTIPLY¡¢SUBSTRACT
//typedef Position polynomail;
int cmp(int a, int b){
int ret;
if(a > b){
ret = 1;
}
else if(a < b){
ret = -1;
}
else{
ret = 0;
}
return ret;
}
//INTERFACE count of element
void Creatpolyn(polynomail *p, int m){
ElemType temp;
polynomail *new , *h;
int i;
p->data.coef = 0.0;
p->data.expn = 0;
p->next = NULL;
h = p;
for(i = 1; i <= m ; i++){
scanf("(%f,%d)", &temp.coef, &temp.expn);
if(flag)
temp.coef = -1 * temp.coef;
new = (polynomail *)malloc(sizeof(polynomail));
new->data = temp;
new->next = h->next;
h->next = new;
h = new;
}//for
}
//INTERFACE polynomail a & b
void addpolyn(polynomail *pa, polynomail *pb){
Link tempa, tempb, ha, hb, new;
int expna, expnb;
int sum;
tempa = tempb = (Link)malloc(sizeof(polynomail));
ha = pa;
hb = pb;
tempa = ha->next;
tempb = hb->next;
while(tempa && tempb){
expna = tempa->data.expn;
expnb = tempb->data.expn;
switch( cmp(expna, expnb) ){
case -1://a < b
ha = tempa;
tempa = tempa->next;
break;
case 0://a = b
sum = tempa->data.coef + tempb->data.coef;
if(sum == 0){
ha->next = tempa->next;
tempa = ha->next;
hb = tempb;
tempb = tempb->next;
break;
}
else{
tempa->data.coef = sum;
ha = tempa;
tempa = tempa->next;
hb = tempb;
tempb = tempb->next;
}
break;
case 1://a > b
new = (polynomail *)malloc(sizeof(polynomail));
new->data = tempb->data;
new->next = ha->next;
ha->next = new;
ha = ha->next;
tempa = ha->next;
hb = tempb;
tempb = tempb->next;
break;
}//switch
}//while
if(tempb){
ha->next = tempb;
}
}
Link multiplypolynOne(polynomail *pa, polynomail *pb){
if(pa->next == NULL || pa->next->data.coef == 0|| pb == NULL || pb->next->data.coef == 0){
return NULL;
}
Link tempa, tempb, new, tempc, pc;
int expna, expnb;
int coef, expn;
pc = tempc = (Link)malloc(sizeof(polynomail));
tempa = pa->next;
tempb = pb->next;
tempc->next = NULL;
while(tempb){
coef = tempa->data.coef * tempb->data.coef;
expn = tempa->data.expn + tempb->data.expn;
new = (Link)malloc(sizeof(polynomail));
new->data.coef = coef;
new->data.expn = expn;
new->next = tempc->next;
tempc->next = new;
tempc = tempc->next;
tempb = tempb->next;
}
return pc;
}
void multiplypolyn(polynomail *pa, polynomail *pb, polynomail *pc){
if(pa->next && pa->next->data.coef && pb->next && pb->next->data.coef){
Link pos, temp;
pc->next = NULL;
pc->data.coef = 0.0;
pc->data.expn = 0;
pos = pa;
for(; pos->next; pos = pos->next){
temp = multiplypolynOne(pos, pb);
// printf("dsds %d\n" , temp->next->data.expn);
addpolyn(pc, temp);
}
}
}
void calculatepolyn(polynomail *pa, int x){
Link temp;
float coef, sum = 0.0;
int expn;
temp = pa->next;
while(temp){
coef = temp->data.coef;
expn = temp->data.expn;
sum += coef * (pow(x, expn));
temp = temp->next;
}
printf("%0.0f\n", sum);
}
//
void diffpolyn(polynomail *pa){
Link temp;
float coef;
int expn;
temp = pa->next;
while(temp){
coef = temp->data.coef;
expn = temp->data.expn;
temp->data.coef = coef * expn;
temp->data.expn = expn - 1;
temp = temp->next;
}
}
void printpolyn(polynomail *pa){
if(pa->next == NULL){
printf("0\n");
return;
}
while(pa->next){
pa = pa->next;
if(pa->data.expn == 0 ){
printf("%0.0f", pa->data.coef);
}
//diff
else if(pa->data.expn < 0){
continue;
}
else if(pa->data.expn == 1 ){
if(pa->data.coef == 1){
printf("X");
}
else if(pa->data.coef == -1){
printf("-X");
}
else{
printf("%0.0fX", pa->data.coef);
}
}
else{
if(pa->data.coef == 1){
printf("X^%d", pa->data.expn);
}
else if(pa->data.coef == -1){
printf("-X^%d", pa->data.expn);
}
else{
printf("%0.0fX^%d", pa->data.coef, pa->data.expn);
}
}
if(pa->next&&pa->next->data.coef > 0){
printf("+");
}
}
}
int main(int argc, char *argv[]){
polynomail pa, pb, pc;
int count1, count2;
int x;
/*
//creatpolyn(polynmail *p, int count);
*/
scanf("%d", &count1);
getchar();
Creatpolyn(&pa, count1);
//scanf("%d", &count2);
//getchar();
//flag = 1; //status
//Creatpolyn(&pb, count2);
/*
//addpolyn(polynmail *pa, polynmail *pb);
//destroypolyn(polynmail p);
*/
//addpolyn(&pa, &pb);
/*
//multiplypolyn(polynmail *pa, polynamil *pb, polynomail *pc);
//destroypolyn(polynmail p);
*/
//multiplypolyn(&pa, &pb, &pc);
/*
//calculatepolyn(polynomail *p, int number);
//print value
*/
//scanf("%d", &x);
//getchar();
//calculatepolyn(&pa, x);
diffpolyn(&pa);
/*
//printpolyn(polynmail *p);
*/
printpolyn(&pa);
}