チェーンテーブルの適用:多項式の加減法
7266 ワード
2つの多項式出力を入力する2つの多項式の和または差
#include
#include
#include
struct DuoXiangShi{
int XiShu;
int Mi;
int length;
struct DuoXiangShi * next;
};
struct DuoXiangShi * create(){ //
char a[1000];
int b[100],c[100],s=0;
printf("
");
struct DuoXiangShi *head , *p1 , *p2;
scanf("%s",a); //
getchar(); //
int i=0;
head = p1 = p2 = (struct DuoXiangShi *)malloc(sizeof(struct DuoXiangShi));
for(int i=0;a[i]!='\0';i++){ //
if(a[i]>='0'&&a[i]<='9'){ //
int inde = 0;
while(a[i]>='0'&&a[i]<='9'){
inde = inde*10 + (a[i]-'0');
i++;
}
b[s] = inde;
}
if(a[i]=='+'){ //
i++;
if(a[i]>='0'&&a[i]<='9'){
int inde = 0;
while(a[i]>='0'&&a[i]<='9'){
inde = inde*10 + (a[i]-'0');
i++;
}
b[s] = inde;
}
}
if(a[i]=='-'){ //
i++;
if(a[i]>='0'&&a[i]<='9'){
int inde = 0;
while(a[i]>='0'&&a[i]<='9'){
inde = inde*10 + (a[i]-'0');
i++;
}
b[s] =0-inde;
}
}
if(a[i]=='x'){ // x x
if(i==0||!(a[i-1]>='0'&&a[i-1]<='9')){
if(a[i-1]=='-'){
b[s] = -1;
}else{
b[s] = 1;
}
}
if(a[i+1]!='^'){ // x
c[s]=1;
s++;
}
}
if(a[i]=='^'){ //
i++;
int inde = 0;
while(a[i]>='0'&&a[i]<='9'){
inde = inde*10 + (a[i]-'0');
i++;
}
c[s]=inde;
s++;
i--;
}
}
for(int i=0;iXiShu = b[i];
p1->Mi = c[i];
p1->length = s;
printf("@@@@@@@@@@@@@@@%d %d
",p1->XiShu,p1->Mi);
p1 = (struct DuoXiangShi *)malloc(sizeof(struct DuoXiangShi));
p2->next = p1;
}
p2->next = NULL;
return head;
}
void print(struct DuoXiangShi * head){ //
if(head->XiShu == 0){
head = head->next;
}
printf("%dx^%d",head->XiShu,head->Mi);
head = head->next;
while(head){
if(head->XiShu == 0){
head = head->next;
}
printf("+(%dx^%d)",head->XiShu,head->Mi);
head = head->next;
}
printf("
");
}
struct DuoXiangShi * DataSort(struct DuoXiangShi * head){ //
struct DuoXiangShi *p1,*p2,*p3;
p3=p2=p1=head;
p1=p1->next;
for(int i=0;ilength;i++)
{
while(p2->next!=NULL)
{
if((p2->Mi)>(p1->Mi)){
if(p2==head){
head=p1;
p2->next=p1->next;
p1->next=p2;
}else if(p1->next==NULL){
p3->next=p1;
p1->next=p2;
p2->next=NULL;
}else{
p2->next=p1->next;
p1->next=p3->next;
p3->next=p1;
}
}
p3=p2;
p2=p1;
p1=p1->next;
}
p3=p2=p1=head;
p1=p1->next;
}
return (head);
}
struct DuoXiangShi * combineListAdd(struct DuoXiangShi *head1,struct DuoXiangShi *head2){ //
struct DuoXiangShi * head3,* head4;
head4=head3=head1->Mi<=head2->Mi?head1:head2;
if(head1->MiMi){
head1=head1->next;
}else if(head1->Mi>head2->Mi){
head2=head2->next;
}else{
head1->XiShu = head1->XiShu+head2->XiShu;
head1 = head1->next;
head2 = head2->next;
}
while(head1&&head2){
if(head1->MiMi){
head3->next = head1;
head3 = head3->next;
head1 = head1->next;
}else if(head1->Mi>head2->Mi){
head3->next = head2;
head3 = head3->next;
head2 = head2->next;
}else if(head1->Mi==head2->Mi){
if(head3->next==head1){
head1->XiShu = head1->XiShu+head2->XiShu;
head3 = head3->next;
head1 = head1->next;
head2 = head2->next;
}else{
head2->XiShu = head1->XiShu+head2->XiShu;
head3 = head3->next;
head1 = head1->next;
head2 = head2->next;
}
}
printf("%d\t",head3->Mi);
}
head3->next = head3->next==head1?head2:head1;
printf("%d
",head3->Mi);
return head4;
}
struct DuoXiangShi * combineListMinus(struct DuoXiangShi *head1,struct DuoXiangShi *head2){//
struct DuoXiangShi * head3,* head4;
head4=head3=head1->Mi<=head2->Mi?head1:head2;
if(head1->MiMi){
head1=head1->next;
}else if(head1->Mi>head2->Mi){
head2=head2->next;
}else{
head1->XiShu = head1->XiShu-head2->XiShu;
head1 = head1->next;
head2 = head2->next;
}
while(head1&&head2){
if(head1->MiMi){
head3->next = head1;
head3 = head3->next;
head1 = head1->next;
}else if(head1->Mi>head2->Mi){
head3->next = head2;
head3 = head3->next;
head2 = head2->next;
}else if(head1->Mi==head2->Mi){
if(head3->next==head1){
head1->XiShu = head1->XiShu-head2->XiShu;
head3 = head3->next;
head1 = head1->next;
head2 = head2->next;
}else{
head2->XiShu = head1->XiShu-head2->XiShu;
head3 = head3->next;
head1 = head1->next;
head2 = head2->next;
}
}
printf("%d\t",head3->Mi);
}
head3->next = head3->next==head1?head2:head1;
printf("%d
",head3->Mi);
return head4;
}
struct DuoXiangShi * Add(struct DuoXiangShi *a1,struct DuoXiangShi *a2){
a1 = DataSort(a1);
a2 = DataSort(a2);
return combineListAdd(a1,a2);
}
struct DuoXiangShi * Minus(struct DuoXiangShi *a1,struct DuoXiangShi *a2){
a1 = DataSort(a1);
a2 = DataSort(a2);
return combineListMinus(a1,a2);
}
int main(){
int a = 0;
printf("============================================
");
printf("\t\t
");
printf(" 0、
");
printf(" 1、
");
printf(" 2、
");
printf(" 3、
");
printf(" 4、
");
printf("============================================
");
scanf("%d",&a);
struct DuoXiangShi * list1 , * list2,*list;
while(a){
switch(a){
case 1:{
list1 = create();
print(list1);
list2 = create();
print(list2);
break;
}
case 2:{
print(list1);
print(list2);
break;
}
case 3:{
list = Add(list1,list2);
print(list);
break;
}
case 4:{
list = Minus(list1,list2);
print(list);
break;
}
}
printf("============================================
");
printf("\t\t
");
printf(" 0、
");
printf(" 1、
");
printf(" 2、
");
printf(" 3、
");
printf(" 4、
");
printf("============================================
");
scanf("%d",&a);
}
}