пишу интерпретатор команд. Есть входной файл, в котором даны инструкции вида:
myvar=15;
bg=25;
ccc=bg+myvar;
print ccc;
bg=ccc*myvar;
var=12;
b=c+d;
az=128;
print;
Столкнулся с такой проблемой: не получается отсортировать структуру по полю имени. Я искал на форумах способы реализаций, и, казалось бы, все должно работать, но в итоге сортировка не работает совсем, как будто бы и не запускается. Сама структура выглядит именно так:
typedef struct memoryCell
{
char *name;
int data;
}memoryCell;
Сам фрагмент кода, в котором применяется сортировка, выглядит следующим образом:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memorycell.h"
char *parse_string(char string[], int size); // просто возвращает массив строк вида "myvar" "=" "15" ";"
int struct_cmp_by_name(const void a, const void b);
int dichotomy_search(memoryCell variables, int left, int right, char string);
/task - variables processing
returns 1 if correct/
int variable_processing(FILE file, memoryCell variables)
{
int i, j;
char **parsed_string = NULL;
char string[BUFSIZ];
int size;
int amount_of_variables;
int counter;
size = 0;
counter = 1;
amount_of_variables = 0;
variables = NULL;
while(!feof(file))
{
fgets(string, sizeof(string), file);
strtok(string, "\n");
parsed_string = parse_string(string, &size);
/*initialization of variable*/
if(strcmp(parsed_string[0], "print") && size == 4)
{
variables = (memoryCell *)realloc(variables, ++amount_of_variables * sizeof(memoryCell));
variables[amount_of_variables - 1].name = (char *)malloc(strlen(parsed_string[0]) * sizeof(char));
qsort(variables, amount_of_variables - 1, sizeof(memoryCell), struct_cmp_by_name);
strcpy(variables[amount_of_variables - 1].name, parsed_string[0]);
variables[amount_of_variables - 1].data = atoi(parsed_string[2]);
printf("[INITIALIZATION]: %s == %d\n\n", variables[amount_of_variables - 1].name, variables[amount_of_variables - 1].data);
}
/*operations with variables*/
else if(strcmp(parsed_string[0], "print") && size == 6)
{
//проверка на существование элементов операции и произведение операции
printf("[OPERATION]: new operation\n\n");
}
/*print variables*/
else if(!strcmp(parsed_string[0], "print"))
{
if(size == 2)
{
printf("[PRINT ALL]: %s == %d\n", variables[0].name, variables[0].data);
for(i = 1; i < amount_of_variables; i++)
{
printf("------------ %s = %d\n", variables[i].name, variables[i].data);
}
printf("\n");
}
else if(size == 4)
{
// if(dichotomy_search(variables, 0, amount_of_variables - 1, parsed_string[2]))
// {
// printf("\n%s is exist!!!\n\n", parsed_string[2]);
// }
// else
// return (counter);
}
else
{
return (counter);
}
}
counter++;
}
for(i = 0; i < amount_of_variables; i++)
{
printf("%s = %d\n", variables[i].name, variables[i].data);
}
return (0);
}
int struct_cmp_by_name(const void x, const void y)
{
memoryCell a = (memoryCell )x;
memoryCell b = (memoryCell )y;
return strcmp(a->name, b->name);
}
/return 1 - correct(found)
return 0 - incorrect(didn`t find)/
int dichotomy_search(memoryCell variables, int left, int right, char string)
{
//todo search element in struct
}
while(!feof(file))- доколе?!! – Harry Nov 16 '20 at 11:02qsortчто-то типаputs("Call Qsort")и проверьте - может, она у вас и в самом деле не вызывается?... – Harry Nov 16 '20 at 11:08