大数整数相加(忽略负数)
2011-07-11 17:48
323 查看
/*
Name:
Copyright:
Author:
Date: 10/07/11 21:33
Description:
define the carry symbol of each byte is iCarry
define the sum symbol of each byte is iSum
利用整数序列前后多于的0换来了代码的可读性。
注意0+0没有处理。因为0+0 =0 打印结果事后会忽略多余的0
*/
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//get user input. we use the string type. init the lenth 1000 with '0'
string GetUserInPut(string szInPutPrompt)
{
string szInPut (1000,'0');
cout<<szInPutPrompt<<endl;
cin>>szInPut;
szInPut.append(1000-szInPut.size(),'0'); //必须的,string重载了>>会重新生成字符串复制。长度会变化的,不是以前的。应且NULL结束
return szInPut;
}
//calculate the sum bwteen the summand and the addend. calculate the summmand adn addend with the reversed sequence
string CalculateSum(string szSummandStr,string szAddendStr)
{
string szSumStr(10010,'0');
int iIndexAddend = szAddendStr.size()-1;
int iIndexSummand = szSummandStr.size() -1;
int iCarray = 0;
int iSum = 0;
int iIndexSum = 0;
for(iIndexSum = 0; iIndexAddend >= 0;iIndexAddend--,iIndexSummand--, iIndexSum++)
{
iSum = ((szSummandStr.at(iIndexSummand) - '0') + (szAddendStr.at(iIndexAddend) - '0') + iCarray) % 10; // sum
iCarray = ((szSummandStr.at(iIndexSummand) - '0') + (szAddendStr.at(iIndexAddend) - '0')) / 10; // carry
szSumStr.at(iIndexSum) = iSum + '0'; //sum
}
szSumStr.at(iIndexSum) = szSumStr.at(iIndexSum) + iCarray; // add the last carry
return szSumStr;
}
void PrintIngoreCharacter(string szPrintStr ,char ch)
{
int iIndexEnd = 0;
int iIndexStart =0;
//ignore the 0 front,find the lowest byte
for(iIndexStart = 0;iIndexStart<szPrintStr.size();iIndexStart++)
{
if(szPrintStr.at(iIndexStart)!=ch)
break;
}
//ignore the 0 back ,find the highest byte.
for(iIndexEnd = szPrintStr.size() -1 ; iIndexEnd >= 0 ; iIndexEnd--)
{
if(szPrintStr.at(iIndexEnd)!=ch)
{
break;
}
}
for(int iIndex=iIndexEnd;iIndex >= iIndexStart; iIndex--)
{
cout<<szPrintStr.at(iIndex);
}
cout<<endl;
system("pause");
}
int main()
{
string szSummandStr = GetUserInPut("inPut Summand please");
string szAddendStr = GetUserInPut("inPut Addend Please");
string szSumStr = CalculateSum(szSummandStr,szAddendStr);
PrintIngoreCharacter(szSumStr,'0');
return 0;
}
Name:
Copyright:
Author:
Date: 10/07/11 21:33
Description:
define the carry symbol of each byte is iCarry
define the sum symbol of each byte is iSum
利用整数序列前后多于的0换来了代码的可读性。
注意0+0没有处理。因为0+0 =0 打印结果事后会忽略多余的0
*/
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//get user input. we use the string type. init the lenth 1000 with '0'
string GetUserInPut(string szInPutPrompt)
{
string szInPut (1000,'0');
cout<<szInPutPrompt<<endl;
cin>>szInPut;
szInPut.append(1000-szInPut.size(),'0'); //必须的,string重载了>>会重新生成字符串复制。长度会变化的,不是以前的。应且NULL结束
return szInPut;
}
//calculate the sum bwteen the summand and the addend. calculate the summmand adn addend with the reversed sequence
string CalculateSum(string szSummandStr,string szAddendStr)
{
string szSumStr(10010,'0');
int iIndexAddend = szAddendStr.size()-1;
int iIndexSummand = szSummandStr.size() -1;
int iCarray = 0;
int iSum = 0;
int iIndexSum = 0;
for(iIndexSum = 0; iIndexAddend >= 0;iIndexAddend--,iIndexSummand--, iIndexSum++)
{
iSum = ((szSummandStr.at(iIndexSummand) - '0') + (szAddendStr.at(iIndexAddend) - '0') + iCarray) % 10; // sum
iCarray = ((szSummandStr.at(iIndexSummand) - '0') + (szAddendStr.at(iIndexAddend) - '0')) / 10; // carry
szSumStr.at(iIndexSum) = iSum + '0'; //sum
}
szSumStr.at(iIndexSum) = szSumStr.at(iIndexSum) + iCarray; // add the last carry
return szSumStr;
}
void PrintIngoreCharacter(string szPrintStr ,char ch)
{
int iIndexEnd = 0;
int iIndexStart =0;
//ignore the 0 front,find the lowest byte
for(iIndexStart = 0;iIndexStart<szPrintStr.size();iIndexStart++)
{
if(szPrintStr.at(iIndexStart)!=ch)
break;
}
//ignore the 0 back ,find the highest byte.
for(iIndexEnd = szPrintStr.size() -1 ; iIndexEnd >= 0 ; iIndexEnd--)
{
if(szPrintStr.at(iIndexEnd)!=ch)
{
break;
}
}
for(int iIndex=iIndexEnd;iIndex >= iIndexStart; iIndex--)
{
cout<<szPrintStr.at(iIndex);
}
cout<<endl;
system("pause");
}
int main()
{
string szSummandStr = GetUserInPut("inPut Summand please");
string szAddendStr = GetUserInPut("inPut Addend Please");
string szSumStr = CalculateSum(szSummandStr,szAddendStr);
PrintIngoreCharacter(szSumStr,'0');
return 0;
}
相关文章推荐
- 两个大数(包括负数)相加
- 大数相加,高精度阶乘,大整数进制转换,大整数判断被小整数整除(JOJ--1029)
- 用字符串实现大数的相加,没有考虑存在负数的情况
- C#实现2个大正整数相加(负数和浮点型正在考虑中...)
- C++大数(正整数)相加相减v1
- 剑指offer之面试题12 大数相加 实现任意两个整数的加法
- 给一个数组,元素都是整数(有正数也有负数),寻找连续的元素相加之和为最大的序列。
- 两个大数(整数)相加模板
- 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
- 算法大数整数相加
- 九度OJ 1119:Integer Inquiry(整数相加) (大数运算)
- 有一个整数数组(包括正数 负数 和0),给定一个M值,要求数组中的一个或多个值相加的和等于M,有多少种组合?
- 面试常见题目:大数相加的Java实现(考虑负数情况)
- C++实现两个大整数的相加(考虑到负数异常情况)
- 【模板小程序】十进制大数相加(正整数版本+整数版本【正负0】),包含合法性检查
- 大数相加
- 超长字符整数的相加(UCWEB 2010校园招聘笔试)
- ACM大数相加
- HDOJ1002大数相加
- 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。