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

时间: 2024-09-19 09:30:46

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

《UNIX网络编程 卷1:套接字联网API(第3版)》——1.5 一个简单的时间获取服务器程序

1.5 一个简单的时间获取服务器程序 我们可以编写一个简单的TCP时间获取服务器程序,它和1.2节中的客户程序一道工作.图1-9给出了这个服务器程序,它使用了上一节中讲过的包裹函数. 创建TCP套接字10 TCP套接字的创建与客户程序相同. 把服务器的众所周知端口捆绑到套接字11~15 通过填写一个网际套接字地址结构并调用bind函数,服务器的众所周知端口(对于时间获取服务是13)被捆绑到所创建的套接字.我们指定IP地址为INADDR_ANY,这样要是服务器主机有多个网络接口,服务器进程就可以在

使用C# Builder创建一个简单的ASP.NET应用程序

一般网站建设通常要求开发人员做后台的程序设计,前面有专业的美工做界面设计.虽然有时候开发人员也会做些界面设计,但是通常都无法达到专业的要求.在以前的ASP中,由于代码和HTML页面语言混杂在一起,这就使得网站的建设变得相当的困难.但在ASP.NET中,这种情况完全改变了.下面就用C# Builder创建一个简单的ASP.NET应用程序. 打开C# Builder,选择 File>New>other-菜单项,你将会看到下面的窗口: 我们选择C# ASP Projects,你就会看到右边有3种可供

数据结构-设计一个简单的英文关键词提取程序

问题描述 设计一个简单的英文关键词提取程序 设计一个简单的英文关键词提取程序,可实现对一段英文短文中出现的频率最高的三个到五个词或短语进行提取.要求:1. 从文件中读取一篇英文短文(300词以内),并显示在屏幕上.2. 按照出现频率顺序显示三个到五个词语,并注明出现的次数 解决方案 你把这里当成可以免费帮你写作业的地方么 解决方案二: 如果按你的要求,则可以统计所有单词出现的频率,看那个出现的次数最多,就是你想要的结果. 解决方案三: string[] 数据 = { ""zero&qu

界面-基于WinPcap,编写一个简单的数据包捕获程序

问题描述 基于WinPcap,编写一个简单的数据包捕获程序 * 功能要求: 1) 要求提供图形界面(类似Wireshark),可以捕获网络数据包:提供包过滤功能,可以输入过滤表达式:可以指定时段抓包:显示指定数据包的内容: 2) 提供网络数据包统计功能:提供输入IP地址,向指定IP地址发送数据包 3) 提供数据转储功能,将捕获到的数据包存储到磁盘文件,并可以读取转储的文件 4) 界面美观.大方 解决方案 一个简单的摄像头视频浏览和捕获的程序(转载)WinPcap捕获数据包 解决方案二: ** 看

BtBatStat 0.4发布 一个简单的OS X应用程序

BtBatStat是一个简单的OS X应用程序,显示您的苹果蓝牙鼠标和键盘在菜单栏中的电池状态.它支持苹果BT键盘,苹果Mighty鼠标,和苹果魔术鼠标.其他设备也可以工作. BtBatStat 0.4该版本现在同时支持触控板+鼠标. 下载地址:http://code.google.com/p/btbatstat/downloads/detail?name=BtBatStat-0.4.zip

密码学-求一个简单ElGamal数字签名,控制台程序代码就行

问题描述 求一个简单ElGamal数字签名,控制台程序代码就行 求一个简单ElGamal数字签名,C或C++控制台程序代码就行, rsa数字签名也有的更好,谢谢大神,让我应付下密码学的算法设计. 解决方案 http://www.pudn.com/downloads113/sourcecode/crypt/detail472998.htmlhttp://www.codesoso.net/Record/122168_115197_1.htmlhttp://download.csdn.net/down

【求助】如果要编写一个简单的SQL漏洞注入程序,需要学习哪方面的知识?

问题描述 我想学习编写一个简单的SQL漏洞注入程序,可惜完全没头绪,希望高手能指点迷津,最好能介绍点相关资料,语言也推荐下

oj问题-为什么我的这个大数相加程序在oj上跑出来的结果是OLE

问题描述 为什么我的这个大数相加程序在oj上跑出来的结果是OLE #include<stdio.h>#include<string.h>int main(){ char str1[1001]str2[1001]*num1*num2*p1*p2; int ncase/*多组输入数目*/mcase=1/*输出时的第几个输出计数器*/up/*进位存储器*/len1len2len; scanf(""%d""&ncase); while(nca

c++-这个大数相加程序用了哪些算法在逻辑结构上定义了哪些算法并求流程图

问题描述 这个大数相加程序用了哪些算法在逻辑结构上定义了哪些算法并求流程图 #include #include #include #define max(a,b) (a)>(b)?(a):(b) int big_add(char*,char*,char*); int big_compare(char*,char*,int*); int is_number_string(char*); int main() { char num1[80],num2[80],num3[100]; puts("