0

пишу интерпретатор команд. Есть входной файл, в котором даны инструкции вида:

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, &quot;\n&quot;);
    parsed_string = parse_string(string, &amp;size);

    /*initialization of variable*/
    if(strcmp(parsed_string[0], &quot;print&quot;) &amp;&amp; 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(&quot;[INITIALIZATION]: %s == %d\n\n&quot;, variables[amount_of_variables - 1].name, variables[amount_of_variables - 1].data);
    }

    /*operations with variables*/
    else if(strcmp(parsed_string[0], &quot;print&quot;) &amp;&amp; size == 6)
    {
        //проверка на существование элементов операции и произведение операции
        printf(&quot;[OPERATION]: new operation\n\n&quot;);
    }

    /*print variables*/
    else if(!strcmp(parsed_string[0], &quot;print&quot;))
    {
        if(size == 2)
        {
            printf(&quot;[PRINT ALL]: %s == %d\n&quot;, variables[0].name, variables[0].data);
            for(i = 1; i &lt; amount_of_variables; i++)
            {
                printf(&quot;------------ %s = %d\n&quot;, variables[i].name, variables[i].data);
            }
            printf(&quot;\n&quot;);
        }
        else if(size == 4)
        {
            // if(dichotomy_search(variables, 0, amount_of_variables - 1, parsed_string[2]))
            // {
            //     printf(&quot;\n%s is exist!!!\n\n&quot;, parsed_string[2]);
            // }
            // else
            //     return (counter);
        }
        else
        {
            return (counter);
        }
    }
    counter++;
}
for(i = 0; i &lt; amount_of_variables; i++)
{
    printf(&quot;%s = %d\n&quot;, 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:02
  • А вы допишите в строке прямо перед qsort что-то типа puts("Call Qsort") и проверьте - может, она у вас и в самом деле не вызывается?... – Harry Nov 16 '20 at 11:08

0 Answers0