一道编程算法题?
设a和b是两个带有头结点的循环链表表示的多项式,试编写一个算法,计算这两个多项式的乘积c=a*b,要求计算后多项式a和b保持原状。...
设a和b是两个带有头结点的循环链表表示的多项式,试编写一个算法,计算这两个多项式的乘积c=a*b,要求计算后多项式a和b保持原状。
展开
2个回答
展开全部
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define EPS 1E-6
typedef struct item {
double coefficient;
int power;
struct item *next;
} *POLYNOMIAL,*pItem;
POLYNOMIAL Create() { // 创建多项式
pItem head,p;
double coe;
int pwr,iterms,i;
head = p = (pItem)malloc(sizeof(struct item));
scanf("%d",&iterms); // 读入多项式的项数
for(i = 0; i < iterms; ++i) {
scanf("%lf%d",&coe,&pwr);
p->next = (pItem)malloc(sizeof(struct item));
p->next->coefficient = coe;
p->next->power = pwr;
p = p->next;
}
p->next = NULL;
return head;
}
void Sort(POLYNOMIAL head) { // 按幂次降排序
pItem pt,q,p = head;
while(p->next) {
q = p->next;
while(q->next) {
if(p->next->power < q->next->power) {
pt = p->next;
p->next = q->next;
q->next = p->next->next;
p->next->next = pt;
}
else q = q->next;
}
p = p->next;
}
}
void Show(POLYNOMIAL head) { // 显示
POLYNOMIAL p = head->next;
int flag = 1;
if(p == NULL) return;
while(p) {
if(flag) {
if(fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("%.2lf",p->coefficient);
else if(p->power == 1) {
if(p->coefficient == 1.0) printf("x ");
else if(p->coefficient == -1.0) printf("-x ");
else printf("%.2lfx ",p->coefficient);
}
else if(p->coefficient == 1.0) printf("x^%d ",p->power);
else if(p->coefficient == -1.0) printf("-x^%d ",p->power);
else printf("%.2lfx^%d ",p->coefficient,p->power);
flag = 0;
}
}
else if(p->coefficient > 0.0 && fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("+ %.2lf ",p->coefficient);
else if(p->power == 1) {
if(p->coefficient == 1.0) printf("+ x ");
else printf("+ %.2lfx ",p->coefficient);
}
else if(p->coefficient == 1.0) printf("+ x^%d ",p->power);
else printf("+ %.2lfx^%d ",p->coefficient,p->power);
}
else if(p->coefficient < 0.0 && fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("- %.2lf ",-p->coefficient);
else if(p->power == 1) {
if(p->coefficient == -1.0) printf("- x ");
else printf("- %.2lfx ",-p->coefficient);
}
else if(p->coefficient == -1.0) printf("- x^%d ",-p->power);
else printf("- %.2lfx^%d ",-p->coefficient,p->power);
}
p = p->next;
}
printf("\n");
}
double Power(double x,int n) {
double value = 1.0;
int i;
for(i = 0; i < n; ++i) value *= x;
return value;
}
double Value(POLYNOMIAL head,double x) { // 多项式求值
POLYNOMIAL p;
double value = 0.0;
for(p = head->next; p; p = p->next)
value += p->coefficient * Power(x,p->power);
return value;
}
POLYNOMIAL Copy(POLYNOMIAL A) {
POLYNOMIAL head,t,p;
head = t = (pItem)malloc(sizeof(struct item));
for(p = A->next; p; p = p->next) {
t->next = (pItem)malloc(sizeof(struct item));
t->next->coefficient = p->coefficient;
t->next->power = p->power;
t = t->next;
}
t->next = NULL;
return head;
}
POLYNOMIAL Additive(POLYNOMIAL A, POLYNOMIAL B) { // 多项式加
POLYNOMIAL head,p,q,t;
head = Copy(A);
for(p = B; p->next; p = p->next) {
q = head;
while(q->next) {
if(p->next->power == q->next->power) {
q->next->coefficient += p->next->coefficient;
if(fabs(q->next->coefficient) <= EPS) {
t = q->next;
q->next = t->next;
free(t);
}
break;
}
q = q->next;
}
if(q->next == NULL) {
q->next = (pItem)malloc(sizeof(struct item));
q->next->coefficient = p->next->coefficient;
q->next->power = p->next->power;
q->next->next = NULL;
}
}
Sort(head);
return head;
}
POLYNOMIAL Subtract(POLYNOMIAL A, POLYNOMIAL B) { // 多项式减
POLYNOMIAL head,p,q,t;
head = Copy(A);
for(p = B; p->next; p = p->next) {
q = head;
while(q->next) {
if(p->next->power == q->next->power) {
q->next->coefficient -= p->next->coefficient;
if(fabs(q->next->coefficient) <= EPS) {
t = q->next;
q->next = t->next;
free(t);
}
break;
}
q = q->next;
}
if(q->next == NULL) {
q->next = (pItem)malloc(sizeof(struct item));
q->next->coefficient = -p->next->coefficient;
q->next->power = p->next->power;
q->next->next = NULL;
}
}
Sort(head);
return head;
}
POLYNOMIAL Multiplication(POLYNOMIAL A, POLYNOMIAL B) { // 多项式乘
POLYNOMIAL head,t,p,q;
head = t = (pItem)malloc(sizeof(struct item));
for(p = A->next; p; p = p->next) { // 完成相乘过程
for(q = B->next; q; q = q->next) {
t->next = (pItem)malloc(sizeof(struct item));
t->next->coefficient = p->coefficient * q->coefficient;
t->next->power = p->power + q->power;
t = t->next;
}
}
t->next = NULL;
Sort(head); // 排序
p = head;
while(p->next) { // 合并同类项
q = p->next;
while(q->next) {
if(p->next->power == q->next->power) {
p->next->coefficient += q->next->coefficient;
t = q->next;
q->next = t->next;
free(t);
}
else q = q->next;
}
p = p->next;
}
return head;
}
void FreeMemory(POLYNOMIAL head) {
POLYNOMIAL q,p = head;
while(p) {
q = p;
p = q->next;
free(q);
}
}
int main() {
char ops[3];
POLYNOMIAL A,B,C = NULL,D = NULL,E = NULL;
printf("创建多项式A:\n");
printf("多项式A的项数:");
A = Create();
Sort(A);
printf("A(x) = ");Show(A);
printf("创建多项式B:\n");
printf("多项式B的项数:");
B = Create();
Sort(B);
printf("B(x) = ");Show(B);
printf("运算符 : ");
fflush(stdin);
gets(ops);
for(int i = 0; ops[i]; ++i) {
switch(ops[i]) {
case '+' : C = Additive(A,B);
printf("C(x) = ");
Show(C);
break;
case '-' : D = Subtract(A,B);
printf("D(x) = ");
Show(D);
break;
case '*' : E = Multiplication(A,B);
printf("E(x) = ");
Show(E);
break;
default : printf("不能识别运算符 : %s\n",ops[i]);
}
}
printf("A(2) = %.2lf\n",Value(A,2.0));
printf("B(2) = %.2lf\n",Value(B,2.0));
if(C) {
printf("C(2) = %.2lf\n",Value(C,2.0));
FreeMemory(C);
}
if(D) {
printf("D(2) = %.2lf\n",Value(D,2.0));
FreeMemory(D);
}
if(E) {
printf("E(2) = %.2lf\n",Value(E,2.0));
FreeMemory(E);
}
FreeMemory(A);
FreeMemory(B);
return 0;
}
#include <math.h>
#include <stdlib.h>
#define EPS 1E-6
typedef struct item {
double coefficient;
int power;
struct item *next;
} *POLYNOMIAL,*pItem;
POLYNOMIAL Create() { // 创建多项式
pItem head,p;
double coe;
int pwr,iterms,i;
head = p = (pItem)malloc(sizeof(struct item));
scanf("%d",&iterms); // 读入多项式的项数
for(i = 0; i < iterms; ++i) {
scanf("%lf%d",&coe,&pwr);
p->next = (pItem)malloc(sizeof(struct item));
p->next->coefficient = coe;
p->next->power = pwr;
p = p->next;
}
p->next = NULL;
return head;
}
void Sort(POLYNOMIAL head) { // 按幂次降排序
pItem pt,q,p = head;
while(p->next) {
q = p->next;
while(q->next) {
if(p->next->power < q->next->power) {
pt = p->next;
p->next = q->next;
q->next = p->next->next;
p->next->next = pt;
}
else q = q->next;
}
p = p->next;
}
}
void Show(POLYNOMIAL head) { // 显示
POLYNOMIAL p = head->next;
int flag = 1;
if(p == NULL) return;
while(p) {
if(flag) {
if(fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("%.2lf",p->coefficient);
else if(p->power == 1) {
if(p->coefficient == 1.0) printf("x ");
else if(p->coefficient == -1.0) printf("-x ");
else printf("%.2lfx ",p->coefficient);
}
else if(p->coefficient == 1.0) printf("x^%d ",p->power);
else if(p->coefficient == -1.0) printf("-x^%d ",p->power);
else printf("%.2lfx^%d ",p->coefficient,p->power);
flag = 0;
}
}
else if(p->coefficient > 0.0 && fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("+ %.2lf ",p->coefficient);
else if(p->power == 1) {
if(p->coefficient == 1.0) printf("+ x ");
else printf("+ %.2lfx ",p->coefficient);
}
else if(p->coefficient == 1.0) printf("+ x^%d ",p->power);
else printf("+ %.2lfx^%d ",p->coefficient,p->power);
}
else if(p->coefficient < 0.0 && fabs(p->coefficient) >= EPS) {
if(p->power == 0) printf("- %.2lf ",-p->coefficient);
else if(p->power == 1) {
if(p->coefficient == -1.0) printf("- x ");
else printf("- %.2lfx ",-p->coefficient);
}
else if(p->coefficient == -1.0) printf("- x^%d ",-p->power);
else printf("- %.2lfx^%d ",-p->coefficient,p->power);
}
p = p->next;
}
printf("\n");
}
double Power(double x,int n) {
double value = 1.0;
int i;
for(i = 0; i < n; ++i) value *= x;
return value;
}
double Value(POLYNOMIAL head,double x) { // 多项式求值
POLYNOMIAL p;
double value = 0.0;
for(p = head->next; p; p = p->next)
value += p->coefficient * Power(x,p->power);
return value;
}
POLYNOMIAL Copy(POLYNOMIAL A) {
POLYNOMIAL head,t,p;
head = t = (pItem)malloc(sizeof(struct item));
for(p = A->next; p; p = p->next) {
t->next = (pItem)malloc(sizeof(struct item));
t->next->coefficient = p->coefficient;
t->next->power = p->power;
t = t->next;
}
t->next = NULL;
return head;
}
POLYNOMIAL Additive(POLYNOMIAL A, POLYNOMIAL B) { // 多项式加
POLYNOMIAL head,p,q,t;
head = Copy(A);
for(p = B; p->next; p = p->next) {
q = head;
while(q->next) {
if(p->next->power == q->next->power) {
q->next->coefficient += p->next->coefficient;
if(fabs(q->next->coefficient) <= EPS) {
t = q->next;
q->next = t->next;
free(t);
}
break;
}
q = q->next;
}
if(q->next == NULL) {
q->next = (pItem)malloc(sizeof(struct item));
q->next->coefficient = p->next->coefficient;
q->next->power = p->next->power;
q->next->next = NULL;
}
}
Sort(head);
return head;
}
POLYNOMIAL Subtract(POLYNOMIAL A, POLYNOMIAL B) { // 多项式减
POLYNOMIAL head,p,q,t;
head = Copy(A);
for(p = B; p->next; p = p->next) {
q = head;
while(q->next) {
if(p->next->power == q->next->power) {
q->next->coefficient -= p->next->coefficient;
if(fabs(q->next->coefficient) <= EPS) {
t = q->next;
q->next = t->next;
free(t);
}
break;
}
q = q->next;
}
if(q->next == NULL) {
q->next = (pItem)malloc(sizeof(struct item));
q->next->coefficient = -p->next->coefficient;
q->next->power = p->next->power;
q->next->next = NULL;
}
}
Sort(head);
return head;
}
POLYNOMIAL Multiplication(POLYNOMIAL A, POLYNOMIAL B) { // 多项式乘
POLYNOMIAL head,t,p,q;
head = t = (pItem)malloc(sizeof(struct item));
for(p = A->next; p; p = p->next) { // 完成相乘过程
for(q = B->next; q; q = q->next) {
t->next = (pItem)malloc(sizeof(struct item));
t->next->coefficient = p->coefficient * q->coefficient;
t->next->power = p->power + q->power;
t = t->next;
}
}
t->next = NULL;
Sort(head); // 排序
p = head;
while(p->next) { // 合并同类项
q = p->next;
while(q->next) {
if(p->next->power == q->next->power) {
p->next->coefficient += q->next->coefficient;
t = q->next;
q->next = t->next;
free(t);
}
else q = q->next;
}
p = p->next;
}
return head;
}
void FreeMemory(POLYNOMIAL head) {
POLYNOMIAL q,p = head;
while(p) {
q = p;
p = q->next;
free(q);
}
}
int main() {
char ops[3];
POLYNOMIAL A,B,C = NULL,D = NULL,E = NULL;
printf("创建多项式A:\n");
printf("多项式A的项数:");
A = Create();
Sort(A);
printf("A(x) = ");Show(A);
printf("创建多项式B:\n");
printf("多项式B的项数:");
B = Create();
Sort(B);
printf("B(x) = ");Show(B);
printf("运算符 : ");
fflush(stdin);
gets(ops);
for(int i = 0; ops[i]; ++i) {
switch(ops[i]) {
case '+' : C = Additive(A,B);
printf("C(x) = ");
Show(C);
break;
case '-' : D = Subtract(A,B);
printf("D(x) = ");
Show(D);
break;
case '*' : E = Multiplication(A,B);
printf("E(x) = ");
Show(E);
break;
default : printf("不能识别运算符 : %s\n",ops[i]);
}
}
printf("A(2) = %.2lf\n",Value(A,2.0));
printf("B(2) = %.2lf\n",Value(B,2.0));
if(C) {
printf("C(2) = %.2lf\n",Value(C,2.0));
FreeMemory(C);
}
if(D) {
printf("D(2) = %.2lf\n",Value(D,2.0));
FreeMemory(D);
}
if(E) {
printf("E(2) = %.2lf\n",Value(E,2.0));
FreeMemory(E);
}
FreeMemory(A);
FreeMemory(B);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询