【面试】大整数四则运算

面试手写代码,当时只写出了加减乘,还写的漏洞百出。下面是加减法:

  1 #include<iostream>
  2 using namespace std;
  3
  4 struct BigN {
  5     int data[1000]; //设最长1000位,数组的每个数字代表一位,data[0]存最低位
  6     int len;
  7     int sign;//0为负,1为正
  8     BigN() {
  9         memset(data, 0, sizeof(data));
 10         len = 0;
 11         sign = 1;
 12     }
 13 };
 14 BigN subFun(BigN a, BigN b);
 15 void output(BigN a) {
 16     if (a.len == 0) cout << "0";
 17     for (int i = a.len-1; i >=0; i--) {
 18         if (a.sign != 1 && i == a.len - 1) cout << "-";
 19         cout << a.data[i];
 20     }
 21     cout << endl;
 22 }
 23 bool com(BigN a, BigN b) { //仅比较绝对值,a<b返回true
 24     if (a.len < b.len) return true;
 25     else if (a.len > b.len) return false;
 26     else {
 27         int tmp = a.len - 1;
 28         while (a.data[tmp]==a.data[tmp]&&tmp>0) {
 29             tmp--;
 30         }
 31         if (a.data[tmp] < a.data[tmp]) return true;
 32         else return false;
 33     }
 34
 35 }
 36 BigN addFun(BigN a, BigN b) {//同号数相加
 37     BigN c;
 38     int carry = 0;
 39     for (int i = 0; i<a.len || i<b.len; i++) {
 40         int temp = a.data[i] + b.data[i] + carry;
 41         c.data[c.len++] = temp % 10;
 42         carry = temp / 10;
 43     }
 44     if (carry != 0) {
 45         c.data[c.len++] = carry;
 46     }
 47     c.sign = a.sign;
 48     return c;
 49 }
 50 BigN subFun(BigN a, BigN b) {//非负大数减非负小数a-b
 51     BigN c;
 52     for (int i = 0; i<a.len || i<b.len; i++) {
 53         int temp = a.data[i] - b.data[i];
 54         if (temp < 0) {
 55             temp += 10;
 56             a.data[i + 1]--;
 57         }
 58         c.data[c.len++] = temp;
 59     }
 60     while (c.data[c.len - 1] == 0 && c.len >= 1) {  //高位为0注意修正长度
 61         c.len--;
 62     }
 63     return c;
 64 }
 65 BigN add(BigN a, BigN b) {
 66     BigN c;
 67     if (a.sign != b.sign) {
 68         if (com(a, b)) {
 69             if (a.sign == 0) {
 70                 a.sign = 1;
 71                 return subFun(b,a);
 72             }
 73             else {
 74                 b.sign = 1;
 75                 c = subFun(b, a);
 76                 c.sign = 0;
 77                 return c;
 78             }
 79         }
 80         else {
 81             if (a.sign == 0) {
 82                 a.sign = 1;
 83                 c = subFun(a, b);
 84                 c.sign = 0;
 85                 return c;
 86             }
 87             else {
 88                 b.sign = 1;
 89                 c = subFun(a, b);
 90                 return c;
 91             }
 92         }
 93     }
 94     else{
 95         return addFun(a, b);
 96     }
 97
 98 }
 99
100 BigN sub(BigN a, BigN b) {
101     BigN c;
102     if (a.sign != b.sign) {
103         b.sign = a.sign;
104         return addFun(a, b);
105     }
106     else {
107         b.sign = (b.sign+1)%2;
108         return add(a, b);
109     }
110
111 }
112 int main() {
113     BigN a, b;
114     for (int i = 0; i < 3; i++) {
115         a.data[i] = i + 1;
116     }
117     a.len = 3;
118
119     for (int i = 0; i < 2; i++) {
120         b.data[i] = 7;
121     }
122     b.len = 2;
123
124
125     BigN aN = a;
126     aN.sign = 0;
127     BigN bN = b;
128     bN.sign = 0;
129
130     cout << "a:";
131     output(a);
132     cout << "b:";
133     output(b);
134     cout << "-a:";
135     output(aN);
136     cout << "-b:";
137     output(bN);
138
139     BigN c = add(a, b);
140     cout << "a+b:";
141     output(c);
142     cout << "a+ -b:";
143     c = add(a, bN);
144     output(c);
145     cout << "-a+b:";
146     c = add(aN, b);
147     output(c);
148     cout << "-a + -b:";
149     c = add(aN, bN);
150     output(c);
151
152     cout << "a-b:";
153     c = sub(a, b);
154     output(c);
155     cout << "a- -b:";
156     c = sub(a, bN);
157     output(c);
158     cout << "-a -b:";
159     c = sub(aN, b);
160     output(c);
161     cout << "-a- -b:";
162     c = sub(aN, bN);
163     output(c);
164 }

原文地址:https://www.cnblogs.com/yuchi328/p/9688609.html

时间: 2024-08-29 03:19:01

【面试】大整数四则运算的相关文章

大整数四则运算(vector)

目录 基础 1. 高精度加法 2. 高精度减法 3. 高精度乘低精度 4. 高精度除以低精度 5. 高精度乘高精度 6. 高精度除以高精度 综合 总结 每逢大整数四则运算,都会怯懦,虽是算法竞赛必会的东西,也零散的学过,简单的总结过,但不成体系的东西心里一直没底. 所以今天消耗了大量的卡路里,啃了几套模板之后终于总结成了一套自己的模板 再也不用担心大整数啦 基础 1. 高精度加法 高精度加法等同于算术加法,做单个的加法运算之后存下进位 A和B都为正整数 vector中下标为0存的是低位(以下都是

大整数四则运算

============ 日后再写!先将设计思想留下 ============= 定义并实现超长整数类double long,要求如下: 64位数据长度,有符号 支持+.-.*./运算 支持+=.-=./=运算 支持cin>>和cout<<操作 首先,我们的运算对象是大整数并且要支持cout<<和cin>>操作,数组和链表是可选择项.在这里我们用数组.数组有int型和char型,选哪个好呢?只能选char型,因为我们的大整数是有符号位的,必须用一个char字

7.2 大整数四则运算

7-2 BigInt1.c 1 #include <stdio.h> 2 #include <string.h> 3 typedef struct bigint 4 { 5 char *num; //指向长整数数组(序号0中保存着最低位) 6 char minus; //符号(1表示正数,-1表示负数) 7 int digit; //保存该数的位数(实际位数) 8 }BIGINT, *pBIGINT; 9 void BigIntTrans(pBIGINT num); //字符串转数

模板 高精度大整数

#include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int MaxL=400; //大数长度 //大整数 //减法只能大减小 struct bign { int len, s[MaxL]; //构造函数 bign () { memset(s, 0, sizeof(s)); len = 1; } bign (int n

大整数的存储与加减乘除四则运算

当需要计算的整数或计算结果可能会超出long long 所能表示的范围时,应该用大整数来存储和计算(Java里面有BigInteger来存储大整数,这里讨论的是C++语言). 大整数的存储形式是下面这个结构体(包含了构造函数): // 大整数结构体 struct bign{ int d[1000]; int len; bign(){ memset(d, 0, sizeof(d)); len = 0; } }; 首先以字符串的形式读去数据,然后将字符串逐个逆序字符存入d[]数组(可以采用用reve

大整数类

大整数类又叫高精度. 就是求大数的四则运算的算法, (其实就是模拟小学生算数的方法, 什么? 你不会, 那你还不如小学生, 哈哈!). 在这里只贴加法运算符的重载,其他的运算符与加法类似.闲言少叙, 直接上代码(小声告诉你, 里面用了几个库函数和STL, 嘿嘿!!!). 1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<iostream> 5 using namespac

日常记录(c语言)--字符串实现大整数加法

运行环境:CentOs 64位--vim 最近在看<剑指offer>这本书,看了前面的关于面试的能力,顿时觉得自己的编程能力差得好远. 可能我对鲁棒的代码理解还不深,我觉得鲁棒应该就是代码可以应对各种不同的输入,都能有相应的处理,并给出相应的输出. 下面是我看了之后对之前做过的大整数加法做了一些完善,之前的只能实现对纯数字字符进行求和,甚至连对空指针的处理都没有,好惭愧.我会用注释来记录自己对此算法的理解. 1 #include <stdio.h> 2 #include <s

大整数的加减乘除

多项式的加减乘除能够利用多项式的加减乘除进行运算,所以下面程序採用了多项式的加减乘除.多项式运算已经在<算法导论>第30章有简要的介绍,详细的请參考数学书. 大整数加法:(利用书上公式轻松得出) //多项式加法-大数加法 #include <iostream> #include <time.h> using namespace std; #define m1 4 #define m2 5 //a[0]=x^0 a[1]=x^1....a[n]=x^n的关于x的多项式系数

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

首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了一个高精度类,允许大整数的四则运算 这个类利用字符串进行输入输出,并利用数组进行储存与处理,通过模拟四则运算,可以计算很大的整数的加减乘除比大小. 贴上我的代码: #include<string> #include<iostream> #include<iosfwd> #i