利用雙向迴圈連結串列實現任意長的整數進行加法運算
阿新 • • 發佈:2019-01-04
【問題描述】 設計一個實現任意長的整數進行加法運算的演示程式 。
【基本要求】 利用雙向迴圈連結串列實現長整數的儲存,每個結點含一個整形變數。輸入和輸出形式:按中國對於長整數的表示習慣,每四位一組,組間用逗號隔開。
【測試資料】
(1)0;0;應輸出“0”。
(2)-2345,6789;-7654,3211;應輸出“-1,0000,0000”。
(3)-9999,9999;1,0000,0000,0000;應輸出“9999,0000,0001”。
(4)1,0001,0001;-1,0001,0001;應輸出“0”。
(5)1,0001,0001;-1,0001,0000;應輸出“1”。
(6)-9999,9999,9999;-9999,9999,9999;應輸出“-1,9999,9999,9998”。
(7)1,0000,9999,9999;1;應輸出“1,0001,0000,0000”。
加法運算分為同號和異號兩部分,具體實現程式碼如下:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "ctype.h"
typedef struct DualNode
{
int data;
struct DualNode *prior, *next;
}DualNode, *DualList;
DualList InitList(int sign)
{
//頭結點存放符號位,1為正,-1為負
DualList L;
L = (DualList)malloc(sizeof(DualNode));
L-> next = L->prior = L;
L->data = sign;
return L;
}
void InsertNodeAtTail(DualList L, int data)
{
//尾插,用於儲存資料的輸入
DualNode *s;
s = (DualList)malloc(sizeof(DualNode));
s->data = data;
s->next = L;
s->prior = L->prior;
L->prior->next = s;
L-> prior = s;
}
void InsertNodeAtHead(DualList L, int data)
{
// 即插在頭結點之後,用於計算結果的儲存
DualNode *s;
s = (DualList)malloc(sizeof(DualNode));
s->data = data;
s->next = L->next;
s->prior = L;
L->next->prior = s;
L->next = s;
}
void PrintList(DualList L)
{
//列印結果
int FirstTime = 1;
DualNode *p = L;
if (p->data == -1) printf("-");
p = p->next;
while(p != L)
{
if (FirstTime)
{
FirstTime = 0;
printf("%d", p->data);
}
else
{
printf(",%4d", p->data);
}
p = p->next;
}
printf("\n");
}
DualList InputData()
{
int FirstNum = 1, data;
char c;
DualList L;
L = (DualList)malloc(sizeof(DualNode));
L->next = L->prior = L;
printf("Please Input as Format: -1234,1234,1234\n");
if ((c = getchar()) == '-')
L = InitList(-1);
else
L = InitList(1);
if (isdigit(c))
// 退格處理
ungetc(c, stdin);
do{
scanf("%d", &data);
InsertNodeAtTail(L, data);
}while((c = getchar()) != '\n');
printf("Your Input is:\n");
PrintList(L);
return L;
}
void DelNode(DualList L, DualNode *p)
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
void Add(DualList a, DualList b, DualList c)
{
DualList pa, pb;
int carry = 0, tmp;
pa = a->prior;
pb = b->prior;
while((pa != a) && (pb != b))
{
tmp = pa->data + pb->data + carry;
if (tmp >= 10000)
{
carry = 1;
tmp -= 10000;
}else
carry = 0;
InsertNodeAtHead(c, tmp);
pa = pa->prior;
pb = pb->prior;
}
while(pa != a)
{
// pb = b
tmp = pa->data + carry;
if (tmp >= 1000)
{
carry = 1;
tmp -= 10000;
}
else
carry = 0;
InsertNodeAtHead(c, tmp);
pa = pa->prior;
}
while(pb != b)
{
// pa = a
tmp = pb->data + carry;
if (tmp >= 1000)
{
carry = 1;
tmp -= 10000;
}
else
carry = 0;
InsertNodeAtHead(c, tmp);
pb = pb->prior;
}
if (carry != 0)
InsertNodeAtHead(c, 1);
}
void Sub(DualList a, DualList b, DualList c)
{
DualList pa, pb, pc;
int borrow = 0,tmp;
pa = a->prior;
pb = b->prior;
while((pa != a) && (pb != b))
{
if (pa->data >= pb->data + borrow)
{
tmp = pa->data - pb->data - borrow;
borrow = 0;;
}
else
{
tmp = 10000 + pa->data - pb->data - borrow;
borrow = 1;
}
InsertNodeAtHead(c, tmp);
pa = pa->prior;
pb = pb->prior;
}
if (pa != a || (pa == a && pb == b && borrow == 0))
{
// a >= b
c->data = a->data;
}
if (c->data != a->data)
{
// a < b
pc = c->prior;
while(pc != c)
{
// 結果轉換
if (pc == c->prior)
pc->data = 10000 - pc->data;
else
pc->data = 9999 - pc->data;
pc = pc->prior;
}
// 因為符號判斷錯誤,所以borrow要取反
borrow = borrow?0:1;
while(pb != b)
{
if (pb->data >= borrow)
{
tmp = pb->data - borrow;
borrow = 0;
}
// 繼續借位
else
{
tmp = 10000 + pb->data + borrow;
borrow = 1;
}
InsertNodeAtHead(c, tmp);
pb = pb -> prior;
}
}
else{
// a>b
while(pa != a)
{
if (pa->data >= borrow)
{
tmp = pa->data - borrow;
borrow = 0;
}
else
{
tmp = 10000 - pa->data - borrow;
borrow = 1;
}
InsertNodeAtHead(c, tmp);
pa = pa->prior;
}
}
pc = c->next;
while(pc->next !=c && pc->data == 0)
{
pc = pc->next;
DelNode(c, pc->prior);
}
}
DualList AddList(DualList a, DualList b)
{
DualList c;
if (a->data * b->data > 0)
{
c = InitList(a->data);
Add(a, b, c);
}else
{
c=InitList(b->data);
Sub(a, b, c);
}
return c;
}
int main(int argc, char const *argv[])
{
while(1)
{
//便於測試
DualList a, b, c;
a = InputData();
b = InputData();
c = AddList(a, b);
printf("The result is:");
PrintList(c);
}
return 0;
}