еще
parent
a77894cfba
commit
bb5efa3893
|
@ -5,19 +5,17 @@
|
|||
|
||||
int main()
|
||||
{
|
||||
char** words = NULL;
|
||||
getWordsAndCount("insert car_vendor=kia, car_model=light, car_year=2009, car_id=M785FF, divise=cam carrying=21, axles=10", words);
|
||||
/*time_t now = time(NULL);
|
||||
char *stroks[20] = { "Test 1", "Test 2", "Test 3", "Test 4"};
|
||||
putElementToDB("Ulanovsky", "George", 2, 4, &now, &now, stroks, 3);
|
||||
putElementToDB("Ulanovsky", "George", 2, 1, &now, &now, stroks, 3);
|
||||
putElementToDB("Ulanovsky", "George", 3, 1, &now, &now, stroks, 3);
|
||||
time_t now = time(NULL);
|
||||
int stroks[99];
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
stroks[i] = i % 3 == 1 ? 1 : 0;
|
||||
|
||||
/*putElementToDB("Ulanovsky", "George", 2, 4, &now, &now, stroks);
|
||||
putElementToDB("Ulanovsky", "George", 2, 1, &now, &now, stroks);
|
||||
putElementToDB("Ulanovsky", "George", 3, 1, &now, &now, stroks);
|
||||
|
||||
|
||||
putElementToDB("Ulanovsky", "George", 2, 4, &now, &now, stroks);
|
||||
putElementToDB("Ulanovsky", "George", 2, 1, &now, &now, stroks);
|
||||
putElementToDB("Ulanovsky", "George", 2, 1, &now, &now, stroks);
|
||||
|
@ -31,18 +29,34 @@ int main()
|
|||
|
||||
putElementToDB("Fidarov", "German", 2, 2, &now, &now, stroks);
|
||||
|
||||
printDataBase();
|
||||
printDataBase();*/
|
||||
|
||||
printf("uniq:%d \n", deleteNonUniqElements());
|
||||
//printf("uniq:%d \n", deleteNonUniqElements());
|
||||
|
||||
char selects[7] = { 0, 5, 3, -1, -1, -1, -1 };
|
||||
|
||||
Condition a = { moreEqual, 2, 0, NULL, 0, NULL };
|
||||
Condition* conditions[7] = {NULL, NULL, NULL, &a, NULL, NULL, NULL};
|
||||
printf("\nselect:%d \n", selectFunc(selects, conditions));
|
||||
Condition* conditions[7] = { NULL, NULL, NULL, &a, NULL, NULL, NULL };
|
||||
/*printf("\nselect:%d \n", selectFunc(selects, conditions));
|
||||
printf("\nupdate:%d \n", updateFunc("TeStNaMe", NULL, -1, -1, NULL, NULL, NULL, conditions));
|
||||
printf("\nselect:%d \n", selectFunc(selects, conditions));
|
||||
printf("\nselect:%d \n", selectFunc(selects, conditions));*/
|
||||
|
||||
printDataBase();
|
||||
|
||||
parceLine("insert last_nm=Ulanovsky, first_nm=George, curse_id=2, lab_id=2, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
parceLine("insert last_nm=Ulanovsky, first_nm=George, curse_id=2, lab_id=1, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
parceLine("insert last_nm=Ulanovsky, first_nm=George, curse_id=1, lab_id=3, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
parceLine("insert last_nm=Ulanovsky, first_nm=George, curse_id=2, lab_id=4, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
parceLine("insert last_nm=Ulanovsky, first_nm=George, curse_id=3, lab_id=6, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
|
||||
parceLine("insert last_nm=Baranova, first_nm=Varvara, curse_id=3, lab_id=6, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
printDataBase();
|
||||
parceLine("insert last_nm=Ulanovsky, first_nm=Grisha, curse_id=3, lab_id=6, start_tm=2000.10.10!22:1:20, end_tm=2000.10.10!23:0:20, result=[]");
|
||||
printDataBase();
|
||||
parceLine("uniq first_nm");
|
||||
printDataBase();
|
||||
parceLine("exit");
|
||||
|
||||
|
||||
printDataBase();*/
|
||||
saveStats();
|
||||
}
|
|
@ -331,7 +331,7 @@ void printDataBase()
|
|||
}
|
||||
}
|
||||
|
||||
int deleteNonUniqElements()
|
||||
int deleteNonUniqElements(char whatToDelete[7])
|
||||
{
|
||||
char* needToDel = (char*)malloc(sizeof(char) * dataBaseSize);
|
||||
mallocCount++;
|
||||
|
@ -345,13 +345,13 @@ int deleteNonUniqElements()
|
|||
tmp2 = tmp->nextElement;
|
||||
while (tmp2 != NULL)
|
||||
{
|
||||
if (!strcmp(tmp->last_nm, tmp2->last_nm))
|
||||
if (!strcmp(tmp->first_nm, tmp2->first_nm))
|
||||
if (tmp->curse_id == tmp2->curse_id)
|
||||
if (tmp->lab_id == tmp2->lab_id)
|
||||
if (tmp->start_tm == tmp2->start_tm)
|
||||
if (tmp->end_tm == tmp2->end_tm)
|
||||
if (isResultsSame(tmp, tmp2))
|
||||
if (!whatToDelete[0] || !strcmp(tmp->last_nm, tmp2->last_nm))
|
||||
if (!whatToDelete[1] || !strcmp(tmp->first_nm, tmp2->first_nm))
|
||||
if (!whatToDelete[2] || tmp->curse_id == tmp2->curse_id)
|
||||
if (!whatToDelete[3] || tmp->lab_id == tmp2->lab_id)
|
||||
if (!whatToDelete[4] || tmp->start_tm == tmp2->start_tm)
|
||||
if (!whatToDelete[5] || tmp->end_tm == tmp2->end_tm)
|
||||
if (!whatToDelete[6] || isResultsSame(tmp, tmp2))
|
||||
{
|
||||
needToDel[currInd] = 1;
|
||||
}
|
||||
|
@ -392,6 +392,7 @@ int deleteNonUniqElements()
|
|||
}
|
||||
|
||||
}
|
||||
printf("uniq:%d \n", add);
|
||||
return add;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
malloc:9
|
||||
malloc:76
|
||||
relloc:0
|
||||
calloc:0
|
||||
free:0
|
||||
free:75
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include "db_struct.h"
|
||||
|
||||
int f_proverka(const char* y)
|
||||
int workMode(char* y)
|
||||
{
|
||||
const char* com[6] = { "insert", "select", "delete", "update", "uniq", "exit" };
|
||||
for (int i = 0; i < 6; i++)
|
||||
|
@ -12,9 +12,106 @@ int f_proverka(const char* y)
|
|||
return -1;
|
||||
}
|
||||
|
||||
int getWordsAndCount(char* line, char** array)
|
||||
void parceCondition();
|
||||
|
||||
int isField(char* str)
|
||||
{
|
||||
char buf[500];
|
||||
// 0 -> 6 fields
|
||||
if (!strcmp("last_nm", str))
|
||||
return 0;
|
||||
if (!strcmp("first_nm", str))
|
||||
return 1;
|
||||
if (!strcmp("curse_id", str))
|
||||
return 2;
|
||||
if (!strcmp("lab_id", str))
|
||||
return 3;
|
||||
if (!strcmp("start_tm", str))
|
||||
return 4;
|
||||
if (!strcmp("end_tm", str))
|
||||
return 5;
|
||||
if (!strcmp("result", str))
|
||||
return 6;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Âîçâðàùàåò íîìåð ïîëÿ, åñëè îíî ñîäåðæèòñÿ â ëèêñåìå
|
||||
int isChangingField(char* line)
|
||||
{
|
||||
char* eq = strchr(line, '=');
|
||||
if (eq != NULL && *(eq + 1) != '=')
|
||||
{
|
||||
char buf[500], ** array;
|
||||
strcpy(buf, line);
|
||||
char* pch = strtok(buf, "=");
|
||||
int x = isField(pch);
|
||||
return x;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
int parceField(char* line, int* _field_num, char** _string, int* _int, time_t** _time, int* results)
|
||||
{
|
||||
int field_num = isChangingField(line);
|
||||
int i;
|
||||
if (field_num != -1)
|
||||
{
|
||||
char* st1 = strtok(line, "=");
|
||||
char* st2 = strtok(NULL, "=");
|
||||
switch (field_num)
|
||||
{
|
||||
case 0:
|
||||
case 1:
|
||||
(*_string) = st2;
|
||||
*_field_num = field_num;
|
||||
return 1;
|
||||
case 2:
|
||||
case 3:
|
||||
i = atoi(st2);
|
||||
*_int = i;
|
||||
*_field_num = field_num;
|
||||
return 1;
|
||||
case 4:
|
||||
case 5:
|
||||
*_field_num = field_num;
|
||||
time_t result = 0;
|
||||
int year = 0, month = 0, day = 0, hour = 0, min = 0;
|
||||
if (sscanf(st2, "%4d.%2d.%2d!%2d:%2d", &year, &month, &day, &hour, &min) == 5) {
|
||||
struct tm breakdown = { 0 };
|
||||
breakdown.tm_year = year - 1900; /* years since 1900 */
|
||||
breakdown.tm_mon = month - 1;
|
||||
breakdown.tm_mday = day;
|
||||
breakdown.tm_hour = hour;
|
||||
breakdown.tm_min = min;
|
||||
|
||||
if ((result = mktime(&breakdown)) == (time_t)-1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
puts(ctime(&result));
|
||||
|
||||
(*_time) = &result;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
case 6:
|
||||
*_field_num = field_num;
|
||||
results[0] = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
int getWordsAndCount(char* line, char*** arr)
|
||||
{
|
||||
char buf[500], ** array;
|
||||
strcpy(buf, line);
|
||||
char* pch = strtok(buf, " ,");
|
||||
int count = 0;
|
||||
|
@ -25,6 +122,8 @@ int getWordsAndCount(char* line, char** array)
|
|||
count++;
|
||||
}
|
||||
|
||||
if (count == 0)return 0;
|
||||
|
||||
array = (char**)malloc(sizeof(char*) * count);
|
||||
mallocCount++;
|
||||
|
||||
|
@ -34,32 +133,150 @@ int getWordsAndCount(char* line, char** array)
|
|||
|
||||
while (pch != NULL) // ïîêà åñòü ëåêñåìû
|
||||
{
|
||||
array[count] = (char*) malloc(sizeof(char)*50);
|
||||
array[count] = (char*)malloc(sizeof(char) * 50);
|
||||
mallocCount++;
|
||||
strcpy(array[count], pch);
|
||||
pch = strtok(NULL, " ,");
|
||||
count++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
/*for (int i = 0; i < count; i++)
|
||||
{
|
||||
printf("Word is: %s\n", array[i]);
|
||||
}
|
||||
}*/
|
||||
*arr = array;
|
||||
return count;
|
||||
}
|
||||
|
||||
void error(char* input)
|
||||
{
|
||||
char* buf[21];
|
||||
strncpy(buf, input, 20);
|
||||
|
||||
printf("\nnot correct");
|
||||
printf("%s\n", buf);
|
||||
}
|
||||
|
||||
void parceLine(char* input)
|
||||
{
|
||||
int x = f_proverka(input);
|
||||
switch (x)
|
||||
{
|
||||
char** wrd = NULL;
|
||||
|
||||
case 6:
|
||||
while (head != NULL)
|
||||
int lixCount = getWordsAndCount(input, &wrd);
|
||||
char whatToSearch[7] = { 0,0,0,0,0,0,0 };
|
||||
char family[50], name[50], * str = NULL;
|
||||
int ints[2] = { 0,0 };
|
||||
int f_num = -1;
|
||||
int _int = -1;
|
||||
time_t* tim = NULL, * start = NULL, * end=NULL;
|
||||
int stroks[99];
|
||||
|
||||
if (lixCount != 0) {
|
||||
int x = workMode(wrd[0]);
|
||||
switch (x)
|
||||
{
|
||||
deleteElementFromDB(head);
|
||||
case 1: //insert
|
||||
if (lixCount != 8) {
|
||||
error(input); exit(100);
|
||||
}
|
||||
|
||||
for (int i = 1; i < lixCount; i++)
|
||||
{
|
||||
|
||||
x = parceField(wrd[i], &f_num, &str, &_int, &tim, stroks);
|
||||
if (x == -1)
|
||||
{
|
||||
error(input); exit(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
whatToSearch[f_num]++;
|
||||
switch (f_num)
|
||||
{
|
||||
case 0:
|
||||
strcpy(family, str);
|
||||
break;
|
||||
case 1:
|
||||
strcpy(name, str);
|
||||
break;
|
||||
case 2:
|
||||
ints[0] = _int;
|
||||
break;
|
||||
case 3:
|
||||
ints[1] = _int;
|
||||
break;
|
||||
case 4:
|
||||
start = tim;
|
||||
break;
|
||||
case 5:
|
||||
end = tim;
|
||||
break;
|
||||
case 6:
|
||||
break;
|
||||
default:
|
||||
error(input); exit(100);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (whatToSearch[i] >= 2)
|
||||
{
|
||||
error(input); exit(100);
|
||||
}
|
||||
}
|
||||
putElementToDB(family, name, ints[0], ints[1], start, end, stroks);
|
||||
break;
|
||||
case 5: //uniq
|
||||
if (lixCount > 7) {
|
||||
error(input); exit(100);
|
||||
}
|
||||
for (int i = 1; i < lixCount; i++)
|
||||
{
|
||||
x = isField(wrd[i]);
|
||||
if (x == -1)
|
||||
{
|
||||
error(input); exit(100);
|
||||
}
|
||||
else
|
||||
{
|
||||
whatToSearch[x]++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (whatToSearch[i] >= 2)
|
||||
{
|
||||
error(input); exit(100);
|
||||
}
|
||||
}
|
||||
deleteNonUniqElements(whatToSearch);
|
||||
break;
|
||||
|
||||
case 6: // exit
|
||||
while (head != NULL)
|
||||
{
|
||||
deleteElementFromDB(head);
|
||||
}
|
||||
for (int i = 0; i < lixCount; i++)
|
||||
{
|
||||
free(wrd[i]);
|
||||
freeCount++;
|
||||
}
|
||||
free(wrd);
|
||||
freeCount++;
|
||||
saveStats();
|
||||
exit(0);
|
||||
default:
|
||||
error(input);
|
||||
exit(100);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
exit(100);
|
||||
}
|
||||
for (int i = 0; i < lixCount; i++)
|
||||
{
|
||||
free(wrd[i]);
|
||||
freeCount++;
|
||||
}
|
||||
free(wrd);
|
||||
freeCount++;
|
||||
}
|
Loading…
Reference in New Issue