Advertisement
Miketo_prog

Menu Pascal Fibonacci Armstrong

Jun 11th, 2020 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define SALTO1 cout<<"\n"
  5. #define SALTO2 cout<<"\n\n"
  6. #define TAB3 cout<<"\t\t   "
  7.  
  8. //Menú
  9. bool programa();
  10. void menu();
  11. int leerOpc();
  12.  
  13. //Pascal
  14. void triangPascal(int);
  15. void impMat(int**, int);
  16. void espaciar(int, int, int);
  17. void leerPascal();
  18.  
  19. ///Fibonacci
  20. void fibonacci(int, long long int *);
  21. void leerFib();
  22.  
  23. ///Armstrong
  24. int digitar(const int, int [], int);
  25. bool esNumArmstrong(int);
  26. long int pot(int, int);
  27. void leerArmstrong();
  28.  
  29.  
  30. int main()
  31. {
  32.     while(programa());
  33.    
  34.     return 0;
  35. }
  36.  
  37. bool programa(){
  38.     int opc;
  39.    
  40.     do{
  41.         system("clear");
  42.         menu();
  43.         opc = leerOpc();
  44.     }while( (opc<1) || (opc>4) );
  45.    
  46.     system("clear");
  47.    
  48.     switch (opc){
  49.         case 1:
  50.             leerPascal();
  51.             SALTO2;
  52.             system("pause");
  53.             return 1;
  54.         case 2:
  55.             leerFib();
  56.             system("pause");
  57.             return 1;
  58.         case 3:
  59.             leerArmstrong();
  60.             system("pause");
  61.             return 1;
  62.         case 4:
  63.             return 0;
  64.     }
  65.    
  66.    
  67. }
  68.  
  69. ///MENÚ
  70.  
  71. void menu(){
  72.     string menuOps[]={"M     E     N     Ú",
  73.                       "1. Triángulo de Pascal",
  74.                       "2. Fibonacci",
  75.                       "3. Armstrong",
  76.                       "4. Salir"};
  77.     int elem_menu=5;
  78.    
  79.     SALTO2;
  80.    
  81.     for(int i=0; i<elem_menu; i++){
  82.         TAB3;
  83.         cout<<menuOps[i]<<endl;
  84.         if( i==0 ) SALTO2;
  85.     }
  86.    
  87.     SALTO2;
  88.     TAB3;
  89.    
  90. }
  91.  
  92. int leerOpc(){
  93.     int opc;
  94.    
  95.     cin>>opc;
  96.    
  97.     return opc;
  98. }
  99.  
  100. /// De MENÚ
  101.  
  102.  
  103. ///PASCAL
  104.  
  105. void leerPascal(){
  106.     int n_filas;
  107.    
  108.     cout<<"Cuántas filas deseas del triángulo de Pascal?\n\n\t";
  109.     cin>>n_filas;
  110.    
  111.     SALTO2;
  112.    
  113.     triangPascal(n_filas);
  114.    
  115. }
  116.  
  117. void triangPascal(int n){
  118.  
  119.     int **triangle = new int*[n];
  120.     for(int i=0; i<n; i++)
  121.         triangle[i] = new int[n];
  122.    
  123.     //Rellenamos la matriz
  124.     //0's
  125.     for(int i=0; i<n; i++)
  126.         for(int j=0; j<n; j++)
  127.             triangle[i][j] = 0;
  128.            
  129.     // 1's
  130.     for(int i=0; i<n; i++)
  131.         for(int j=0; j<n; j++)
  132.             if( (j==0) || (i==j) )
  133.                 triangle[i][j] = 1;
  134.                
  135.     // sumas
  136.     for(int i=2; i<n; i++)
  137.         for(int j=1; j<n && i!=j; j++)
  138.             triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
  139.    
  140.     //Imprime la matriz
  141.     impMat(triangle, n);
  142.    
  143.     //Borra la matriz triangle
  144.     for(int i=0; i<n; i++)
  145.         delete[] triangle[i];
  146.        
  147.     delete[] triangle;
  148.    
  149. }
  150.  
  151.  
  152. void impMat(int **mat, int n){
  153.     int spacing=3;
  154.     int m=n+4;
  155.    
  156.     for(int i=0; i<n; i++, m--){
  157.         for(int j=0; j<n; j++)
  158.             if( mat[i][j] != 0 ){
  159.                 if( j==0 )
  160.                     espaciar(i+1, m, spacing);
  161.                 cout<<"  "<<mat[i][j]<<" ";
  162.             }
  163.         cout<<endl;
  164.     }
  165. }
  166.  
  167. void espaciar(int fil, int n, int spcng){
  168.     int spaces = (spcng*n-fil)/2;
  169.    
  170.     for(int i=0; i<=spaces; i++)
  171.         cout<<" ";
  172.    
  173. }
  174.  
  175. /// De PASCAL
  176.  
  177.  
  178. /// FIBONACCI
  179.  
  180. void fibonacci(const int elem, long long int *arr){
  181.     int fib=1;
  182.    
  183.     arr[0]=0;
  184.     for(int i=1; i<elem; i++){
  185.         arr[i]=fib;
  186.         fib = arr[i-1] + fib;
  187.     }
  188.    
  189. }
  190.  
  191.  
  192. void leerFib(){
  193.     int numero;
  194.     long long int *arr;
  195.    
  196.     //Si el num solicitado es menor o igual que 1
  197.    do
  198.    {
  199.          cout << "Introduce un número mayor que 1: ";
  200.          cin >> numero;
  201.    }while(numero<=1);
  202.    
  203.    system("clear");
  204.    
  205.    cout << endl;
  206.     cout << "Los " << numero << " primeros numeros de la serie de Fibonacci son:" << endl;
  207.    
  208.     arr = new long long int[numero];
  209.    
  210.     fibonacci(numero, arr);
  211.    
  212.    cout << endl << endl;
  213.    
  214.    //imp fib
  215.    for(int j=0; j<numero; j++){
  216.     cout<<arr[j];
  217.     if( j!=(numero-1) )
  218.         cout<<", ";
  219.     else
  220.     cout<<" ...";
  221.    }
  222.    
  223.    SALTO2;
  224.  
  225. }
  226.  
  227. /// De FIBONACCI
  228.  
  229.  
  230. ///ARMSTROMG
  231.  
  232. bool esNumArmstrong(int num){
  233.     int tam=10,
  234.          arr[tam],
  235.          digits,
  236.          sumDig=0;
  237.     bool armstrong;
  238.    
  239.     //dígitos
  240.     digits = digitar(num, arr, tam);
  241.    
  242.     //suma
  243.     for(int i=0; i<digits; i++)
  244.         sumDig += pot(arr[i], digits);
  245.        
  246.     //Vemoa si es un armstrong
  247.     ( sumDig == num )?
  248.     (armstrong=true): (armstrong=false);
  249.    
  250.     return armstrong;
  251. }
  252.  
  253. void leerArmstrong(){
  254.     int num;
  255.    
  256.     cout<<"Escribe un número y vamos a ver si es un número narcisista. Por ejemplo, el 153 lo es.";
  257.    
  258.     SALTO2;
  259.    
  260.     cin>>num;
  261.    
  262.     SALTO2;
  263.    
  264.     if( esNumArmstrong(num) )
  265.         cout<<"Vaya! Vaya! Has encontrado uno! El número "<<num<<" es un número Armstromg.";
  266.     else
  267.         cout<<"Lo siento! Este número no es Armstromg.";
  268.        
  269.     SALTO2;
  270.    
  271. }
  272.  
  273. int digitar(const int num, int buffer[], int tam){
  274.     int buf[tam], n=num, i, j;
  275.    
  276.     //digitando
  277.     for(i=0; i<tam && n!=0; i++){
  278.         buf[i] = (n%10);
  279.         n /= 10;
  280.     }
  281.     i--;
  282.    
  283.     //inversión
  284.     for(j=0; j<tam && i>=0; j++, i--)
  285.         buffer[j] = buf[i];
  286.        
  287.     return j;
  288. }
  289.  
  290.  
  291. long int pot(int base, int exp){
  292.     long int prod=1;
  293.    
  294.     //mult sucesivas
  295.     for(int i=1; i<=exp; i++)
  296.         prod *= base;
  297.    
  298.     return prod;
  299. }
  300.  
  301. ///De ARMSTRONG
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement