isefire

gc_quicksort.c

Mar 5th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.72 KB | None | 0 0
  1. //Quicksort implementation in C
  2. //@author Daniel Latham
  3. //input: $0=file with list of numbers on line 0
  4. //       $1=type of number(char, short, int, long, float, double)
  5. //       $2=# of numbers
  6. //output: specified inputs and sorted array last
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12.  
  13. #define TYPE char
  14. #define READ strtol(buff, &end, 10)
  15. #define PRINT printf("%c ", nums[i])
  16. #include "gc_quicksort.h"
  17. #undef TYPE
  18. #undef PRINT
  19.  
  20. #define TYPE short
  21. #define PRINT printf("%hi ", nums[i]);
  22. #include "gc_quicksort.h"
  23. #undef TYPE
  24. #undef PRINT
  25.  
  26. #define TYPE int
  27. #define PRINT printf("%d ", nums[i]);
  28. #include "gc_quicksort.h"
  29. #undef TYPE
  30. #undef PRINT
  31.  
  32. #define TYPE long
  33. #define PRINT printf("%li ", nums[i]);
  34. #include "gc_quicksort.h"
  35. #undef TYPE
  36. #undef READ
  37. #undef PRINT
  38.  
  39. #define READ strtod(buff, &end)
  40. #define TYPE float
  41. #define PRINT printf("%f ", nums[i]);
  42. #include "gc_quicksort.h"
  43. #undef TYPE
  44. #undef PRINT
  45.  
  46. #define TYPE double
  47. #define PRINT printf("%F ", nums[i]);
  48. #include "gc_quicksort.h"
  49. #undef TYPE
  50. #undef PRINT
  51.  
  52. struct TYPE_VALUES {
  53.     int CHAR, SHORT, INT, LONG, FLOAT, DOUBLE;
  54. };
  55.  
  56. typedef struct TYPE_VALUES type_v;
  57.  
  58. void build_type_values(type_v* t){
  59.     const char* types[] = {"char", "short", "int", "long", "float", "double" };
  60.     // Changes argv[2] type to lowercase
  61.     // Computes number associated with the addition of all chars in argv[2]
  62.    
  63.     int identifier = 0;
  64.     const int PRIME = 3;
  65.     for (int j = 0; j < 6; j++){
  66.         for (int i = 0; types[j][i]; i++){
  67.             identifier = PRIME * (identifier + types[j][i]);
  68.         }
  69.         // J used to determine which array element we're on
  70.         // so, 0 should be char, from types array
  71.         switch (j){
  72.             case 0:
  73.                 t->CHAR = identifier;
  74.                 break;
  75.             case 1:
  76.                 t->SHORT = identifier;
  77.                 break;
  78.             case 2:
  79.                 t->INT = identifier;
  80.                 break;
  81.             case 3:
  82.                 t->LONG = identifier;
  83.                 break;
  84.             case 4:
  85.                 t->FLOAT = identifier;
  86.                 break;
  87.             case 5:
  88.                 t->DOUBLE = identifier;
  89.                 break;
  90.         }
  91.         // Reset identifier
  92.         identifier = 0;
  93.     }
  94.  
  95. }
  96.  
  97. int main(int argc, char** argv){
  98.  
  99.    
  100.         // Prints file name for user
  101.     printf("%s\n", argv[1]);
  102.     // Type of array
  103.     printf("Type: %s\n", argv[2]);
  104.     // Size of array
  105.     printf("Size: %s\n", argv[3]);
  106.        
  107.         // Create struct
  108.         struct TYPE_VALUES t;
  109.         // Initialize values in t
  110.        
  111.         // Create pointer to struct in memory
  112.         struct TYPE_VALUES* tt = &t;
  113.    
  114.         build_type_values(tt);
  115.  
  116.     // Changes argv[2] type to lowercase
  117.     // Computes number associated with the addition of all chars in argv[2]
  118.     int identifier = 0;
  119.     const int PRIME = 3;
  120.     for (int i = 0; argv[2][i]; i++){
  121.         argv[2][i] = tolower(argv[2][i]);
  122.         identifier = PRIME * (identifier + argv[2][i]);
  123.     }
  124.  
  125.     if (identifier == tt->CHAR){
  126.             choose_type_char(argc, argv, 0);
  127.             return 1;
  128.         } else if (identifier == tt->SHORT){
  129.             choose_type_short(argc, argv, 1);
  130.             return 1;
  131.         } else if (identifier == tt->INT){
  132.             choose_type_int(argc, argv, 2);
  133.             return 1;
  134.         } else if (identifier == tt->LONG){
  135.             choose_type_long(argc, argv, 3);
  136.             return 1;
  137.         } else if (identifier == tt->FLOAT){
  138.             choose_type_float(argc, argv, 4);
  139.             return 1;
  140.         } else if (identifier == tt->DOUBLE){
  141.             choose_type_double(argc, argv, 5);
  142.             return 1;
  143.         } else {
  144.             printf("ERROR: Incorrect Number Type Specified%s", "\n");
  145.             return -1;
  146.         }
  147.        
  148. }
Advertisement
Add Comment
Please, Sign In to add comment