ช่วยแปลงโค๊ดจากArray เป็น linked List ให้หน่อยค่ะ ด่วน เลย
#include<stdio.h> #include<stdlib.h> #define MAX_DEGREE 100 typedef struct poly { int coeff_array[MAX_DEGREE+1]; int high_power; }polynomial;
typedef polynomial *POLYNOMIAL;
void zero_polynomial(POLYNOMIAL poly) { int i; for (i=0;i<=MAX_DEGREE;i++) { poly->coeff_array[i]=0; } poly->high_power=0; } int max(int x, int y) { if (x>y) { return x; } else { return y; } } void add_polynomial(POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL poly_sum) { int i; zero_polynomial(poly_sum); poly_sum->high_power=max(poly1->high_power,poly2->high_power); for (i=poly_sum->high_power;i>=0;i--) { poly_sum->coeff_array[i]=poly1->coeff_array[i]+poly2->coeff_array[i]; } } void mult_polynomial(POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL poly_prod) { int i,j; zero_polynomial(poly_prod); poly_prod->high_power=poly1->high_power+poly2->high_power; if (poly_prod->high_power>MAX_DEGREE) { printf("Exceeded Array size\n"); } else { for (i=0;i<=poly1->high_power;i++) { for (j=0;j<=poly2->high_power;j++) { poly_prod->coeff_array[i+j] += poly1->coeff_array[i]*poly2->coeff_array[j]; } } } }
void input_polynomial(POLYNOMIAL poly) { int i; printf("High power = "); scanf("%d",&poly->high_power); for (i=0;i<=poly->high_power;i++) { printf("Degree = %d : coeff = ",i); scanf("%d",&poly->coeff_array[i]); } }
void show_polynomial(POLYNOMIAL poly) { int i; for (i=poly->high_power;i>=0;i--) { printf("%d x^%d",poly->coeff_array[i],i); if (i>0) { printf("+"); } } printf("\n"); }
POLYNOMIAL create_polynomial(void) { POLYNOMIAL tmp; tmp=(POLYNOMIAL)malloc(sizeof(polynomial)); return (tmp); }
int main() { POLYNOMIAL poly1, poly2, poly3, poly4,; poly1=create_polynomial(); poly2=create_polynomial(); poly3=create_polynomial(); poly4=create_polynomial(); printf("Please Enter poly1\n"); input_polynomial(poly1); printf("Please Enter poly2\n"); input_polynomial(poly2); add_polynomial(poly1,poly2,poly3); printf("The result is\n"); show_polynomial(poly3); mult_polynomial(poly1,poly2,poly4); printf("The result is\n"); show_polynomial(poly4); return 0; }
ช่วยหน่อยนะคะ
แอด เอม มาแนะนำก้อได้ จะส่งแล้งอะค่ะ
|