C 链表实现多项式
原来文章是用于转载,随笔才是原创,好吧,那发过来吧。
第一篇文章,如题用链表实现了多项式,包括输入、输出和三种运算。
这次代码主要遇到两个问题:scanf的连续调用,以及一个神奇的很不科学的bug——i的初始化问题
代码贴下来如下:
//本程序使用C语言创建一个链表来表示多项式 //多项式按照升序进行排列 //实现多项式的加、减、乘三个运算 #include<stdlib.h> #include<stdio.h> #define new(type) (type*)malloc(sizeof(type)) typedef struct _Term Term; struct _Term{ float coef; //系数 int exp; //指数 Term *next; }; Term* plus (Term* head1,Term* head2); Term* minus(Term* head1,Term* head2); Term* multi(Term* head1,Term* head2); void print(Term* head); void scan(Term* head); int main(){ Term *head1 = new(Term); head1->next = NULL; Term *head2 = new(Term); head2->next = NULL; printf("Input format: coef,exp\n"); printf("Enter the ### to end the input.\n"); printf("Input Polynomal 1:\n"); scan(head1); printf("Input Polynomal 2:\n"); scan(head2); Term * result1 = new(Term); Term * result2 = new(Term); Term * result3 = new(Term); result1 = plus(head1,head2); result2 = minus(head1,head2); result3 = multi(head1,head2); printf("result of plus:"); print(result1); printf("result of minus:"); print(result2); printf("result of multi:"); print(result3); return 0; } void print(Term *head){ Term *temp = head->next; while(temp != NULL){ if(temp->coef > 0) printf("+%gx^%d",temp->coef,temp->exp); else if(temp->coef < 0) printf("%gx^%d",temp->coef,temp->exp); temp = temp->next; } printf("\n"); } void scan(Term *head){ float coef; int exp; while(scanf("%f,%d",&coef,&exp) == 2){ Term *ptr = new(Term); ptr->exp = exp; ptr->coef = coef; if(head->next == NULL){ ptr->next = NULL; head->next = ptr; } else{ Term* temp = head->next; Term* temp2 = head; while(temp != NULL && temp->exp > ptr->exp){ temp = temp->next; temp2 = temp2->next; } if(temp == NULL){ ptr->next = NULL; temp2->next = ptr; } else{ ptr->next = temp2->next; temp2->next = ptr; } } } while(getchar() != '\n'); return; } Term* plus(Term* head1,Term* head2){ Term *newhead = new(Term); newhead->next = NULL; Term *temp1 = head1->next; Term *temp2 = head2->next; Term *temp = newhead; while(temp1 != NULL && temp2 != NULL){ Term *ptr = new(Term); if(temp1->exp < temp2->exp){ ptr->next = NULL; ptr->coef = temp2->coef; ptr->exp = temp2->exp; temp->next = ptr; temp2 = temp2->next; } else if(temp1->exp > temp2->exp){ ptr->next = NULL; ptr->coef = temp1->coef; ptr->exp = temp1->exp; temp->next = ptr; temp1 = temp1->next; } else{ ptr->next = NULL; ptr->coef = temp1->coef + temp2->coef; ptr->exp = temp1->exp; temp->next = ptr; temp1 = temp1->next; temp2 = temp2->next; } temp = temp->next; } if(temp1 == NULL){ while(temp2 != NULL){ Term *ptr = new(Term); ptr->next = NULL; ptr->coef = temp2->coef; ptr->exp = temp2->exp; temp->next = ptr; temp2 = temp2->next; temp = temp->next; } } else if(temp2 == NULL){ while(temp1 != NULL){ Term *ptr = new(Term); ptr->next = NULL; ptr->coef = temp1->coef; ptr->exp = temp1->exp; temp->next = ptr; temp1 = temp1->next; temp = temp->next; } } return newhead; } Term* minus(Term* head1,Term* head2){ Term *newhead = new(Term); newhead->next = NULL; Term *temp = newhead; Term *temp2 = head2->next; while(temp2 != NULL){ Term *ptr = new(Term); ptr->next = NULL; ptr->coef = -temp2->coef; ptr->exp = temp2->exp; temp->next = ptr; temp2 = temp2->next; temp = temp->next; } newhead = plus(head1,newhead); return newhead; } Term* multi(Term* head1,Term* head2){ int maxExp,i; Term *newhead = new(Term); newhead->next = NULL; Term *temp = newhead; Term *temp1 = head1->next; Term *temp2 = head2->next; if(temp1 != NULL && temp2 != NULL) maxExp = temp1->exp + temp2->exp; printf("maxExp:%d\n",maxExp); float *result = malloc(sizeof(float) * (maxExp+1)); i = 0; while(i <= maxExp){ result[i] = 0.0; i ++; } while(temp1 != NULL){ temp2 = head2->next; while(temp2 != NULL){ i = temp1->exp + temp2->exp; printf("exp:%d,%d\n",temp1->exp,temp2->exp); printf("coef:%f,%f\n",temp1->coef,temp2->coef); printf("result:%f",result[i]); result[i] = result[i] + temp1->coef * temp2->coef; printf("result[%d]=%f\n",i,result[i]); temp2 = temp2->next; } temp1 = temp1->next; } for(i = maxExp; i >= 0; i --){ if(result[i] != 0){ Term *ptr = new(Term); ptr->next = NULL; ptr->coef = result[i]; ptr->exp = i; temp->next = ptr; temp = temp->next; } } free(result); return newhead; }
先说第一个问题,在连续两次使用scanf的时候,发现总是第二个scanf不能获取输入。
查阅网上相关资料后明白了其中的原理,现在举例如下:
第一次使用scanf("%d\n",&i);
你会有类似这样的输入:1\n,这里问题的关键就在于第二个字符,回车,他还留在缓存区的,
这样当你第二次调用scanf()的时候,他会先读取回车自然就不能继续输入了,
解决这个问题只需要添加一句getchar(),将其从缓存区取出,就能解决了。
第二个不科学的bug,我至今也没有想明白,就是在169行,最开始我没有初始化i,那么在main函数中的调用肯定错误
但是当我在调用之前,加上一句printf("xxxx");后他就神奇的对了,至今不明白,希望懂的人赐教。