rendered paste body#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct stack
{
struct stackEl *base;
int cnt;
};
struct stackEl
{
struct stackEl *next;
char *word;
int len; //zodzio ilgis
};
int push(struct stack *a, struct stackEl *b);
struct stackEl *newStackEl(int len, char *word);
struct stackEl *pop(struct stack *stack);
int main (int argc, char *argv[])
{
struct stack *stackNel = malloc(sizeof(struct stack));
struct stack *stackLyg = malloc(sizeof(struct stack));
struct stack *stackBendr = malloc(sizeof(struct stack));
stackNel->base = NULL;
stackLyg->base = NULL;
stackBendr->base = NULL;
stackNel->cnt = 0;
stackLyg->cnt = 0;
stackBendr->cnt = 0;
FILE *fptr;
system("PAUSE");
if ((fptr = fopen("txt", "rb")) != NULL)
{
char c, tmpword[30];
int len = 0;
struct stackEl *tmp = NULL;
while (!feof(fptr))
{
c = fgetc(fptr);
while ((isalnum(c) != 0) && (!feof(fptr)))
{
len++;
tmpword[len-1] = c;
c = fgetc(fptr);
}
if (len != 0)
{
tmpword[len] = '\0';
tmp = newStackEl(len, tmpword);
printf("new el: [%s] len = %d\n", tmp->word, tmp->len);
if (len % 2 == 0)
{
push(stackLyg, tmp);
// push(stackBendr, tmp);
}
else
{
push(stackNel, tmp);
// push(stackBendr, tmp);
}
len = 0;
}
}
}
fclose(fptr);
struct stackEl *tmp;
if ((fptr = fopen("outLYG", "wb")) != NULL)
{
while(stackLyg->cnt > 0)
{
tmp = pop(stackLyg);
fputs(tmp->word, fptr);
fputc('\n', fptr);
}
}
if ((fptr = fopen("outNELYG", "wb")) != NULL)
{
while(stackNel->cnt > 0)
{
tmp = pop(stackNel);
fputs(tmp->word, fptr);
fputc('\n', fptr);
}
}
if ((fptr = fopen("outBendr", "wb")) != NULL)
{
while(stackBendr->cnt > 0)
{
tmp = pop(stackBendr);
fputs(tmp->word, fptr);
fputc('\n', fptr);
}
}
printf("done\n");
system("PAUSE");
return 0;
}
int push(struct stack *stack, struct stackEl *newEl)
{
if (stack == NULL || newEl == NULL)
return -1;
struct stackEl *curr = stack->base;
if (stack->base == NULL)
{
stack->base = newEl;
}
else
{
while(curr->next != NULL)
curr = curr->next;
curr->next = newEl;
}
stack->cnt++;
return 0;
}
struct stackEl *pop(struct stack *stack)
{
if (stack == NULL)
return NULL;
struct stackEl *prev, *curr = stack->base;
if (curr != NULL)
{
prev = curr;
while (curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
if (prev != curr)
prev->next = NULL;
else
stack->base = NULL;
stack->cnt--;
return curr;
}
else
return NULL;
}
struct stackEl *newStackEl(int len, char *tmpword)
{
if (len == 0 || tmpword == NULL)
return NULL;
struct stackEl *newEl = malloc(sizeof(struct stackEl));
newEl->next = NULL;
newEl->len = len;
newEl->word = malloc(sizeof(char)*(len+1));
strncpy(newEl->word, tmpword, len+1);
return newEl;
}