Skip to content

Instantly share code, notes, and snippets.

@fuCtor
Created December 19, 2015 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuCtor/694f3d855563f416482e to your computer and use it in GitHub Desktop.
Save fuCtor/694f3d855563f416482e to your computer and use it in GitHub Desktop.
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "locale.h"
#include <string.h>
typedef struct Date {
int d;
int m;
int y;
} Date;
typedef struct Auto {
char * model;
char * number;
char * owner;
int stealing;
Date Inspection;
} Auto;
typedef void(*MyFunc)(Auto *Car[], int n);
void Input(Auto **pCar,int n)
{
Auto *Car = (Auto*)malloc(sizeof(Auto) * n);
(*pCar) = Car;
int i;
for(i=0;i<n;i++)
{
printf("Модель: "); scanf("%s", &Car[i].model, 50);
printf("Номер: "); scanf("%s", &Car[i].number, 50);
printf("Владелец: "); scanf("%s", &Car[i].owner, 50);
printf("Числится в угоне: "); scanf("%d", &Car[i].stealing);
printf("Дата техосмотра:\n");
printf("Число - "); scanf("%d", &Car[i].Inspection.d);
printf("Месяц - "); scanf("%d", &Car[i].Inspection.m);
printf("Год - "); scanf("%d", &Car[i].Inspection.y);
printf("\n");
}
}
void Output(Auto *Car, int i)
{
printf("Модель: %s\n", Car[i].model);
printf("Номер: %s\n", Car[i].number);
printf("Владелец: %s\n", Car[i].owner);
printf("Числится в угоне: %d\n", Car[i].stealing);
printf("Дата техосмотра: %d.%d.%d\n\n", Car[i].Inspection.d, Car[i].Inspection.m, Car[i].Inspection.y);
}
void SortingInspection(Auto *Car, int n)
{
int i, j;
Auto Buffer;
for (j = 0; j < (n - 1); j++)
{
for (i = 0; i < (n - 1); i++)
{
if (Car[i].Inspection.y > Car[i + 1].Inspection.y)
{
Buffer = Car[i];
Car[i] = Car[i + 1];
Car[i + 1] = Car[i];
}
}
}
for (j = 0; j < (n - 1); j++)
{
for (i = 0; i < (n - 1); i++)
{
if (Car[i].Inspection.y == Car[i + 1].Inspection.y && Car[i].Inspection.m > Car[i + 1].Inspection.m)
{
Buffer = Car[i];
Car[i] = Car[i + 1];
Car[i + 1] = Car[i];
}
}
}
for (j = 0; j < (n - 1); j++)
{
for (i = 0; i < (n - 1); i++)
{
if (Car[i].Inspection.y == Car[i + 1].Inspection.y && Car[i].Inspection.m == Car[i + 1].Inspection.m && Car[i].Inspection.d > Car[i + 1].Inspection.d)
{
Buffer = Car[i];
Car[i] = Car[i + 1];
Car[i + 1] = Car[i];
}
}
}
}
void SearchNumber(Auto *Car, int n)
{
int i;
char * a;
printf("Введите номер машины: ");
scanf("%s", &a, 50);
for (i = 0; i < n; i++)
{
if (strcmp(a, Car[i].number) == 0)
{
Output(Car, i);
break;
break;
}
}
}
void SearchStealing(Auto *Car, int n)
{
int a, i;
printf("Машина числиться в угоне: ");
scanf("%d", &a);
for (i = 0; i < n; i++)
{
if (a == Car[i].stealing)
{
Output(Car, i);
break;
break;
}
}
}
void SaveToFile(Auto *Car, int n)
{
FILE *file;
file = fopen("/Users/albertgumerov/Desktop/ГИБДД/ГИБДД/Cars.txt", "wb");
if (file)
{
fwrite(&n, sizeof(n), 1, file);
fwrite(Car, sizeof(Auto), n, file);
fclose(file);
}
}
int ReadFromFile(Auto **pCar)
{
FILE *file;
file = fopen("/Users/albertgumerov/Desktop/ГИБДД/ГИБДД/Cars.txt", "rb");
int n = 0;
if (file)
{
Auto *Car = 0;
fread(&n, sizeof(n), 1, file);
if (*pCar)
{
Car = (Auto *)realloc(*pCar, sizeof(Auto) * n);
}
else
{
Car = (Auto *)malloc(sizeof(Auto) * n);
}
(*pCar) = Car;
fread(Car, sizeof(Auto), n, file);
fclose(file);
}
return n;
}
int main(int argc, const char * argv[])
{
setlocale(LC_ALL, "Russian");
int n = 0, run = 1, i, x, y;
Auto *Car = 0;
while (run)
{
printf("1 - Ввод\n2 - Вывод\n3 - Сортировка по дате техосмотра\n4 - Поиск по номеру\n5 - Поиск по угону\n"
"6 - Сохранить в файл\n7 - Считать из файла\n8 - Выход\n");
fflush(stdin);
scanf("%d", &x);
switch (x)
{
case 1:
printf("Введите количество машин: ");
scanf("%d", &n);
Input(&Car, n);
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 2:
for (i = 0; i < n; i++)
{
Output(Car, i);
}
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 3:
SortingInspection(Car, n);
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 4:
SearchNumber(Car, n);
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 5:
SearchStealing(Car, n);
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 6:
SaveToFile(Car, n);
printf("Готово.\n");
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 7:
n = ReadFromFile(&Car);
printf(" Количество считанных элементов = %d\n", n);
printf("\n1 - В меню\n");
scanf("%d", &y);
if (y == 1)
{
break;
}
case 8:
run = 0;
break;
}
}
free(Car);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment