D:大整数的加减乘除
描述
给出两个正整数以及四则运算操作符(+ - * /),求运算结果。
输入第一行:正整数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:大整数的加减乘除的更多相关文章
- POJ C++程序设计 编程题#1 大整数的加减乘除
编程题#4:大整数的加减乘除 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 ...
- N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...
- Java 实现大整数加减乘除
自己用Java实现的大整数加减乘除运算.还有可以改进的地方,有兴趣的童鞋可以加以改进.仅供参考,请勿转载! package barrytest; import java.util.ArrayList; ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- C++高精度计算(大整数类)
Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需 ...
- poj2389-Bull Math(大整数乘法)
一,题意: 大整数乘法模板题二,思路: 1,模拟乘法(注意"逢十进一") 2,倒序输出(注意首位0不输出) 三,步骤: 如:555 x 35 = 19425 5 5 5 5 5 ...
- AC日记——大整数的因子 openjudge 1.6 13
13:大整数的因子 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k. 输入 ...
- Ac日记——大整数减法 openjudge 1.6 11
11:大整数减法 总时间限制: 1000ms 内存限制: 65536kB 描述 求两个大的正整数相减的差. 输入 共2行,第1行是被减数a,第2行是减数b(a > b).每个大整数不超过20 ...
- AC日记——大整数加法 openjudge 1.6 10
10:大整数加法 总时间限制: 1000ms 内存限制: 65536kB 描述 求两个不超过200位的非负整数的和. 输入 有两行,每行是一个不超过200位的非负整数,可能有多余的前导0. 输出 ...
随机推荐
- timequest学习之黑金动力(一)
黑金动力的资料还是非常有价值的.通过建模篇,对于给定的时序关系,我总能实现.但是,这总是很初级的能力.也只是为后面的建模服务.所以,现阶段我的能力还是非常有限.我相信我一定会成为牛人,能够独挡一面.借 ...
- Android Binder机制中的异步回调
“Binder通信是同步而不是异步的”,但是在实际使用时,是设计成客户端同步而服务端异步. 看看Framwork层的各service类java源码便会知道,在客户端调用服务端的各种方法时,通常会传递一 ...
- String to Integer (atoi) ???
#define INT_MAX 2147483647 #define INT_MIN -2147483648 class Solution { public: int atoi(const char ...
- BugkuCTF 逆向
Easy_vb 下载文件,打开 用 IDA 或 OD 打开 在中间部分发现 flag 把 MCTF 改成 flag,就是正确的 flag 了 Easy_Re 下载附件,打开
- 分布式爬虫搭建系列 之三---scrapy框架初用
第一,scrapy框架的安装 通过命令提示符进行安装(如果没有安装的话) pip install Scrapy 如果需要卸载的话使用命令为: pip uninstall Scrapy 第二,scrap ...
- srvctl和crs_start命令无法启动oracle RAC实例, 但sqlplus可以启动
今天遇到一个奇怪问题,发现srvctl和crs_start命令无法启动Oracle RAC实例,但用sqlplus却可以正常启动.最终发现原因是在OCR中数据库的状态变成了disable,将此状态更改 ...
- leetcode343
public class Solution { public int IntegerBreak(int n) { ) { ; } ) { ; } var max = int.MinValue; ; i ...
- 【280】◀▶ ArcPy 常用工具说明
目录: 一.相关技巧 二.工具说明 一.相关技巧 技巧1:将工具从工具箱拖拽到 Python 窗体中自动会生成相应的函数,于是可以快速定位函数名称! 技巧2:将通过工具箱实现的操作结果拖拽到 Pyth ...
- EKF model&realization
REF: https://pythonrobotics.readthedocs.io/en/latest/modules/localization.html#unscented-kalman-filt ...
- 利用JDBC连接数据库(MySQL)
1 在eclipse中创建一个maven项目(在Java模式下,不要在JavaEE模式下) 1.1 file -->> new -->> project 2 下载数据库驱动 ...