一元稀疏多项式计算器源代码

更新时间:2024-06-03 04:35:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include #include typedef struct node {

float coef; int exp;

struct node *next; }Lnode, *polynmial;

void create(polynmial &L); //输入并建立多项式L void display(polynmial L); //显示,输出多项式L void sort(polynmial &L); //多项式L按指数排序 void reverse(polynmial &L); //逆置 void select(); //用户选择加减操作

void add(polynmial La, polynmial Lb, polynmial &Lc); //多项式void subtract(polynmial La, polynmial Lb, polynmial &Ld); //多项式

void create(polynmial &L) //输入并建立多项式L {

int i, n;

static struct node *p; scanf(\

L = (struct node *)malloc (sizeof(struct node)); L->next = NULL; for(i = 0; i < n; i++) {

p = (struct node *)malloc(sizeof(struct node)); scanf(\ p->next = L->next; L->next = p; } }

void display(polynmial L)//显示,输出多项式L {

struct node *p, *q; int flag = 0; int k = 0; q = L->next; while(q) {

if(q->coef != 0) k++;

q = q->next;

La,Lb相加

La减去LbLd ,结果给

}

printf(\p = L->next; if(p->coef != 0) {

printf(\ flag++; }

for(p = p->next; p; p = p->next) {

if(p->coef != 0) {

printf(\ flag++; } }

if(flag == 0)

printf(\else

printf(\}

void sort(polynmial &L)//多项式L按指数排序 {

polynmial p, q, r, u; p = L->next;

L->next = NULL; while(p != NULL) {

r = L;

q = L->next;

while((q != NULL) && (q->exp <= p->exp)) {

r = q;

q = q->next; }

u = p->next; r->next = p; p->next = q; p = u; } }

void reverse(polynmial &L)//逆置

{

polynmial H;

static struct node *p, *q, *s;

H = (struct node*)malloc(sizeof(struct node)); H->next = NULL;

p = (struct node*)malloc(sizeof(struct node)); s = L->next;

p->coef = s->coef; p->exp = s->exp; p->next = s->next; while(s) {

p->coef = s->coef; p->exp = s->exp; p->next = s->next; q = H->next; H->next = p; p->next = q;

p = (struct node*)malloc(sizeof(struct node)); s = s->next; }

p = H->next; q = L->next; while(p) {

q->coef = p->coef; q->exp = p->exp; q = q->next; p = p->next; } }

void select() //用户选择加减操作 {

printf(\请选择加减操作\\n\

printf(\两个一元多项式相加\\n\printf(\两个一元多项式相减\\n\}

void add(polynmial La, polynmial Lb, polynmial &Lc)//{

struct node *pa, *pb; static struct node *pc;

Lc = (struct node*)malloc(sizeof(struct node));

La,Lb多项式相加

pa = La->next; pb = Lb->next; Lc->next = NULL; while(pa && pb) {

pc = (struct node*)malloc(sizeof(struct node)); if(pa->exp < pb->exp) {

pc->next = Lc->next; Lc->next = pc;

pc->coef = pa->coef; pc->exp = pa->exp; pa = pa->next; } else

if(pa->exp == pb->exp) {

pc->next = Lc->next; Lc->next = pc; pc->exp = pa->exp;

pc->coef = pa->coef + pb->coef; pa = pa->next; pb = pb->next; } else {

pc->next = Lc->next; Lc->next = pc;

pc->coef = pb->coef; pc->exp = pb->exp; pb = pb->next; } }

while(pa) {

pc = (struct node*)malloc(sizeof(struct node)); pc->next = Lc->next; Lc->next = pc;

pc->coef = pa->coef; pc->exp = pa->exp; pa = pa->next; }

while(pb) {

pc = (struct node*)malloc(sizeof(struct node)); pc->next = Lc->next; Lc->next = pc;

pc->coef = pb->coef; pc->exp = pb->exp; pb = pb->next; } }

void subtract(polynmial La, polynmial Lb, polynmial &Ld)//多项式{

struct node *pa, *pb; static struct node *pd;

Ld = (struct node*)malloc(sizeof(struct node)); pa = La->next; pb = Lb->next; Ld->next = NULL; while(pa && pb) {

pd = (struct node*)malloc(sizeof(struct node)); if(pa->exp < pb->exp) {

pd->next = Ld->next; Ld->next = pd;

pd->coef = pa->coef; pd->exp = pa->exp; pa = pa->next; } else

if(pa->exp == pb->exp) {

pd->next = Ld->next; Ld->next = pd; pd->exp = pa->exp;

pd->coef = pa->coef - pb->coef; pa = pa->next; pb = pb->next; } else {

pd->next = Ld->next; Ld->next = pd;

pd->coef = pb->coef; pd->exp = pb->exp;

La减去Lb,结果给Ld

pb = pb->next; } }

while(pa) {

pd = (struct node*)malloc(sizeof(struct node)); pd->next = Ld->next; Ld->next = pd;

pd->coef = pa->coef; pd->exp = pa->exp; pa = pa->next; }

while(pb) {

pd = (struct node*)malloc(sizeof(struct node)); pd->next = Ld->next; Ld->next = pd;

pd->coef = -pb->coef; pd->exp = pb->exp; pb = pb->next; } }

int main() {

int sign;

polynmial La, Lb, Lc, Ld;

printf(\请输入第一个多项式:\\n\create(La); sort(La);

printf(\请输入第二个多项式:\\n\create(Lb); sort(Lb);

select();

scanf(\switch(sign) {

case 1:

printf(\多项式之和为:\\n\ add(La, Lb, Lc); sort(Lc);

reverse(Lc); display(Lc); break; default:

printf(\多项式之差为:\\n\ subtract(La, Lb, Ld); sort(Ld); reverse(Ld); display(Ld); break; } int a;

printf(\是否继续进行运算?scanf(\if(a==1) main(); else if (a==0) return 0; }

,是;2,否\1

本文来源:https://www.bwwdw.com/article/1ve6.html

Top