题目二: 长整数、高精度运算

一、问题描述:
设计一个程序实现两个任意长的整数(包括正数和负数)、任意精度实数的算术运算。

二、提示:
(1)用动态链表存贮数据,每结点含一个整型变量,表示若干位数。
(2)整数输入和输出按中国对于长整数的习惯表示,每3位1组,组间用逗号隔开。
(3)实现长整数的加、减运算。
(4)程序运行界面清晰实用。

选做:
(1)求两长整数之商、之积。
(2)高精度实数算术运算。
本篇只实现最基本功能

#include <stdio.h>
#include <iostream>
using namespace std;
struct nums
{
    char number;
    nums *left;
    nums *right;
};
void initializenums(nums *&, int &, int &);
void add(nums *, nums *);
void subtract(nums *, nums *);
void output(nums *, int);
int main()
{
    char operators;
    int a, b, n;
    int c1, c2, c3;
    nums *head1, *head2, *t, *p, *q;
    cout << " Please enter two long integers in a comma-separated manner by 1 group of each of the 3 digits, and press Enter for each input to indicate that the input is complete:" << endl;
    initializenums(head1, a, c1);
    initializenums(head2, b, c2);
    cout << endl
         << "Please select the operation that needs to be performed, add +, subtract please enter - :";
    cin >> operators;
    cout << "The result of the run is:" << endl;
    switch (operators)
    {
    case '+':
        c3 = 0;
        break;
    case '-':
        c3 = 1;
        break;
    default:
        cout << "error" << endl;
        return 0;
    }
    c2 = c2 + c3;
    if (c2 == 2)
        c2 = 0;
    if (b > a)
    {
        t = head1;
        head1 = head2;
        head2 = t;
        n = a;
        a = b;
        b = n;
        n = c1;
        c1 = c2;
        c2 = n;
    }
    while (a > b)
    {
        t = new nums;
        t->number = '0';
        t->left = NULL;
        t->right = head2;
        head2->left = t;
        head2 = t;
        b = b + 1;
    }
    if (c1 == 0 && c2 == 0)
        add(head1, head2);
    else if (c1 == 1 && c2 == 1)
    {
        cout << "-";
        add(head1, head2);
    }
    else
    {
        if (a == b)
        {
            p = head1;
            q = head2;
            while (p->number == q->number && p->right != NULL)
            {
                p = p->right;
                q = q->right;
            }
            if (p->number == q->number)
            {
                cout << "0" << endl;
                return 0;
            }
            if (q->number > p->number)
            {
                t = head1;
                head1 = head2;
                head2 = t;
                n = c1;
                c1 = c2;
                c2 = n;
            }
        }
        if (c1 == 1)
            cout << "-";
        subtract(head1, head2);
    }
    output(head1, a + 1);
    system("pause");
}
void initializenums(nums *&head, int &n, int &m)
{
    char c;
    nums *s, *p;
    n = 0;
    s = new nums;
    c = getchar();
    head = NULL;
    if (c == '-')
    {
        m = 1;
        c = getchar();
    }
    else
        m = 0;
    while (c != '\n')
    {
        if (c == ',')
        {
            c = getchar();
            continue;
        }
        s->number = c;
        if (head == NULL)
        {
            head = s;
            head->left = NULL;
            head->right = NULL;
            p = head;
        }
        else
        {
            s->left = p;
            s->right = NULL;
            p->right = s;
            p = s;
        }
        n = n + 1;
        s = new nums;
        c = getchar();
    }
    delete s;
    p = new nums;
    p->number = '0';
    p->left = NULL;
    p->right = head;
    head->left = p;
    head = p;
}
void output(nums *head, int a)
{
    nums *s;
    s = head;
    while (s->number == '0' && s->right != NULL)
    {
        s = s->right;
        a = a - 1;
    }
    if (s->number == '0')
    {
        cout << "0" << endl;
        return;
    }
    while (s->right != NULL)
    {
        cout << s->number;
        a = a - 1;
        if (a % 3 == 0 && a != 0)
            cout << ',';
        s = s->right;
    }
    cout << s->number;
    cout << endl;
}
void add(nums *head1, nums *head2)
{
    nums *p, *q;
    int a, b, m, t;
    p = head1;
    q = head2;
    while (p->right != NULL)
        p = p->right;
    while (q->right != NULL)
        q = q->right;
    m = 0;
    do
    {
        a = p->number - '0';
        b = q->number - '0';
        t = a + b + m;
        m = t / 10;
        t = t % 10;
        p->number = '0' + t;
        p = p->left;
        q = q->left;
    } while (q != NULL);
}

void subtract(nums *head1, nums *head2)
{
    nums *p, *q;
    int a, b, t, m;
    p = head1;
    q = head2;
    while (p->right != NULL)
        p = p->right;
    while (q->right != NULL)
        q = q->right;
    m = 0;
    do
    {
        a = p->number - '0';
        b = q->number - '0';
        t = a - b + m;
        if (t < 0 && p->left != NULL)
        {
            m = -1;
            t = t + 10;
        }
        else
            m = 0;
        p->number = '0' + t;
        p = p->left;
        q = q->left;
    } while (q != NULL);
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值