Advertisement
Kyrexar

Potencias y raices

May 24th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int pot_pos( int base, int exp ){
  6.     int i, res=base;
  7.  
  8. // POTENCIA CERO
  9.     if( exp==0 ) return 1;
  10.  
  11. // POTENCIA POSITIVA
  12.     if( exp>0 ){
  13.         for( i=1 ; i<exp ; i++ ){
  14.             res=res*base;
  15.         }
  16.     return res;
  17.     }
  18. }
  19.  
  20. float pot_neg( int base, int exp ){
  21.     int i;
  22.     float res=1.0/base;
  23.  
  24. // POTENCIA NEGATIVA
  25.     printf(" Potencia negativa \n ");
  26.     return 0;
  27. /*
  28.     if( exp<0 ){
  29.         exp=exp*-1;
  30.         for( i=1 ; i<exp ; i++ ){
  31.              res=res*(1/base);
  32.         }
  33.     return res;
  34.     }      */
  35. }
  36.  
  37. float raiz( int base, int ind ){
  38.     float i, res;
  39.    
  40.     if( ind==1 ) return base;
  41.  
  42. // RAIZ EXACTA
  43.     for( i=1 ; i<base ; i++ ){
  44.         if( base==pot_pos(i,ind) ) return i;
  45.     }
  46.  
  47. //RAIZ INEXACTA
  48.     printf(" Raiz inexacta \n ");
  49.     return 0;
  50. }
  51.  
  52. int main(){
  53.     int menu, retry, num, aux;
  54.    
  55.     do{
  56.         do{
  57.             system("cls");
  58.             printf(" \n 1. Potencia \n 2. Raiz \n 0. Salir \n ");
  59.             scanf("%d",&menu);
  60.         }while( menu<0 || menu>2 );
  61.    
  62.         system("cls");
  63.         fflush(stdin);
  64.  
  65.         switch( menu ){
  66.             case 0: return 0;
  67.                  break;
  68.             case 1: printf(" \n -= Potencia =- \n Introduce numero (espacio) exponente: ");
  69.                     scanf("%d %d",&num,&aux);
  70.                     if( aux>=0 ) printf(" %d \n ",pot_pos(num,aux));
  71.                     else printf(" %f \n ",pot_neg(num,aux));
  72.                  break;
  73.             case 2: printf(" \n -= Raiz =- \n Introduce numero (espacio) indice: ");
  74.                     scanf("%d %d",&num,&aux);
  75.                     if( raiz(num,aux)!=0 ) printf(" %f \n ",raiz(num,aux));
  76.         }
  77.  
  78.         printf(" \n Volver al menu(1=si)? ");
  79.         scanf("%d",&retry);
  80.     }while( retry==1 );
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement