Escriba una función «C», int addOvf(int* result, int a, int b) Si no hay desbordamiento, la función coloca la resultante = sum a+b en «resultado» y devuelve 0. De lo contrario, devuelve -1. No se permite la solución de lanzar demasiado y sumar para encontrar detectando el desbordamiento.
Método 1
Puede haber desbordamiento solo si los signos de dos números son iguales y el signo de la suma es opuesto a los signos de los números.
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 <bits/stdc++.h> 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<<addOvf(res, x, y); cout<<"\n"<<*res; return 0; } // This code is contributed by rathbhupendra
C
#include<stdio.h> #include<stdlib.h> /* 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; }
Java
// Java program for the above approach class GFG { /* 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 */ static 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 public static void main(String args[]) { int res = -2147483646; // size of an Integer int x = 2147483640; int y = 10; System.out.println(addOvf(res, x, y)); System.out.print(res); } } // This code is contributed by sanjoy_62.
C++
#include <bits/stdc++.h> using namespace std; int addOvf(int* result, int a, int b) { if (a >= 0 && b >= 0 && (a > INT_MAX - b)) { return -1; } else if (a < 0 && b < 0 && (a < INT_MIN - b)) { return -1; } else { *result = a + b; return 0; } } int main() { int a, b; int* res; a = INT_MAX; b = 8192; cout << addOvf(res, a, b); return 0; } // This code is contributed by Sonu Kumar Pandit
C
#include <limits.h> #include <stdio.h> #include <stdlib.h> 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; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA