求两个大数之和
来源:互联网 发布:北京网络职业学院真吗 编辑:程序博客网 时间:2024/06/11 16:28
在计算机中,计算大数的时候,一般都不会超过8个字节的数,如果超过了该怎么计算呢,这个时候我们需要将大数转换成数组,然后进行运算。
/*** Name: Add two big number** Author: WJY** Date: 2014-07-26*/#include <iostream>using namespace std;#define ARRAY_SIZE128// Get number from stdinvoid GetNumber(char buff[], int& size);// Reverse the elements of the array buffervoid ReverseArray(char buff[], int size);// Adding two big numbervoid AddBigNumber(char buff1[], int size1, char buff2[], int size2, char result[], int& size3);// Show the adding resultvoid DisplayResult(char result[], int size);int main(int argc, char *argv[]){char aData[128], bData[128], sum[128];int size1 = 0, size2 = 0, resultsize = 0;cout << "Add two big number" << endl;cout << "Enter the first big number, ending with an enter: " << endl;GetNumber(aData, size1);cout << "Enter the second big number, ending with an enter: " << endl;GetNumber(bData, size2);ReverseArray(aData, size1);ReverseArray(bData, size2);AddBigNumber(aData, size1, bData, size2, sum, resultsize);DisplayResult(sum, resultsize);system("pause");return 0;}void GetNumber(char buff[], int& size){char next;cin.get(next);while (next != '\n' && size < ARRAY_SIZE){buff[size++] = next;cin.get(next);}}void ReverseArray(char buff[], int size){char temp;int i, j;i = 0;j = size - 1;while (i <= j){temp = buff[i];buff[i] = buff[j];buff[j] = temp;i++;j--;}}void AddBigNumber(char buff1[], int size1, char buff2[], int size2, char result[], int& size3){int i = 0, j = 0;int res = 0;int carry = 0;while (i < size1 && j < size2){res = (buff1[i]^0x30) + (buff2[j]^0x30) + carry;result[size3++] = (res % 10)^0x30;carry = res / 10;i++;j++;}// when the first big number greater than the second numberwhile (i < size1){int ret = (buff1[i]^0x30) + carry;result[size3++] = (ret % 10)^0x30;carry = ret / 10;i++;}//when the second big number greater than the first numberwhile (j < size2){int ret = (buff2[j]^0x30) + carry;result[size3++] = (ret % 10)^0x30;carry = ret / 10;j++;}if (i == size1 && j == size2 && carry != 0)result[size3++] = carry^0x30;result[size3] = '\0';}void DisplayResult(char result[], int size){ReverseArray(result, size);cout << "The adding result is : " << result << endl;}
0 0
- 求两个大数之和
- 求两个数之和
- 求两个整数之和
- 求两个大数的和
- 求a和b两个数之和
- 求两个对称矩阵之和与乘积
- 求a和b两个数之和
- 【c语言】求两个整数之和
- 求两个矩阵之和(重载)
- C语言:求两个整数之和
- 求两个数字之和(c++)
- 求A和B两个数之和
- C语言 求两个整数之和
- POJ 1519 求大数各位上数字之和 大数处理 递归
- 不用+-×÷求两个整数的之和
- C#第三周 任务4 求两个数之和
- 最简单的OJ题目(求两个整数之和)
- 不用+-×÷求两个整数的之和
- Billboard
- 超级玛丽HTML5源代码学习------(一)
- UIView的中处理点击事件
- 段错误bug的调试
- UVA 536 - Tree Recovery(树)
- 求两个大数之和
- MAT(Memory Analyzer Tool)工具入门介绍
- hashcode和equals的读书笔记
- [Cocoa]深入浅出 Cocoa 之多线程 NSThread
- linux stat函数讲解
- toms sale HfZp sAcs
- fifa coins online ZOle lKQA
- toms sale CNbE NxPC
- [Cocoa]深入浅出 Cocoa 之消息