第一次写这么长的作业,各种困难,啥也不会,最后总算写完了,虽然不完美也不简洁,但是还是想记录一下
带头结点的双向循环链表存储,4位十进制一组,逗号分隔,实现任意长整数的加法减法运算,头结点做符号位
#include
#include
typedef int Elem;
typedef struct Node
{
Elem Data;
Node *next;
Node *prior;
}Node,*DList;
void Init(DList &L)
{
L = (DList)malloc(sizeof(Node));
L->next = L->prior = L;
}
//1<=i<=n
Node* FindKth(DList L,int i) //L 是 i=0
{
int j=0;
Node *p = L;
while(j {
p = p->next;
j++;
}
return p;
}
//1<=i<=n+1;
//指针p指向第i-1个位置的结点 s新分配结点
void Insert(DList &L,int i,Elem X)
{
Node *p,*s;
p = FindKth(L,i-1);
s = (Node*)malloc(sizeof(Node));
s->Data = X;
s->next = p->next;
s->prior = p;
p->next->prior = s;
p->next = s;
}
void Traverse(DList L)
{
Node *p;
p = L->next;
if(L->Data==-1)
printf("-");
while(p!=L)
{
printf("%d,",p->Data);
p = p->next;
}
printf("\n");
}
DList Copy(DList L)
{
DList L3;
Init(L3);
L3->Data = L->Data;
Node *p;
p = L->prior;
while(p!=L)
{
Insert(L3,1,p->Data);
p = p->prior;
}
return L3;
}
void Input(DList &L)
{
int group,i,num,symbol;
printf("how many group you want to input?\n");
scanf("%d",&group);
printf("input 1 for positive -1 for negtive\n");
scanf("%d",&symbol);
L->Data = symbol;
printf("input your numbers\n");
for(i=1;i<=group;i++)
{
scanf("%d",&num);
getchar();
Insert(L,i,num);
}
}
DList add(DList &L1,DList &L2)
{
//p 指向L1的结点 q指向L2的结点
int flag=0,result,i; //flag标志进位 result是4位相加的结果
DList L3;
Init(L3);
L3->Data = 1; //先不考虑符号,假设两个都为正
Node *p = L1->prior;
Node *q = L2->prior;
while(p!=L1&&q!=L2)
{
if(flag!=0)
result = p->Data+q->Data+flag;
else
result = p->Data+q->Data;
result=result%10000;
flag = (p->Data+q->Data)/10000;
Insert(L3,1,result);
p = p->prior;
q = q->prior;
}
if(p==L1&&q==L2&&flag!=0)
{
Insert(L3,1,flag);
}
if(p!=L1) //L1未遍历结束
{
while(p!=L1)
{
if(flag!=0)
result = p->Data+flag;
else
result = p->Data;
Insert(L3,1,result);
p = p->prior;
}
}
if(q!=L2) //L2未遍历结束
{
while(q!=L2)
{
if(flag!=0)
result = q->Data+flag;
else
result = q->Data;
Insert(L3,1,result);
q = q->prior;
}
}
return L3;
}
bool Bigger(DList &L1,DList &L2);
DList subtract(DList &L1,DList &L2) //不考虑符号位,只考虑两个正数相减,但是有小减大和大减小
{
Node *p,*q;
//p 指向大数的结点 q指向小数的结点
int flag=0,result,i; //flag标志借位 result是4位相减的结果
DList L3;
Init(L3);
if(Bigger(L1,L2))
{
p = L1->prior;
q = L2->prior;
while(p!=L1&&q!=L2)
{
if(p->Data < q->Data) //小的减大的要加上10000
{
if(flag!=0)
result = 10000+p->Data-q->Data-flag;
else
result = 10000+p->Data-q->Data;
}
else //四位大的减小的
{
if(flag!=0)
result = p->Data-q->Data-flag;
else
result = p->Data-q->Data;
}
if(p->Data < q->Data) //小要借位
flag = 1;
else
flag = 0;
Insert(L3,1,result);
p = p->prior;
q = q->prior;
}
if(p!=L1) //L1未遍历结束
{
while(p!=L1)
{
if(flag!=0)
result = p->Data-flag;
else
result = p->Data;
Insert(L3,1,result);
flag = 0; //接下来没有借位
p = p->prior;
}
}
if(q!=L2) //L2未遍历结束
{
while(q!=L2)
{
if(flag!=0)
result = q->Data-flag;
else
result = q->Data;
Insert(L3,1,result);
flag = 0;
q = q->prior;
}
}
}
else //L1
p = L2->prior;
q = L1->prior;
while(p!=L2&&q!=L1)
{
if(p->Data < q->Data) //小的减大的要加上10000
{
if(flag!=0)
result = 10000+p->Data-q->Data-flag;
else
result = 10000+p->Data-q->Data;
}
else
{
if(flag!=0)
result = p->Data-q->Data-flag;
else
result = p->Data-q->Data;
}
if(p->Data < q->Data) //小要借位
flag = 1;
else
flag = 0;
Insert(L3,1,result);
p = p->prior;
q = q->prior;
}
if(p!=L2) //L2未遍历结束
{
while(p!=L2)
{
if(flag!=0)
result = p->Data-flag;
else
result = p->Data;
Insert(L3,1,result);
p = p->prior;
}
}
if(q!=L1) //L1未遍历结束
{
while(q!=L1)
{
if(flag!=0)
result = q->Data-flag;
else
result = q->Data;
Insert(L3,1,result);
q = q->prior;
}
}
}
if(Bigger(L1,L2))
L3->Data = 1;
else
L3->Data = -1;
return L3;
}
int Length(DList &L)
{
int i =0;
Node *p = L->next;
while(p!=L)
{
p = p->next;
i++;
}
return i;
}
bool Bigger(DList &L1,DList &L2)
{
if(Length(L1) > Length(L2))
return true;
if(Length(L1) < Length(L2))
return false;
if(Length(L1) == Length(L2))
{
Node *p,*q;
p = L1->next;
q = L2->next;
while(p!=L1&&q!=L2)
{
if(p->Data > q->Data)
return true;
if(p->Data < q->Data)
return false;
if(p->Data = q->Data)
{
p = p->next;
q = q->next;
}
}
}
}
//改变符号位
void Chsym(DList &L)
{
if(L->Data==1)
L->Data = -1;
else
L->Data = 1;
}
DList realminus(DList &L1,DList &L2)
{
DList L3;
Init(L3);
if(L1->Data==1&&L2->Data==1)
L3=subtract(L1,L2);
if(L1->Data==-1&&L2->Data==-1)
{
L3=add(L1,L2);
Chsym(L3);
}
if(L1->Data==1&&L2->Data==-1) //变成加法
{
L3=add(L1,L2);
}
if(L1->Data==-1&&L2->Data==1) //变成加法
{
L3=add(L1,L2);
Chsym(L3);
}
return L3;
}
DList realadd(DList &L1,DList &L2)
{
DList L3;
Init(L3);
if(L1->Data==1&&L2->Data==1)
L3=add(L1,L2);
if(L1->Data==-1&&L2->Data==-1)
{
L3=add(L1,L2);
Chsym(L3);
}
if(L1->Data==1&&L2->Data==-1) //变成减法
{
L3=subtract(L1,L2);
}
if(L1->Data==-1&&L2->Data==1) //变成L2-L1
{
L3=subtract(L2,L1);
}
return L3;
}
int main()
{
char symbol;
DList L1,L2,L3;
Init(L1);
Init(L2);
Init(L3);
Input(L1);
Input(L2);
printf("input - or +\n");
scanf("%c",&symbol);
switch(symbol)
{
case '+':
{
printf("\n\n");
Traverse(L1);
printf("%c\n",symbol);
Traverse(L2);
L3 = realadd(L1,L2);break;
}
case '-':
{
printf("\n\n");
Traverse(L1);
printf("%c\n",symbol);
Traverse(L2);
L3 = realminus(L1,L2);break;
}
}
Traverse(L3);
return 0;
}