Created
November 21, 2012 23:09
-
-
Save alexesDev/4128439 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 <stdio.h> | |
| #include <string.h> | |
| #define SIZE 10 | |
| #define STRING_SIZE 20 | |
| struct Detail | |
| { | |
| // индекс элемента в массиве перед сортировкой | |
| unsigned int index; //4 | |
| char name[STRING_SIZE] ;//20 | |
| char type; //1 | |
| unsigned int count; //4 | |
| unsigned int weight; //4 | |
| //4 + 20 + 1 + 4 + 4 == 33 == sizeof(struct Detail) | |
| } details[SIZE]; | |
| // индекс последнего введённого элемента | |
| int lastRowIndex; | |
| // Функция для вывода массива данных | |
| void printTable(void) | |
| { | |
| int i; | |
| printf("+----+----------------------+------+-------+--------+\n"); | |
| printf("| # | Name | Type | Count | Weight |\n"); | |
| printf("+----+----------------------+------+-------+--------+\n"); | |
| for(i = 0; i < SIZE; i++) | |
| { | |
| if(i > lastRowIndex) | |
| break; | |
| printf("| %2d | %20s | %4s | %5d | %6d |\n", details[i].index + 1, details[i].name, &details[i].type, details[i].count, details[i].weight); | |
| printf("+----+----------------------+------+-------+--------+\n"); | |
| } | |
| printf("\n"); | |
| } | |
| // функция запрашивает все данные | |
| void enterTable(void) | |
| { | |
| int i; | |
| for(i = 0; i < SIZE; i++) | |
| { | |
| printf("Enter detail data:\n"); | |
| printf(" - name: "); | |
| scanf("%s", details[i].name); | |
| //string compare | |
| if(!strcmp(details[i].name, "***")) | |
| { | |
| lastRowIndex = i - 1; | |
| break; | |
| } | |
| printf(" - type (Z, O, P): "); | |
| scanf("%s", &details[i].type); | |
| // проверка типа детали | |
| if(details[i].type != 'Z' && details[i].type != 'O' && details[i].type != 'P') | |
| { | |
| printf("Unknown type. Try again!\n\n"); | |
| // уменьшаем индекс, чтобы потребовать ввести данные заново | |
| i--; | |
| continue; | |
| } | |
| printf(" - count: "); | |
| scanf("%d", &details[i].count); | |
| printf(" - weight: "); | |
| scanf("%d", &details[i].weight); | |
| printf("\n"); | |
| // запоминаем индекс элемента | |
| details[i].index = i; | |
| } | |
| printf("\n"); | |
| } | |
| // функция для сортировки массива | |
| void sortTable(void) | |
| { | |
| int i, j, m; | |
| struct Detail temp; | |
| for(i = 0; i < lastRowIndex; i++) | |
| { | |
| m = i; | |
| for(j = i + 1; j <= lastRowIndex; j++) | |
| { | |
| if(strcmp(details[m].name, details[j].name) > 0) | |
| m = j; | |
| } | |
| if(m > i) | |
| { | |
| //details[i] <=> details[m] | |
| //memcpy(адрес куда, адрес откуда, сколько) | |
| memcpy(&temp, &details[i], sizeof(temp)); | |
| memcpy(&details[i], &details[m], sizeof(temp)); | |
| memcpy(&details[m], &temp, sizeof(temp)); | |
| // strcpy(temp.name, details[i].name); | |
| // temp.type = details[i].type; | |
| // temp.count = details[i].count; | |
| // temp.weight = details[i].weight; | |
| // temp.index = details[i].index; | |
| // strcpy(details[i].name, details[m].name); | |
| // details[i].type = details[m].type; | |
| // details[i].count = details[m].count; | |
| // details[i].weight = details[m].weight; | |
| // details[i].index = details[m].index; | |
| // strcpy(details[m].name, temp.name); | |
| // details[m].type = temp.type; | |
| // details[m].count = temp.count; | |
| // details[m].weight = temp.weight; | |
| // details[m].index = temp.index; | |
| } | |
| } | |
| } | |
| int main(void) | |
| { | |
| // вызов функции для ввода таблицы | |
| enterTable(); | |
| // вывод исходной таблицы | |
| printf("Raw table:\n"); | |
| printTable(); | |
| // вызов функции для сортировки таблицы | |
| sortTable(); | |
| // вывод отсортированной таблицы | |
| printf("Sorted table:\n"); | |
| printTable(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment