Fondamenti di Programmazione a.a. 2011-2012
Soluzioni prova scritta - 19 settembre 2012
#include <stdlib.h>
SL rot(SL L, int k) {
SE *p = L;
while (k > 1 && p != NULL) {
p = p->next;
k--;
}
if (p == NULL) return NULL;
if (p->next == NULL) return L;
SE *first = p->next;
p->next = NULL;
p = first;
while (p->next != NULL) p = p->next;
p->next = L;
return first;
}
#include <stdio.h>
int maxpref(char *pre, char *txt) {
FILE *fpre = fopen(pre, "r");
FILE *ftxt = fopen(txt, "r");
int max = 0;
int pos = ftell(ftxt);
while (fseek(ftxt, pos, SEEK_SET) == 0) {
rewind(fpre);
int count = 0, c1, c2;
while ((c1 = fgetc(fpre)) != EOF && (c2 = fgetc(ftxt)) != EOF && c1 == c2)
count++;
if (count > max) max = count;
fseek(ftxt, pos, SEEK_SET);
if (fgetc(ftxt) == EOF) break;
pos = ftell(ftxt);
}
fclose(fpre);
fclose(ftxt);
return max;
}
#include <stdlib.h>
#include <string.h>
char **tasks(TList *pL, int hh, int mm, int ss, int *dT) {
char **deltasks = NULL;
int n = 0;
TList *p = pL;
while (*p != NULL) {
Task *t = *p;
int h = t->hh, m = t->mm, s = t->ss;
if (h < hh || (h == hh && m < mm) || (h == hh && m == mm && s < ss)) {
deltasks = realloc(deltasks, (n + 1)*sizeof(char *));
deltasks[n] = malloc(strlen(t->name) + 1);
strcpy(deltasks[n], t->name);
n++;
*p = t->next;
free(t);
} else
p = &(t->next);
}
*dT = n;
return deltasks;
}