高精度(大数)的四则运算与逻辑运算---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;}


一直觉得除法的效率很低,但是其他方法不会啊2333333

以后有时间再优化一下吧。。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 点痣留下褐色印怎么办 氮氧传感器坏了怎么办 考试车离合太松怎么办 胎心监护不过关怎么办 羚羊角的功效与作用发烧怎么办 小孩上课注意力不集中怎么办 2岁宝宝不会说话怎么办 小孩脖子上长淋巴结怎么办 小孩子上课注意力不集中该怎么办 脸过敏干燥起皮怎么办 脸上皮肤干燥起皮怎么办 身上皮肤干燥起皮怎么办 皮肤暗黄有色斑怎么办 我皮肤干燥暗黄怎么办 脸上有皮肤暗黄怎么办 皮肤暗黄毛孔大怎么办 脸上很干燥起皮怎么办 脸上的皮肤起皮怎么办 身体的皮肤好干怎么办 滴油雾化器炸油怎么办 已经发炎的痘痘怎么办 被养生馆骗了怎么办 做完微针结痂了怎么办 秋季脸干燥起皮怎么办 身上的皮肤太干怎么办 板材眼镜腿松了怎么办 超声刀后喝酒了怎么办 开眼角疤痕增生了怎么办 开了眼角有增生怎么办 全切双眼皮留疤怎么办 开内眼角留疤了怎么办 开眼角长了颗粒怎么办 开刀后疤痕庝痛怎么办 眼角开得太尖了怎么办 眼角开得太大了怎么办 开眼角后增生了怎么办 下颌骨宽怎么办不整容 脸两边骨头太宽怎么办 接了睫毛眼睛疼怎么办 下颌骨错位脸歪怎么办 假体隆鼻感冒了怎么办