局部变量是不能进行返回的 高精度加法

以为这样的变量会再函数被运行结束后由系统释放



#include <iostream>
using namespace std;
int GetLength(char *x1)
{
char * temp = x1;
int i = 0;
while(*temp)
{
++i;
++temp;
}
return i;
}
void addzezo(char * &x2,int count)
{
int i(0);
char * temp = new char[20];
for(i = 0;i<count;++i)
{
temp[i] = '0';
}
temp[i] = '\0';
x2 = strcat(temp,x2);
}


char * plus(char *x1,char *x2)
{
int length(0);
int count = abs(GetLength(x1)-GetLength(x2));
if(GetLength(x1)>GetLength(x2))//in this case the x2 should add 0
{
addzezo(x2,count);
cout<<x2<<endl;;
length = GetLength(x1);
}
else
{
addzezo(x1,count);
cout<<"x1 = "<<x1<<endl;
length = GetLength(x2);
   }
//ready end


//begin to caculate
int result[11] = {0};
for(int i = length-1;i>-1;--i)
{
result[length-1-i] = (x1[i]-'0')+(x2[i]-'0');
}


//deal with the result arrary
int flag;
for(int i =0;i<length;++i)
{
result[i+1] =result[i+1]+result[i]/10;//advanced numbers add to the advanced poisition
result[i]=result[i]%10;//the left left
flag=i;
}

//get the char result
char * ch_result = new char[20];
if(result[flag+1]!=0)//in this case there is an advanced in the highest bit the length++
for(int i = 0;i<=length;++i)
{
ch_result[i] = result[i]+'0'; 
}
else
for(int i = 0;i<length;++i)
{
ch_result[i] = result[i]+'0'; 
}
//ch_result[i] = '\0';
return ch_result;
}


int main()
{
cout<<
plus("123456","22211111111")<<endl;
int x1 = 2;
cout<<x1<<endl;
cout<<x1;


 system("pause");
return 0;
}


其它的体会:

1、对于已经释放的变量 这个变量会产生新的地址但是 这个地址上不能运行 realloc函数等等 

2、要想进行局部变量的返回,需要在heap区进行 进行 new 但是 如何释放他们是个问题


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值