C++实现的一个简单两个大数相加程序!

简介:
#include <iostream>


using namespace std;
#define ARRAY_SIZE 50


//Enter a big number, and store it as a string into an array ch,
//the size is the numbers of char.
void inputNumbers(char ch[], int& size);


//Reverse the elements of the array ch.
void reverseArray(char ch[], int size);


//Adding two big numbers, and the result will be stored in the array ch3,
//and return the size of the array ch3.
void computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3);


//show the adding result.
void displayResult(char ch[], int size);


int main()
{
char ch1[ARRAY_SIZE], ch2[ARRAY_SIZE], result[ARRAY_SIZE];
int size1 = 0, size2 = 0, resultSize = 0;


cout << "Enter the first big number, ending with an enter:" << endl;
inputNumbers(ch1, size1);
cout << "Enter the second big number, ending with an enter:" << endl;
inputNumbers(ch2, size2);


reverseArray(ch1, size1);
reverseArray(ch2, size2);


computeAdding(ch1, size1, ch2, size2, result, resultSize);
displayResult(result, resultSize);


system("pause");
    return 0;
}


//Function inputNumbers
void inputNumbers(char ch[],  int& size)
{
char next;


    cin.get(next);
    while (next != '\n'  && size < ARRAY_SIZE)
    {
          ch[size++] = next;
          cin.get(next);
    }
}//inputNumbers


//Function reverseArray
void reverseArray(char ch[], int size)
{
int i = 0, j = size-1;
    char temp;


while (i <= j)
{
temp =  ch[i];
ch[i] = ch[j];
ch[j] = temp;
i ++;
j --;
}
}//end reverseArray function


//function computeAdding's definition
void computeAdding(char ch1[], int size1, char ch2[], int size2, char ch3[], int& size3)
{
    int i,
          tmp,                  //As the temporary sum of two array elements.
          carryBit = 0;    //The carry-bit is initialized to zero


    for (i = 0; i < size1 && i < size2;  i++)
    {
        tmp = (ch1[i]-'0') + (ch2[i]-'0') + carryBit;
        ch3[i] = tmp % 10 + '0';
        carryBit = tmp/10;
    }
    while ( i<size1 ) { //If the array ch1 has more bits, execute this while loop.
        tmp = (ch1[i] - '0') + carryBit;
        ch3[i] = tmp % 10 + '0';
        carryBit = tmp / 10;
        i ++;
    }
    while ( i < size2 ){
        tmp = (ch2[i] - '0') + carryBit;
        ch3[i] = tmp % 10 +'0';
        carryBit = tmp / 10;
        i ++;
    }
    if( carryBit)
{
      ch3[i] = carryBit + '0';
      i ++;
     }
    ch3[i] = '\0';


    size3 = i;
}//End reverseArray


//function displayResult
void displayResult(char ch[], int size)
{
reverseArray(ch, size);//make the number to be normal


    cout << "The adding result is:" ;
for (int i = 0; i < size; i++)
   cout << ch[i] ;
    cout << endl;
}

















目录
相关文章
|
26天前
|
存储 缓存 算法
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
【C/C++ 性能优化】提高C++程序的缓存命中率以优化性能
112 0
|
1月前
|
编译器 Linux C++
3C++程序的编写和实现
3C++程序的编写和实现
17 2
|
1月前
|
C++ 开发者
2C++的程序的构成和书写形式
2C++的程序的构成和书写形式
16 2
|
21天前
|
存储 缓存 C++
C++链表常用的函数编写(增查删改)内附完整程序
C++链表常用的函数编写(增查删改)内附完整程序
|
1月前
|
缓存 编译器 程序员
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
C/C++编译器并行优化技术:并行优化针对多核处理器和多线程环境进行优化,以提高程序的并行度
61 0
|
1月前
|
缓存 编译器 程序员
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
C/C++编译器全局优化技术:全局优化是针对整个程序进行的优化,包括函数之间的优化
27 0
|
1月前
|
缓存 算法 编译器
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
C/C++编译器内存优化技术:内存优化关注程序对内存的访问和使用,以提高内存访问速度和减少内存占用。
37 0
|
1月前
|
自然语言处理 编译器 调度
深入gcc编译器:C/C++代码如何变为可执行程序
深入gcc编译器:C/C++代码如何变为可执行程序
75 0
|
1月前
|
并行计算 安全 编译器
【C/C++ 编译相关 gcc】一次搞懂GCC编译选项:优化代码、调试程序必备!
【C/C++ 编译相关 gcc】一次搞懂GCC编译选项:优化代码、调试程序必备!
34 0
|
1月前
|
C++ 容器
【C++】程序题( STL标准模板库)
【C++】程序题( STL标准模板库)
25 0
【C++】程序题( STL标准模板库)

热门文章

最新文章