高精度(大数)的四则运算与逻辑运算---c++ struct版
来源:互联网 发布:mysql自动断开 编辑:程序博客网 时间:2024/05/21 05:42
因为刚好做一个高精度的加法题,就顺便把高精度的四则运算都写了。
以下代码是用struct实现的,下次再用类实现吧。
ps:保证大数都是正整数(有负数或带小数的写不来23333OTZ)。
直接上代码吧。。
#include <iostream>#include <string>#include <string.h>#include <stdio.h>using namespace std;const int maxn = 1000; // 最大运算位数int max(int a, int b){return a > b ? a : b;}struct bign{int len, s[maxn]; //len记录数字的位数,s存储数字s[0],s[1],s[2]。分别是个位十位百位(以此类推)bign() { memset(s, 0, sizeof(s)); len = 1; }bign operator=(const char * num) //将字符串赋值给大数类型{len = strlen(num);for (int i = 0; i < len; i++)s[i] = num[len - i - 1] - '0';int i = len - 1;while (i > 0 && s[i] == 0) i--; // 验证数字的真正大小,避免“0000010”的这样情况len = i + 1;return *this;}bign operator=(int num) //将int类型赋值给大数类型{char s[maxn];sprintf(s, "%d", num);*this = s;return *this;}bign operator=(const bign & b) //大数类型的互相赋值{len = b.len;for (int i = 0; i < len; i++)s[i] = b.s[i];return *this;}bign(int num) { *this = num; } //将int转换为大数类型bign(const char * num) { *this = num; } //同上string str() const //将大数类型(副本)转换为string{string res = "";for (int i = 0; i < len; i++)res = (char)(s[i] + '0') + res;if (res == "")res == "0";return res;}bign operator+(const bign & b) const {bign c;c.len = 0;for (int i = 0, g = 0; g || i < max(len, b.len); i++){int x = g;if (i < len)x += s[i];if (i < b.len)x += b.s[i];c.s[c.len++] = x % 10;g = x / 10;}return c;}bign operator-(const bign & b) const{bign c;bign temp;temp = *this;c.len = 0;for (int i = 0; i < max(temp.len, b.len); i++){int x = 0;if (i < temp.len)x += temp.s[i];if (i < b.len)x -= b.s[i];if (x >= 0)c.s[c.len++] = x;else{c.s[c.len++] = x + 10;temp.s[i + 1]--;}}int i = c.len - 1;while (i > 0 && c.s[i] == 0) i--; //确定数字的真正位数c.len = i + 1;return c;}bign operator*(const bign & b) const{bign c;for (int i = 0; i < len; i++){for (int j = 0; j < b.len; j++){c.s[i + j] += s[i] * b.s[j];}}for (int i = 0; i < len + b.len - 1; i++){c.s[i + 1] += c.s[i] / 10;c.s[i] = c.s[i] % 10;}for (int i = len + b.len + 2; i > 0; i--)if (c.s[i] != 0){c.len = i + 1;break;}return c;} //定义逻辑运算符bool operator<(const bign & b) const{if (len != b.len)return len < b.len;for (int i = len; i > 0; i--)if (s[i] != b.s[i])return s[i] < b.s[i];}bool operator>(const bign & b) const{return b < *this;}bool operator<=(const bign & b) const{return !(b < *this);}bool operator>=(const bign & b) const{return !(*this < b);}bool operator==(const bign & b) const{if (*this < b)return false;else if (*this > b)return false;elsereturn true;}bool operator!=(const bign & b) const{return !(*this == b);}//除法(整除)bign operator/(const bign & b) const{ bign temp, result; temp = *this; result = 0; while (temp > b) { temp = temp - b; result = result + 1; } return result;}//取余数bign operator%(const bign & b) const{ bign result; result = *this; while (result > b) result = result - b; return result;}};istream & operator>>(istream & in, bign & x){string s;in >> s;x = s.c_str();return in;}ostream & operator<<(ostream & out, const bign & x){out << x.str();return out;}int main(){bign a, b, c;cin >> a >> b; cout << a + b << endl; cout << a * b << endl; cout << a - b << endl; cout << a / b << endl; cout << a % b << endl;return 0;}
以后有时间再优化一下吧。。
0 0
- 高精度(大数)的四则运算与逻辑运算---c++ struct版
- 大数的高精度四则运算
- 大数四则运算(高精度)
- C++实现高精度大整数(大数)的四则运算
- 大数四则运算(C语言)
- 大数之四则运算(C++)
- 大数的四则运算与比较
- 任意长度大数 高精度四则运算
- 大数的四则运算 (模板)
- 大数四则运算c
- 大数加法 减法 乘法 除法 高精度四则运算
- 大数加法 减法 乘法 除法 高精度四则运算
- 高精度整数的四则运算
- 大数四则运算的实现
- 大数的四则运算
- 算法-大数的四则运算
- 大数的四则运算
- 大数的四则运算
- hdu 1251 统计
- FZU 2184 Moon Game(几何题)
- as汇编命令
- 读360SDK心得
- 权限android.permission.WRITE_EXTERNAL_STORAGE 内外置sd卡写权限
- 高精度(大数)的四则运算与逻辑运算---c++ struct版
- 实战Jquery(二)--可以编辑的表格
- 第二周工作报告
- Objective-C研究之词典对象学习(八)
- 获取鼠标点击兼容实现
- fork子进程时和父进程之间的文件描述符的问题
- Openstack前传八(KVM高级特性)
- 心在下雨
- 25 A Famous Music Composer