如何用C语言实现一元多项式简单计算器的设计〔问题描述〕输入并建立两个多项式并输出多项式设计一个程序:对两个多项式进行加、减法及乘法运算,建立一个新多项式并输出.或设计一个

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 12:20:04
如何用C语言实现一元多项式简单计算器的设计〔问题描述〕输入并建立两个多项式并输出多项式设计一个程序:对两个多项式进行加、减法及乘法运算,建立一个新多项式并输出.或设计一个

如何用C语言实现一元多项式简单计算器的设计〔问题描述〕输入并建立两个多项式并输出多项式设计一个程序:对两个多项式进行加、减法及乘法运算,建立一个新多项式并输出.或设计一个
如何用C语言实现一元多项式简单计算器的设计
〔问题描述〕输入并建立两个多项式并输出多项式设计一个程序:对两个多项式进行加、减法及乘法运算,建立一个新多项式并输出.或设计一个程序对其中一个多项式求导.〔实现提示〕 选择带头结点的单链表或循环链表存储多项式,头结点中存放多项式的参数.此单链表的数据结构为:typedef struct pdode{ float coef ; /* 系数域*/ int expn ; /* 指数域*/ struct pnode *next ; /* 链域*/ } pnode ,*polylink

如何用C语言实现一元多项式简单计算器的设计〔问题描述〕输入并建立两个多项式并输出多项式设计一个程序:对两个多项式进行加、减法及乘法运算,建立一个新多项式并输出.或设计一个
编了两天,希望楼主打赏点财富值
#include
#include
#include
struct polylink
{
\x09char ch; /*变量名*/
\x09float coef; /* 系数域*/
\x09int expn; /* 指数域*/
\x09struct polylink *next; /* 链域*/
};
/*初始化链表*/
polylink *initpoly()
{
\x09polylink *p;
\x09p = (polylink *)malloc(sizeof(polylink));
\x09if(p == NULL)
\x09{
\x09\x09printf("memory error!");
\x09\x09exit(0);
\x09}
\x09p->next = NULL;
\x09return p;
}
/*销毁链表*/
void destroy_link(polylink *p)
{
\x09polylink *q;
\x09while(p != NULL)
\x09{
\x09\x09q = p;
\x09\x09p = p->next;
\x09\x09free(q);
\x09}
}
/*将多项式存入链表*/
polylink * creat_ploy(polylink *p)
{
\x09char c[1];
\x09int i = 0, n=0;
\x09polylink *p1;
\x09p = initpoly();
\x09printf("input variable's name 输入变量名:\n");
\x09scanf("%s", c);\x09\x09
\x09printf("input the polynomial's number 输入多项式项数:\n");
\x09scanf("%d", &n);
\x09for(i = 0; i < n; i++)
\x09{
\x09\x09p1 = (polylink *)malloc(sizeof(polylink));
\x09\x09if(p1 == NULL)/*分配不成功时*/
\x09\x09{
\x09\x09\x09printf("memory error!");
\x09\x09\x09exit(0);
\x09\x09}
\x09\x09printf("input the %dst's coefficient 输入第%d项的系数:\n", i+1, i+1);
\x09\x09scanf("%f", &p1->coef);
\x09\x09printf("input the variable's index 输入变量的指数:\n");
\x09\x09scanf("%d", &p1->expn);
\x09\x09p1->ch = c[0];
\x09\x09p1->next = p->next;
\x09\x09p->next = p1;
\x09}
\x09return p;
}
/*删除节点函数*/
polylink *delnode(polylink * h , polylink * maxp) /*删除一个节点,返回剩下的链表首地址*/
{
\x09polylink *t;
\x09t = h;
while(t->next != maxp )
\x09\x09t=t->next;\x09\x09\x09\x09 /*找到maxp 的前节点t */
\x09t->next = maxp->next ; /*删除maxp,maxp后面的接到t后面*/
maxp->next = NULL;
return h; /*链首依然是h ,返回 */
}
/*求表长函数*/
void link_lenth(polylink *p, int &a)/* &是引用*/
{
\x09while(p->next != NULL)
\x09{
p = p->next;
\x09\x09a++;
\x09}
}
/*排序*/
polylink *sort(polylink *h)
{
\x09int min; /*保存指数最小的值*/
\x09polylink *t=NULL, *minp=NULL, *head=NULL;
\x09if(h ->next == NULL)
\x09{
\x09\x09printf("don't exist number");
\x09\x09exit(0);
\x09}
while(h->next !=NULL) /*只要当前链中不为空就循环 */
{
t= h->next; // t :临时指针
min=t->expn; minp=t; /*把当前t中的值作为最小*/
while (t->next !=NULL) /*只要t后面还有节点就循环*/
{
\x09\x09\x09t=t->next ; /*t往后移动一个*/
if (t->expn < min) /*如果t中的值大于maxn,则记下其值和位置*/
{
\x09\x09\x09\x09min = t->expn;
minp=t;
\x09\x09\x09}
} /*找出当前头开始在链中最大节点 maxp*/
h = delnode(h, minp); /*删除minp节点,返回剩下的链表*/
minp->next=head; /*将每次的最小节点接在头结点之前*/
\x09\x09head = minp;\x09 /*head 重新回到该链的开头*/
\x09}
\x09h->next = head; /*将head 接在头节点之后*/
\x09return h;
}
/*对一元多项式求导*/
polylink *poly_derivation(polylink *p)
{
\x09polylink *n, *q;
\x09q= p;
\x09if(p->next == NULL)
\x09{
\x09\x09printf("polynomial don't exist!\n");
\x09}
\x09while(p->next != NULL)
\x09{
\x09\x09p = p->next;
\x09\x09if(p->expn == 0 || p->coef == 0) /*指数为零或者系数为零就删除*/
\x09\x09{
\x09\x09\x09q = delnode(q, p);
\x09\x09}
\x09}
\x09n = q; /*保存删除无用节点的链表之后的头结点, 用于返回*/
\x09if(q->next == NULL)
\x09{
\x09\x09printf("polynomial don't exist!\n");
\x09}
\x09while(q->next != NULL)
\x09{
\x09\x09q = q->next;
\x09\x09q->coef = q->coef * q->expn; /*系数乘指数*/
\x09\x09q->expn = q->expn - 1;\x09 /*指数减一*/
\x09}
\x09return n;
}
/*显示多项式*/
void display_poly(polylink *p)
{
\x09int a;
\x09p = sort(p); /*排序*/
\x09while(p->next != NULL )
\x09{
\x09\x09p = p->next;
\x09\x09a = 1;
\x09\x09if(p->coef < 0) a = 0;
\x09\x09a ? printf(" +"): printf(" ");
\x09\x09printf("%.1f%c^%d", p->coef, p->ch, p->expn);
\x09}
\x09printf("\n");
}
/*查找函数*/
polylink *locate_link(polylink *p, int index)
{
\x09
\x09p = p->next;
\x09while(p != NULL && p->expn != index)
\x09\x09p = p->next;
\x09if(p == NULL)
\x09\x09return NULL;
\x09else
\x09 return p;
}
/*为了代码重用*/
void creat(polylink *&p1, polylink *&p2)
{
\x09p1 = creat_ploy(p1);\x09\x09\x09\x09/*创建多项式*/
\x09printf("another polynomial building:\n");
\x09p2 = creat_ploy(p2);\x09\x09\x09\x09/*创建多项式*/
\x09printf("first :\n");
\x09display_poly(p1);\x09\x09\x09\x09\x09/*显示创建的多项式*/
\x09printf("the second:\n");
display_poly(p2);\x09\x09\x09\x09\x09/*显示创建的多项式*/
}
/*多项式相加*/
polylink * poly_add(polylink *head1, polylink *head2, char a)
{
\x09char ch[1];
\x09polylink *p1, *p2, *p, *r;
\x09p1 = head1;
\x09p2 = head2;
\x09if(a == '+')\x09\x09/*其他函数调用时不执行*/
\x09{
\x09\x09system("cls");
\x09\x09creat(p1, p2);\x09/*创建p1 p2*/
\x09}
\x09r = p1;
\x09while(r->next != NULL)
\x09{
\x09\x09p1 = r->next;
\x09\x09r = delnode(r, p1);\x09\x09\x09\x09/*删除p1*/
\x09\x09p = locate_link(p2, p1->expn); /*查找p2中是否有 系数等于p1->expn的节点,有就返回该点,否则返回NULL*/
\x09\x09if(p != NULL)
\x09\x09\x09p->coef = p->coef + p1->coef;
\x09\x09else\x09\x09\x09/*把节点p1插入到链表p2*/
\x09\x09{
\x09\x09\x09
\x09\x09\x09p1->next = p2->next;
\x09\x09\x09p2->next = p1;
\x09\x09}
\x09}
\x09if(a == '+') /*被减或者乘运算时不调用*/
\x09{
\x09\x09printf("add result:\n");
\x09 display_poly(p2); /*显示*/
\x09\x09printf("whether to derivat:y/n\n");
\x09\x09scanf("%s", ch);
\x09\x09if( ch[0] == 'Y' || ch[0] == 'y')
\x09\x09{
p2 = poly_derivation(p2);
\x09\x09 display_poly(p2);
\x09\x09\x09destroy_link(p2); /*销毁链表*/
\x09\x09}
\x09\x09else
\x09\x09\x09destroy_link(p2);/*销毁链表*/
\x09\x09getchar();/*让屏幕显示停在运算结果*/
\x09\x09return NULL;
\x09}
\x09return p2;
}
/*多项式相减*/
void poly_sub(polylink *p1, polylink *p2)
{
\x09char ch[1];
polylink *n, *p;
\x09system("cls"); /*清屏*/
\x09creat(p1, p2);
\x09p = p2;
\x09while(p->next != NULL)
\x09{
\x09\x09p = p->next;
\x09\x09p->coef = -1 * p->coef;
\x09}
\x09printf("\nsub 1st - 2st\n");
\x09n = poly_add(p1, p2, '-'); /*调用加运算函数*/
\x09display_poly(n);
\x09printf("whether to derivat:y/n\n");
\x09scanf("%s", ch);
\x09if( ch[0] == 'Y' || ch[0] == 'y')\x09
\x09{
n = poly_derivation(n); /*求导*/
\x09 display_poly(n);
\x09\x09destroy_link(n);
\x09}
\x09else destroy_link(n);
\x09getchar();
}
/*多项式相乘*/
polylink *multiply(polylink *p, float coef, int index)
{\x09\x09\x09\x09\x09\x09/*传入系数和指数,分别与p的每个节点运算 最后返回该链*/
\x09polylink *q, *n;
\x09n = (polylink *)malloc(sizeof(polylink));
\x09n->next = NULL;\x09\x09\x09/*n作为头节点*/
\x09while(p->next != NULL)
\x09{
\x09\x09p = p->next;
\x09\x09q = (polylink *)malloc(sizeof(polylink));
\x09\x09q->ch = p->ch;
\x09\x09q->coef = coef * p->coef;
\x09\x09q->expn = index + p->expn;
\x09\x09q->next = n->next;
\x09\x09n->next = q;\x09\x09\x09
\x09}
\x09return n;
}
/*多项式相乘所用,另外为了提高代码重用性*/
void run_mul(polylink *p1, polylink *p2, int num)
{
\x09int i = 0;
\x09char ch[1];
\x09polylink **p, *q; /*二维指针存每次multiply()函数返回的链表*/
\x09p = (polylink **)malloc( num *sizeof(polylink));
while(p1->next != NULL)
\x09{
\x09\x09p1 = p1->next;
\x09\x09p[i++] = multiply(p2, p1->coef, p1->expn);
\x09}
\x09q = poly_add(p[0], p[1], '*');
\x09for(i = 2; i < num; i++)
\x09{
\x09 q = poly_add(q, p[i], '*');
\x09}
printf("mul result:\n");
\x09display_poly(q);
\x09printf("whether to derivat:y/n\n");
\x09scanf("%s", ch);
\x09if( ch[0] == 'Y' || ch[0] == 'y')
\x09{
q = poly_derivation(q);
\x09\x09 display_poly(q);
\x09\x09 destroy_link(q);
\x09}
\x09else destroy_link(q);
\x09getchar();
}
void poly_mul(polylink *p1, polylink *p2)
{
\x09int num1 =0 , num2 = 0;\x09\x09/*用于存放p1 ,p2的表长*/
\x09system("cls");
\x09creat(p1, p2);
link_lenth(p1, num1);
\x09link_lenth(p2, num2);
\x09if(num2 >= num1) /*根据链表*/
\x09{
\x09\x09run_mul(p1, p2, num1);
\x09}
\x09else
\x09{
\x09\x09run_mul(p2, p1, num2);
\x09}
}
/*选择函数*/
int menu_select()
{
\x09int n;
\x09printf("press enter key to enter the menu."); /*按任一键进入主菜单*/
\x09getchar(); /*从键盘读取一个字符,但不显示于屏幕*/
system("cls"); /*清屏*/
printf("\n\n\n\n\n\n");
\x09printf("\t\t\tIt just for a variables\n\n");
\x09printf("\t\n\n");
\x09do{
\x09\x09\x09printf("\n\t输入你的选择Enter your choice(1~4):");
\x09\x09\x09scanf("%d",&n);
\x09}while(n < 1 || n > 4); /*如果选择项不在1~4之间则重输*/
\x09return n; /*返回选择项,主函数根据该数调用相应的函数*/
}
void main()
{
\x09polylink *head1=NULL, *head2=NULL, *head3=NULL; /*head3的作用不是很大*/

\x09for(;;) /*循环无限次*/
\x09{
\x09\x09switch( menu_select() )
\x09\x09{
\x09\x09\x09case 1: head3 = poly_add(head1, head2, '+'); break;
\x09\x09\x09case 2: poly_sub(head1, head2); break;
\x09\x09\x09case 3: poly_mul(head1, head2); break;
\x09\x09\x09case 4: exit(0); /*如菜单返回值为4则程序结束*/
\x09\x09}
\x09}
}

如何用C语言实现一元多项式简单计算器的设计〔问题描述〕输入并建立两个多项式并输出多项式设计一个程序:对两个多项式进行加、减法及乘法运算,建立一个新多项式并输出.或设计一个 一元稀疏多项式简单的计算器 高分悬赏,设计一个一元多项式简单的计算器(fortran语言)问题描述:设计一个一元多项式简单的计算器.基本要求:一元多项式简单计算器的基本功能为:(1) 输入并建立多项式;(2) 输 如何用简单的C语言实现带有括号的四则运算 如何用C语言实现两个一元多项式的相加和相乘?就是说,加法时如何将它们的同类项相加,乘法时将每一项都与另一个一元多项式相乘.用C语言描述出来,如果可以麻烦将程序大致描述一下. 数据结构(C语言)用单链表存储一元多项式,并实现两个多项式的相加运算 设计一个一元多项式简单的计算器(C语言)要求:(1)用带头节点的单链表表示多项式,表中每一个节点表示多项式的一项 (2)一元多项式简单计算器的基本功能为:输入并建立多项式,输出 如何用C语言实现2的n次方 如何用C语言实现NFA向DFA的转换 设计一个一元多项式简单的计算器(数据结构C语言版)急要求:一元多项式计算器的基本功能定为 (1) 建立多项式 (2) 输出多项式 (3) 两个多项式相加,建立并输出和多项式 (4) 两个多项式相减 数据结构(C语言) 如何分别用顺序和链式 实现一元多项式的相加? 如何用C语言实现n元多项式乘法急求详细代码,最好是两个三元二次多项式的乘法的详细代码,急啊,提前向各位大侠道谢了!C语言的,要能运行. C语言计算器问题解决,要实现简单的四则运算,输入一个式子,可以返回运算结果如:输入 4+3= 然后输出结果 一元稀疏多项式计算器设计任务:设计一个一元稀疏多项式简单计算器.设计要求:一元稀疏多项式简单计算器的基本功能是:(1)输入并建立多项式——creatpolyn();(2)输出多项式,输出形式为 如何用科学计算器解一元二次方程 如何用C语言实现多项式的加法和乘法给定两个多项式,用程序实现这两个多项式的相加和相乘.要求多项式的系数只能取1或者0;同时满足1+1=0;例如给定多项式(1+X)*(1+X)=X2.基本要求:(1)实 用C语言编程实现一个简单的四则运算计算器编程:编程实现一个简单的四则运算计算器:从键盘输入一个四则运算表达式(没有空格和括号),遇等号=说明输入结束,输出结果.假设计算器只能 如何用C#制作简单的计算器,思路是什么啊