Problem B: 大整数的加法运算

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 112  Solved: 57
[Submit][Status][Web Board]

Description

我们知道,C++中的整数类型,如short、int、long和long long等都有确定的表示范围,超大的整数是不能表示的。请定义一个类Decimal,用于表示大整数,并实现如下方法:

1.根据给出的main函数定义的构造函数。

2. 重载加法(“+”)运算符,可以实现一个Decimal对象与另一个Decimal对象求和、与一个int类型的数值求和。

3. 重载前缀自增运算符。

4. 重载下标运算符,用于求当前对象的指定下标位置的数字。

5. 重载输入、输出运算符。

Input

输入3个数,前2个可能会超出unsigned long long的表示范围,最后1个是一个int类型的非负整数。

不考虑负数。

Output

见样例。

Sample Input

876543210012345678889
123456789987654321111
15

Sample Output

a = 876543210012345678889
b = 123456789987654321111
i = 15
a = 876543210012345678890
c = 1000000000000000000000
d = 876543210012345678890
e = 123456789987654321126
f = 554433
g = 12345
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int a[1001],b[1001];
char c[1001];
class Decimal
{
public:
    string s1;
    Decimal(string a=""):s1(a){}
    Decimal(unsigned long long int n)
    {
    stringstream ss;
    string str;
    ss<<n;
    ss>>str;
    s1=str;
    }
    friend istream &operator >>(istream & it,Decimal &T)
    {
        it>>T.s1;
        return it;
    }
    friend ostream &operator << (ostream & os,const Decimal &T)
    {
        os<<T.s1;
        return os;
    }
    friend Decimal operator + ( Decimal T1,Decimal T2)
    {
    string a = T1.s1;
    string b = T2.s1;
    int i1 = a.size() - 1;
    int i2 = b.size() - 1;

    string s;
    int carry = 0;
    while (i1 >= 0 || i2 >= 0)
    {
        char ch = carry;
        if (i1 >= 0)
        {
            if (a[i1] < '0' || a[i1] > '9')
                continue;
            ch += a[i1] - '0';
        }
        if (i2 >= 0)
        {
            if (b[i2] < '0' || b[i2] > '9')
                continue;
            ch += b[i2] - '0';
        }
        if (ch >= 10)
        {
            carry = 1;
            ch -= 10;
        }
        else carry = 0;
        s.push_back(ch + '0');
        i1--;
        i2--;
    }
    if (carry) s.push_back('1');
    reverse(s.begin(), s.end());
    return Decimal(s);
    }
    Decimal &operator ++()
    {   string &t=s1;
        int l=t.size();
        l--;
        for(int i=l;i>=0;i--)
        {
            if(t[i]=='9')
            t[i]='0';
            else
            {
                t[i]+=1;
                break;
            }
        }
        return *this;
    }
    char operator [](int i)
    {
        return s1[i];
    }
    int getLength()
    {
        return s1.size();
    }
};
int main()
{
    Decimal a, b, c, d, e, f("554433"), g(12345);
    int i;
    cin>>a>>b>>i;
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"i = "<<i<<endl;
    c = a + b;
    d = ++a;
    e = b + i;
    cout<<"a = "<<a<<endl;
    cout<<"c = "<<c<<endl;
    cout<<"d = "<<d<<endl;
    cout<<"e = "<<e<<endl;
    cout<<"f = "<<f<<endl;
    cout<<"g = "<<g<<endl;

    cout<<c[0];
    for (i = 1; i < c.getLength(); i++)
    {
        cout<<" "<<c[i];
    }
    cout<<endl;
    return 0;
}

本菜瑟瑟发抖

Problem B: 大整数的加法运算的更多相关文章

  1. Problem B: 大整数的加法运算 升级版

    #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...

  2. 大整数加减运算的C语言实现

    目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...

  3. 用Java的大整数类BigInteger来实现大整数的一些运算

    关于BigInteger的构造函数,一般会用到两个: BigInteger(String val); //将指定字符串转换为十进制表示形式: BigInteger(String val,int rad ...

  4. RNN入门(4)利用LSTM实现整数加法运算

      本文将介绍LSTM模型在实现整数加法方面的应用.   我们以0-255之间的整数加法为例,生成的结果在0到510之间.为了能利用深度学习模型模拟整数的加法运算,我们需要将输入的两个加数和输出的结果 ...

  5. 1024 Palindromic Number int_string转换 大整数相加

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  6. 基于Java的大整数运算的实现(加法,减法,乘法)学习笔记

    大整数,顾名思义就是特别大的整数. 一台64位的机器最大能表示的数字是2的64次方减一: 18446744073709551615 java语言中所能表示的整数(int)最小为-2147483648 ...

  7. HDU1002——大整数加法

    题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...

  8. A——大整数加法(HDU1002)

    题目: I have a very simple problem for you. Given two integers A and B, your job is to calculate the S ...

  9. C语言课程设计大整数运算

    该大整数运算系统用于对有符号的位数不超过500位的大整数进行加.减.乘.除四则运算和计算N(0<=N<=10000)的阶乘.注意事项 :    1.操作期间,进行四则运算时若大整数为正数请 ...

随机推荐

  1. TEXT宏

    TEXT宏是windows程序设计中经常遇到的宏,定义在 <winnt.h>中 TCHAR *P = TEXT("this is a const string"); 如 ...

  2. 关于离线底图和离线shp文件的加载

    首先底图是我自己用百度地图18级别的瓦片图在armap中制作的TPK文件,shp图层是我用同样的百度地图18级别的瓦片图矢量化的,二者在arcmap中的空间参考是一致的,所以我以为在移动端加入的时候二 ...

  3. Java 浅拷贝和深拷贝的理解和实现方式

    Java中的对象拷贝(Object Copy)指的是将一个对象的所有属性(成员变量)拷贝到另一个有着相同类类型的对象中去.举例说明:比如,对象A和对象B都属于类S,具有属性a和b.那么对对象A进行拷贝 ...

  4. 入门VMware Workstation下的Debian学习之基本命令(二)

    本章记录如何在Linux终端进行命令操作命令下载路径,模拟终端.dkpg管理软件包.用户组和用户管理.文件属性.文件与目录管理.查看磁盘使用量. (1)命令下载路径: wegt 路径; (2)模拟终端 ...

  5. python netifaces模块

    简介 在Linux系统中,我们可以通过ifconfig,route等shell命令来查看系统接口配置,网关和路由等信息.通过shell的正则表达式功能,通过系列复杂操作,我们可以从字符串中提取出相关的 ...

  6. ZOJ2212 Argus 优先队列 结构体

    #include <iostream> #include <string> #include <queue> using namespace std; struct ...

  7. ELK日志收集分析系统配置

    ELK是日志收益与分析的利器. 1.elasticsearch集群搭建 略 2.logstash日志收集 我这里的实现分如下2步,中间用redis队列做缓冲,可以有效的避免es压力过大: 1.n个ag ...

  8. DOM 节点 课程表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. C++类中静态变量和静态方法使用介绍

    静态成员的提出是为了解决数据共享的问题.实现共享有许多方法,如:设置全局性的变量或对象是一种方法.但是,全局变量或对象是有局限性的.这一章里,我们主要讲述类的静态成员来实现数据的共享. 静态数据成员 ...

  10. [解读REST] 6.REST的应用经验以及教训

    衔接上文[解读REST] 5.Web的需求 & 推导REST,上文根据Web的需求推导出了REST架构风格,以及REST的详细描述和解释.自从1994年以来,REST架构风格被用于指导Web架 ...