减去存储在数组中的2个整数 [英] Subtraction of 2 integers stored in arrays

查看:53
本文介绍了减去存储在数组中的2个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我编写了一个代码来减去存储在数组中的2个大整数!但问题是,当minuend小于减数时,即当答案为负时,它不起作用。我将2个char数组作为参数,将它们转换为int数组,使用它们,将结果存储在char数组中。回来吧!有人可以指出代码中所需的优化!

Hi! I have written a code to subtract 2 large integers stored in arrays! But the problem is, its not working when the minuend is smaller than the subtrahend, i.e, when the answer would be negative. I am taking 2 char arrays as parameters, converting them to int arrays, working with them, storing the result in a char array & returning it! Can somebody please point out the optimizations needed in the code!

#include<iostream.h>
#include<conio.h>
#include<string.h>

class Sub{
 public:
 char *subArray(char a[], char b[])
 {
  int i, j, k, diff[255], borr, size1=0, size2=0, x[255], y[255];
  char temp[255], *p;
  cout<<"\nNuM1: "  ;
  for(i=0; a[i]!=NULL; i++)
  {
    x[i]=a[i]-'0';                 //conversion into int array 
    size1++;                     
    cout<<x[i];
  }
  cout<<"\nNuM2: "  ;
  for(i=0; b[i]!=NULL; i++)
  {
    y[i]=b[i]-'0';                //conversion into int array 
    size2++;
    cout<<y[i];
  }
  if(size1<size2)                
  {
	p=subArray(b, a);
        p[0]=-p[0];               //not printing the correct value
	return p;
  }
  else
  {
	 for(j=0; j<size2; j++)
	 {
	  y[j]=-y[j];
	 }
	 k=borr=0;

	 for(i=size1-1, j=size2-1;i>=0 && j>=0; i--, j--, k++)
	 {
	  diff[k]=x[i]+y[j]-borr;
	 if(diff[k]<0)
	 {
	  diff[k]+=10;
	  borr=1;
	 }
	 else
	 borr=0;
	 }
	 if(i>=0)
	{
	 for(;i>=0;i--,k++)
	 {
	  diff[k]=x[i]-borr;
	  if(diff[k]<0)
	  {
		diff[k]+=10;
		borr=1;
	  }
	  else
	  borr=0;
	}
  }

  i=j=0;
  for(i=k-1; i>=0; i--)
  {
	 if(diff[i]<0)
	 {
	  temp[j++]='-';
	  temp[j++]=-(diff[i])+'0';
	 }
	 else
	 temp[j++]=diff[i]+'0';
  }
  temp[j]='\0';
  return temp;
  }
 }
};
void main()
{
 Sub s;
 char num1[255], num2[255], *diff;
 cout<<"NuM1: ";
 cin>>num1 ;
 cout<<"\nNuM2: ";
 cin>>num2;
 diff = s.subArray(num1, num2);
 cout<<"\nDifference: ";
 while(*diff != NULL)
 {
  cout<<*diff;
  diff++;
 }
}

推荐答案

最大的问题:你正在返回指向分配的临时数组的指针堆栈。一旦从函数返回,该数组就不存在了,如果调用函数可以看到该数组中的任何内容,那就很幸运。



解决方案:分配结果调用者的数组并将指向该数组的指针作为函数的参数传递:

The biggest problem: You are returning a pointer to the temp array that allocated on the stack. Once you return from your function, that array goes out of existence and it is pure luck if the calling function can see anything in that array.

Solution: Allocate the result array by the caller and pass a pointer to that array as parameter to your function:
char diff[255];
...
Sub (res, num1, num2);





你解决了一些有点笨拙的事情。检查两个参数的长度基本上是无用的,因为它们中的任何一个都可能包含前导零。



我将处理参数的符号并将结果单独处理变量,这将节省您对函数的递归调用。



There are a couple of things that you have solved a little clumsy. Checking for the length of both arguments is basically useless, because any of them might contain leading zeros.

I would handle the sign of the arguments and the result in a separate variable and that will save you the recursive call to your function.


这篇关于减去存储在数组中的2个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆