체인 테이블 형식으로 대정수를 저장하고 연산하다
//////Digital       ,number          (       ),insert               ,nplus  number          。
#include "stdafx.h"
#include  
#include  
using namespace std;
struct Digital
{
    int x;
    Digital*next;
};
class number
{
    Digital*head;
public:
    number() { head = NULL; }
    number(string&);
    void insert(int);
    void output();
    Digital*Gethead() { return head; }
};
number::number(string& x)
{
    reverse(x.begin(), x.end());
    for (unsigned int i = 0; i < x.length(); i++)
    {
        insert(x[i]-'0');
    }
}
void number::insert(int x)
{
    Digital*d=head;
    if (d==NULL)
    {
        d = new Digital;
        d->next = NULL;
        d->x = x;
        head = d;
    }
    else
    {
        while (d->next != NULL)
        {
            d = d->next;
        }
        d->next = new Digital;
        d->next->next = NULL;
        d->next->x = x;
    }
}
void number::output()
{
    Digital*d = head;
    string s="";
    string t = "";
    while (d->next != NULL)
    {
        t = char(d->x) + '0';
        s.insert(0,t);
        d = d->next;
    } 
    t = char(d->x) + '0';
    s.insert(0, t);
    for (unsigned int i = 0; i < s.length(); i++)
    {
        cout << s[i];
    }
    cout << endl;
}
number nplus(number&a, number&b)
{
    number c;
    Digital*x = a.Gethead();
    Digital*y = b.Gethead();
    int o = 0, p= 0;
    while (x->next!=NULL&&y->next!=NULL)
    {
        int t = x->x + y->x;
        o=t%10;
        c.insert(o+p);
        p = t / 10;
        x = x->next;
        y = y->next;
    }
    int t = x->x + y->x;
    o = t % 10;
    c.insert(o + p);
    p = t / 10;
    if (x->next!=NULL)
    {
        while (x->next != NULL)
        {
            int t = x->x;
            o = t;
            c.insert(o + p);
            p =0;
            x = x->next;
        }
    }
    else
    {
        while (y->next != NULL)
        {
            int t = y->x;
            o = t;
            c.insert(o + p);
            p = 0;
            y = y->next;
        }
    }
    return c;
}
int main()
{
    string s1="1145141919810";
    string s2 = "97";
    number n1(s1);
    number n2(s2);
    n1.output();
    n2.output();
    number n3 = nplus(n1,n2);
    n3.output();
    getchar();
    return 0;
}
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.