📜  检查整数溢出

📅  最后修改于: 2021-05-07 09:46:01             🧑  作者: Mango

编写一个“ C”函数,int addOvf(int * result,int a,int b)如果没有溢出,该函数将结果= sum a + b放入“结果”并返回0。否则返回-1。不允许强制转换为long并添加以发现检测溢出的解决方案。

方法1
仅当两个数字的符号相同且总和的符号与数字的符号相反时,才可能出现溢出。

1)  Calculate sum
2)  If both numbers are positive and sum is negative then return -1
     Else 
        If both numbers are negative and sum is positive then return -1
        Else return 0
C++
#include 
using namespace std;
  
/* Takes pointer to result and two numbers as 
    arguments. If there is no overflow, the function 
    places the resultant = sum a+b in “result” and 
    returns 0, otherwise it returns -1 */
int addOvf(int* result, int a, int b) 
{ 
    *result = a + b; 
    if(a > 0 && b > 0 && *result < 0) 
        return -1; 
    if(a < 0 && b < 0 && *result > 0) 
        return -1; 
    return 0; 
} 
  
// Driver code
int main() 
{ 
    int *res = new int[(sizeof(int))]; 
    int x = 2147483640; 
    int y = 10; 
  
    cout<


C
#include
#include
  
/* Takes pointer to result and two numbers as
    arguments. If there is no overflow, the function
    places the resultant = sum a+b in “result” and
    returns 0, otherwise it returns -1 */
 int addOvf(int* result, int a, int b)
 {
     *result = a + b;
     if(a > 0 && b > 0 && *result < 0)
         return -1;
     if(a < 0 && b < 0 && *result > 0)
         return -1;
     return 0;
 }
  
 int main()
 {
     int *res = (int *)malloc(sizeof(int));
     int x = 2147483640;
     int y = 10;
  
     printf("%d", addOvf(res, x, y));
  
     printf("\n %d", *res);
     getchar();
     return 0;
}


C++
#include 
using namespace std;
  
int addOvf(int* result, int a, int b) 
{ 
    if( a > INT_MAX - b) 
        return -1; 
    else
    { 
        *result = a + b; 
        return 0; 
    } 
} 
  
int main() 
{ 
    int *res = new int[(sizeof(int))]; 
    int x = 2147483640; 
    int y = 10; 
      
    cout<


C
#include
#include
#include
  
int addOvf(int* result, int a, int b)
{
   if( a > INT_MAX - b)
     return -1;
   else
   {
     *result = a + b;
      return 0;
   }
}
  
int main()
{
  int *res = (int *)malloc(sizeof(int));
  int x = 2147483640;
  int y = 10;
  
  printf("%d", addOvf(res, x, y));
  printf("\n %d", *res);
  getchar();
  return 0;
}


输出:
-1
-2147483646

时间复杂度: O(1)
空间复杂度: O(1)

方法二
感谢Himanshu Aggarwal添加此方法。如果发生溢出,此方法不会修改*结果。

C++

#include 
using namespace std;
  
int addOvf(int* result, int a, int b) 
{ 
    if( a > INT_MAX - b) 
        return -1; 
    else
    { 
        *result = a + b; 
        return 0; 
    } 
} 
  
int main() 
{ 
    int *res = new int[(sizeof(int))]; 
    int x = 2147483640; 
    int y = 10; 
      
    cout<

C

#include
#include
#include
  
int addOvf(int* result, int a, int b)
{
   if( a > INT_MAX - b)
     return -1;
   else
   {
     *result = a + b;
      return 0;
   }
}
  
int main()
{
  int *res = (int *)malloc(sizeof(int));
  int x = 2147483640;
  int y = 10;
  
  printf("%d", addOvf(res, x, y));
  printf("\n %d", *res);
  getchar();
  return 0;
}
输出:
-1
0


时间复杂度:
O(1)
空间复杂度: O(1)