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;
}

















目录
相关文章
|
7月前
|
C++
C++ 根据程序运行的时间和cpu频率来计算在另外的cpu上运行所花的时间
C++ 根据程序运行的时间和cpu频率来计算在另外的cpu上运行所花的时间
69 0
|
5月前
|
存储 程序员 编译器
简述 C、C++程序编译的内存分配情况
在C和C++程序编译过程中,内存被划分为几个区域进行分配:代码区存储常量和执行指令;全局/静态变量区存放全局变量及静态变量;栈区管理函数参数、局部变量等;堆区则用于动态分配内存,由程序员控制释放,共同支撑着程序运行时的数据存储与处理需求。
293 22
|
6月前
|
C++
【C++基础】程序流程结构详解
这篇文章详细介绍了C++中程序流程的三种基本结构:顺序结构、选择结构和循环结构,包括if语句、三目运算符、switch语句、while循环、do…while循环、for循环以及跳转语句break、continue和goto的使用和示例。
97 2
|
7月前
|
PHP C++ Python
右手坐标系,空间点绕轴旋转公式&程序(Python和C++程序)
右手坐标系,空间点绕轴旋转公式&程序(Python和C++程序)
171 0
|
7月前
|
C++
c++学习笔记03 程序流程结构
C++学习笔记,主要介绍了程序流程结构,包括顺序结构、选择结构和循环结构。选择结构中详细解释了if语句、三目运算符和switch语句的用法和注意事项。循环结构部分则涵盖了while循环、do-while循环和for循环的语法和使用技巧。此外,还介绍了跳转语句,包括break、continue和goto语句的用途和用法。
51 0
|
9月前
|
存储 算法 编译器
程序与技术分享:C++模板元编程简介
程序与技术分享:C++模板元编程简介
70 0
|
9月前
|
存储 JavaScript 前端开发
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
|
9月前
|
自然语言处理 C语言 C++
程序与技术分享:C++写一个简单的解析器(分析C语言)
程序与技术分享:C++写一个简单的解析器(分析C语言)
|
9月前
|
存储 算法 编译器
程序与技术分享:C++模板元编程学习笔记
程序与技术分享:C++模板元编程学习笔记