【问题描述】
用 C 语言设计并实现一个一元稀疏多项式的简单计算器。
【基本要求】
一元稀疏多项式简单计算器的基本功能是:
输入并建立多项式
输出多项式,序列按指数降序排列
多项式 A(x)和 B(x)相加,并建立多项式 A(x)+B(x)
多项式 A(x)和 B(x)相减,并建立多项式 A(x)-B(x)
给定 x 的值,计算多项式。
代码如下:
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
typedef struct term { //项的表示,多项式的项作为 LinkList 的数据元素
oat coef; //系数
int expn; //指数
struct term *next;
}term;
term* CreatPolyn(term *P,int m) { // 算法 2.22
// 输入 m 项的系数和指数,建立表示一元多项式的有序链表 P
if(m <= 0) return NULL;
term *h = P = (term*)malloc(sizeof(term)), *q;
P->coef = 0.0;
int i;
printf("依次输入%d 个数(前一个为系数,后一个为指数)",m*2);
for (i = 1; i <= m; ++i) { // 依次输入 m 个非零项
scanf("%f%d",&P->coef,&P->expn);
if(P->coef)
q = P;
P = P->next = (term*)malloc(sizeof(term));
}
q->next = NULL;
free(P);
return h;
} // CreatPolyn
term* selsort(term *h) {
term *g, *p, *q;
if(!h) return NULL;
oat f;
int i, 6ni = 1;
评论6
最新资源