#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>
typedef struct node_s{
int data;
struct node_s *next;
} Node;
typedef Node* Node_p;
Node_p create(int data){
Node_p newNode
=(Node_p
)malloc(sizeof(Node
)); newNode->data=data;
newNode->next=NULL;
return newNode;
}
int counting_length(Node_p p){
Node_p current=p;
int length=0;
while(current!=NULL){
length+=1;
current=current->next;
}
return length-1;
}
void printing(Node_p p){
int length=counting_length(p);
int max=length;
Node_p current=p;
while(current!=NULL){
if(length==max){
if(current->data==0){
else{
length--;
max--;
}
}
else {
if(current->data==1){
}
else if(current->data==-1){
}
else printf("%dx^%d",current
->data
,length
--); }
}
else if(length==1){
if(current->data>0){
if(current
->data
==1) printf("+x"); else printf("+%dx",current
->data
); }
else if(current->data<0){
if(current
->data
==-1) printf("-x"); else printf("%dx",current
->data
); }
else length--;
length=0;
}
else if(length>0){
if(current->data>0){
if(current
->data
==1) printf("+x^%d",length
--); else printf("+%dx^%d",current
->data
,length
--); }
else if(current->data<0){
if(current
->data
==-1) printf("-x^%d",length
--); else printf("%dx^%d",current
->data
,length
--); }
else length--;
}
else{
if(current->data>0){
}
else if(current->data<0){
}
else length--;
}
current=current->next;
}
}
void insertNodeback(Node_p *p,int data){
Node_p newNode=create(data);
if((*p)==NULL) (*p)=newNode;
else{
Node_p current=(*p);
while(current->next!=NULL) current=current->next;
current->next=newNode;
}
}
void insertNodefront(Node_p *p,int data){
Node_p newNode=create(data);
if((*p)==NULL) (*p)=newNode;
else{
newNode->next=(*p);
(*p)=newNode;
}
}
void separate_to_data(char equation[], Node_p *p) {
char *temp
= strtok(equation
, " "); while (temp != NULL) {
insertNodefront(p, number);
}
}
void getdata(Node_p *p){
char equation[100];
scanf("%[^\n]%*c",equation
); separate_to_data(equation,p);
}
Node_p add(Node_p p_1,Node_p p_2){
Node_p equa_1=p_1;
Node_p equa_2=p_2;
Node_p answer=NULL;
int length_1=counting_length(equa_1);
int length_2=counting_length(equa_2);
if(length_1>length_2){
for(int i=0;i<length_1-length_2;i++) insertNodeback(&equa_2,0);
}
else if(length_1<length_2){
for(int i=0;i<length_2-length_1;i++) insertNodeback(&equa_1,0);
}
while(equa_1!=NULL&&equa_2!=NULL){
int temp=(equa_1->data)+(equa_2->data);
insertNodefront(&answer,temp);
equa_1=equa_1->next;
equa_2=equa_2->next;
}
return answer;
}
Node_p minus(Node_p p_1,Node_p p_2){
Node_p equa_1=p_1;
Node_p equa_2=p_2;
Node_p answer=NULL;
int length_1=counting_length(equa_1);
int length_2=counting_length(equa_2);
if(length_1>length_2){
for(int i=0;i<length_1-length_2;i++) insertNodeback(&equa_2,0);
}
else if(length_1<length_2){
for(int i=0;i<length_2-length_1;i++) insertNodeback(&equa_1,0);
}
while(equa_1!=NULL&&equa_2!=NULL){
int temp=(equa_1->data)-(equa_2->data);
insertNodefront(&answer,temp);
equa_1=equa_1->next;
equa_2=equa_2->next;
}
return answer;
}
Node_p times(Node_p p_1,Node_p p_2){
Node_p equa_1=p_1;
Node_p equa_2=p_2;
Node_p answer=NULL;
int length_1=counting_length(equa_1);
int length_2=counting_length(equa_2);
int max_length=length_1+length_2+1;
for(int i=0;i<max_length;i++) insertNodefront(&answer,0);
Node_p origin=answer;
while(equa_1!=NULL){
Node_p current=answer;
while(equa_2!=NULL){
int temp=(equa_1->data)*(equa_2->data);
(current->data)+=temp;
current=current->next;
equa_2=equa_2->next;
}
answer=answer->next;
equa_1=equa_1->next;
equa_2=p_2;
}
Node_p final_answer=NULL;
while(origin!=NULL){
insertNodefront(&final_answer,origin->data);
origin=origin->next;
}
return final_answer;
}
int main() {
Node_p equation_1=NULL;
Node_p equation_2=NULL;
Node_p answer=NULL;
getdata(&equation_1);
getdata(&equation_2);
printing(add(equation_1,equation_2));
printing(minus(equation_1,equation_2));
printing(times(equation_1,equation_2));
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5o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ICAgICAgICAgICBpZihjdXJyZW50LT5kYXRhPT0xKXsKICAgICAgICAgICAgICAgICAgICBwcmludGYoInheJWQiLGxlbmd0aC0tKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgaWYoY3VycmVudC0+ZGF0YT09LTEpewogICAgICAgICAgICAgICAgICAgIHByaW50ZigiLXheJWQiLGxlbmd0aC0tKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgcHJpbnRmKCIlZHheJWQiLGN1cnJlbnQtPmRhdGEsbGVuZ3RoLS0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYobGVuZ3RoPT0xKXsKICAgICAgICAgICAgaWYoY3VycmVudC0+ZGF0YT4wKXsKICAgICAgICAgICAgICAgIGlmKGN1cnJlbnQtPmRhdGE9PTEpIHByaW50ZigiK3giKTsKICAgICAgICAgICAgICAgIGVsc2UgcHJpbnRmKCIrJWR4IixjdXJyZW50LT5kYXRhKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKGN1cnJlbnQtPmRhdGE8MCl7CiAgICAgICAgICAgICAgICBpZihjdXJyZW50LT5kYXRhPT0tMSkgcHJpbnRmKCIteCIpOwogICAgICAgICAgICAgICAgZWxzZSBwcmludGYoIiVkeCIsY3VycmVudC0+ZGF0YSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBsZW5ndGgtLTsKICAgICAgICAgICAgbGVuZ3RoPTA7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYobGVuZ3RoPjApewogICAgICAgICAgICBpZihjdXJyZW50LT5kYXRhPjApewogICAgICAgICAgICAgICAgaWYoY3VycmVudC0+ZGF0YT09MSkgcHJpbnRmKCIreF4lZCIsbGVuZ3RoLS0pOwogICAgICAgICAgICAgICAgZWxzZSBwcmludGYoIislZHheJWQiLGN1cnJlbnQtPmRhdGEsbGVuZ3RoLS0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYoY3VycmVudC0+ZGF0YTwwKXsKICAgICAgICAgICAgICAgIGlmKGN1cnJlbnQtPmRhdGE9PS0xKSBwcmludGYoIi14XiVkIixsZW5ndGgtLSk7CiAgICAgICAgICAgICAgICBlbHNlIHByaW50ZigiJWR4XiVkIixjdXJyZW50LT5kYXRhLGxlbmd0aC0tKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGxlbmd0aC0tOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICAgaWYoY3VycmVudC0+ZGF0YT4wKXsKICAgICAgICAgICAgICAgIHByaW50ZigiKyVkIixjdXJyZW50LT5kYXRhKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKGN1cnJlbnQtPmRhdGE8MCl7CiAgICAgICAgICAgICAgICBwcmludGYoIiVkIixjdXJyZW50LT5kYXRhKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGxlbmd0aC0tOyAgIAogICAgICAgIH0KICAgICAgICBjdXJyZW50PWN1cnJlbnQtPm5leHQ7CiAgICB9CiAgICBwcmludGYoIlxuIik7Cn0Kdm9pZCBpbnNlcnROb2RlYmFjayhOb2RlX3AgKnAsaW50IGRhdGEpewogICAgTm9kZV9wIG5ld05vZGU9Y3JlYXRlKGRhdGEpOwogICAgaWYoKCpwKT09TlVMTCkgKCpwKT1uZXdOb2RlOwogICAgZWxzZXsKICAgICAgICBOb2RlX3AgY3VycmVudD0oKnApOwogICAgICAgIHdoaWxlKGN1cnJlbnQtPm5leHQhPU5VTEwpIGN1cnJlbnQ9Y3VycmVudC0+bmV4dDsKICAgICAgICBjdXJyZW50LT5uZXh0PW5ld05vZGU7CiAgICB9Cn0Kdm9pZCBpbnNlcnROb2RlZnJvbnQoTm9kZV9wICpwLGludCBkYXRhKXsKICAgIE5vZGVfcCBuZXdOb2RlPWNyZWF0ZShkYXRhKTsKICAgIGlmKCgqcCk9PU5VTEwpICgqcCk9bmV3Tm9k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