---
title: "C++之大整数相加"
author: "Qaohao"
date: "2009-09-17T17:38:46Z"
tags: ["C", "C#", "C++", "OS", "工作"]
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2010-06-13T10:08:51Z"
id: 470598
url: "https://qaohao.iteye.com/blog/470598"
---

好久没有用过c++写代码了，自从工作一直都用的是java，今天有空就写了端代码，放在自己博客里面晒晒。 自己用c++封装了一个bigDecimal类，里面实现了大整数相加的操作，目前不支持负数相加。 \[b]代码如下：\[/b] \[code="c++"] /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** * Class : bigDecimal * Function : It mainly processes math logical for bigDecimal, such as add, minus, mutiply, divide and ect. * There only presents add operation for bigDecimal ,other to do in future. * Author : qaohao * Version : 1.0 * Date : Sep 17th, 2009. * \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ #include // assign buffer size for console input. const int IN\_STREAM\_BUFFER\_SIZE = 1000; const int OK= 0; const int ERR\_CODE\_NULL= 1; const int ERR\_CODE\_NOTHING =2; const int ERR\_CODE\_UNSUPPORT\_NUM =3; /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** * Class define part \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ class bigDecimal { public: bigDecimal(void); bigDecimal(const char* decimal); ~bigDecimal(void); bigDecimal& operator+(const bigDecimal& rDecimal); friend std::ostream& operator&lt;&lt;(std::ostream& os, const bigDecimal& decimal); friend std::istream& operator&gt;&gt;(std::istream& is, bigDecimal& decimal); private: int bits; char* contents; int validate(const char* input); void throwExp(int errCode); }; /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** * Class Implementation part \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ /* * create an bigDecimal with its content is 0. \*/ bigDecimal::bigDecimal(void) { this-&gt;contents = new char\[2]; \*this-&gt;contents = '0'; \*(this-&gt;contents+1) = '\\0'; this-&gt;bits = 1; } /* * create an bigDecimal with its content is decimal char sequence. \*/ bigDecimal::bigDecimal(const char \*decimal) { int errCode = this-&gt;validate(decimal); if (errCode!=OK) { this-&gt;throwExp(errCode); } this-&gt;bits = strlen(decimal); this-&gt;contents = new char\[bits]; strcpy(this-&gt;contents,decimal); } /* * release its memory, which called by compiler. \*/ bigDecimal::~bigDecimal(void) { delete \[]contents; } /* * add two bigDecimal, return its total. \*/ bigDecimal& bigDecimal::operator +(const bigDecimal &rDecimal) { // calculate max number of bits for total, and set maxBits more than it 1. int maxBits; if (this-&gt;bits &gt; rDecimal.bits) { maxBits = this-&gt;bits + 1; } else { maxBits = rDecimal.bits + 1; } char* total = new char\[maxBits+1]; \*(total+maxBits)='\\0'; bool carry = false; int lNum,rNum,lNumBitCnt=1,rNumBitCnt=1; // calculate total of big decimals. for (int cnt=1;cnt this-&gt;bits) { lNum = 0; } else { lNum = \*(this-&gt;contents + this-&gt;bits - lNumBitCnt) - '0'; } // rNum has added the highest bit. if (rNumBitCnt &gt; rDecimal.bits) { rNum = 0; } else { rNum = \*(rDecimal.contents + rDecimal.bits - rNumBitCnt) - '0'; } /* * when there is a carry at highest bit, return an bigDecimal which directly instanciates by total char sequence. * Else, return an bigDecimal by char sequence which omits the first char of total. \*/ if (carry) { \*(total+maxBits-cnt) = (rNum + lNum + 1)%10 + '0'; carry = (rNum + lNum + 1)/10 != 0; } else { \*(total+maxBits-cnt) = (rNum + lNum)%10 + '0'; carry = (rNum + lNum)/10 != 0; } rNumBitCnt++; lNumBitCnt++; } bigDecimal* totalDecimal; // has carry bit at lower bit when adding. if (carry) { \*total = '1'; totalDecimal = new bigDecimal(total); } else { totalDecimal = new bigDecimal(total+1); } delete\[] total; return \*totalDecimal; } /* * validate input char sequence. * if input is null, return ERR\_CODE\_NULL, * if input is none, return ERR\_CODE\_NOTHING, * if input contains illegal characters(beside 0-9), return ERR\_CODE\_UNSUPPORT\_NUM, * other, return OK. \*/ int bigDecimal::validate(const char* input) { if (input == NULL) { return ERR\_CODE\_NULL; } int len = strlen(input); if (len==0) { return ERR\_CODE\_NOTHING; } for (int cnt=0;cnt'9') { return ERR\_CODE\_UNSUPPORT\_NUM; } } return OK; } /* * throw exception to client when error occurs. \*/ void bigDecimal::throwExp(int errCode) { char* msg = NULL; switch(errCode) { case ERR\_CODE\_NULL: msg = "Warning!Passing parameter for bigDecimal is null."; break; case ERR\_CODE\_NOTHING: msg = "Warning!Passing parameter for bigDecimal is none."; break; case ERR\_CODE\_UNSUPPORT\_NUM: msg = "Warning!Passing parameter for bigDecimal has illegal character."; break; default:; } if (msg) { throw std::exception(msg); } } /* * override output operator '&lt;&lt;' to print bigDecimal directly on console. \*/ std::ostream& operator &lt;&lt;(std::ostream &os, const bigDecimal& decimal) { os &lt;&lt; decimal.contents; return os; } /* * override input operator '&gt;&gt;' to read bigDecimal directly on console. \*/ std::istream& operator&gt;&gt;(std::istream& is, bigDecimal& decimal) { char* buf = new char\[IN\_STREAM\_BUFFER\_SIZE]; is&gt;&gt;buf; int errCode = decimal.validate(buf); if(errCode!=OK){ decimal.throwExp(errCode); } decimal.bits = strlen(buf); decimal.contents = new char\[decimal.bits+1]; strcpy(decimal.contents, buf); delete\[] buf; return is; } /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** * Main \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ int main(void) { char key='y'; while (key=='y') { try { bigDecimal a,b; std::cout&lt;&lt;"Input left operated decimal: "; std::cin&gt;&gt;a; std::cout&lt;&lt;"Input right operated decimal: "; std::cin&gt;&gt;b; std::cout&lt;&lt;"Total:"&lt;&gt;key; } catch (std::exception e) { std::cout&lt;