---
title: "求大数和的两种方案"
author: "misswolf"
date: "2006-07-15T22:18:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2006-07-15T22:18:00Z"
id: 270448
url: "https://misswolf.iteye.com/blog/270448"
---

问题描述:

**5、大数和(30分)&lt;o:p&gt;&lt;/o:p&gt;**

有一些可能达100位的大正整数，求所有这些数的和。当输入#时，运行结束。

 

样例输入：

9999999999123456789011111234&lt;o:p&gt;&lt;/o:p&gt;

90000001012301214&lt;o:p&gt;&lt;/o:p&gt;

111122244413&lt;o:p&gt;&lt;/o:p&gt;

#&lt;o:p&gt;&lt;/o:p&gt;

 &lt;o:p&gt;&lt;/o:p&gt;

样例输出：&lt;o:p&gt;&lt;/o:p&gt;

9999999999213456901145656861&lt;o:p&gt;&lt;/o:p&gt;

方案1:  
这个是用动态数组的SUM函数就是求两数字串的和  
按题意要求可分解为循环求两个数字串的和

Main(){

     建立文件对象

     如果读文件失败则退出程序&lt;o:p&gt;&lt;/o:p&gt;

     结果=”&lt;st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="”" w:st="on"&gt;0”&lt;/st1:chmetcnv&gt;;&lt;o:p&gt;&lt;/o:p&gt;

     While(文件是否已读完){

              读入一串数字到str缓冲区;&lt;o:p&gt;&lt;/o:p&gt;

        结果=Sum(结果,str);&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

打印结果;&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

Char * Sum(char* strLastRes/\*上次的结\*/,const char* strNum){&lt;o:p&gt;&lt;/o:p&gt;

   If(IsNumberString(strLastRes)==false&&IsNumberString(strNum)==false)&lt;o:p&gt;&lt;/o:p&gt;

      Return  “&lt;st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="”" w:st="on"&gt;0”&lt;/st1:chmetcnv&gt;;有一个不为数字串//退出这次加法运算&lt;o:p&gt;&lt;/o:p&gt;

   Int len1=数字串1长度;&lt;o:p&gt;&lt;/o:p&gt;

   Int len2=数字串2长度;&lt;o:p&gt;&lt;/o:p&gt;

   Max=len1&gt;len2?len1:len2;&lt;o:p&gt;&lt;/o:p&gt;

   Result=new char\[Max+1];&lt;o:p&gt;&lt;/o:p&gt;

   If(Result==NULL) 退出程序; &lt;o:p&gt;&lt;/o:p&gt;

用’&lt;st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="’" w:st="on"&gt;0’&lt;/st1:chmetcnv&gt;初始化result;&lt;o:p&gt;&lt;/o:p&gt;

进位标志flat=0;&lt;o:p&gt;&lt;/o:p&gt;

Char a=0,b=0;&lt;o:p&gt;&lt;/o:p&gt;

For(i=Max;i&gt;=0;i--){&lt;o:p&gt;&lt;/o:p&gt;

//从数字串尾逐位往前读数字&lt;o:p&gt;&lt;/o:p&gt;

        len1--; &lt;o:p&gt;&lt;/o:p&gt;

         len2--;&lt;o:p&gt;&lt;/o:p&gt;

     if(len1&lt;0)&lt;o:p&gt;&lt;/o:p&gt;

         a=0;//字符串1已经取完&lt;o:p&gt;&lt;/o:p&gt;

     else{&lt;o:p&gt;&lt;/o:p&gt;

         a=strLastRes\[lengthNum1]-'0';&lt;o:p&gt;&lt;/o:p&gt;

     }&lt;o:p&gt;&lt;/o:p&gt;

     if(len2&lt;0)&lt;o:p&gt;&lt;/o:p&gt;

         b=0;//字符串2已经取完&lt;o:p&gt;&lt;/o:p&gt;

     else&lt;o:p&gt;&lt;/o:p&gt;

                b=strNum\[len2]-'0';&lt;o:p&gt;&lt;/o:p&gt;

     result\[i]=a+b+flag+'0';&lt;o:p&gt;&lt;/o:p&gt;

if(result\[i]-‘&lt;st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="’" w:st="on"&gt;0’&lt;/st1:chmetcnv&gt;&gt;=10){  
   result\[i]减10;&lt;o:p&gt;&lt;/o:p&gt;

 进位标志=1;&lt;o:p&gt;&lt;/o:p&gt;

}else&lt;o:p&gt;&lt;/o:p&gt;

   进位标志=0;&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

Delete \[]strLastRes；//释放上次申请的内存&lt;o:p&gt;&lt;/o:p&gt;

     Return result;&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

ShowResult(const \*str){&lt;o:p&gt;&lt;/o:p&gt;

   Start=pos(str);//第一个不为0位置&lt;o:p&gt;&lt;/o:p&gt;

   For(i=start;i&lt;strlen(str);i++)&lt;o:p&gt;&lt;/o:p&gt;

      Cout&lt;&lt;str\[i];&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

Bool IsNumberString(const char \*str){&lt;o:p&gt;&lt;/o:p&gt;

  For(i=0;i&lt;strlen(str);i++){&lt;o:p&gt;&lt;/o:p&gt;

  If(‘&lt;st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="’" w:st="on"&gt;0’&lt;/st1:chmetcnv&gt;≤str\[i] ≤’&lt;st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="9" numbertype="1" negative="False" unitname="’" w:st="on"&gt;9’&lt;/st1:chmetcnv&gt;)

       Continue;

   Else

           Return false;&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

Return true;&lt;o:p&gt;&lt;/o:p&gt;

}&lt;o:p&gt;&lt;/o:p&gt;

程序流程序图:如 图五  
![](http://p.blog.csdn.net/images/p_blog_csdn_net/woundedsoul/ACM5.jpg)&lt;o:p&gt;&lt;/o:p&gt;

&lt;v:shapetype o:spt="75" coordsize="21600,21600" filled="f" stroked="f" id="\_x0000\_t75" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t"&gt;&lt;v:stroke joinstyle="miter"&gt;&lt;/v:stroke&gt;&lt;v:formulas&gt;&lt;v:f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 1 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum 0 0 @1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @2 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 0 1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @6 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @8 21600 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @10 21600 0"&gt;&lt;/v:f&gt;&lt;/v:formulas&gt;&lt;v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"&gt;&lt;/v:path&gt;&lt;o:lock v:ext="edit" aspectratio="t"&gt;&lt;/o:lock&gt;&lt;/v:shapetype&gt;&lt;v:shape id="\_x0000\_i1025" type="#\_x0000\_t75" style="WIDTH: 414.75pt; HEIGHT: 8in"&gt;&lt;v:imagedata src="file:///C:\\DOCUME~1\\MissWolf\\LOCALS~1\\Temp\\msohtml1\\03\\clip\_image001.jpg" o:title="ACM5"&gt;&lt;/v:imagedata&gt;&lt;/v:shape&gt;&lt;o:p&gt;&lt;/o:p&gt;

图示5&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt; 方案二:  
用一个结构体存放每一位  
struct Node{&lt;/o:p&gt;

&lt;o:p&gt; char data;  
 Node \*next;&lt;/o:p&gt;

&lt;o:p&gt;};&lt;/o:p&gt;

&lt;o:p&gt;Main()  
{&lt;/o:p&gt;

&lt;o:p&gt;     Node \*pResult=new Node;//假设已运算了一次且结果为0  
   pResult-&gt;data='0';  
  .....&lt;/o:p&gt;

&lt;o:p&gt;}  
方案二&lt;/o:p&gt;

&lt;o:p&gt;在输入时逆向插入结点生成链表也就是说我输入123456789存放到链表时成了987654321运算时就可以顺向往后进行运算了! 得到运算结果后,存放结果的&lt;/o:p&gt;

&lt;o:p&gt;链表又成了顺序链表也就是说:假设两个大数的运算结果为1234567那么在一次运算结果的链表时也是1234567&lt;/o:p&gt;

&lt;o:p&gt;要想把这个结果参与到下一次运算得把它倒转过来(#-\_-#,这样是不是很麻烦)才能参与下一次运算  
在输出结果是也得倒转过来!具体看代码吧!这个代码是刚刚写好的,还没有文档的!流程序还没有画的!我的VISIO还没有装的(昨天重装系统了)&lt;/o:p&gt;

&lt;o:p&gt;源程序1:#include &lt;fstream&gt;  
#include &lt;iostream&gt;  
#include &lt;cstdio&gt;  
#include &lt;string&gt;&lt;/o:p&gt;

&lt;o:p&gt;using namespace std;&lt;/o:p&gt;

&lt;o:p&gt;char * Sum(char \*strLastRes,const char \*strNum);  
void   ShowResult(const char \*str);  
int    Pos(const char \*Data,char ch);  
inline bool   IsNumString(const char \*str);  
void   TestSum();  
int main(int argc, char \*argv\[])  
{  
    fstream fileIn("acm5.txt",ios::in);  
    if(!fileIn){  
        cout&lt;&lt;"load file failed!"&lt;&lt;endl;  
        exit(-1);  
    }  
    char \*pStrRes=NULL;//=new char;  
    pStrRes="0";  
    string str;  
    while(!fileIn.eof()){  
         fileIn&gt;&gt;str;  
   if(str\[0]=='#')  
    break;  
         pStrRes=Sum(pStrRes,str.c\_str());  
   //pStrRes=Sum(pStrRes,str.c\_str());  
    }  
    ShowResult(pStrRes);  
// char* pstrTest="23242423421423412412341234d";  
// if(IsNumString(pstrTest)){  
//     cout&lt;&lt;"this is number string!"&lt;&lt;endl;  
// }  
// else{  
//  cout&lt;&lt;"not is Number String!"&lt;&lt;endl;  
// }  
//    cout&lt;&lt;pStrRes&lt;&lt;endl;  
// cout&lt;&lt;endl&lt;&lt;"Invoke TestSum Function!"&lt;&lt;endl;  
//   TestSum();     
     
 system("PAUSE");  
    fileIn.close();  
 return EXIT\_SUCCESS;  
}  
char * Sum(char \*strLastRes,const char \*strNum){  
     int lengthNum1=strlen(strLastRes);  
     int lengthNum2=strlen(strNum);  
  if(IsNumString(strLastRes)==false||IsNumString(strNum)==false)  
   return "0";//传入的参数之一不是由数字组成的字符串 既不进入加法运算  
//  cout&lt;&lt;"strNum:"&lt;&lt;strNum&lt;&lt;endl;  
     int Max=lengthNum1&gt;lengthNum2?lengthNum1:lengthNum2;  
     char \*result=new char\[Max+2];  
  result\[Max+1]='\\0';  
     if(result==NULL){  
         cout&lt;&lt;"Alloc memory failed!"&lt;&lt;endl;  
         exit(-1);  
     }  
     memset(result,'\\0',Max);  
     int flag=0;//进位标志  
     char a=0,b=0;  
     for(int i=Max;i&gt;=0;i--){  
          // if(lengthNum1&lt;0||lengthNum2&lt;0)  
          //      break;  
          // result\[i]=(int)result\[i]+flag;//加上进位  
           lengthNum1--;  
           lengthNum2--;  
           if(lengthNum1&lt;0)  
                a=0;//字符串1已经取完  
           else{  
                a=strLastRes\[lengthNum1]-'0';  
           }  
           if(lengthNum2&lt;0)  
                b=0;//字符串2已经取完  
           else  
                b=strNum\[lengthNum2]-'0';  
           result\[i]=a+b+flag+'0';  
           if((result\[i]-'0')&gt;=10){  
              result\[i]-=10;  
              flag =1;  
           }else{  
              flag=0;  
           }  
     }  
   //  delete \[] strLastRes;  
   //  strLastRes=NULL;  
     return result;  
}  
void TestSum(){  
    char \*pStrNum1="1234567898848487848484848484848484848484887454";  
    char \*pStrNum2="123456234214";  
    cout&lt;&lt;"\\nin TestSum function!\\n"&lt;&lt;endl;  
 cout&lt;&lt;pStrNum1&lt;&lt;"+"&lt;&lt;pStrNum2&lt;&lt;" = "&lt;&lt;Sum(pStrNum1,pStrNum2)&lt;&lt;endl;  
 }&lt;/o:p&gt;

&lt;o:p&gt;void ShowResult(const char \*str){  
 int len=strlen(str);  
 int strPos=Pos(str,'0');  
 if(strPos!=-5000){     
  for(int i=strPos;i&lt;len;i++){  
    cout&lt;&lt;str\[i];  
  }  
 }  
 cout&lt;&lt;endl;  
}  
int Pos(const char \*Data,char ch)  
{  
    int length\_Data=strlen(Data);  
    for(int i=0;i&lt;length\_Data;i++)  
    {  
      if(Data\[i]!=ch)  
        return i;  
      else  
        continue;  
    }  
    return -5000;  
}  
inline bool   IsNumString(const char \*str){  
 int length=strlen(str);  
 for(int i=0;i&lt;length;i++){  
  if(str\[i]&gt;='0'&&str\[i]&lt;='9')  
   continue;  
  else  
   return false;  
 }  
 return true;  
}  
&lt;/o:p&gt;

&lt;o:p&gt;方案二的源程序:&lt;/o:p&gt;

&lt;o:p&gt;#include&lt;iostream&gt;  
using namespace std;  
struct Node{  
 char  data;  
 Node \*next;  
};&lt;/o:p&gt;

&lt;o:p&gt;void AddNewNodeBack(Node \*\*pCur);//往前增加结点  
void DestoryList(Node \*pList);//头结点  
bool Sum(Node \*\*pResult,const Node \*pListNum);//求两数字串链表和  
inline bool IsNumber(char ch);//当前字符是否为数字 内联函数  
void ShowList(Node \*pList);  
void Reverse(Node \*\*pList);  
int main(int argc,char\*argv\[]){  
 Node \*pListResult=NULL;//保存结果的链表指针  
 Node \*pListNum=NULL;//用户输入数字的链表指针  
 pListResult=new Node;  
 pListResult-&gt;data='0';//假设已经做了一次加法且结果为0;  
 pListResult-&gt;next=NULL;  
 char ch;  
 do{  
  ch=getchar();  
  if(ch=='#')  
   break;  
  else{  
    while(ch!='\\n'){  
     if(IsNumber(ch)==false){  
      DestoryList(pListNum);//如果输入的不是数字则删除这之前的输入  
      pListNum=NULL;  
      ch=getchar();//要保证输入的是数字串  
      continue;//如果输入的不是数字则进入下一次输入  
     }  
    //向前添加结点  
    AddNewNodeBack(&pListNum);  
    pListNum-&gt;data=ch;  
    ch=getchar();  
   }  
  }  
  Sum(&pListResult,pListNum);//求和  
  DestoryList(pListNum);//释放数字链以便下一次输入  
  pListNum=NULL;//防止野指针  
  Reverse(&pListResult);//倒转一下链表为做下一次运算准备  
 }while(1);&lt;/o:p&gt;

&lt;o:p&gt; Reverse(&pListResult);//再把结果倒转一次  
 ShowList(pListResult);  
 cout&lt;&lt;endl;  
 DestoryList(pListNum);//释放空间  
 DestoryList(pListResult);//释放空间  
 return 0;  
    //return EXIT\_SUCCESS;  
}  
/* 向前添加结点\*/  
void AddNewNodeBack(Node \*\*pCur){  
 Node \*pNode=new Node;  
 if(pNode==NULL){  
  cout&lt;&lt;"alloc memory failed!\\n";  
  exit(-1);  
 }  
 pNode-&gt;data='0';  
 pNode-&gt;next=\*pCur;  
 \*pCur=pNode;  
}  
void DestoryList(Node \*pList){  
    Node \*pCur=NULL,\*pNode=NULL;  
 pCur=pList;//指向头结点;  
 if(pList!=NULL){//链表不为空则释放内存  
  while(pCur!=NULL){  
   pNode=pCur;  
   pCur=pCur-&gt;next;  
   delete pNode;  
  }  
 }  
}  
/\*递归打印链表\*/  
void ShowList(Node \*pList){  
 if(pList!=NULL){  
  cout&lt;&lt;pList-&gt;data;  
  if(pList-&gt;next!=NULL)  
   ShowList(pList-&gt;next);  
 }  
}  
bool Sum(Node \*\*pResult,const Node \*pListNum){  
 if(\*pResult==NULL)  
  return false;//返回失败  
 if(pListNum==NULL)  
  return false;//返回失败  
 int flag=0;//进位标志;  
 Node \*pListResult=\*pResult;  
 Node \*pTempCur;  
 pTempCur=NULL;  
 char a=0,b=0;  
 while(\*pResult!=NULL||pListNum!=NULL){//当两个链表都读完了，既做完了运算才退出循环  
  if(\*pResult==NULL)  
   a=0;  
  else  
   a=(\*pResult)-&gt;data-'0';  
  if(pListNum==NULL)  
   b=0;  
  else  
   b=pListNum-&gt;data-'0';  
  AddNewNodeBack(&pTempCur);//添加一个结点保存当前位的运算结果  
  pTempCur-&gt;data=a+b+flag+'0';  
  if(pTempCur-&gt;data-'0'&gt;=10){//如果有进位  
   pTempCur-&gt;data-=10;  
   flag=1;  
  }else  
   flag=0;  
  if((\*pResult)!=NULL)  
   \*pResult=(\*pResult)-&gt;next;//往下读  
  else  
   (\*pResult)=NULL;  
  if(pListNum!=NULL)  
   pListNum=pListNum-&gt;next;  
  else  
   pListNum=NULL;  
 }  
 if(flag==1){//如果两个串链运算有进位  
  AddNewNodeBack(&pTempCur);  
  pTempCur-&gt;data='1';  
 }  
 DestoryList(\*pResult);//释放掉上次的运算结果  
 \*pResult=pTempCur;  
 return true;  
   
}&lt;/o:p&gt;

&lt;o:p&gt;inline bool IsNumber(char ch){  
 if('0'&lt;=ch&&ch&lt;='9')  
  return true;  
 else  
  return false;  
}  
void Reverse(Node \*\*pList){  
 Node \*pCur=NULL;  
 Node \*pTemp=\*pList;  
 while(\*pList!=NULL){  
  AddNewNodeBack(&pCur);  
  pCur-&gt;data=(\*pList)-&gt;data;  
  \*(pList)=(\*pList)-&gt;next;  
 }  
 DestoryList(\*pList);  
 \*pList=pCur;  
}  
测试数据:  
方案一的测试数据放在文件里  
sum.txt文件的内容是&lt;/o:p&gt;

&lt;o:p&gt;9999999999123456789011111234  
90000001012301214  
111122244413  
111122244413  
111122244413  
111122244413  
1  
99999999991234567890111112343434  
#&lt;/o:p&gt;

&lt;o:p&gt;方案二的测试数据是从标准输入流输入的&lt;/o:p&gt;

&lt;o:p&gt;结果完全正确(#-\_-#我也没有做太多的测试)&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;