⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 math_number1.cpp

📁 计算一切超大数据功能,包括所有实数的一些基本的运算(不足之处望网友多多指教)
💻 CPP
字号:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
	char ch;
	int number;
	struct node *next;
	struct node *prior;
}sjd;
//======================================================
//初始
void init(sjd **jd)
{
	if(((*jd)=(sjd*)malloc(sizeof(sjd)))==NULL)
	{
		printf("磁盘不足!\n");
		exit(1);
	}
	(*jd)->ch='0';
	(*jd)->next=NULL;
	(*jd)->prior=NULL;
	(*jd)->number=0;
}
//======================================================
//插入(带头链表)
int insert(sjd *h,char ch,int pos)
{
	int i;
	sjd *p,*q,*t,*cr;
	init(&p);
	p->ch=ch;
	for(i=1,cr=h,q=h->next;q!=NULL && i<pos;i++,q=q->next,cr=cr->next);
	if(i!=pos)
	{
		printf("输入参数有误!\n");
		return 0;
	}
	p->number=pos;
	for(t=q;t!=NULL;t=t->next)
		t->number+=1;
	if(q==NULL)
	{
		p->next=q;
		cr->next=p;
		p->prior=cr;
		return 1;
	}
	p->next=q;
	p->prior=q->prior;
	q->prior->next=p;
	q->prior=p;
	return 1;
}
//======================================================
//数据修改
void mend(sjd *h,int pos,char ch)
{
	sjd *p;
	for(p=h->next;p!=NULL && p->number!=pos;p=p->next);
	if(p==NULL || p->number!=pos)
	{
		printf("输入参数有误!\n修改失败!\n");
		return;
	}
	p->ch=ch;
}
//======================================================
//数据查找
char search(sjd *h,int pos)
{
	sjd *p;
	for(p=h;p->next!=NULL;p=p->next);  //初始p
	while(p->number!=pos)
	{
		p=p->prior;
		if(p==NULL)          //说明该结点中没有数据
			return 'a';   
	}
	if(p->prior==NULL)   //说明是头
		return '\0';
	return p->ch;        //说明该结点中有数据
}
//======================================================
//数据显示
void display(sjd *h)
{
	sjd *p;
	for(p=h;p->next!=NULL;p=p->next);  //初始p
	printf("这个数据是:");
	if(p->prior==NULL)
	{
		printf("无信息!\n");
		return;
	}
	for(;p->prior->prior!=NULL;p=p->prior)
		printf("%c",p->ch);
	printf("%c\n",p->ch);
}
//显示计数
void displayjs(sjd *h)
{
	sjd *p;
	printf("这个数据是:");
	for(p=h->next;p->next!=NULL;p=p->next)
		printf("%d",p->number);
	printf("%d\n",p->number);
}
//====================================================
//计数倒立
void jsdl(sjd *h)
{
	sjd *p,*q;
	int t;
	for(q=h;q->next!=NULL;q=q->next);  //初始q
	p=h;            //初始p
	while(p!=q && p->next!=q && p->prior!=q)
	{
		t=p->number;
		p->number=q->number;
		q->number=t;
		p=p->next;
		q=q->prior;
	}	
	t=p->number;
	p->number=q->number;
	q->number=t;
}
//======================================================
//数据倒立
void sjdl(sjd *h)
{
	sjd *p,*q;
	char t;
	for(q=h;q->next!=NULL;q=q->next);  //初始q
	p=h->next;            //初始p
	if(p==NULL)
	{
		printf("无数据!\n");
		return;
	}
	while(p!=q && p->next!=q && p->prior!=q)
	{
		t=p->ch;
		p->ch=q->ch;
		q->ch=t;
		p=p->next;
		q=q->prior;
	}	
	t=p->ch;
	p->ch=q->ch;
	q->ch=t;
}
//======================================================
//复制数据
sjd *fzsj(sjd *x)
{
	sjd *y,*p=x->next,*q,*t;
	init(&y);
	t=y;
	y->number=x->number;
	while(p!=NULL)
	{
		init(&q);
		q->ch=p->ch;
		q->number=p->number;
		q->next=t->next;
		q->prior=t;
		t->next=q;
		p=p->next;
		t=t->next;
	}
	return y;
}
//======================================================
//删除数据(记数倒立后使用,按位删除)               !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void del(sjd *h,int i)
{
	sjd *p=h;
    while(p!=NULL && i<p->number)                 //(记数倒立后)
		p=p->next;
	if(p==h)
	{
		h=p->next;
		h->prior=NULL;
		free(p);
		printf("可能在删除数据时误删了头!\n");
		exit(1);
	}
	else if(p->next==NULL)
	{
		p->prior->next=NULL;
		free(p);
	}
	else
	{
		p->prior->next=p->next;
		p->next->prior=p->prior;
		free(p);
	}
}
//======================================================
//数据清理              (原始数据进入)                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void ql(sjd *h)
{
	sjdl(h);
	jsdl(h);
	sjd *p=h->next,*q=h;
	if(p==NULL)
	{
		printf("无数据!\n");
		return;
	}
	if(p->next==NULL)
	{
		sjdl(h);
		return;
	}
	if(p->ch=='-')
		p=p->next;
	if(p->ch=='0' && p->next!=NULL && p->next->ch=='0' && p->next->next==NULL)
	{
		del(h,p->number);
		h->number--;
		sjdl(h);
		return;
	}
	while(p->ch=='0')
	{
		del(h,p->number);
		h->number--;                        //(记数倒立后)
		if(q->next==NULL || q->next->next==NULL)
			break;
		p=q->next;
		q=q->next;
	}
	sjdl(h);
}
//======================================================
//撤消数据
void freesjd(sjd *h)
{
	sjd *p=h,*q=h;
	while(p!=NULL)
	{
		p=p->next;
		free(q);
		q=p;
	}
}
//====================================================== 
//取长为固定s的子串                           !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sjd *qzc(sjd *x,int begin,int length)
{ 
	sjd *y;
	int i,j=0;
	jsdl(x);
	init(&y);
	for(i=begin;i<begin+length;i++)       //(记数原始) 
	{
		if(!search(x,i))
			break;
		insert(y,search(x,i),++j);
	}
	jsdl(x);
	return y;
}
//======================================================
//判断串的大小
int pdcdx(sjd *x,sjd *y)
{
	if(x->number>y->number)            //(记数和数据均倒立后)
		return 1;
	else if(x->number==y->number)
	{
		while(x!=NULL)
		{
			if(x->ch!=y->ch)
				break;
			x=x->next;
			y=y->next;
		}
		if(x!=NULL)
		{
			if(x->ch>y->ch)
				return 1;
			else return -1;
		}
		return 0;
	}
	else return -1;
}
//======================================================
//链表初始
sjd *initsjdlist(void)
{
	sjd *x;
	init(&x);
	insert(x,'0',1);
	return x;
}
//======================================================
//链表数据初始
sjd *initsjd(void)
{
	sjd *x,*p;
	int i,j;
	char *cx;
	cx=(char *)malloc(sizeof(cx));
	init(&x);
	printf("请输入数据!\n");
	printf("cx=");scanf("%s",cx);
	for(i=0,j=0,p=x;cx[i]!='\0';++i)
	{
		if(cx[i]>='0' && cx[i]<='9')
		{
			insert(x,cx[i],++j);
			p=p->next;
		}
		else
			continue;
	}
	jsdl(x);
	return x;
}
//======================================================
//计算(乘法)
sjd *by(sjd *x,sjd *y)
{
	sjd *z;
	int i,j,m,n,jg=0,s=0,g=0;
	char ch;
	init(&z);
	i=1;										//以下是计算阶段
	while(1)
	{
		j=1;
		if(search(x,i-1)=='\0')
			break;
		m=search(x,i-1)-48;
		while(1)
		{
			if(search(y,j-1)=='\0')
				break;
			n=search(y,j-1)-48;
			if(search(z,i+j-1)=='a')
				jg=m*n+s;
			else
				jg=m*n+s+search(z,i+j-1)-48;
			s=jg/10;
			g=jg%10;
			ch=g+48;
			if(search(z,i+j-1)!='a')
				mend(z,i+j-1,ch);
			else
				insert(z,ch,i+j-1);
			j++;
		}
		insert(z,s+48,i+j-1);
		s=0;
		i++;
	}
	ql(z);
	return z;
}
//========================================================
//加法
sjd *and(sjd *x,sjd *y)
{
	sjd *z;
	int i=1,j=0,m,n,jg=0,s=0,g=0;
	char ch;
	init(&z);
	while((search(x,i-1)!='\0' && search(x,i-1)!='a') || (search(y,i-1)!='\0' && search(y,i-1)!='a'))
	{
		if(search(x,i-1)=='a' || search(x,i-1)=='\0')
			m=0;
		else
			m=search(x,i-1)-48;
		if(search(y,i-1)=='a' || search(y,i-1)=='\0')
			n=0;
		else
			n=search(y,i-1)-48;
		jg=m+n+s;
		g=jg%10;
		s=jg/10;
		ch=g+48;
		insert(z,ch,++j);
		i++;
	}
	insert(z,s+48,++j);
	ql(z);
	return z;
}
//========================================================
//减法
sjd *jf(sjd *x,sjd *y)
{
	sjd *z,*t,*p,*q;
	int i=1,j=0,m,n,jg=0;
	char ch;
	p=fzsj(x);
	q=fzsj(y);
	init(&z);
	if(pdcdx(x,y)<0)
	{t=x;x=y;y=t;}
	while((search(x,i-1)!='\0' && search(x,i-1)!='a') || (search(y,i-1)!='\0' && search(y,i-1)!='a'))
	{
		if(search(x,i-1)=='a' || search(x,i-1)=='\0')
			m=0;
		else
			m=search(x,i-1)-48;
		if(search(y,i-1)=='a' || search(y,i-1)=='\0')
			n=0;
		else
			n=search(y,i-1)-48;
		if(m<n)
		{
			m+=10;
			jg=m-n;
			ch=search(x,i);
			ch-=1;
			mend(x,i,ch);
		}
		else
			jg=m-n;
		ch=jg+48;
		insert(z,ch,++j);
		i++;
	}
	jsdl(z);
	i=z->number;
	jsdl(z);
	jsdl(x);
	if(pdcdx(p,q)<0)
	{
		for(i=1,t=q->next;t!=NULL;t=t->next,i++)           //复原
			mend(x,i,t->ch);
		for(i=p->number-1,t=p->next;t!=NULL;t=t->next,i--)           //复原
			mend(y,i,t->ch);
	}
	else
	{
		for(i=1,t=p->next;t!=NULL;t=t->next,i++)           //复原
			mend(x,i,t->ch);
		for(i=q->number-1,t=q->next;t!=NULL;t=t->next,i--)           //复原
			mend(y,i,t->ch);
	}
	jsdl(x);
	if(pdcdx(p,q)<0)
	{t=x;x=y;y=t;}
	jsdl(z);jg=z->number+1;jsdl(z);
	if(pdcdx(x,y)<0)
		insert(z,'-',jg);
	freesjd(p);	freesjd(q);
	ql(z);
	return z;
}
//========================================================
//在链表尾部插入数据
void insertlast(sjd *h,char ch)
{
	sjd *p,*q;
	init(&p);
	p->ch=ch;
	for(q=h;q->next!=NULL;q=q->next);
	p->next=q->next;
	p->prior=q;
	q->next=p;
	p->number=q->number+1;
}
//========================================================
//除法
sjd *cf(sjd *x,sjd *y)
{
	sjd *z,*p;
	int j=0,m=0,n,length=0;
	char ch;
	init(&z);
	if(pdcdx(x,y)<0)
	{
		insert(z,'0',1);
		return z;
	}
	else
	{
		length=y->number;
		p=qzc(x,1,length);
		jsdl(p);
        n=x->number-length;
		while(1)
		{
			if(pdcdx(p,y)<0)
				goto recom;
			while(pdcdx(p,y)>=0)
			{
				p=jf(p,y);
				sjdl(p);
				++m;
			}
recom:
			ch=m+48;
			insertlast(z,ch);
			if(--n==-1)
				break;
			ch=search(x,n);
			jsdl(p);
			insertlast(p,ch);
			sjdl(p);
			ql(p);
			sjdl(p);
			m=0;
		}
	}
	sjdl(z);
	ql(z);
	return z;	
}
//========================================================
//求模
sjd *modc(sjd *x,sjd *y)
{
	sjd *p;
	int m=0,n,length=0;
	char ch;
	if(pdcdx(x,y)<0)
	{
		p=fzsj(x);
		sjdl(p);
		return p;
	}
	else
	{
		length=y->number;
		p=qzc(x,1,length);
		jsdl(p);
        n=x->number-length;
		while(1)
		{
			if(pdcdx(p,y)<0)
				goto recom;
			while(pdcdx(p,y)>=0)
			{
				p=jf(p,y);
				sjdl(p);
				++m;
			}
recom:
			ch=m+48;
			if(--n==-1)
				break;
			ch=search(x,n);
 			jsdl(p);
			insertlast(p,ch);
			sjdl(p);
			ql(p);
			sjdl(p);
			m=0;
		}
	}
	sjdl(p);
	return p;
}
//========================================================
//求幂
sjd *powc(sjd *x,sjd *y)
{
	sjd *z,*tp,*p;
	init(&z);
	z=fzsj(x);
	init(&tp);
	insertlast(tp,'1');
	jsdl(tp);
	p=y;
	p=jf(p,tp);
	sjdl(p);
	for(;p->next->ch!='0';)
	{
		z=by(z,x);
		sjdl(z);
		p=jf(p,tp);
		sjdl(p);
	}
	sjdl(z);
	return z;
}
//========================================================
//主函数 
void main()
{
	sjd *x,*y,*z;
	x=initsjd();
	y=initsjd();
	z=by(x,y);   //乘法
	display(z);
	z=and(x,y);  //加法
	display(z);	
	z=jf(x,y);   //减法
	display(z);
 	z=cf(x,y);   //除法
	display(z);
	z=modc(x,y);   //求模
	display(z);
	z=powc(x,y);   //求幂
	display(z);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -