|
|
#2
寒风中的细雨2010-04-28 19:06
//不考虑存在指数为负数的情况 输入的时候指数是从低到高操作
#include <stdio.h> #include <stdlib.h> #include <math.h> #define LEN sizeof(struct Polynomial) typedef struct LNode { float coef; int expn; }LNode; typedef struct Polynomial { LNode data; struct Polynomial *next; }*Poly; //1建立多项式 void CreatePoly( Poly &P ) { Poly temp, rear; P = (Poly) malloc (LEN); if( !P ) exit(0); P->next = NULL; int i=1, n; rear = P; printf("输入多项式的项数:"); scanf("%d", &n); while( i<=n ) { temp = (Poly) malloc (LEN); if( !temp ) exit(0); loop: printf("输入第%d项的系数:", i); scanf("%f", &temp->data.coef); if( !temp->data.coef ) goto loop; printf("输入指数:"); scanf("%d", &temp->data.expn); temp->next = rear->next; rear->next = temp; rear = temp; ++i; } } //输出多项式 void PrintPoly( Poly P ) { Poly temp = P->next; if( !temp ) exit(0); printf(" %.2f", temp->data.coef); printf("X^"); printf("%d", temp->data.expn ); temp = temp->next; while( temp ) { if( temp->data.coef>0 ) printf(" +"); printf(" %.2f", temp->data.coef ); printf("X^"); printf("%d", temp->data.expn ); temp = temp->next; } printf("\n"); } //求导函数 void DerivationPoly( Poly P ) { Poly rear, temp, q; temp = (Poly) malloc (LEN); if( !temp ) exit(0); temp->next = NULL; rear = temp; if( !P->next ) return; P = P->next; while( P ) { if( P->data.expn ) { q = (Poly) malloc (LEN); q->data.coef = P->data.coef * P->data.expn; q->data.expn = P->data.expn - 1; q->next = rear->next; rear->next = q; rear = q; } P = P->next; } if( !temp->next ) printf("0\n"); else PrintPoly( temp ); } //求多项式的积分 void Inteqral( Poly P ) { Poly rear, temp, q; if( !P->next ) return; P = P->next; //不考虑存在指数为负数的情况 temp = (Poly) malloc (LEN); if( !temp ) exit(0); temp->next = NULL; rear = temp; while( P ) { q = (Poly) malloc (LEN); q->data.coef = P->data.coef/(P->data.expn+1); q->data.expn = P->data.expn+1; q->next = rear->next; rear->next = q; rear = q; P = P->next; } PrintPoly( temp ); } //求给定X的初值求多项式的值 void Evalution( Poly P ) { if( !P->next ) exit(0); double x, sum=0.0; printf("输入x的值:"); scanf("%f", &x); while( P ) { sum += P->data.coef*pow(x,P->data.expn); P = P->next; } printf("多项式的值为:%.2f\n", sum); } //两多项式相加 void AddPoly( Poly &temp, Poly P1, Poly P2 ) { Poly p, rear; temp = (Poly) malloc (LEN); if( !temp ) exit(0); temp->next = NULL; rear = temp; P1 = P1->next; P2 = P2->next; while( P1 && P2 ) { if( P1->data.expn > P2->data.expn ) { p = (Poly) malloc (LEN); p->data.coef = P2->data.coef; p->data.expn = P2->data.expn; p->next = rear->next; rear->next = p; rear = p; P2 = P2->next; } else if( P1->data.expn < P2->data.expn ) { p = (Poly) malloc (LEN); p->data.coef = P1->data.coef; p->data.expn = P1->data.expn; P1 = P1->next; p->next = rear->next; rear->next = p; rear = p; } else if( P1->data.expn == P2->data.expn ) { P1->data.coef += P2->data.coef; if( P1->data.coef )//为零就删除该结点 { p = (Poly) malloc (LEN); p = (Poly) malloc (LEN); p->data.coef = P1->data.coef; p->data.expn = P1->data.expn; P1 = P1->next; P2 = P2->next; p->next = rear->next; rear->next = p; rear = p; } } } if( P2 ) rear->next = P2; if( P1 ) rear->next = P1; if( !temp->next ) printf("0\n");//相加的结果刚好为0 } //两多项式相乘 void Mult(Poly &temp, Poly P1, LNode P2 ) { temp = (Poly) malloc (LEN); if( !temp ) exit(0); temp->next = NULL; Poly rear = temp, p; P1 = P1->next; while( P1 ) { P1->data.coef *= P2.coef; if( P1->data.coef ) { p = (Poly) malloc (LEN); if( !p ) exit(0); p->data.coef = P1->data.coef; p->data.expn = P1->data.expn+P2.expn; p->next = rear->next; rear->next = p; rear = p; } P1 = P1->next; } // PrintPoly(temp); } void MultPoly( Poly P1, Poly P2 ) { if( !P2 && !P1 ) return; Poly sum1, sum2, temp; P2 = P2->next; Mult( temp, P1, P2->data ); sum2 = sum1 = temp; P2 = P2->next; while( P2 ) { sum1 = sum2; Mult( temp, P1, P2->data ); AddPoly( sum2, sum1, temp ); //AddPoly( sum, temp ); P2 = P2->next; } printf("输出相乘后的多项式:"); PrintPoly(sum2); } int main() { Poly p1, p2, temp; CreatePoly( p1 ); printf("输出多项式:\n"); PrintPoly( p1 ); CreatePoly( p2 ); printf("输出多项式:\n"); PrintPoly( p2 ); MultPoly( p1, p2 ); // AddPoly(temp, p1, p2 ); // printf("输出相加后的多项式:\n"); // PrintPoly( temp ); /* printf("输出对多项式求导的结果:\n"); DerivationPoly( p1 ); printf("输出对多项式积分的结果:\n"); Inteqral( p1 ); */ // Evalution( p1 ); return 0; } |
这题怎样编程?
请设计一个数据结构,实现多项式的操作,具体模式如下:
1) 通过键盘输入或者文件输入模式接受多项式,并识别出相关的多项式;
2) 建立多项式;
3) 完成多项式f(x) 的导数、积分运算;
4) 根据x的取值,求出多项式的值;
5) 实现两个多项式f(x)、h(x)的加法/减法、乘法;
6) 比较两个多项式f(x)、h(x)的值的大小,并给定其具体取值范围,f(x) > g(x);
7) 建立一系列多项式,并实现3)~5)的操作。
算了,没望了