问题描述:
5、大数和(30分)<o:p></o:p>
有一些可能达100位的大正整数,求所有这些数的和。当输入#时,运行结束。
样例输入:
9999999999123456789011111234<o:p></o:p>
90000001012301214<o:p></o:p>
111122244413<o:p></o:p>
#<o:p></o:p>
<o:p></o:p>
样例输出:<o:p></o:p>
9999999999213456901145656861<o:p></o:p>
方案1:
这个是用动态数组的SUM函数就是求两数字串的和
按题意要求可分解为循环求两个数字串的和
Main(){
建立文件对象
如果读文件失败则退出程序<o:p></o:p>
结果=”<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="”" w:st="on">0”</st1:chmetcnv>;<o:p></o:p>
While(文件是否已读完){
读入一串数字到str缓冲区;<o:p></o:p>
结果=Sum(结果,str);<o:p></o:p>
}<o:p></o:p>
打印结果;<o:p></o:p>
}<o:p></o:p>
Char * Sum(char* strLastRes/*上次的结*/,const char* strNum){<o:p></o:p>
If(IsNumberString(strLastRes)==false&&IsNumberString(strNum)==false)<o:p></o:p>
Return “<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="”" w:st="on">0”</st1:chmetcnv>;有一个不为数字串//退出这次加法运算<o:p></o:p>
Int len1=数字串1长度;<o:p></o:p>
Int len2=数字串2长度;<o:p></o:p>
Max=len1>len2?len1:len2;<o:p></o:p>
Result=new char[Max+1];<o:p></o:p>
If(Result==NULL) 退出程序; <o:p></o:p>
用’<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="’" w:st="on">0’</st1:chmetcnv>初始化result;<o:p></o:p>
进位标志flat=0;<o:p></o:p>
Char a=0,b=0;<o:p></o:p>
For(i=Max;i>=0;i--){<o:p></o:p>
//从数字串尾逐位往前读数字<o:p></o:p>
len1--; <o:p></o:p>
len2--;<o:p></o:p>
if(len1<0)<o:p></o:p>
a=0;//字符串1已经取完<o:p></o:p>
else{<o:p></o:p>
a=strLastRes[lengthNum1]-'0';<o:p></o:p>
}<o:p></o:p>
if(len2<0)<o:p></o:p>
b=0;//字符串2已经取完<o:p></o:p>
else<o:p></o:p>
b=strNum[len2]-'0';<o:p></o:p>
result[i]=a+b+flag+'0';<o:p></o:p>
if(result[i]-‘<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="’" w:st="on">0’</st1:chmetcnv>>=10){
result[i]减10;<o:p></o:p>
进位标志=1;<o:p></o:p>
}else<o:p></o:p>
进位标志=0;<o:p></o:p>
}<o:p></o:p>
Delete []strLastRes;//释放上次申请的内存<o:p></o:p>
Return result;<o:p></o:p>
}<o:p></o:p>
ShowResult(const *str){<o:p></o:p>
Start=pos(str);//第一个不为0位置<o:p></o:p>
For(i=start;i<strlen(str);i++)<o:p></o:p>
Cout<<str[i];<o:p></o:p>
}<o:p></o:p>
Bool IsNumberString(const char *str){<o:p></o:p>
For(i=0;i<strlen(str);i++){<o:p></o:p>
If(‘<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="0" numbertype="1" negative="False" unitname="’" w:st="on">0’</st1:chmetcnv>≤str[i] ≤’<st1:chmetcnv tcsc="0" hasspace="False" sourcevalue="9" numbertype="1" negative="False" unitname="’" w:st="on">9’</st1:chmetcnv>)
Continue;
Else
Return false;<o:p></o:p>
}<o:p></o:p>
Return true;<o:p></o:p>
}<o:p></o:p>
程序流程序图:如 图五
<o:p></o:p>
<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"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" o:connecttype="rect" gradientshapeok="t"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style="WIDTH: 414.75pt; HEIGHT: 8in"><v:imagedata src="file:///C:\DOCUME~1\MissWolf\LOCALS~1\Temp\msohtml1\03\clip_image001.jpg" o:title="ACM5"></v:imagedata></v:shape><o:p></o:p>
图示5<o:p></o:p>
<o:p> 方案二:
用一个结构体存放每一位
struct Node{</o:p>
<o:p> char data;
Node *next;</o:p>
<o:p>};</o:p>
<o:p>Main()
{</o:p>
<o:p> Node *pResult=new Node;//假设已运算了一次且结果为0
pResult->data='0';
.....</o:p>
<o:p>}
方案二</o:p>
<o:p>在输入时逆向插入结点生成链表也就是说我输入123456789存放到链表时成了987654321运算时就可以顺向往后进行运算了! 得到运算结果后,存放结果的</o:p>
<o:p>链表又成了顺序链表也就是说:假设两个大数的运算结果为1234567那么在一次运算结果的链表时也是1234567</o:p>
<o:p>要想把这个结果参与到下一次运算得把它倒转过来(#-_-#,这样是不是很麻烦)才能参与下一次运算
在输出结果是也得倒转过来!具体看代码吧!这个代码是刚刚写好的,还没有文档的!流程序还没有画的!我的VISIO还没有装的(昨天重装系统了)</o:p>
<o:p>源程序1:#include <fstream>
#include <iostream>
#include <cstdio>
#include <string></o:p>
<o:p>using namespace std;</o:p>
<o:p>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<<"load file failed!"<<endl;
exit(-1);
}
char *pStrRes=NULL;//=new char;
pStrRes="0";
string str;
while(!fileIn.eof()){
fileIn>>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<<"this is number string!"<<endl;
// }
// else{
// cout<<"not is Number String!"<<endl;
// }
// cout<<pStrRes<<endl;
// cout<<endl<<"Invoke TestSum Function!"<<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<<"strNum:"<<strNum<<endl;
int Max=lengthNum1>lengthNum2?lengthNum1:lengthNum2;
char *result=new char[Max+2];
result[Max+1]='\0';
if(result==NULL){
cout<<"Alloc memory failed!"<<endl;
exit(-1);
}
memset(result,'\0',Max);
int flag=0;//进位标志
char a=0,b=0;
for(int i=Max;i>=0;i--){
// if(lengthNum1<0||lengthNum2<0)
// break;
// result[i]=(int)result[i]+flag;//加上进位
lengthNum1--;
lengthNum2--;
if(lengthNum1<0)
a=0;//字符串1已经取完
else{
a=strLastRes[lengthNum1]-'0';
}
if(lengthNum2<0)
b=0;//字符串2已经取完
else
b=strNum[lengthNum2]-'0';
result[i]=a+b+flag+'0';
if((result[i]-'0')>=10){
result[i]-=10;
flag =1;
}else{
flag=0;
}
}
// delete [] strLastRes;
// strLastRes=NULL;
return result;
}
void TestSum(){
char *pStrNum1="1234567898848487848484848484848484848484887454";
char *pStrNum2="123456234214";
cout<<"\nin TestSum function!\n"<<endl;
cout<<pStrNum1<<"+"<<pStrNum2<<" = "<<Sum(pStrNum1,pStrNum2)<<endl;
}</o:p>
<o:p>void ShowResult(const char *str){
int len=strlen(str);
int strPos=Pos(str,'0');
if(strPos!=-5000){
for(int i=strPos;i<len;i++){
cout<<str[i];
}
}
cout<<endl;
}
int Pos(const char *Data,char ch)
{
int length_Data=strlen(Data);
for(int i=0;i<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<length;i++){
if(str[i]>='0'&&str[i]<='9')
continue;
else
return false;
}
return true;
}
</o:p>
<o:p>方案二的源程序:</o:p>
<o:p>#include<iostream>
using namespace std;
struct Node{
char data;
Node *next;
};</o:p>
<o:p>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->data='0';//假设已经做了一次加法且结果为0;
pListResult->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->data=ch;
ch=getchar();
}
}
Sum(&pListResult,pListNum);//求和
DestoryList(pListNum);//释放数字链以便下一次输入
pListNum=NULL;//防止野指针
Reverse(&pListResult);//倒转一下链表为做下一次运算准备
}while(1);</o:p>
<o:p> Reverse(&pListResult);//再把结果倒转一次
ShowList(pListResult);
cout<<endl;
DestoryList(pListNum);//释放空间
DestoryList(pListResult);//释放空间
return 0;
//return EXIT_SUCCESS;
}
/* 向前添加结点*/
void AddNewNodeBack(Node **pCur){
Node *pNode=new Node;
if(pNode==NULL){
cout<<"alloc memory failed!\n";
exit(-1);
}
pNode->data='0';
pNode->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->next;
delete pNode;
}
}
}
/*递归打印链表*/
void ShowList(Node *pList){
if(pList!=NULL){
cout<<pList->data;
if(pList->next!=NULL)
ShowList(pList->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)->data-'0';
if(pListNum==NULL)
b=0;
else
b=pListNum->data-'0';
AddNewNodeBack(&pTempCur);//添加一个结点保存当前位的运算结果
pTempCur->data=a+b+flag+'0';
if(pTempCur->data-'0'>=10){//如果有进位
pTempCur->data-=10;
flag=1;
}else
flag=0;
if((*pResult)!=NULL)
*pResult=(*pResult)->next;//往下读
else
(*pResult)=NULL;
if(pListNum!=NULL)
pListNum=pListNum->next;
else
pListNum=NULL;
}
if(flag==1){//如果两个串链运算有进位
AddNewNodeBack(&pTempCur);
pTempCur->data='1';
}
DestoryList(*pResult);//释放掉上次的运算结果
*pResult=pTempCur;
return true;
}</o:p>
<o:p>inline bool IsNumber(char ch){
if('0'<=ch&&ch<='9')
return true;
else
return false;
}
void Reverse(Node **pList){
Node *pCur=NULL;
Node *pTemp=*pList;
while(*pList!=NULL){
AddNewNodeBack(&pCur);
pCur->data=(*pList)->data;
*(pList)=(*pList)->next;
}
DestoryList(*pList);
*pList=pCur;
}
测试数据:
方案一的测试数据放在文件里
sum.txt文件的内容是</o:p>
<o:p>9999999999123456789011111234
90000001012301214
111122244413
111122244413
111122244413
111122244413
1
99999999991234567890111112343434
#</o:p>
<o:p>方案二的测试数据是从标准输入流输入的</o:p>
<o:p>结果完全正确(#-_-#我也没有做太多的测试)</o:p>
<o:p></o:p>
分享到:
相关推荐
我们提出了一种解决共形场理论的新方法,并将其应用于切恩-西蒙斯物质理论和三维玻化二元... 我们还发现了另外两个非统一的解决方案,它们应该类似于鱼网理论。 结果也可以看作是轻微破坏的较高自旋对称性的明确实现。
"大数 据" 技术可以把这些数据整合起来进行数据挖掘, 从而在某些情况下通过模型模拟来判断丌 同变量(比如丌同地区丌同促销方案)的情况下何种方案投入回报最高。 提高投入回报率 提高 "大数据" 成果在各相关部门的...
本文实例讲述了基于私钥加密公钥解密的RSA算法C#实现方法,是一种应用十分广泛的算法。分享给大家供大家参考之用。具体方法如下: 一、概述 RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。 RSA...
的医院就诊人数越来越多袁导致医疗数据储存量不断地 增长袁海量数据代表了医院大数据时代的降临遥 医院中 存储的数据主要有两种院淤在提供医疗服务过程中产生 的数据袁包括护理管理系统尧PACS尧手术及麻醉系统尧病 ...
需要额外说明的是, 基础层的两种分类属性都可以结合业务需求进行自我进化、组合,形成更加丰富的宽表 属性以满足灵活需求。 通常来说,基础层属性能满足大多数的银行业务经营需求。无论是营销类分析 决策,还是风险...
大多数大数 据在本质上是非结构化或半结构化数据,因此要求采用不同的方法和工具进行处理和分 析。 让我们来详细了解一下最显著的特征大数据的结构,如图1所示。 图1显示了不同类型的数据结构,未来 80%-90% 的数据...
文章介绍了大数据特征、作用以及对大数据分析的方法理论,对大数 据的两种处理模式、处理流程以及关键技术进行了分析,提出MapReduce与关系数据库融 合技术,为未来大数据的工作提供了一个参考。 云计算简介 正如...
2)只考虑了<和>两种情况,没有考虑不等式符号和>=,也就是没有考虑是否包含边界的问题。 解决方案:1)可以设定一下xlim,ylim,然后加在原不等式组的基础上追加4个不等式xlim的下限的上限,ylim的下限的上限。 ...
4、根据数据的特点,发现绝大数都有触发词,故采用trigger左右两端动态池化特征作为全局特征; 5、因 time loc 并非每个句子中都存在,并且分布较为稀疏,将 time & loc 和 subject & object 的提取分开,采用两个...
4、根据数据的特点,发现绝大数都有触发词,故采用trigger左右两端动态池化特征作为全局特征; 5、因 time loc 并非每个句子中都存在,并且分布较为稀疏,将 time & loc 和 subject & object 的提取分开,采用两个...
的开展核查工作和切实实有效的提出分类处置方案案并落实,与地方党委、纪纪委是否 充分履行了"两个个责任"息息相关。xx在在对前期调查结果复核过程程中,执行"三级复 核"制制度,即纪委负责对调查核核实情况汇总并...
算法的名字就是发明者的名字:Ron Rivest, AdiShamir 和Leonard Adleman, 但RSA的安全性一直未能得到理论上的证明, RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价。...
如同上世纪80年代的个人电脑,眼下,大数 据还处于早期发展阶段,但它已成为新时代的关键生产要素和产品形态,具有广阔的应 用前景及改变世界经济的力量。大数据的开发和应用不仅能为企业带来更出色的商业效 益,...
面对交通拥堵,大力发展公共交通是一种有效手段,但事实上公共 交通也存在着资源分配不合理的现象,导致等车时间上、乘车拥挤甚至挤不上车等问题 。那如何解决这一系列的问题,最终解决交通拥堵问题,已经引起了我们...
支持静态链接其它编程语言(如C/C++、汇编等)编译生成的静态库(.LIB或.OBJ),但仅限于COFF格式,支持cdecl和stdcall两种函数调用约定。 使用说明如下:函数声明和调用方法与DLL命令一致;“库文件名”以.lib...