mutishi addition

#include<stdio.h>
#include<stdlib.h>
struct nominal{
	float num;
	int level;
	struct nominal* next;
};
struct nominal* creat (int n);
struct nominal* add(struct nominal*p,struct nominal*q);
void show(struct nominal*);
void putin(float num0,int level0,struct nominal*rr);
int main(void){
	struct nominal* h1,*h2,*h3;
	h1=creat(2);
	h2=creat(1);
	h3=add(h1,h2);
	show(h3);
	return 0;
}
struct nominal* creat(int n){
	struct nominal* guard,*p,*p0;
	guard=(struct nominal*)malloc(sizeof(struct nominal));
	p0=guard;
	while(n!=0){
		p=(struct nominal*)malloc(sizeof(struct nominal));
		scanf("%f",&p->num);
		scanf("%d",&p->level);
		p->next=NULL;
		p0->next=p;
		p0=p;
		n--;
	}
	return guard->next;
}
void show(struct nominal* head){
	while(head!=NULL){
		printf("%.2f x %d\n",head->num,head->level);
		head=head->next;
	}
}
struct nominal* add(struct nominal* p,struct nominal*q){
	struct nominal *g,*r0;
	g=(struct nominal*)malloc(sizeof(struct nominal));
	r0=g;
	while(p!=NULL&&q!=NULL){
		if(p->level>q->level){
			putin(p->num,p->level,r0);
			p=p->next;
			r0=r0->next;
		}
		else if(p->level<q->level){
			putin(q->num,q->level,r0);
			q=q->next;
			r0=r0->next;
		}
		else{
			putin(p->num+q->num,p->level,r0);
			p=p->next;
			q=q->next;
			r0=r0->next;
		}
	}
	while(p!=NULL){
		putin(p->num,p->level,r0);
		r0=r0->next;
		p=p->next;
	}
	while(q!=NULL){
		putin(q->num,q->level,r0);
		r0=r0->next;
		q=q->next;
	}
	return g->next;
}
void putin(float num0,int level0,struct nominal* rr){
	struct nominal* r;
	r=(struct nominal*)malloc(sizeof(struct nominal));
	r->num=num0;
	r->level=level0;
	rr->next=r;
	r->next=NULL;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值