描述

给出两个正整数以及四则运算操作符(+ - * /),求运算结果。

输入第一行:正整数a,长度不超过100
第二行:四则运算符o,o是“+”,“-”,“*”,“/”中的某一个
第三行:正整数b,长度不超过100

保证输入不含多余的空格或其它字符输出一行:表达式“a o b”的值。

补充说明:
1. 减法结果有可能为负数
2. 除法结果向下取整
3. 输出符合日常书写习惯,不能有多余的0、空格或其它字符样例输入

9876543210
+
9876543210

样例输出

19753086420

Code:

#include<iostream>
#include<cstring>
using namespace std; const int MAXLEN = 201; int Substract(int *p1, int *p2, int len1, int len2) {
int i;
if (len1 == len2) {
for (i = len1 - 1; i >= 0; ++i) {
if (p1[i] < p2[i]) return -1; // p1 < p2
else if (p1[i] > p2[i]) break; // p1 > p2
}
}
for (i = 0; i < len1; ++i) { // 要求调用本函数确保当i >= len2 时, p2[i] = 0
p1[i] -= p2[i];
if (p1[i] < 0) {
p1[i] += 10;
--p1[i+1];
}
}
for (i = len1 - 1; i >= 0; --i) {
if (p1[i]) return i + 1; // 找到最高位第一个不为0
return 0; // 全部为0说明两者相等
}
} class Integer {
private:
int is_neg;
int len;
int s[MAXLEN];
char str[MAXLEN]; public:
Integer(const char *string = "") {
memset(s, 0, MAXLEN*sizeof(int));
memset(str, 0, MAXLEN*sizeof(char));
strcpy(str, string);
is_neg = 0;
len = strlen(str);
for (int i = 0; i < len; ++i) {
s[i] = int(str[len-1-i]) - 48; // s[i]低位在左, 高位在右。
}
} Integer& operator = (const Integer& oth) {
if (this == &oth) return *this;
memset(s, 0, MAXLEN*sizeof(int));
memset(str, 0, MAXLEN*sizeof(char));
is_neg = oth.is_neg;
len = oth.len;
for (int i = 0; i < oth.len; ++i) {
s[i] = oth.s[i];
}
strcpy(str, oth.str);
return *this;
} bool operator == (const Integer& oth) {
if (this == &oth) return true;
bool ret = true;
if (len != oth.len || is_neg != oth.is_neg)
ret = false;
if (strcmp(str, oth.str)) ret = false;
for (int i = 0; i < oth.len; ++i) {
if (s[i] != oth.s[i]) ret = false;
}
return ret;
} bool operator != (const Integer& oth) {
return !(*this == oth);
} Integer operator+(const Integer& oth) {
Integer c;
int length = len >= oth.len ? len : oth.len;
length += 1;
c.len = length;
for (int i = 0; i < c.len; ++i) {
c.s[i] += s[i] + oth.s[i];
c.s[i+1] += c.s[i] / 10;
c.s[i] = c.s[i] % 10;
} while ((c.len > 1) && (c.s[c.len-1] == 0))
c.len--; return c;
} Integer operator-(const Integer& oth) {
Integer c;
int flag = 0; if (len > oth.len) flag = 1;
else if (len < oth.len) flag = -1;
else flag = strcmp(str, oth.str); if (flag >= 0) {
c.len = len;
int borrow = 0;
for (int i = 0; i < c.len; ++i) {
c.s[i] += s[i] - oth.s[i];
if (borrow) c.s[i] -= 1;
if (c.s[i] < 0) {
c.s[i] += 10;
borrow = 1;
} else {
borrow = 0;
}
}
while ((c.len > 1) && (c.s[c.len-1] == 0))
c.len--;
} else {
c.is_neg = 1;
c.len = oth.len;
int borrow = 0;
for (int i = 0; i < c.len; ++i) {
c.s[i] += oth.s[i] - s[i];
if (borrow) c.s[i] -= 1;
if (c.s[i] < 0) {
c.s[i] += 10;
borrow = 1;
} else {
borrow = 0;
}
}
while ((c.len > 1) && (c.s[len-1] == 0))
c.len--;
}
return c;
} Integer operator*(const Integer& oth) {
Integer c;
c.len = len + oth.len + 1;
for (int i = 0; i < len; ++i) {
for (int j = 0; j < oth.len; ++j) {
c.s[i+j] += s[i] * oth.s[j];
c.s[i+j+1] += c.s[i+j] / 10;
c.s[i+j] = c.s[i+j] % 10;
}
}
while ((c.len > 1) && (c.s[c.len-1] == 0))
c.len--; return c;
} Integer operator/(Integer& oth) {
Integer c;
c.len = MAXLEN;
int i, temp;
if (len < oth.len) {
for (int i = 0; i < MAXLEN; ++i) {
c.s[i] = 0;
}
return c;
}
int nTimes = len - oth.len;
if (nTimes > 0) {
for (i = len-1; i >= nTimes; --i) {
oth.s[i] = oth.s[i-nTimes];
}
for (; i >= 0; --i) {
oth.s[i] = 0;
}
oth.len = len;
}
for (i = 0; i <= nTimes; ++i) {
while ((temp = Substract(s, oth.s+i, len, oth.len-i)) >= 0) {
len = temp;
++c.s[nTimes-i];
}
}
while ((c.len > 1) && (c.s[c.len-1] == 0))
c.len--; return c;
} friend ostream & operator<<(ostream& out, const Integer& oth) {
if (oth.is_neg) out << "-";
for (int k = oth.len-1; k >= 0; --k)
out << oth.s[k];
return out;
}
}; int main() {
char s1[MAXLEN], s2[MAXLEN];
char ope;
cin >> s1;
cin >> ope;
cin >> s2;
Integer a(s1);
Integer b(s2); switch (ope) {
case '+':
cout << a + b << endl;
break;
case '-':
cout << a - b << endl;
break;
case '*':
cout << a * b << endl;
break;
case '/':
cout << a / b << endl;
break;
} return 0;
}

  

reference:

https://blog.csdn.net/ApplePeel_90/article/details/80066215

D:大整数的加减乘除的更多相关文章

  1. POJ C++程序设计 编程题#1 大整数的加减乘除

    编程题#4:大整数的加减乘除 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 ...

  2. N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)

    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...

  3. Java 实现大整数加减乘除

    自己用Java实现的大整数加减乘除运算.还有可以改进的地方,有兴趣的童鞋可以加以改进.仅供参考,请勿转载! package barrytest; import java.util.ArrayList; ...

  4. 大整数类BIGN的设计与实现 C++高精度模板

    首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...

  5. C++高精度计算(大整数类)

    Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需 ...

  6. poj2389-Bull Math(大整数乘法)

    一,题意: 大整数乘法模板题二,思路: 1,模拟乘法(注意"逢十进一") 2,倒序输出(注意首位0不输出) 三,步骤: 如:555 x 35 = 19425  5 5 5  5 5 ...

  7. AC日记——大整数的因子 openjudge 1.6 13

    13:大整数的因子 总时间限制:  1000ms 内存限制:  65536kB 描述 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k. 输入 ...

  8. Ac日记——大整数减法 openjudge 1.6 11

    11:大整数减法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个大的正整数相减的差. 输入 共2行,第1行是被减数a,第2行是减数b(a > b).每个大整数不超过20 ...

  9. AC日记——大整数加法 openjudge 1.6 10

    10:大整数加法 总时间限制:  1000ms 内存限制:  65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输出 ...

随机推荐

  1. StampedLock

    StampedLock是Java8引入的一种新的所机制,简单的理解,可以认为它是读写锁的一个改进版本,读写锁虽然分离了读和写的功能,使得读与读之间可以完全并发,但是读和写之间依然是冲突的,读锁会完全阻 ...

  2. ansibel---tag模块

    如果你有一个大的剧本,你可以在不运行整个剧本的情况下运行一个特定的部分.  由于这个原因,两个游戏和任务都支持一个“标记:”属性.您只能根据命令行中的标记(标记或- skip- tags)对任务进行筛 ...

  3. List批量赋值的几种方法

    List<int> list = new List<int>();list.AddRange(new int[] { 1, 5, 10, 20 ,33 }); //也可直接赋值 ...

  4. 【树莓派智能门锁】使用脚本控制GPIO来开锁【4】

    假定你已经通过此文章或者其他方式完成了树莓派的基本配置 [树莓派]RASPBIAN镜像初始化配置 我们通过VNC View连接到树莓派查看一下~ 1.更新一下基本的设置:更新一下源,把python-d ...

  5. LinqHelper连接数据库配置

    LinqHelper连接数据库配置/// <summary> /// Linq通用数据访问类 /// 指定TDataBase来代替后面要使用的数据上下文(指代) /// where:说明指 ...

  6. leetcode766

    本题经过一下午的思考,终于解出来了.使用的是层次遍历的思想. class Solution { public: bool isToeplitzMatrix(vector<vector<in ...

  7. Visual C++ Samples-------------Code Project

    https://msdn.microsoft.com/en-us/library/hyds2fy1(v=vs.80).aspx

  8. 解决Axis2在webservice中遇到特殊字符的无法传输的缺陷(<CDATA>数据类型)

    在使用Axis2进行soa webservice开发时,遇到类似以下的错误信息: com.ctc.wstx.sw.BaseStreamWriter.writeCharacters(BaseStream ...

  9. 01 asp.net编程笔记

    1.asp.net 获取当前网址url 参考地址:http://www.cnblogs.com/190196539/archive/2011/12/13/2286072.html 设当前页完整地址是: ...

  10. Bootstrap 概览

    目录1.移动设备2.响应式图片3.Normalize4.Containers 1.移动设备在Bootstrap 3中,我们重写了整个框架,使其一开始就是对移动设备友好的.这次不是简单的增加一些可选的针 ...