#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 200
#define BUFF 10000
#define MODBUFFER 10
/*Количество имен 200 x (10 + 10 + 20 ) = 8000*/
/*Записная книга*/
int modecorrect(char *str); /*Прототипы*/ /*Проверка упр. строки*/
int getdata(unsigned char *field, FILE *file); /*Получение данных формы*/
struct person{ /*Структура записи*/
unsigned char name[10];
unsigned char surname[10];
unsigned char number[20];
};
int main(void)
{
FILE *MyBook;
unsigned int i, count; //for initializing structure
struct person NewData;
struct person Contacts[SIZE]; /*Массив структур*/
unsigned char *out = malloc(BUFF * sizeof(unsigned char));
/*указатель в файле при выводе и сортировке*/
unsigned char *pname = NewData.name;
unsigned char *psurname = NewData.surname;
unsigned char *pnumber = NewData.number;
unsigned char *mode = malloc(MODBUFFER * sizeof(unsigned char)); /* Read or Add */
unsigned char *answer = malloc( 4 * sizeof(unsigned char)); /* YES / NO */
printf("MyBook 1.0\nEnglish language is used(!) \nChoose your mode:");
while(1){ //// !!!!!Начало общего зацикливания
printf("\nInput \"Read\" or \"Add\"\n");
gets(mode);
if(!strcmp(mode,"Add")) /*Режим добавления*/
{
if((MyBook = fopen("MyBook.txt", "a+")) == NULL)//
printf("Writing MyBook.txt error");
printf("Please, input new data\nto stop press <Enter>\n");
while(1){//////////////////
printf("Name: ");
getdata(pname, MyBook);
fprintf(MyBook, " ");
if(!strcmp(pname, ""))
break;
printf("Surname: ");
getdata(psurname, MyBook);
fprintf(MyBook, " ");
printf("Number: ");
getdata(pnumber, MyBook);
fprintf(MyBook, "\n");
}//////////////////////////
fclose(MyBook);
}
/* Режим чтения и сортировки */
if(!strcmp(mode,"Read"))
{
if((MyBook = fopen("MyBook.txt", "r")) == NULL)
printf("Reading MyBook.txt error");
do{
fscanf(MyBook, "%c" , out);
putchar(*out);
}while(!feof(MyBook));
do{
printf("\n Alphabet sort? Input \"Yes\" or \"No\" ");
gets(answer);
}while(strcmp(answer, "Yes") && strcmp(answer, "No"));
if(!strcmp(answer, "Yes"))
{
count = fillStruct(MyBook, Contacts[SIZE], SIZE ); //заполняем структуру из файла
printf("%u", count);
for (i = 0; i < count; ++i)
printf("%s %s %s \n", Contacts[i].name, Contacts[i].surname, Contacts[i].number);
}
fclose(MyBook);
}
////////////// QSORT //////////////////
} ////!!!!
return 0;
}
/* small functions */
int modecorrect(char *str)
{
if(!strcmp(str, "Add") || !strcmp(str, "Read"))
return 1;
return 0;
}
int getdata(unsigned char *field, FILE *file)
{
gets(field);
fputs(field, file);
return;
}
////////////////////// ФУНКЦИИ ДЛЯ СОРТИРОВКИ ПО АЛФАВИТУ //////////////////////////
int qsort(struct person *left, int allnumber, int (*compare)(struct person *A, struct person *B ))
{
struct person *last, *right = left + allnumber - 1, *i;
if (left >= right)
return 1;
if (!swap(left, &left[(right-left)/2]))
return 0;
last = left;
for (i = left + 1; i <= right; ++i)
{
if (compare(i, left) < 0)
if (!swap(++last, i))
return 0;
}
if (!swap(left, last))
return 0;
qsort(left, last-left, compare);
qsort(last+1, right-last, compare);
return 1;
}
//////////////////////// Обмен указателей на эл.массива структур //////////
int swap(struct person *A, struct person *B )
{
struct person *temp;
if ((temp = (struct person *)malloc(sizeof(struct person)))==NULL)
return 0;
*temp = *A;
*A = *B;
*B = *temp;
return 1;
}
void compare(struct person *A, struct person *B ) //alphabet
{
return strcmp(A->name, B->name);
}
/////////////////////// Заполнение массива структур //////////////////////
unsigned int fillStruct(FILE *file, struct person bigArray[], unsigned int arraySize)
{
unsigned int index = 0;
while((index < arraySize) && !feof(file))
if (fscanf(file, " %9s %9s %19s \n", bigArray[index].name, bigArray[index].surname, bigArray[index].number) == 3)
++index;
printf("%u", index);
return index;
}