uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

menu.c (1701B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <math.h>
      4 #include "menu.h"
      5 
      6 
      7 void menu(int a, int b)
      8 {
      9     int menuChoice;
     10 
     11     do
     12     {
     13         system("clear||cls");
     14 
     15         printf("[1] %d to the power of %d\n[2] %d! and %d!\n[3] Number of combinations between %d and %d\n[4] Exit\n", a, b, a, b, a, b);
     16         printf("Choice: ");
     17         scanf(" %d", &menuChoice);
     18 
     19         switch(menuChoice)
     20         {
     21             case 1:
     22                 results(a, b, 1);
     23                 break;
     24             case 2:
     25                 results(a, b, 2);
     26                 break;
     27             case 3:
     28                 results(a, b, 3);
     29                 break;
     30         }
     31         
     32         pause();
     33     } while (menuChoice != 4);
     34 
     35     results(a, b, 4);
     36 }
     37 
     38 void pause()
     39 {
     40     do
     41     {
     42         printf("\nPress [Enter] to continue. . .");
     43         getchar();
     44     } while (getchar() != '\n');
     45     
     46 }
     47 
     48 void results(int a, int b, int menuChoice)
     49 {
     50     static int numChoices = 0;
     51     
     52     switch(menuChoice)
     53     {
     54         case 1:
     55             printf("%d^%d = %d\n", a, b, power(a, b));
     56             break;
     57         case 2:
     58             printf("%d! = %d\n", a, factorial(a));
     59             printf("%d! = %d\n", b, factorial(b));
     60             break;
     61         case 3:
     62             printf("Combinations between %d and %d: %d\n", a, b, combinations(a, b));
     63             break;
     64         case 4:
     65             printf("Total number of choices: %d\n", numChoices);
     66     }
     67 
     68     numChoices++;
     69 }
     70 
     71 int power(int a, int b)
     72 {
     73     return pow(a, b);
     74 }
     75 
     76 int factorial(int a)
     77 {
     78     int i, fac;
     79 
     80     for (i = 1, fac = 1; i <= a; i++)
     81         fac *= i;
     82 
     83     return fac;
     84 }
     85 
     86 int combinations(int a, int b)
     87 {
     88     return factorial(a) / (factorial (b) * factorial(a - b));
     89 }