Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define MAX 2
  5. #define COUNT_OF(x) (sizeof(x) / sizeof(0[x])) //length of arrays at compile time
  6.  
  7. struct Person {
  8. char firstname[64];
  9. char lastname[64];
  10. int age;
  11. };
  12.  
  13. int Person_cmp_firstname(const void* x, const void* y) {
  14. struct Person *ix = (struct Person *)x;
  15. struct Person *iy = (struct Person *)y;
  16. return strcmp(ix->firstname, iy->firstname);
  17. }
  18.  
  19.  
  20. int Person_cmp_lastname(const void* x, const void* y ) {
  21. struct Person *ix = (struct Person *)x;
  22. struct Person *iy = (struct Person *)y;
  23. return strcmp(ix->lastname, iy->lastname);
  24. }
  25. int Person_cmp_age(const void* x, const void* y) {
  26. struct Person *px = (struct Person *) x;
  27. struct Person *py = (struct Person *) y;
  28. return px->age - py->age;
  29. }
  30.  
  31. int main(){;
  32. int choice;
  33. struct Person *P[10];
  34. printf("t***PROGRAM TO SORT PERSONS***nn");
  35. for(int i=0; i<3; i++){
  36. P[i] = (struct Person *) malloc(sizeof(struct Person));
  37. printf("Firstname: ");
  38. scanf("%s", P[i]->firstname);
  39. printf("Lastname: ");
  40. scanf("%s", P[i]->lastname);
  41. printf("Age: ");
  42. scanf("%d", &P[i]->age);
  43. printf("t***NEXT PERSON***nn");
  44. }
  45. do{
  46. printf("nt***CHOOSE HOW TO SORT***nntBy firstname: 1ntBy lastname: 2ntBy age: 3ntExit Program: 4nn");
  47. scanf("%d", &choice);
  48. switch(choice){
  49. case 1:
  50. printf("t***SORTING BY FIRSTNAME...***nn");
  51. qsort(P, COUNT_OF(P), sizeof(struct Person), Person_cmp_firstname);
  52. printf("t***DONE***nn");
  53. for(unsigned int i=0; i<3; i++){
  54. printf( "Firstname: %st| Lastname: %st| Age: %dn", P[i]->firstname, P[i]->lastname, P[i]->age );
  55. }
  56. break;
  57. case 2:
  58. printf("t***SORTING BY LASTNAME...***nn");
  59. qsort(P, COUNT_OF(P), sizeof(struct Person ), Person_cmp_lastname);
  60. printf("t***DONE***nn");
  61. for(unsigned int i=0; i<3; i++){
  62. printf( "Firstname: %st| Lastname: %st| Age: %dn", P[i]->firstname, P[i]->lastname, P[i]->age );
  63. }
  64. break;
  65. case 3:
  66. printf("t***SORTING BY AGE...***nn");
  67. qsort(P, COUNT_OF(P), sizeof(struct Person), Person_cmp_age);
  68. printf("t***DONE***nn");
  69. for(unsigned int i=0; i<3; i++){
  70. printf( "Firstname: %st| Lastname: %st| Age: %dn",P[i]->firstname, P[i]->lastname, P[i]->age );
  71. }
  72. break;
  73. case 4:
  74. printf("t***EXITING PROGRAM***nn");
  75. for (int j = 0; j < 3; j++) {
  76. free(P[j]);
  77. }
  78. exit(0);
  79. default:
  80. printf("t***INVALID OPTION***nn");
  81. break;
  82. }
  83. }while(1);
  84. return EXIT_SUCCESS;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement