Created
November 18, 2015 21:08
-
-
Save stepancar/e55f7600140501e5f4ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdafx.h" | |
| #define _CRT_SECURE_NO_WARNINGS | |
| #pragma once | |
| #pragma warning(disable:4996) | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <locale.h> | |
| #include <string.h> | |
| #include <malloc.h> | |
| struct X { | |
| char f[21]; | |
| char i[21]; | |
| char o[21]; | |
| int tel; | |
| }; | |
| void sortq(X *a1, long n, int(*cmp)(X*, X*)) | |
| { | |
| X p; | |
| X t; | |
| int i, j; | |
| if (n < 2) /*обязательно */ | |
| return; | |
| p = a1[n / 2]; /*опорный элемент - среднее значение*/ | |
| for (i = 0, j = n - 1;; i++, j--)/*i - номер левого элем-та,j - последнего .i++ - идем с первого вправо ,j— с правого - назад. ;; - чтобы работали оба цикла */ | |
| { | |
| while (cmp(&a1[i], &p)) /*пока элемент левой части < опорного - идем вправо */ | |
| i++; | |
| while (cmp(&p, &a1[j])) /*пока элемент правой части > опорного - идем влево */ | |
| j--; | |
| if (i >= j) /*когда i стал > j - прерываем цикл*/ | |
| break; | |
| t = a1[i];/*меняем левый и правый элем местами*/ | |
| a1[i] = a1[j]; | |
| a1[j] = t; | |
| } | |
| sortq(a1, i, cmp);/*вызываем функцию(указатель,левый элем)*/ | |
| sortq(a1 + i, n - i, cmp);/*вызываем функцию(указатель+левый элем,размер- левый элем*/ | |
| } | |
| int int_cmp(void *a, void *b){ return (int)a > (int)b; } | |
| int compare(X* a, X* b) | |
| { | |
| int xf, xi, xo; | |
| xf = strcmp(a->f, b->f); | |
| xi = strcmp(a->i, b->i); | |
| xo = strcmp(a->o, b->o); | |
| if ((xf < 0) || (xf == 0 && xi < 0) || (xf == 0 && xi == 0 && xo < 0)) | |
| { | |
| return 1; | |
| } | |
| else return 0; | |
| } | |
| int main(void) | |
| { | |
| X *q; | |
| int n = 5; | |
| X arr[100]; | |
| int i; | |
| FILE* in; | |
| in = fopen("C:\\projects\\z.txt", "r"); | |
| //p = (X*)malloc(n* sizeof(X)); | |
| //íà÷àëî realloc'a | |
| for (int i = 0;i<=n; i++) | |
| { | |
| fscanf(in, "%s", arr[i].f); | |
| fscanf(in, "%s", arr[i].i); | |
| fscanf(in, "%s", arr[i].o); | |
| fscanf(in, "%i", &arr[i].tel); | |
| /*if (i == n) | |
| if (q = (X*)realloc(p, sizeof(X)*n * 2)) /*ââîäèì name2 ïîòîìó ÷òî åñëè realloc áóäåò íåâåðåí*/ | |
| /*{ | |
| p = q; /*÷òîáû âåðíóòü çíà÷åíèå name1*/ | |
| /*n *= 2; | |
| } | |
| /*else | |
| { | |
| printf("No memory\n "); | |
| break; | |
| } | |
| if (fscanf(in, "%s %s %s %i", p[i].f, p[i].i, p[i].o, &p[i].tel) != 4) | |
| { | |
| break; | |
| }*/ | |
| } | |
| sortq(arr, n, compare); | |
| for (i = 0; i <= n - 1; i++) | |
| { | |
| printf("%s ", arr[i].f); | |
| printf("%s ", arr[i].i); | |
| printf("%s ", arr[i].o); | |
| printf("%i\n ", arr[i].tel); | |
| } | |
| printf("\n \n "); | |
| getchar(); | |
| fclose(in); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment