Guest User

Untitled

a guest
Apr 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SIZE 20
  4.  
  5. void bubble_sort(int array[],int n); // from rosetta code
  6. void selection_sort(int array[],int b); // from rosetta code
  7. void normalpop(int array[],int n, int *numtobedltp);
  8. void stackpop(int array[] ,int *numtobedltp);
  9. void pushforboth(int array[], int n,int *numtobedltp);
  10. void print(int array[],int* numtobedltp);
  11.  
  12. // Main function
  13. int main()
  14. {
  15. FILE *dat;
  16. int array[SIZE],firstmenu,secondmenu,num,numtobedlt=SIZE,i=0; // initialisation and declaration of variables
  17. // File process
  18. dat = fopen("nums.txt","r");
  19. while(i<SIZE){
  20. fscanf(dat,"%d",&num);
  21. array[i]=num;
  22. i++;
  23. }
  24. fclose(dat);
  25. //First loop
  26. while(firstmenu!= 0){ // First Menu
  27. printf("Please, type a number of the above: \n 1 => Normal arrayay \n 2 => Stack arrayay \n 0 => Escape\n Your Choice => ");
  28. scanf("%d",&firstmenu);
  29. secondmenu=1; // Initialisation
  30. // Switch First Menu possibilities
  31. switch(firstmenu){
  32. case 0:
  33. printf("Already quiting us? :( \n");
  34. break;
  35. case 1: // Normal arr
  36. while(secondmenu!=0){
  37. if(firstmenu !=0){
  38. printf("\nPlease, choose a number of the above:\n 1 => Sort Using the bubble_selection\n 2 => Sort Using the sort_selection\n 3 => Push \n 4 => Pop\n 0 => Back to first menu\n");
  39. scanf("%d",&secondmenu);
  40. }
  41. switch(secondmenu){
  42. case 0 :
  43. printf("\nYou're going back to the first menu\n\n"); // Escape
  44. break;
  45. case 1 :
  46. bubble_sort(array,SIZE); // calling bubble_sort function
  47. print(array,&numtobedlt); // printing the result sorted
  48. break;
  49. case 2 :
  50. selection_sort(array,SIZE); // calling selection_sort function
  51. print(array,&numtobedlt); // printing the result sorted
  52. break;
  53. case 3 :
  54. pushforboth(array,SIZE,&numtobedlt); // calling the push function
  55. print(array,&numtobedlt); // printing the result
  56. break;
  57. case 4 :
  58. normalpop(array,SIZE,&numtobedlt); // calling the pop function (normal array)
  59. print(array,&numtobedlt); // printing the result
  60. break;
  61. default :
  62. printf("\nPlease, enter an appropriate integer (1, 2, 3, 4 or 0)\n"); // Error message (Second Menu)
  63. }
  64. }
  65. break;
  66. case 2 :
  67. // Stack array
  68. while(secondmenu!=0){
  69. if(firstmenu !=0){
  70. printf("\nPlease, choose a number of the above:\n 1 => Sort Using the bubble_selection\n 2 => Sort Using the sort_selection\n 3 => Push \n 4 => Pop\n 0 => Escape\n");
  71. scanf("%d",&secondmenu);
  72. }
  73. switch(secondmenu){
  74. case 0 :
  75. printf("\nYou're going back to the first menu\n\n"); // Escape
  76. break;
  77. case 1 :
  78. bubble_sort(array,SIZE); // calling bubble_sort function
  79. print(array,&numtobedlt); // printing the result sorted
  80. break;
  81. case 2 :
  82. selection_sort(array,SIZE); // calling selection_sort function
  83. print(array,&numtobedlt); // printing the result sorted
  84. break;
  85. case 3 :
  86. pushforboth(array,SIZE,&numtobedlt); // calling the push function
  87. print(array,&numtobedlt); // printing the result
  88. break;
  89. case 4 :
  90. stackpop(array,&numtobedlt); // calling the pop function (stack array)
  91. print(array,&numtobedlt); // printing the result
  92. break;
  93. default :
  94. printf("\nPlease, enter an appropriate integer (1, 2, 3, 4 or 0)\n"); // Error message (Second Menu)
  95. }
  96. }
  97. break;
  98. default:
  99. printf("\nPlease, enter an appropriate integer (1, 2 or 0)\n"); // Error message (First Menu)
  100. }
  101. }
  102. return (0);
  103. }
  104.  
  105. // Functions declaration
  106.  
  107. void bubble_sort(int *a, int n) {
  108. int j, t = 1;
  109. while (n-- && t)
  110. for (j = t = 0; j < n; j++) {
  111. if (a[j] <= a[j + 1]) continue;
  112. t = a[j], a[j] = a[j + 1], a[j + 1] = t;
  113. t=1;
  114. }
  115. }
  116. void selection_sort (int array[], int n) {
  117. int i, j, m, t;
  118. for (i = 0; i < n; i++) {
  119. for (j = i, m = i; j < n; j++) {
  120. if (array[j] < array[m])
  121. m = j;
  122. }
  123. t = array[i];
  124. array[i] = array[m];
  125. array[m] = t;
  126. }
  127. }
  128. void normalpop(int array[],int n, int* numtobedltp){
  129. if(*numtobedltp>0){
  130. int i=0;
  131. while(i<*numtobedltp){
  132. array[i]=array[i+1];
  133. i++;
  134. }
  135. *numtobedltp-=1;
  136. }
  137. else {
  138. printf("\nEMPTY FILE! We can't pop any value\n");
  139. }
  140. }
  141. void stackpop(int array[],int* numtobedltp){
  142. if(*numtobedltp>=0){
  143. *numtobedltp-=1;
  144. array[*numtobedltp]=0;
  145. }
  146. else
  147. printf("\nEMPTY FILE! We can't pop any value\n");
  148. }
  149. void pushforboth(int array[],int n,int* numtobedltp){
  150. int num;
  151. if(*numtobedltp<n){
  152. printf("\nEnter an appropriate integer to push => ");
  153. scanf("%d",&num);
  154. array[*numtobedltp]=num;
  155. *numtobedltp+=1;
  156. }
  157. else
  158. printf("\nYou can't exceed 20 integers! It's already full\n");
  159. }
  160. void print(int array[], int* numtobedltp){
  161. int i=0;
  162. int num=*numtobedltp-1;
  163. while(i<*numtobedltp){
  164. printf(" %d ",array[i]);
  165. i++;
  166. }
  167. }
Add Comment
Please, Sign In to add comment