Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Dividir.c
- * Copyright 2021 jvargasp1303 <jvargasp1303@DESKTOP-SPA89KF>
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
- /* ** // ** // ** / Código fuente de programa aplica un algoritmo para dividir / ** // ** // ** */
- /* ** // ** // ** // 2 números enteros y obtiene como resultado otro entero // ** // ** // ** */
- /* ** // ** // ** // * Licenciado bajo GNU General Public License (GPL) 3.0 * // ** // ** // ** */
- /* ** // ** // ** // ** // ** // ** // Created by: XeBuZer0 // ** // ** // ** // ** // ** // ** */
- /* ** // ** // ** // ** // ** // Windows beta tester: Rafael Mora // ** // ** // ** // ** // ** */
- /* ** // ** EDITED V2.0 RC1: Improved compatibility with windows. For better results ** // ** */
- /* ** // ** // * this was tested with windows, compiled with Geany as IDE and TCC * // ** // ** */
- /* ** // ** // ** Under Linux there's no trouble, tested with GCC, Clang and TCC ** // ** // ** */
- /* ** // ** // ** // ** // ** // Finished on 11 - December - 2021 // ** // ** // ** // ** // ** */
- /* ** // ** // ** // ** // ** // * F v q _ U k r a N a z i s ! * // ** // ** // ** // ** // ** */
- /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h>
- int divideW(int n1, int n2);
- int main(void){
- int dividend=0, divisor=0;
- printf("Ingrese dividendo (cantidad a dividir): ");
- scanf("%d",÷nd);
- printf("Ingrese divisor (cantidad que divide): ");
- scanf("%d",&divisor);
- printf("El resultado es: %d", divideW(dividend,divisor));
- return 0;
- }
- int divideW(int n1, int n2){
- register int sign = 1, quot=0, t1=0, t2=0, temp=1, r=0, sgn1=1, sgn2=1;
- if(n1 < 0){
- n1 = -n1;
- sign = -sign;
- sgn1 = -1;
- }
- if(n2 < 0){
- n2 = -n2;
- sign = -sign;
- sgn2 = -1;
- }
- if (!n2 || n1<n2){
- errno = 1;
- printf("División imposible en enteros\n");
- exit(-1);
- }
- if (!n1) return n1;
- if (n1>n2)
- for ( t1 = n1 ; t1 >= n2 ; t1 = (t1 - t2) , quot = quot + temp)
- for ( t2 = n2 , temp=1 ; (t2 << 1) <= t1 ; t2 = t2 << 1 , temp=temp<<1 )
- printf("\tn1 = %7d, n2 = %7d , t1 = %7d, t2 = %7d , temp = %7d, quot = %7d\n", n1, n2, t1, t2, temp, quot);
- r = (n1*sgn1) - ((n2*sgn2)*(sign*quot));
- printf("a = bq + r con a = %d, b = %d, q = %d, r = %d\n", n1, n2, sign*quot, r);
- printf("Ordenando: %d = (%d)*(%d) + (%d)\n", sgn1*n1, sgn2*n2, sign*quot, r);
- return (sign==1)?quot:-quot;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement