高精度(大数)的四则运算与逻辑运算---c++ struct版
发布时间:2020-12-14 03:29:06 所属栏目:大数据 来源:网络整理
导读:因为刚好做一个高精度的加法题,就顺便把高精度的四则运算都写了。 以下代码是用struct实现的,下次再用类实现吧。 ps:保证大数都是正整数(有负数或带小数的写不来23333OTZ)。 直接上代码吧。。 #include iostream#include string#include string.h#inclu
因为刚好做一个高精度的加法题,就顺便把高精度的四则运算都写了。 以下代码是用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,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; else return 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; } 以后有时间再优化一下吧。。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |