📄 math_ing.cpp
字号:
//===================================================//
// 对于全体实数 //
//===================================================//
#include"mathc.h"
//======================================================
//数据显示(显示链h中的所有数据)
void display(sjd *h)
{
sjd *p=h->next;
if(p==NULL)
{
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->ch!='.';p=p->next,i++);
return i;
}
//======================================================
//复制数据 (将链x的数据复制到新创的链中并返回该链y)
sjd *fzsj(sjd *x)
{
sjd *y,*p=x->next,*q,*t;
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;
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;
}
}
//========================================================
//在链表尾部插入数据
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;
}
//======================================================
//链表初始 (初试一数据为0的链)
sjd *initsjdlist(void)
{
sjd *x;
init(&x);
insert(x,'0',1);
return x;
}
//======================================================
//链表数据初始
sjd *initsjd(void)
{
sjd *x,*p;
int i,j,flag=0;
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 if(cx[i]=='.' && flag==0)
{
insert(x,cx[i],++j);
p=p->next;
flag+=1;
}
else
continue;
}
if(flag==0)
insertlast(x,'.');
return x;
}
//======================================================
//数据倒立(将链h中的数据倒立过来)
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;
}
//========================================================
//整理数据
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);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);
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;
}
//========================================================
//主函数
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 + -