📄 csjd.cpp
字号:
#include<math.h>
#include"Csjd.h"
//======================================================
//全局函数 初始一个sjd类型数据
sjd *Afxinit(void)
{
sjd *data;
if((data=(sjd*)malloc(sizeof(sjd)))==NULL)
{
printf("磁盘不足!\n");
exit(1);
}
data->ch='0';
data->next=NULL;
data->prior=NULL;
return data;
}
//======================================================
//判断串的大小(比较串x与串y的大小)
int Afxjudge(Csjd &x,Csjd &y)
{
sjd *p,*q;
int flag=0;
if(x.data->next->ch!='-' && y.data->next->ch=='-')
return 1;
if(x.data->next->ch=='-' && y.data->next->ch!='-')
return -1;
if(x.data->next->ch=='-' && y.data->next->ch=='-')
flag=1;
if(x.lengthlist()>y.lengthlist())
{
if(flag)
return -1;
else
return 1;
}
else if(x.lengthlist()<y.lengthlist())
{
if(flag)
return 1;
else
return -1;
}
else
{
for(p=x.data->next,q=y.data->next;p!=NULL;p=p->next,q=q->next)
{
if(p->ch>q->ch)
{
if(flag)
return -1;
else
return 1;
}
else if(p->ch<q->ch)
{
if(flag)
return 1;
else
return -1;
}
else
continue;
}
return 0;
}
}
//======================================================
//判断串的大小(比较串x与串y的大小)(浮点)
int AfxjudgeF(Csjd &x,Csjd &y)
{
Csjd xI,xF,yI,yF;
Afxdispose_the_float_part(x,y);
xI.get_front_decimaldata(x);
xF.get_behind_decimaldata(x);
yI.get_front_decimaldata(y);
yF.get_behind_decimaldata(y);
if(::Afxjudge(xI,yI)<0)
return -1;
else if(::Afxjudge(xI,yI)>0)
return 1;
else
{
if(::Afxjudge(xF,yF)<0)
return -1;
else if(::Afxjudge(xF,yF)>0)
return 1;
else
return 0;
}
}
//======================================================
//将x和y设置成一样长的数据
void Afxdispose_the_float_part(Csjd &x,Csjd &y)
{
Csjd X,Y;
int i;
X.get_behind_decimaldata(x);
Y.get_behind_decimaldata(y);
if(X.lengthlist()==Y.lengthlist())
return;
int length=X.lengthlist()>=Y.lengthlist()?X.lengthlist():Y.lengthlist();
int flag=0;
int dtlength=abs(X.lengthlist()-Y.lengthlist());
if(X.lengthlist()>Y.lengthlist())
flag=1;
if(!flag)
for(i=0;i<dtlength;i++)
x.insertlast('0');
else
for(i=0;i<dtlength;i++)
y.insertlast('0');
}
//======================================================
void Csjd::movedecimal_left(int digit) //将小数点想左移动
{
sjd *p;
int i;
p=this->data->next;
for(i=1;p!=NULL && p->ch!='.';p=p->next,++i);
this->deletesjd(i);
if(i<digit+1)
{
this->insert('.',1);
this->insert('0',1);
for(int j=0;j<digit-i+1;j++)
this->insert('0',3);
}
else if(i>digit+1)
this->insert('.',i-digit);
else
{
this->insert('.',1);
this->insert('0',1);
}
}
//======================================================
void Csjd::movedecimal_right(int digit) //将小数点想右移动
{
sjd *p;
int i,length;
p=this->data->next;
for(i=1;p!=NULL && p->ch!='.';p=p->next,++i);
this->deletesjd(i);
length=this->lengthlist();
if(digit>=length-i+1)
{
while(digit>length-i+1)
{
this->insertlast('0');
digit--;
}
this->insertlast('.');
this->insertlast('0');
}
else
this->insert('.',i+digit);
this->dowithsjd();
}
//======================================================
//插入(带头链表)(在链h中第pos的位置插入数据ch)
int Csjd::insert(char ch,int pos)
{
int i;
sjd *p,*q,*cr;
p=::Afxinit();
p->ch=ch;
for(i=1,cr=data,q=data->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 Csjd::mend(int pos,char ch)
{
sjd *p;
int i;
for(p=data->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 Csjd::searchc(int pos)
{
sjd *p;
int i;
for(p=data->next,i=1;p!=NULL && i<pos;p=p->next,i++); //查找p
if(p==NULL || i!=pos)
return '\0'; //说明不存在这个位置
return p->ch; //说明该结点中有数据
}
//======================================================
//数据显示(显示链h中的所有数据)
void Csjd::display(void)
{
int i=0;
sjd *p=data->next;
if(p==NULL || data==NULL || data->ch!='0')
{
printf("无数据!\n");
return;
}
for(;p->next!=NULL;p=p->next)
{
printf("%c",p->ch);
if(!(++i%100))
printf("\n");
}
printf("%c",p->ch);
}
//======================================================
//求串长(求链h的数据个数,即串长,不包括头结点)
int Csjd::lengthlist()
{
sjd *p;
int i;
if(data->ch!='0' || data==NULL)
{
printf("无数据!\n");
return 0;
}
for(p=data->next,i=0;p!=NULL;p=p->next,i++);
return i;
}
//======================================================
//复制数据 (将链x的数据复制到新创的链中并返回该链y)
void Csjd::copysjd(Csjd &x)
{
sjd *p=x.data->next,*q,*t;
if(x.data==NULL || x.data->next==NULL)
{
printf("无数据!\n");
return;
}
t=data;
while(p!=NULL)
{
q=Afxinit();
q->ch=p->ch;
q->next=t->next;
q->prior=t;
t->next=q;
p=p->next;
t=t->next;
}
}
//======================================================
//删除数据(删除第pos个位置的数据)
void Csjd::deletesjd(int pos)
{
sjd *p,*q;
int i;
for(p=data->next,i=1;p!=NULL && i<pos;p=p->next,i++); //查找第pos个位置的数据
if(p==NULL || i!=pos)
return; //说明不存在这个位置
q=p->prior;
q->next=p->next;
if(p->next!=NULL)
p->next->prior=q;
free(p);
}
//======================================================
//取长为固定s的子串(在链x中的第begin个位置取长为length的链串)
void Csjd::getsubsjd(Csjd &x,int begin,int length)
{
int i,j=0;
for(i=begin;i<begin+length;i++) //(记数原始)
{
if(!x.searchc(i))
break;
this->insert(x.searchc(i),++j);
}
}
//======================================================
//链表初始 (初试一数据为0的链)
void Csjd::initsjdlist(void)
{
this->insert('0',1);
return ;
}
//======================================================
//数据倒立(将链h中的数据倒立过来)
void Csjd::handstandsjd()
{
sjd *p,*q;
char t;
if(data->ch!='0')
{
printf("链中无数据!\n");
return;
}
for(q=data;q->next!=NULL;q=q->next); //初始q
p=data->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 Csjd::insertlast(char ch)
{
sjd *p,*q;
p=::Afxinit();
p->ch=ch;
for(q=data;q->next!=NULL;q=q->next);
p->next=q->next;
p->prior=q;
q->next=p;
}
//========================================================
//整理数据
void Csjd::dowithsjd()
{
sjd *p=data->next,*q;
int flag=0;
if(p==NULL)
{
printf("无数据!\n");
return;
}
if(p->ch=='0' && (p->next==NULL || p->next->ch=='.'))
goto RE;
if(p->ch=='-')
{
p=p->next;
flag=1;
}
while(1)
{
if(p->next!=NULL && p->next->ch!='.' && p->ch=='0')
{
q=p->next;
if(flag)
this->deletesjd(2);
else
this->deletesjd(1);
p=q;
}
if(p->ch!='0' || this->data->next->ch=='0' && this->data->next->next==NULL)
break;
if(p->ch=='0' && p->next==NULL/* && p->next->ch=='.'*/)
break;
if(p->next->ch=='.' && p->next->next!=NULL)
break;
}
RE:
for(p=this->data->next;p!=NULL && p->ch!='.';p=p->next);
if(p!=NULL)
{
for(;p->next!=NULL;p=p->next);
while(p->ch=='0' && p->prior->ch!='.')
{
q=p->prior;
this->deletelast();
p=q;
}
}
}
//======================================================
//撤消数据链(将链h撤消)
void Csjd::freesjd()
{
sjd *p=data->next,*q=data;
if(q==NULL || p==NULL)
return;
q->next=NULL;
/*while(p!=NULL)
{
free(q);
q=p;
p=p->next;
}
data=Afxinit(); //??????*/
/*data->next=NULL;
data->prior=NULL;
data->ch='0';*/
while(p)
{
q=p->next;free(p);
p=q;;
}
p=otherdata.next;q=&otherdata;
if(p==NULL || q==NULL)
return;
q->next=NULL;
while(p)
{
q=p->next;free(p);
p=q;;
}
//otherdata=*Afxinit(); //??????
/*otherdata.next=NULL;
otherdata.prior=NULL;
otherdata.ch='0';*/
}
//======================================================
//在原数据链后面插另一入数据链x
void Csjd::insertlastCsjd(Csjd &x)
{
sjd *t;
for(t=x.data->next;t!=NULL;t=t->next)
this->insertlast(t->ch);
}
//======================================================
//链表数据初始 登录整型数据
void Csjd::load_integerdata(/*char* str*/)
{
sjd *p;
int i,j,flag=0;
char* str=new char[10000];
printf("请输入数据:");
scanf("%s",str);
p=data;i=0;j=0;
while(1)
{
if(str[i]=='-' && !flag)
{
insert(str[i++],++j);
p=p->next;
flag=1;
}
if(str[i]>='0' && str[i]<='9')
break;
}
for(;str[i]!='\0';++i)
{
if(str[i]>='0' && str[i]<='9')
{
insert(str[i],++j);
p=p->next;
}
else
continue;
}
this->dowithsjd();
}
//======================================================
//链表数据初始 登录浮点型数据
void Csjd::load_floatdata(/*char* str*/)
{
sjd *p;
int i,j,flag=0,tlag=0;
char* str=new char[10000];
printf("请输入数据:");
scanf("%s",str);
p=data;i=0;j=0;
while(1)
{
if(str[i]=='-' && !flag)
{
insert(str[i++],++j);
p=p->next;
flag=1;
}
if(str[i]>='0' && str[i]<='9')
break;
}
for(;str[i]!='\0';++i)
{
if(str[i]=='.' && !tlag)
{
insert(str[i],++j);
p=p->next;
tlag=1;
}
else if(str[i]>='0' && str[i]<='9')
{
insert(str[i],++j);
p=p->next;
}
else
continue;
}
if(p->ch=='.' && p->next==NULL)
this->insertlast('0');
if(!tlag)
{
this->insertlast('.');
this->insertlast('0');
}
this->dowithsjd();
}
//======================================================
//取出小数点前面的数据
void Csjd::get_front_decimaldata(Csjd &x)
{
sjd *p=x.data->next;
while(p->ch!='.')
{
this->insertlast(p->ch);
p=p->next;
}
}
//======================================================
//取出小数点后面的数据
int Csjd::get_behind_decimaldata(Csjd &x)
{
sjd *p=x.data->next;
for(;p!=NULL && p->ch!='.';p=p->next);
p=p->next;
while(p!=NULL)
{
this->insertlast(p->ch);
p=p->next;
}
int i;
p=this->data->next;
for(i=0;p!=NULL && p->ch=='0';p=p->next,++i);
return i;
}
//======================================================
void Csjd::ChangeIntointeger() //将一个浮点型数据转换为整形数据
{
Csjd P;
P.copysjd(*this);
this->freesjd();
this->get_front_decimaldata(P);
}
//======================================================
void Csjd::ChangeIntofloat() //将一个浮点型数据转换为浮点形数据
{
this->insertlast('.');
this->insertlast('0');
}
//将一个整形数据转换成sjd数据类型
void Csjd::ChangeIntoSjd(int x)
{
int i,j,flag=0;
char ch;
if(x<0)
{
x=x*(-1);
flag=1;
}
while(1)
{
i=x/10;
j=x%10;
x=i;
ch=j+48;
this->insert(ch,1);
if(!i)
break;
}
if(flag)
{
this->insert('-',1);
x=x*(-1);
}
}
//======================================================
void Csjd::deletelast() //删除最后一个字符
{
int l;
l=this->lengthlist();
this->deletesjd(l);
}
//======================================================
//获得最后一个数据
char Csjd::GetlastChar()
{
sjd *p;
for(p=this->data->next;p->next!=NULL;p=p->next);
return p->ch;
}
//======================================================
//判断是否为浮点型数据
int Csjd::IsnotFloat()
{
sjd* p;
for(p=this->data->next;p!=NULL && p->ch!='.';p=p->next);
if(p)
return 1;
else
return 0;
}
//======================================================
//构造
Csjd::Csjd(void)
{
data=Afxinit();
otherdata=*Afxinit();
//this->initsjdlist(); //将Csjd对象初始为零
}
//析构
Csjd::~Csjd(void)
{
freesjd();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -