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

📄 mathc.h

📁 计算一切超大数据功能,包括所有实数的一些基本的运算(不足之处望网友多多指教)
💻 H
字号:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
	char ch;
	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;
}
//======================================================
//插入(带头链表)(在链h中第pos的位置插入数据ch)
int insert(sjd *h,char ch,int pos)
{
	int i;
	sjd *p,*q,*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;
	}
	if(q==NULL)   //说明是尾
	{
		p->next=cr->next;
		cr->next=p;
		p->prior=cr;
		return 1;
	}
	p->next=q;
	p->prior=cr;
	cr->next=p;
	q->prior=p;
	return 1;
}
//======================================================
//数据修改(在链h中修改第pos的数据,改为ch)
void mend(sjd *h,int pos,char ch)
{
	sjd *p;
	int i;
	for(p=h->next,i=1;p!=NULL && i<pos;p=p->next,i++);
	if(p==NULL || i!=pos)
	{
		printf("输入参数有误!\n修改失败!\n");
		return;
	}
	p->ch=ch;
}
//======================================================
//数据查找(在链h中查找第pos位置的数据,并返回该数据)
char searchc(sjd *h,int pos)
{
	sjd *p;
	int i;
	for(p=h->next,i=1;p!=NULL && i<pos;p=p->next,i++);  //查找p
	if(p==NULL || i!=pos)
		return '\0';                         //说明不存在这个位置
	return p->ch;        //说明该结点中有数据
}
//======================================================
//数据显示(显示链h中的所有数据)
void display(sjd *h)
{
	sjd *p=h->next;
	if(p==NULL || h==NULL || h->ch!='0')
	{
		printf("无数据!\n");
		return;
	}
	for(;p->next!=NULL;p=p->next)
		printf("%c",p->ch);
	printf("%c\n",p->ch);
}
//======================================================
//求串长(求链h的数据个数,即串长,不包括头结点)
int lengthlist(sjd *h)
{
	sjd *p;
	int i;
	if(h->ch!='0' || h==NULL)
	{
		printf("无数据!\n");
		return 0;
	}
	for(p=h->next,i=0;p!=NULL;p=p->next,i++);
	return i;
}
//======================================================
//复制数据 (将链x的数据复制到新创的链中并返回该链y)
sjd *fzsj(sjd *x)
{
	sjd *y,*p=x->next,*q,*t;
	if(x==NULL || x->next==NULL)
	{
		printf("无数据!\n");
		return NULL;
	}
	init(&y);
	t=y;
	while(p!=NULL)
	{
		init(&q);
		q->ch=p->ch;
  		q->next=t->next;
		q->prior=t;
		t->next=q;
		p=p->next;
		t=t->next;
	}
	return y;
}
//======================================================
//删除数据(删除第pos个位置的数据)
void del(sjd *h,int pos)
{
	sjd *p,*q;
	int i;
	for(p=h->next,i=1;p!=NULL && i<pos;p=p->next,i++);  //查找第pos个位置的数据
	if(p==NULL || i!=pos)
	{
		printf("输入参数有误!\n删除失败!\n");
		return;                       //说明不存在这个位置 
	}
	q=p->prior;
	q->next=p->next;
	if(p->next!=NULL)
		p->next->prior=q;
	free(p);
}
//======================================================
//撤消数据链(将链h撤消)
void freesjd(sjd *h)
{
	if(h==NULL)
	{
		printf("无数据,撤消失败!\n");
		return;
	}
	sjd *p=h->next,*q=h;
	while(p!=NULL)
	{
		free(q);
		q=p;
		p=p->next;
	}
	free(p);
}
//====================================================== 
//取长为固定s的子串(在链x中的第begin个位置取长为length的链串)
sjd *qzc(sjd *x,int begin,int length)
{ 
	sjd *y;
	int i,j=0;
	init(&y);
	for(i=begin;i<begin+length;i++)       //(记数原始) 
	{
		if(!searchc(x,i))
			break;
		insert(y,searchc(x,i),++j);
	}
	return y;
}
//======================================================
//判断串的大小(比较串x与串y的大小)
int pdcdx(sjd *x,sjd *y)
{
	sjd *p,*q;
	if(lengthlist(x)>lengthlist(y))
		return 1;
	else if(lengthlist(x)<lengthlist(y))
		return -1;
	else
	{
		for(p=x->next,q=y->next;p!=NULL;p=p->next,q=q->next)
		{
			if(p->ch>q->ch)
				return 1;
			else if(p->ch<q->ch)
				return -1;
			else
				continue;
		}
		return 0;
	}
}
//======================================================
//链表初始 (初试一数据为0的链)
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);
	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;
	}
	return x;
}
//======================================================
//数据倒立(将链h中的数据倒立过来)
void sjdl(sjd *h)
{
	sjd *p,*q;
	char t;
	if(h->ch!='0')
	{
		printf("链中无数据!\n");
		return;
	}
	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;
}
//========================================================
//在链表尾部插入数据
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;
}
//========================================================
//整理数据
void dowith(sjd *h)
{
	sjd *p=h->next,*q;
	if(p==NULL)
	{
		printf("无数据!\n");
		return;
	}
	if(p->ch=='0' && p->next==NULL)
		return;
	while(1)
	{
		if(p->next!=NULL && p->ch=='0')
		{
			q=p->next;
			del(h,1);
			p=q;
		}
		if(p->ch!='0')
			break;
	}
}
//======================================================
//乘法(将数据链x乘以数据链y并返回乘后的结果数据链z)
sjd *by(sjd *x,sjd *y)
{
	sjd *z;
	int i,j,m,n,jg=0,s=0,g=0;
	char ch;
	sjdl(x);sjdl(y);
	if(lengthlist(x)==1 && x->next->ch=='0' || lengthlist(y)==1 && y->next->ch=='0')
	{
		z=initsjdlist();
		return z;
	}
	init(&z);
	i=1;										//以下是计算阶段
	while(1)
	{
		j=1;
		if(searchc(y,i)=='\0')
			break;
		m=searchc(y,i)-48;
		while(1)
		{
			if(searchc(x,j)=='\0')
				break;
			n=searchc(x,j)-48;
			if(searchc(z,i+j-1)=='\0')
				jg=m*n+s;
			else
				jg=m*n+s+searchc(z,i+j-1)-48;
			s=jg/10;
			g=jg%10;
			ch=g+48;
			if(searchc(z,i+j-1)!='\0')
				mend(z,i+j-1,ch);
			else
				insert(z,ch,i+j-1);
			j++;
		}
		insert(z,s+48,i+j-1);
		s=0;
		i++;
	}
	sjdl(x);sjdl(y);sjdl(z);
	dowith(z);
	return z;
}
//========================================================
//加法(将数据链x加上数据链y并返回加后的结果数据链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);sjdl(x);sjdl(y);
	while(searchc(x,i)!='\0' || searchc(y,i)!='\0')
	{
		if(searchc(x,i)=='\0')
			m=0;
		else
			m=searchc(x,i)-48;
		if(searchc(y,i)=='\0')
			n=0;
		else
			n=searchc(y,i)-48;
		jg=m+n+s;
		g=jg%10;
		s=jg/10;
		ch=g+48;
		insert(z,ch,++j);
		i++;
	}
	insert(z,s+48,++j);
	sjdl(x);sjdl(y);sjdl(z);
	dowith(z);
	return z;
}
//========================================================
//减法(将数据链x减去数据链y并返回减后的结果数据链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);
	if(pdcdx(x,y)<0)
	{t=x;x=y;y=t;}
	init(&z);
	if(pdcdx(x,y)==0)
	{
		freesjd(p);	freesjd(q);
		insertlast(z,'0');
		return z;
	}
	sjdl(x);sjdl(y);
	while(searchc(x,i)!='\0' || searchc(y,i)!='\0')
	{
		if(searchc(x,i)=='\0')
			m=0;
		else
			m=searchc(x,i)-48;
		if(searchc(y,i)=='\0')
			n=0;
		else
			n=searchc(y,i)-48;
		if(m<n)
		{
			m+=10;
			jg=m-n;
			ch=searchc(x,i+1);
			ch-=1;
			mend(x,i+1,ch);
		}
		else
			jg=m-n;
		ch=jg+48;
		insert(z,ch,++j);
		i++;
	}
	if(pdcdx(p,q)<0)
	{
		for(i=1,t=q->next;t!=NULL;t=t->next,i++)           //复原
			mend(x,i,t->ch);
		for(i=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=1,t=q->next;t!=NULL;t=t->next,i++)           //复原
			mend(y,i,t->ch);
	}
	if(pdcdx(p,q)<0)
	{t=x;x=y;y=t;}
	if(pdcdx(x,y)<0)
		insertlast(z,'-');
	freesjd(p);	freesjd(q);
	sjdl(z);
	dowith(z);
	return z;
}
//========================================================
//除法(将数据链x除去数据链y并返回除后的结果数据链z)
sjd *cf(sjd *x,sjd *y)
{
	sjd *z,*p;
	int j=0,m=0,n,length=0;
	char ch;
	init(&z);
	if(lengthlist(y)==1 && y->next->ch=='0')
	{
		printf("除数不能为零!\n");
		return NULL;
	}
	if(pdcdx(x,y)<0)
	{
		insert(z,'0',1);
		return z;
	}
	else
	{
		length=lengthlist(y);
		p=qzc(x,1,length);
        n=length;
		while(1)
		{
			if(pdcdx(p,y)<0)
				goto recom;
			while(pdcdx(p,y)>=0)
			{
				p=jf(p,y);
				++m;
			}
recom:
			ch=m+48;
			insertlast(z,ch);
			if(++n==lengthlist(x)+1)
				break;
			ch=searchc(x,n);
			insertlast(p,ch);
			m=0;
		}
	}
	dowith(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);
		return p;
	}
	else
	{
		length=lengthlist(y);
		p=qzc(x,1,length);
        n=length;
		while(1)
		{
			if(pdcdx(p,y)<0)
				goto recom;
			while(pdcdx(p,y)>=0)
			{
				p=jf(p,y);
				++m;
			}
recom:
			ch=m+48;
			if(++n==lengthlist(x)+1)
				break;
			ch=searchc(x,n);
			insertlast(p,ch);
			dowith(p);
			m=0;
		}
	}
	dowith(p);
	return p;
}
//========================================================
//求幂
sjd *powc(sjd *x,sjd *y)
{
	sjd *z,*tp,*p;
	init(&z);
	z=fzsj(x);
	init(&tp);
	insertlast(tp,'1');
	p=fzsj(y);
	p=jf(p,tp);
	for(;p->next->ch!='0';)
	{
		z=by(z,x);
		p=jf(p,tp);
	}
	return z;
}

⌨️ 快捷键说明

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