C++ 鏈表 大數加減法

大數加法

#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
    int data;
    Node *next;
    Node()
    {
        next = nullptr;
    }
    Node (int data)
    {
        this->data = data;
    }
    Node (const Node &temp)
    {
        this->data = temp.data;
    }
};
class Link
{
public:
    Node *head;
    int length = 0;
    Link()
    {
        head = new Node();
    }
    ~Link()
    {
        while(head != nullptr)
        {
            Node *p = head->next;
            free(head);
            head = p;
        }
    }
    void insert(const Node &cache)
    {
        Node *temp = new Node(cache);
        temp->next = head->next;
        head->next = temp;
        length++;
    }
};


void Creatlist(Link &link)//輸入數字
{
    char cache;
    while(1)
    {
        cache = getchar();
        if(cache == '\n')
            break;
        link.insert(Node(cache - '0'));
    }
}
void Add(Link &link_one, Link &link_two, Link &link_three)
{
    int temp = 0,i,m,n;
    n = link_one.length;
    m = link_two.length;
    Node *p = link_one.head->next, *q = link_two.head->next;
    for(i = 0; i<n && i<m; i++)
    {
        int data = p->data + q->data + temp;
        temp = data / 10;
        link_three.insert(data % 10);
        p = p->next;
        q = q->next;
    }
    for(; i<n; i++)
    {
        int data =  p->data + temp;
        temp = data / 10;
        link_three.insert(data % 10);
        p = p->next;
    }
    for(; i<m; i++)
    {
        int data = q->data + temp;
        temp = data / 10;
        link_three.insert(data % 10);
        q = q->next;
    }
    if(temp != 0)
        link_three.insert(1);

}
void Display(const Link &link)//輸出
{
    Node *temp = link.head->next;
    while(temp != nullptr)
    {
        printf("%d", temp->data);
        temp = temp->next;
    }
}
int main()
{
    Link link_one,link_two,link_three;//建立鏈表
    printf("大數加法\n");
    printf("please put first number\n");
    Creatlist(link_one);//輸入第一個數
    printf("please put second number\n");
    Creatlist(link_two);//輸入第二個數
    Add(link_one,link_two,link_three);//大數加法
    Display(link_three);//輸出
    system("pause");
    return 0;
}

大數減法

#include <bits/stdc++.h>
using namespace std;
bool flag = 0;
class Node
{
public:
	int data;
	Node *next;
	Node()
	{
		next = nullptr;
	}
	Node(int data)
	{
		this->data = data;
	}
	Node(const Node &temp)
	{
		this->data = temp.data;
	}
};
class Link
{
public:
	Node *head;
	int length;
	Link()
	{
		head = new Node();
		length = 0;
	}
	~Link()
	{
		while (head != nullptr)
		{
			Node *p = head->next;
			delete head;
			head = p;
		}
	}
	void insert(const Node &cache)
	{
		Node *temp = new Node(cache);
		temp->next = head->next;
		head->next = temp;
		length++;
	}
};

void Creatlist(Link &link)//輸入數字
{
	char cache;
	while (true)
	{
		cache = getchar();
		if (cache == '\n')
			break;
		link.insert(Node(cache - '0'));
	}
}
void Sub(Link &link_one, Link &link_two, Link &link_three)
{
	int i, n, m;
	n = link_one.length;
	m = link_two.length;
	Node *p = link_one.head->next, *q = link_two.head->next;
	for (i = 0; i < m; i++)
	{
		int cache;
		int z=p->data;
		int y=q->data;
		cache = (p->data) - (q->data);
		if (cache < 0)
		{
			p->next->data -= 1;
			cache += 10;
		}
		link_three.insert(cache);
		p = p->next;
		q = q->next;
	}
	for (; i<n; i++)
	{
		int cache = p->data;
		if (cache < 0)
		{
			p->next->data--;
			cache += 10;
		}
		if (p->next == nullptr && p->data == 0)
			break;
		link_three.insert(cache);
		p = p->next;
	}
}
void Display(const Link &link)//輸出
{
	bool zero = 0;
	if (flag)
		printf("-");
	Node *temp = link.head->next;
	while (temp != nullptr)
	{
		if (temp->data != 0)
			zero = 1;
		if (zero)
			printf("%d", temp->data);
		temp = temp->next;
	}
	if (!zero)
		printf("0");
}
int main()
{
	Link link_one, link_two, link_three;//建立鏈表
	printf("大數減法\n");
	printf("please put first number\n");
	Creatlist(link_one);//輸入第一個數
	printf("please put second number\n");
	Creatlist(link_two);//輸入第二個數
	if ((link_one.length < link_two.length) || (link_one.length == link_two.length && link_one.head->next->data < link_two.head->next->data))
	{
		flag = 1;
		Sub(link_two, link_one, link_three);   //大數加法
	}
	else
	{
		flag = 0;
		Sub(link_one, link_two, link_three);   //大數加法
	}
	Display(link_three);//輸出
    system("pause");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章