Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 20
- void bubble_sort(int array[],int n); // from rosetta code
- void selection_sort(int array[],int b); // from rosetta code
- void normalpop(int array[],int n, int *numtobedltp);
- void stackpop(int array[] ,int *numtobedltp);
- void pushforboth(int array[], int n,int *numtobedltp);
- void print(int array[],int* numtobedltp);
- // Main function
- int main()
- {
- FILE *dat;
- int array[SIZE],firstmenu,secondmenu,num,numtobedlt=SIZE,i=0; // initialisation and declaration of variables
- // File process
- dat = fopen("nums.txt","r");
- while(i<SIZE){
- fscanf(dat,"%d",&num);
- array[i]=num;
- i++;
- }
- fclose(dat);
- //First loop
- while(firstmenu!= 0){ // First Menu
- printf("Please, type a number of the above: \n 1 => Normal arrayay \n 2 => Stack arrayay \n 0 => Escape\n Your Choice => ");
- scanf("%d",&firstmenu);
- secondmenu=1; // Initialisation
- // Switch First Menu possibilities
- switch(firstmenu){
- case 0:
- printf("Already quiting us? :( \n");
- break;
- case 1: // Normal arr
- while(secondmenu!=0){
- if(firstmenu !=0){
- 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");
- scanf("%d",&secondmenu);
- }
- switch(secondmenu){
- case 0 :
- printf("\nYou're going back to the first menu\n\n"); // Escape
- break;
- case 1 :
- bubble_sort(array,SIZE); // calling bubble_sort function
- print(array,&numtobedlt); // printing the result sorted
- break;
- case 2 :
- selection_sort(array,SIZE); // calling selection_sort function
- print(array,&numtobedlt); // printing the result sorted
- break;
- case 3 :
- pushforboth(array,SIZE,&numtobedlt); // calling the push function
- print(array,&numtobedlt); // printing the result
- break;
- case 4 :
- normalpop(array,SIZE,&numtobedlt); // calling the pop function (normal array)
- print(array,&numtobedlt); // printing the result
- break;
- default :
- printf("\nPlease, enter an appropriate integer (1, 2, 3, 4 or 0)\n"); // Error message (Second Menu)
- }
- }
- break;
- case 2 :
- // Stack array
- while(secondmenu!=0){
- if(firstmenu !=0){
- 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");
- scanf("%d",&secondmenu);
- }
- switch(secondmenu){
- case 0 :
- printf("\nYou're going back to the first menu\n\n"); // Escape
- break;
- case 1 :
- bubble_sort(array,SIZE); // calling bubble_sort function
- print(array,&numtobedlt); // printing the result sorted
- break;
- case 2 :
- selection_sort(array,SIZE); // calling selection_sort function
- print(array,&numtobedlt); // printing the result sorted
- break;
- case 3 :
- pushforboth(array,SIZE,&numtobedlt); // calling the push function
- print(array,&numtobedlt); // printing the result
- break;
- case 4 :
- stackpop(array,&numtobedlt); // calling the pop function (stack array)
- print(array,&numtobedlt); // printing the result
- break;
- default :
- printf("\nPlease, enter an appropriate integer (1, 2, 3, 4 or 0)\n"); // Error message (Second Menu)
- }
- }
- break;
- default:
- printf("\nPlease, enter an appropriate integer (1, 2 or 0)\n"); // Error message (First Menu)
- }
- }
- return (0);
- }
- // Functions declaration
- void bubble_sort(int *a, int n) {
- int j, t = 1;
- while (n-- && t)
- for (j = t = 0; j < n; j++) {
- if (a[j] <= a[j + 1]) continue;
- t = a[j], a[j] = a[j + 1], a[j + 1] = t;
- t=1;
- }
- }
- void selection_sort (int array[], int n) {
- int i, j, m, t;
- for (i = 0; i < n; i++) {
- for (j = i, m = i; j < n; j++) {
- if (array[j] < array[m])
- m = j;
- }
- t = array[i];
- array[i] = array[m];
- array[m] = t;
- }
- }
- void normalpop(int array[],int n, int* numtobedltp){
- if(*numtobedltp>0){
- int i=0;
- while(i<*numtobedltp){
- array[i]=array[i+1];
- i++;
- }
- *numtobedltp-=1;
- }
- else {
- printf("\nEMPTY FILE! We can't pop any value\n");
- }
- }
- void stackpop(int array[],int* numtobedltp){
- if(*numtobedltp>=0){
- *numtobedltp-=1;
- array[*numtobedltp]=0;
- }
- else
- printf("\nEMPTY FILE! We can't pop any value\n");
- }
- void pushforboth(int array[],int n,int* numtobedltp){
- int num;
- if(*numtobedltp<n){
- printf("\nEnter an appropriate integer to push => ");
- scanf("%d",&num);
- array[*numtobedltp]=num;
- *numtobedltp+=1;
- }
- else
- printf("\nYou can't exceed 20 integers! It's already full\n");
- }
- void print(int array[], int* numtobedltp){
- int i=0;
- int num=*numtobedltp-1;
- while(i<*numtobedltp){
- printf(" %d ",array[i]);
- i++;
- }
- }
Add Comment
Please, Sign In to add comment