| Цитата(t_gran @ 22.3.2012, 03:59) | | Код | #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h>
#define NAME_MAX_LEN 64 #define FACULTY_MAX_LEN 64
typedef struct _TStudent { char name[NAME_MAX_LEN]; unsigned number; char faculty[FACULTY_MAX_LEN]; } TStudent;
typedef struct _TList { TStudent student; struct _TList* next; } TList;
//----------------------------------------------// TList* ListPush(TList** list, const TStudent* student) { TList* node = (TList*) malloc(sizeof(TStudent)); node->student = *student; node->next = *list; *list = node; return node; } //----------------------------------------------// TStudent ListPop(TList** list) { TStudent student = (*list)->student; TList* node = *list; *list = (*list)->next; free(node); return student; } //----------------------------------------------// bool ParseString(const char* str, TStudent* student) { static const char CDelim = ';'; bool isOk = false;
char* delimFirst = strchr(str, CDelim);
char* delimSecond = 0; if (delimFirst) { delimSecond = strchr(delimFirst+1, CDelim); }
if (delimFirst && delimSecond) { memset(student, 0, sizeof(*student)); strncpy(student->name, str, (delimFirst-str)); student->number = atoi(delimFirst+1); strncpy(student->faculty, delimSecond+1, strlen(delimSecond+1) - 1); isOk = true; } return isOk; } //----------------------------------------------// size_t ReadFile(FILE* f, TList** list) { size_t size = 0;
char buff[128]; while (!feof(f)) { TStudent student; *buff = 0; fgets(buff, sizeof(buff), f); if (ParseString(buff, &student)) { ListPush(list, &student); size++; } }
return size; } //----------------------------------------------// void ListToArray(TStudent* array, TList** list) { size_t i = 0; while (*list) { array[i++] = ListPop(list); } } //----------------------------------------------// void PrintStudent(const TStudent* student) { fprintf(stdout, "%s\t%2d %s\n", student->name, student->number, student->faculty); } //----------------------------------------------// void PrintArray(TStudent* array, size_t size) { size_t i; for (i=0; i<size; ++i) { PrintStudent(&array[i]); } } //----------------------------------------------// void Swap(TStudent* a, TStudent* b) { TStudent buff = *a; *a = *b; *b = buff; } //----------------------------------------------// void Sort(TStudent* array, size_t size) { size_t i, j; size_t min; for (i=0; i<size-1; ++i) { min = i; for (j=i+1; j<size; ++j) { if (array[j].number < array[min].number) { min = j; } } if (min != i) { Swap(&array[i], &array[min]); } } } //----------------------------------------------// int Find(TStudent* array, size_t size, const char* who) { int index = -1; if (*who) { size_t i; for (i=0; i<size; ++i) { if (strstr(array[i].name, who)) { index = i; break; } } } return index; } //----------------------------------------------//
int main(int argc, char** argv) { if (argc != 2) { fprintf(stdout, "Usage: program <textfile>\n"); return 0; }
FILE* f = fopen(argv[1], "r"); if (!f) { fprintf(stderr, "Error: file '%s' not found ...\n", argv[1]); return 1; }
TList* list = 0; size_t size = ReadFile(f, &list); fclose(f);
if (size == 0) { fprintf(stdout, "Information: file is empty\n"); return 0; }
TStudent* array = (TStudent*) malloc(sizeof(TStudent) * size); ListToArray(array, &list); Sort(array, size); PrintArray(array, size);
char name[NAME_MAX_LEN]; fprintf(stdout, "search student: "); fgets(name, NAME_MAX_LEN, stdin); name[strlen(name)-1] = 0; int find = Find(array, size, name); if (find != -1) { PrintStudent(&array[find]); } else { fprintf(stdout, "Information: student '%s' not found\n", name); } free(array); return 0; }
|
|
а можно код на Borland C переделать пожалуста) |