Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. /* Ασκηση 2 ερασια 5 */
  4.  
  5. int menu(int a,int b);
  6. void printMenu(void);
  7. void printPow(int a,int b);
  8. void printFactorial(int a,int b);
  9. void printCombinations(int a,int b);
  10. int pow(int a,int b);
  11. int factorial(int n);
  12. int combinations(int n,int k);
  13.  
  14. int main() {
  15.  
  16. int a,b;
  17. scanf("%d %d",&a,&b);
  18. printf("%d",menu(a,b));
  19.  
  20. return 0;
  21. }
  22.  
  23.  
  24. int menu(int a,int b) {
  25. int choice;
  26. int attempts = 0;
  27. do {
  28. printf("\na=%d,b=%d\n",a,b);
  29. printMenu();
  30. scanf("%d",&choice);
  31. switch(choice) {
  32. case 1:
  33. printPow(a,b);
  34. break;
  35. case 2:
  36. printFactorial(a,b);
  37. break;
  38. case 3:
  39. printCombinations(a,b);
  40.  
  41. }
  42. attempts++;
  43. }while(choice != 4);
  44.  
  45. return attempts;
  46. }
  47.  
  48. /* Εμφανιζει απλα το menu */
  49. void printMenu(void) {
  50. printf("Main menu\n");
  51. printf("1. Υπολογισμος α^β\n");
  52. printf("2. Υπολογισμος του α! και του β!\n");
  53. printf("3. Υπολογισμος των συνδιασμων α ανα β\n");
  54. printf("4. εξοδος\n\n");
  55. printf("Βαλε μια επιλογη: ");
  56. }
  57.  
  58. int pow(int a,int b) {
  59. /* Αν β == 0 τοτε α^0 = 1 ετσι επιστρεφουμε 1 */
  60. if(b == 0) {
  61. return 1;
  62. }
  63. /* Αν το β == 1 τοτε επιστρεφουμε α^1 = α */
  64. if(b == 1) {
  65. return a;
  66. }
  67.  
  68. int result;
  69.  
  70. if(b & 1) {
  71. result = pow(a,(b-1)/2);
  72. return result * result * a;
  73. }
  74.  
  75. result = pow(a,b/2);
  76. return result * result;
  77. }
  78.  
  79. /* Υπολογιζει το ν! ##Προσοχη δεν δουλεβει για αρνητικους αριθμους και δεν κανω καποιο ελενχο τιμων## */
  80. int factorial(int n) {
  81.  
  82. if(n < 2) {
  83. return 1;
  84. }
  85.  
  86. return n*factorial(n-1);
  87. }
  88.  
  89. int combinations(int n,int k) {
  90.  
  91. return factorial(n) / (factorial(k) * factorial(n-k));
  92. }
  93.  
  94. void printPow(int a,int b) {
  95. printf("%d^%d = %d\n",a,b,pow(a,b));
  96. }
  97.  
  98. void printFactorial(int a,int b) {
  99. printf("%d! = %d\n",a,factorial(a));
  100. printf("%d! = %d\n\n",b,factorial(b));
  101. }
  102.  
  103. void printCombinations(int a,int b) {
  104. printf("Το πληθος των συνδιασμων που μπορουν να γινουν %d ανα %d ειναι %d\n",a,b,combinations(a,b));
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement