Advertisement
minilose

qsort W32CRT

Feb 23rd, 2023
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. // Winn32CRTconsole.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <locale.h>
  5. #include <stdlib.h>
  6. #include <search.h>
  7.  
  8. struct List
  9. {
  10.     _TCHAR f_name[15];
  11.     _TCHAR s_name[15];
  12.     _TCHAR l_name[15];
  13.     _TCHAR group[4];
  14.     int f_num;
  15. };
  16.  
  17. int compare_fnum(const void* vptr1, const void* vptr2)
  18. {
  19.     if (((struct List*)vptr1)->f_num > ((struct List*)vptr2)->f_num)
  20.         return 1;
  21.  
  22.     if (((struct List*)vptr1)->f_num < ((struct List*)vptr2)->f_num)
  23.         return -1;
  24.  
  25.     return 0;
  26. }
  27.  
  28. int compare_group_then_sname(const void* vptr1, const void* vptr2)
  29. {
  30.     if (_tcscmp(((struct List*)vptr1)->group, ((struct List*)vptr2)->group) == 0)
  31.         return _tcscmp(((struct List*)vptr2)->s_name, ((struct List*)vptr1)->s_name);
  32.  
  33.     return _tcscmp(((struct List*)vptr1)->group, ((struct List*)vptr2)->group);
  34. }
  35.  
  36. int _tmain(int argc, _TCHAR* argv[])
  37. {
  38.     setlocale(LC_CTYPE, "en.UTF-8");
  39.     struct List Arr[100];
  40.  
  41.     FILE* fp;
  42.  
  43.         if (_tfopen_s(&fp,argv[1], _T("r"))!= 0)
  44.             printf("File opening failed!\n");
  45.  
  46.         int j = 0;
  47.  
  48.         while (feof(fp)==0)                                      
  49.         {
  50.             _ftscanf(fp, _T("%s"), Arr[j].f_name);
  51.             _ftscanf(fp, _T("%s"), Arr[j].s_name);
  52.             _ftscanf(fp, _T("%s"), Arr[j].l_name);
  53.             _ftscanf(fp, _T("%s"), Arr[j].group);
  54.             fscanf(fp, "%d", &Arr[j].f_num);
  55.             j++;
  56.         }
  57.  
  58.         if (_ttoi(argv[2]) == 1)
  59.             qsort(&Arr, j, sizeof(struct List), compare_fnum);
  60.  
  61.         if (_ttoi(argv[2]) == 2)
  62.             qsort(&Arr, j, sizeof(struct List), compare_group_then_sname);
  63.  
  64.         for (int i = 0; i < j; i++)
  65.         {
  66.             _tprintf(_T("%s "), Arr[i].f_name);
  67.             _tprintf(_T("%s "), Arr[i].s_name);
  68.             _tprintf(_T("%s "), Arr[i].l_name);
  69.             _tprintf(_T("%s "), Arr[i].group);
  70.             printf("%d\n", Arr[i].f_num);
  71.         }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement