问题描述:
在计算机中,由于处理器位宽限制,只能处理有限精度的十进制整数加减法,比如在32位宽处理器计算机中,
参与运算的操作数和结果必须在-231~231-1之间。如果需要进行更大范围的十进制整数加法,需要使用特殊
的方式实现,比如使用字符串保存操作数和结果,采取逐位运算的方式。如下:
9876543210 + 1234567890 = ?
让字符串 num1="9876543210",字符串 num2="1234567890",结果保存在字符串 result = "11111111100"。
-9876543210 + (-1234567890) = ?
让字符串 num1="-9876543210",字符串 num2="-1234567890",结果保存在字符串 result = "-11111111100"。
要求编程实现上述高精度的十进制加法。
要求实现函数:
void add (const char *num1, const char *num2, char *result)
【输入】num1:字符串形式操作数1,如果操作数为负,则num1[0]为符号位'-'
nu2:字符串形式操作数2,如果操作数为负,则num2[0]为符号位'-'
【输出】result:保存加法计算结果字符串,如果结果为负,则result[0]为符号位。
注:
I、 当输入为正数时,'+'不会出现在输入字符串中;当输入为负数时,'-'会出现在输入字符串中,且一定在输入字符串最左边位置;
II、 输入字符串所有位均代表有效数字,即不存在由'0'开始的输入字符串,比如"0012", "-0012"不会出现;
III、 要求输出字符串所有位均为有效数字,结果为正或0时'+'不出现在输出字符串,结果为负时输出字符串最左边位置为'-'。
示例
输入:num1 = "580"
num2 = "-50"
输出:result = "530"
输入:num1 = "580"
num2 = "-600"
输出:result = "-20"
思路:
同正、同负可以看成是一大类:使用栈可以很方便地实现加法,负负只需添加个负号
一正一负时需要判断哪个的绝对值大一些:用大数减去小数,并加上符号即可
#include "iostream"
#include "stack"
using namespace std;
void Add(char *c1,char* c2,char oper){
stack<int>s1;
stack<int>s2;
stack<int>s3;
int len1=strlen(c1);
int len2=strlen(c2);
int i;
for( i=0;i<len1;i++){
s1.push(c1[i]-'0'); //按输入顺序(高位到低位)入栈1,此时栈顶为最低位
}
for( i=0;i<len2;i++){
s2.push(c2[i]-'0'); //按输入顺序(高位到低位)入栈2,此时栈顶为最低位
}
int temp=0;
while(!s1.empty() && !s2.empty()){
temp += s1.top()+s2.top(); // 将栈1、栈2均pop出栈顶做加法,并考虑进位,结果入栈3,这时栈3正好是低位入栈
s1.pop();
s2.pop();
s3.push(temp%10);
temp = temp/10;
}
while(!s1.empty()){ //处理多余的栈1
temp += s1.top();
s1.pop();
s3.push(temp%10);
temp = temp/10;
}
while(!s2.empty()){ //处理多余的栈2
temp += s2.top();
s2.pop();
s3.push(temp%10);
temp = temp/10;
}
if(temp) //处理多余的进位
s3.push(temp);
printf("两个数相加的结果为:");
if(oper=='-')
cout<<'-';
while(!s3.empty()){ //直接pop出栈3,即正好的从高位到低位的结果
cout<<s3.top();
s3.pop();
}
}
void Minus(char *c1,char* c2,char oper){
stack<int>s1;
stack<int>s2;
stack<int>s3;
int len1=strlen(c1);
int len2=strlen(c2);
int i;
for( i=0;i<len1;i++){
s1.push(c1[i]-'0'); //按输入顺序(高位到低位)入栈1,此时栈顶为最低位
}
for( i=0;i<len2;i++){
s2.push(c2[i]-'0'); //按输入顺序(高位到低位)入栈2,此时栈顶为最低位
}
int temp=0;
while(!s1.empty() && !s2.empty() ){
if(s1.top()-temp>=s2.top()){
s3.push(s1.top()-temp-s2.top() );
s1.pop();
s2.pop();
temp=0;//在高位将不借位
}
else{
s3.push(s1.top()+10-temp-s2.top());
s1.pop();
s2.pop();
temp=1;//需要借位
}
}
while(!s1.empty()){ //处理多余的栈1。 注意不要漏掉temp借位:10000-1=09999
if(s1.top()>=temp){
s3.push(s1.top()-temp);
s1.pop();
temp=0;//后续高位不在借位
}else{
s3.push( s1.top+9);
s1.pop();
temp=1;//高位需要继续借位 100-1=099
}
}
//剔除掉s3顶部的0:因为 100 - 99 答案是1 不是001
while(!s3.empty() && s3.top()==0)
s3.pop();
printf("两个数相加的结果为:");
if(oper=='-')
cout<<'-';
while(!s3.empty()){ //直接pop出栈3,即正好的从高位到低位的结果
cout<<s3.top();
s3.pop();
}
}
void MatchFunc(char *c1,char *c2){//通过比较寻找匹配的函数
if(isdigit(c1[0])&& isdigit(c2[0]))//同正
Add(c1,c2,'+');
else if(c1[0]=='-' && c2[0]=='-') //同负
Add(c1,c2,'-');
else{
if( isdigit(c1[0]) && c2[0]=='-' ){// c1正 c2负
if(strlen(c1)>strlen(c2+1) ||
strlen(c1)==strlen(c2+1) && strcmp(c1,c2+1)>=0){ //正大
Minus(c1,c2+1,'+');
}else{ //负大
Minus(c2+1,c1,'-');
}
}else{ //c2正 c1负
if(strlen(c2)>strlen(c1+1) ||
strlen(c2)==strlen(c1+1) && strcmp(c2,c1+1)>=0){ //正大
Minus(c2,c1+1,'+');
}else{ //负大
Minus(c1+1,c2,'-');
}
}
}
}
int main(void)
{
char c1[100];
char c2[100];
printf("请输入第一个加数:");
while(cin>>c1){
printf("请输入第二个加数:");
cin>>c2;
MatchFunc(c1,c2);
cout<<endl;
printf("请输入第一个加数:");
}
cout<<endl;
system("pause");
return 0;
}
/*
12345678987654321
-79871489419384682
*/