博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言链表实现一元多项式的加减乘运算
阅读量:4963 次
发布时间:2019-06-12

本文共 4275 字,大约阅读时间需要 14 分钟。

#include 
#include
#define SHI sizeof(struct duoxiangshi) #define NULL 0 struct duoxiangshi {
int xishu,zhishu; struct duoxiangshi*next; }; struct duoxiangshi*create() {
struct duoxiangshi*head,*p1,*p2; int n; n=0; head=NULL; p2=NULL; p1=NULL; p1=(struct duoxiangshi*)malloc(SHI); scanf("%d %d",&p1->xishu,&p1->zhishu); while(p1->xishu!=NULL) {++n; if(n==1){head=p1;p2=p1;} else{p2->next=p1;p2=p1,p2->next=NULL;} p1=(struct duoxiangshi*)malloc(SHI); scanf("%d %d",&p1->xishu,&p1->zhishu); p1->next=NULL; } free(p1); return (head); } void paixu(struct duoxiangshi*head) {
struct duoxiangshi*p1,*p2; int t=0; p1=head; p2=head; for( ; ;p1=p1->next) { if(p1->next==NULL)break; for(p2=p1->next; ;p2=p2->next) { if(p1->zhishu>p2->zhishu) { t=p1->zhishu; p1->zhishu=p2->zhishu; p2->zhishu=t; t=p1->xishu; p1->xishu=p2->xishu; p2->xishu=t; } if(p2->next==NULL)break; } } } struct duoxiangshi*add(struct duoxiangshi*head1,struct duoxiangshi*head2) {
struct duoxiangshi*p1,*p2,*s,*p3,*head3; int n=1; p1=head1; p2=head2; head3=NULL; p3=NULL; while(p1!=NULL&&p2!=NULL) {s=(struct duoxiangshi*)malloc(SHI); ++n; if(n==1) head3=s; if(p1->zhishu>p2->zhishu) {s->zhishu=p2->zhishu; s->xishu=p2->xishu; p2=p2->next; } else if(p1->zhishu
zhishu) {s->zhishu=p1->zhishu; s->xishu=p1->xishu; p1=p1->next; } else {s->zhishu=p1->zhishu; s->xishu=p1->xishu+p2->xishu; p1=p1->next; p2=p2->next; } p3->next=s; p3=s; } p3->next=NULL; if(p1!=NULL) p3->next=p1; if(p2!=NULL) p3->next=p2; return (head3); } struct duoxiangshi* minus(struct duoxiangshi*head1,struct duoxiangshi*head2) {
struct duoxiangshi*p2; p2=head2; while(p2!=NULL) {p2->xishu=(-1)*p2->xishu; p2=p2->next; } return (add(head1,head2)); } struct duoxiangshi* multiply(struct duoxiangshi*px,struct duoxiangshi*py) { int n=0,i=0,j=0; struct duoxiangshi*heada,*headb,*pa,*pb,*lasta,*lastb,*p2,*p1; p1=heada=headb=pa=pb=lasta=lastb=NULL; p2=py; while (p2!=NULL) {n++; if(n==1) { p1=px; while(p1!=NULL) {i++; pa=(struct duoxiangshi*)malloc(SHI); if(i==1) heada=pa; else lasta->next=pa; lasta=pa; pa->xishu=p1->xishu*p2->xishu; pa->zhishu=p1->zhishu+p2->zhishu; pa->next=NULL; p1=p1->next; } } else { p1=px; while(p1!=NULL) {j++; pb=(struct duoxiangshi*)malloc(SHI); if(j==1) headb=pb; else lastb->next=pb; lastb=pb; pb->xishu=p1->xishu*p2->xishu; pb->zhishu=p1->zhishu+p2->zhishu; pb->next=NULL; p1=p1->next; } } p2=p2->next; if(n!=1)heada=add(heada,headb); } return(heada); } void print (struct duoxiangshi*head3) {
struct duoxiangshi*p3; p3=head3; do { if(p3->zhishu==0) {printf("%d",p3->xishu); p3=p3->next; continue; } else if(p3==head3) {printf("%dx%d",p3->xishu,p3->zhishu); p3=p3->next; continue; } else if(p3->xishu<0) printf("%dx%d",p3->xishu,p3->zhishu); else if(p3->xishu>0) printf("+%dx%d",p3->xishu,p3->zhishu); p3=p3->next; }while(p3!=NULL); printf("\n\n"); } void main() {
struct duoxiangshi*p1,*p2,*p3; int c=0;char d; do {
printf("please input A:\n"); p1=create(); paixu(p1); printf("\nthe A is:"); print(p1); printf("please input B:\n"); p2=create(); paixu(p2); printf("\nthe B is:"); print(p2); printf("\n please choice :\n"); printf("*********************************\n"); printf("1.add 2.minu 3.multiply\n"); printf("*********************************\n"); scanf("%d",&c); switch(c) {
case 1:p3=add(p1,p2);break; case 2:p3=minus(p1,p2);break; case 3:p3=multiply(p1,p2);break; default:printf("\nerror"); } printf("\nthe result is :"); print(p3); printf("do you still have anything to do?(y/n):\n"); scanf("%c",&d); getchar(); clrscr(); }while(d!='n'&&d!='N'); }

 

转载于:https://www.cnblogs.com/wonderKK/archive/2011/11/07/2240390.html

你可能感兴趣的文章
1.- Netty设计理念-异步和事件驱动
查看>>
原始线性结构数组的实现以及操作
查看>>
css之浮动
查看>>
js的实例方法和静态方法分析
查看>>
CSS 3D旋转 hover 后设置transform 是相对于正常位置
查看>>
VIM 模板
查看>>
[转]JAVA设计模式之单例模式
查看>>
MyBatis学习总结
查看>>
1011-大数加法
查看>>
c#向指定的邮箱发送邮件
查看>>
Hibernate4.x之映射关系--单向一对多
查看>>
轮播插件unslider.min.js使用demo
查看>>
SQL Server学习之路(四):“增删改查”之“删”
查看>>
给windows系统下的linux系统的虚拟机安装jdk
查看>>
html5实践篇之drap and drop
查看>>
Python3.x urlib包
查看>>
Windows 10 安装 Sql Server 2014 反复提示需要安装 .NET Framework 3.5 SP1 的解决方案
查看>>
POJ 3253
查看>>
Linux的IO调度
查看>>
Session.use_trans_sid Session值跨脚本(跨页面)传递
查看>>