feat: Initial commit
This commit is contained in:
53
Laboratorio 8/Esercizio 3/Asset.c
Normal file
53
Laboratorio 8/Esercizio 3/Asset.c
Normal file
@@ -0,0 +1,53 @@
|
||||
// Laboratorio 8 - Esercizio 3 - Asset.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "Asset.h"
|
||||
#include "ExrateBST.h"
|
||||
|
||||
struct asset
|
||||
{
|
||||
char titolo[STR_LEN];
|
||||
ExrateBST exrates;
|
||||
};
|
||||
|
||||
Asset AssetCreate() {
|
||||
Asset a = malloc(sizeof(*a));
|
||||
a->exrates = ExrateBSTCreate();
|
||||
return a;
|
||||
}
|
||||
|
||||
void AssetFree(Asset a) {
|
||||
ExrateBSTFree(a->exrates);
|
||||
free(a);
|
||||
}
|
||||
|
||||
int AssetCompare(Asset a, Asset b) {
|
||||
return strcmp(a->titolo, b->titolo);
|
||||
}
|
||||
|
||||
char * AssetTitle(Asset a) {
|
||||
return a->titolo;
|
||||
}
|
||||
|
||||
ExrateBST AssetGetExrates(Asset a) {
|
||||
return a->exrates;
|
||||
}
|
||||
|
||||
Asset AssetRead(FILE *fp) {
|
||||
Asset n = AssetCreate();
|
||||
Exrate je;
|
||||
int Nitems;
|
||||
|
||||
fscanf(fp, " %s %d",n->titolo, &Nitems);
|
||||
for (int i = 0; i < Nitems; i++) {
|
||||
ExrateRead(&je, fp);
|
||||
ExrateBSTInsert(n->exrates, je);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void AssetPrint(Asset a) {
|
||||
printf(" - %s\n", a->titolo);
|
||||
}
|
||||
16
Laboratorio 8/Esercizio 3/Asset.h
Normal file
16
Laboratorio 8/Esercizio 3/Asset.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// Laboratorio 8 - Esercizio 3 - Asset.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ASSET_DEFINED
|
||||
#define ASSET_DEFINED
|
||||
#define STR_LEN 20
|
||||
#include "ExrateBST.h"
|
||||
|
||||
typedef struct asset *Asset;
|
||||
void AssetFree(Asset a);
|
||||
int AssetCompare(Asset a, Asset b);
|
||||
ExrateBST AssetGetExrates(Asset a);
|
||||
Asset AssetRead(FILE *fp);
|
||||
void AssetPrint(Asset a);
|
||||
char * AssetTitle(Asset a);
|
||||
#endif
|
||||
113
Laboratorio 8/Esercizio 3/AssetList.c
Normal file
113
Laboratorio 8/Esercizio 3/AssetList.c
Normal file
@@ -0,0 +1,113 @@
|
||||
// Laboratorio 8 - Esercizio 3 - AssetList.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "Asset.h"
|
||||
#include "AssetList.h"
|
||||
#include "ExrateBST.h"
|
||||
|
||||
typedef struct node *link;
|
||||
struct node
|
||||
{
|
||||
Asset asset;
|
||||
link next;
|
||||
};
|
||||
|
||||
struct assetList
|
||||
{
|
||||
link head;
|
||||
int N;
|
||||
};
|
||||
|
||||
static link createNode(Asset asset, link next)
|
||||
{
|
||||
link new = malloc(sizeof(*new));
|
||||
new->asset = asset;
|
||||
new->next = next;
|
||||
return new;
|
||||
}
|
||||
|
||||
AssetList AssetListCreate()
|
||||
{
|
||||
AssetList al = malloc(sizeof(*al));
|
||||
|
||||
al->head = NULL;
|
||||
al->N = 0;
|
||||
|
||||
return al;
|
||||
}
|
||||
|
||||
void AssetListFree(AssetList al)
|
||||
{
|
||||
for (link x = al->head, p = x->next; p != NULL; x = p, p = x->next)
|
||||
{
|
||||
free(x);
|
||||
}
|
||||
free(al);
|
||||
}
|
||||
|
||||
int AssetListLength(AssetList al) {
|
||||
return al->N;
|
||||
}
|
||||
|
||||
void AssetListPrint(AssetList al) {
|
||||
link h = al->head;
|
||||
while (h != NULL) {
|
||||
AssetPrint(h->asset);
|
||||
h = h->next;
|
||||
}
|
||||
}
|
||||
|
||||
Asset * AssetListGet(AssetList al, int index) {
|
||||
link h;
|
||||
int i = 0;
|
||||
|
||||
h = al->head;
|
||||
while (h != NULL) {
|
||||
if (i == index)
|
||||
return &(h->asset);
|
||||
h = h->next;
|
||||
i++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Asset * AssetListSearch(AssetList al, char * query) {
|
||||
link h;
|
||||
|
||||
h = al->head;
|
||||
while (h != NULL) {
|
||||
if (strcmp(AssetTitle(h->asset), query) == 0)
|
||||
return &(h->asset);
|
||||
h = h->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void AssetListInsert(AssetList al, Asset asset)
|
||||
{
|
||||
link x, p;
|
||||
if (al->head == NULL)
|
||||
{
|
||||
al->head = createNode(asset, al->head);
|
||||
al->N++;
|
||||
return;
|
||||
}
|
||||
|
||||
for (x = al->head, p = x->next; p != NULL; x = p, p = x->next)
|
||||
{
|
||||
if (AssetCompare(asset, x->asset) == 0)
|
||||
{
|
||||
ExrateBSTMerge(AssetGetExrates(x->asset), AssetGetExrates(asset));
|
||||
return;
|
||||
}
|
||||
else if (AssetCompare(asset, x->asset) < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
x->next = createNode(asset, p);
|
||||
al->N++;
|
||||
}
|
||||
18
Laboratorio 8/Esercizio 3/AssetList.h
Normal file
18
Laboratorio 8/Esercizio 3/AssetList.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// Laboratorio 8 - Esercizio 3 - AssetList.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ASSETLIST_DEFINED
|
||||
#define ASSETLIST_DEFINED
|
||||
#include "Asset.h"
|
||||
|
||||
typedef struct assetList *AssetList;
|
||||
|
||||
AssetList AssetListCreate();
|
||||
void AssetListFree(AssetList al);
|
||||
void AssetListInsert(AssetList al, Asset asset);
|
||||
Asset * AssetListGet(AssetList al, int index);
|
||||
Asset * AssetListSearch(AssetList al, char * query);
|
||||
void AssetListPrint(AssetList al);
|
||||
int AssetListLength(AssetList al);
|
||||
|
||||
#endif
|
||||
36
Laboratorio 8/Esercizio 3/Datetime.c
Normal file
36
Laboratorio 8/Esercizio 3/Datetime.c
Normal file
@@ -0,0 +1,36 @@
|
||||
// Laboratorio 8 - Esercizio 3 - Datetime.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "Datetime.h"
|
||||
|
||||
int DateTimeCompare(DateTime a, DateTime b) {
|
||||
if (a.year > b.year)
|
||||
return 1;
|
||||
else if (a.year < b.year)
|
||||
return -1;
|
||||
|
||||
if (a.month > b.month)
|
||||
return 1;
|
||||
else if (a.month < b.month)
|
||||
return -1;
|
||||
|
||||
if (a.day > b.day)
|
||||
return 1;
|
||||
else if (a.day < b.day)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DateTimeRead(DateTime * dt, FILE * fp) {
|
||||
return fscanf(fp, " %d/%d/%d %d:%d", &dt->year, &dt->month,&dt->day,&dt->hours,&dt->minutes) == 5;
|
||||
}
|
||||
|
||||
int DateRead(DateTime * dt, FILE * fp) {
|
||||
dt->hours = 0;
|
||||
dt->minutes = 0;
|
||||
return fscanf(fp, " %d/%d/%d", &dt->year, &dt->month,&dt->day) == 3;
|
||||
}
|
||||
19
Laboratorio 8/Esercizio 3/Datetime.h
Normal file
19
Laboratorio 8/Esercizio 3/Datetime.h
Normal file
@@ -0,0 +1,19 @@
|
||||
// Laboratorio 8 - Esercizio 3 - Datetime.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef DATETIME_DEFINED
|
||||
#define DATETIME_DEFINED
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct datetime {
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
int hours;
|
||||
int minutes;
|
||||
} DateTime;
|
||||
|
||||
int DateTimeCompare(DateTime a, DateTime b);
|
||||
int DateTimeRead(DateTime * dt, FILE * fp);
|
||||
int DateRead(DateTime * dt, FILE * fp);
|
||||
#endif
|
||||
36
Laboratorio 8/Esercizio 3/Exrate.c
Normal file
36
Laboratorio 8/Esercizio 3/Exrate.c
Normal file
@@ -0,0 +1,36 @@
|
||||
// Laboratorio 8 - Esercizio 3 - Exrate.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include "Exrate.h"
|
||||
#include "Datetime.h"
|
||||
|
||||
int ExrateRead(Exrate *e, FILE *fp)
|
||||
{
|
||||
if (!DateTimeRead(&e->datetime, fp))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return fscanf(fp, " %d %d", &e->q, &e->n) == 1;
|
||||
}
|
||||
|
||||
void ExratePrint(Exrate e)
|
||||
{
|
||||
if (e.n == -1)
|
||||
{
|
||||
puts("Quotazione non valida");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Quotazione: %d, numero di transazioni: %d\n", e.q, e.n);
|
||||
}
|
||||
}
|
||||
|
||||
Exrate ExrateEmpty()
|
||||
{
|
||||
Exrate e;
|
||||
e.n = -1;
|
||||
e.q = -1;
|
||||
return e;
|
||||
}
|
||||
16
Laboratorio 8/Esercizio 3/Exrate.h
Normal file
16
Laboratorio 8/Esercizio 3/Exrate.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// Laboratorio 8 - Esercizio 3 - Exrate.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef EXRATE_DEFINED
|
||||
#define EXRATE_DEFINED
|
||||
#include "Datetime.h"
|
||||
|
||||
typedef struct exrate {
|
||||
DateTime datetime;
|
||||
int q, n;
|
||||
} Exrate;
|
||||
|
||||
int ExrateRead(Exrate *e, FILE *fp);
|
||||
void ExratePrint(Exrate e);
|
||||
Exrate ExrateEmpty();
|
||||
#endif
|
||||
266
Laboratorio 8/Esercizio 3/ExrateBST.c
Normal file
266
Laboratorio 8/Esercizio 3/ExrateBST.c
Normal file
@@ -0,0 +1,266 @@
|
||||
// Laboratorio 8 - Esercizio 3 - ExrateBST.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "ExrateBST.h"
|
||||
|
||||
typedef struct node *link;
|
||||
struct node
|
||||
{
|
||||
Exrate exrate;
|
||||
int N;
|
||||
link left, right;
|
||||
};
|
||||
|
||||
struct exrateBST
|
||||
{
|
||||
link root;
|
||||
link z;
|
||||
int N;
|
||||
};
|
||||
|
||||
static link createNode(Exrate exrate, link l, link r, int N)
|
||||
{
|
||||
link new = malloc(sizeof(*new));
|
||||
new->exrate = exrate;
|
||||
new->left = l;
|
||||
new->right = r;
|
||||
new->N = N;
|
||||
return new;
|
||||
}
|
||||
|
||||
static link rotR(link n)
|
||||
{
|
||||
link t = n->left;
|
||||
n->left = t->right;
|
||||
t->right = n;
|
||||
t->N = n->N;
|
||||
n->N = 1;
|
||||
n->N += (n->left) ? n->left->N : 0;
|
||||
n->N += (n->right) ? n->right->N : 0;
|
||||
return t;
|
||||
}
|
||||
|
||||
static link rotL(link h)
|
||||
{
|
||||
link t = h->right;
|
||||
h->right = t->left;
|
||||
t->left = h;
|
||||
t->N = h->N;
|
||||
h->N = 1;
|
||||
h->N += (h->left) ? h->left->N : 0;
|
||||
h->N += (h->right) ? h->right->N : 0;
|
||||
return t;
|
||||
}
|
||||
|
||||
ExrateBST ExrateBSTCreate()
|
||||
{
|
||||
ExrateBST new = malloc(sizeof(*new));
|
||||
new->z = createNode(ExrateEmpty(), NULL, NULL, 0);
|
||||
new->root = new->z;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
static void freeBST(link n, link z)
|
||||
{
|
||||
if (n == z)
|
||||
return;
|
||||
|
||||
free(n->left);
|
||||
free(n->right);
|
||||
free(n);
|
||||
}
|
||||
|
||||
void ExrateBSTFree(ExrateBST ebst)
|
||||
{
|
||||
freeBST(ebst->root, ebst->z);
|
||||
free(ebst->z);
|
||||
free(ebst);
|
||||
}
|
||||
|
||||
static link search(link n, DateTime dt, link z)
|
||||
{
|
||||
if (n == z)
|
||||
return z;
|
||||
|
||||
int c = DateTimeCompare(dt, n->exrate.datetime);
|
||||
if (c == 0)
|
||||
return n;
|
||||
if (c > 0)
|
||||
return search(n->right, dt, z);
|
||||
else
|
||||
return search(n->left, dt, z);
|
||||
}
|
||||
|
||||
Exrate ExrateBSTSearch(ExrateBST ebst, DateTime dt)
|
||||
{
|
||||
// nota: se search restituisce z, allora z,exrate è l'exrate nullo
|
||||
return search(ebst->root, dt, ebst->z)->exrate;
|
||||
}
|
||||
|
||||
static void ExrateBSTInsertLeaf(ExrateBST ebst, Exrate exrate)
|
||||
{
|
||||
if (ebst->root == ebst->z)
|
||||
{
|
||||
ebst->root = createNode(exrate, ebst->z, ebst->z, 1);
|
||||
ebst->N++;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
link *n = &ebst->root;
|
||||
while (*n != ebst->z)
|
||||
{
|
||||
(*n)->N++;
|
||||
if (DateTimeCompare(exrate.datetime, (*n)->exrate.datetime) > 0)
|
||||
{
|
||||
|
||||
n = &(*n)->right;
|
||||
}
|
||||
else
|
||||
{
|
||||
n = &(*n)->left;
|
||||
}
|
||||
}
|
||||
*n = createNode(exrate, ebst->z, ebst->z, 1);
|
||||
ebst->N++;
|
||||
}
|
||||
|
||||
void ExrateBSTInsert(ExrateBST ebst, Exrate exrate)
|
||||
{
|
||||
link node = search(ebst->root, exrate.datetime, ebst->z);
|
||||
|
||||
if (node != ebst->z)
|
||||
{
|
||||
// Merge exrate
|
||||
node->exrate.q = ((node->exrate.n * node->exrate.q) + (exrate.n * exrate.q)) / (node->exrate.n + exrate.n);
|
||||
node->exrate.n += exrate.n;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExrateBSTInsertLeaf(ebst, exrate);
|
||||
}
|
||||
}
|
||||
|
||||
static void mergeR(ExrateBST dst, link r, link z)
|
||||
{
|
||||
if (r == z)
|
||||
return;
|
||||
|
||||
mergeR(dst, r->left, z);
|
||||
mergeR(dst, r->right, z);
|
||||
ExrateBSTInsert(dst, r->exrate);
|
||||
}
|
||||
|
||||
void ExrateBSTMerge(ExrateBST dst, ExrateBST src)
|
||||
{
|
||||
mergeR(dst, src->root, src->z);
|
||||
}
|
||||
|
||||
static void getMaxMinLengthR(link n, link z, int *min, int *max, int *counter)
|
||||
{
|
||||
if (n == z)
|
||||
{
|
||||
if (*counter < *min)
|
||||
*min = *counter;
|
||||
if (*counter > *max)
|
||||
*max = *counter;
|
||||
return;
|
||||
}
|
||||
|
||||
(*counter)++;
|
||||
getMaxMinLengthR(n->left, z, min, max, counter);
|
||||
getMaxMinLengthR(n->right, z, min, max, counter);
|
||||
(*counter)--;
|
||||
}
|
||||
|
||||
static int getMaxMinDiff(ExrateBST ebst)
|
||||
{
|
||||
int counter = 0, min = ebst->N, max = 0;
|
||||
getMaxMinLengthR(ebst->root, ebst->z, &min, &max, &counter);
|
||||
|
||||
return max - min;
|
||||
}
|
||||
|
||||
link partR(link h, int r)
|
||||
{
|
||||
int t = h->left->N;
|
||||
if (t > r)
|
||||
{
|
||||
h->left = partR(h->left, r);
|
||||
h = rotR(h);
|
||||
}
|
||||
if (t < r)
|
||||
{
|
||||
h->right = partR(h->right, r - t - 1);
|
||||
h = rotL(h);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
static link balanceR(link h, link z)
|
||||
{
|
||||
int r;
|
||||
if (h == z)
|
||||
return z;
|
||||
r = (h->N + 1) / 2 - 1;
|
||||
h = partR(h, r);
|
||||
h->left = balanceR(h->left, z);
|
||||
h->right = balanceR(h->right, z);
|
||||
return h;
|
||||
}
|
||||
|
||||
void visitInOrderCheckInterval(link h, link z, DateTime dt1, DateTime dt2, int interval, Exrate *min, Exrate *max) {
|
||||
if (h == z)
|
||||
return;
|
||||
|
||||
visitInOrderCheckInterval(h->left,z,dt1,dt2,interval,min,max);
|
||||
|
||||
if (!interval || (DateTimeCompare(h->exrate.datetime, dt1) >= 0 && DateTimeCompare(h->exrate.datetime, dt2) <= 0)) {
|
||||
if (h->exrate.q > max->q)
|
||||
*max = h->exrate;
|
||||
if (h->exrate.q < max->q)
|
||||
*min = h->exrate;
|
||||
}
|
||||
|
||||
visitInOrderCheckInterval(h->right,z,dt1,dt2,interval,min,max);
|
||||
}
|
||||
|
||||
void ExrateBSTBalance(ExrateBST bst)
|
||||
{
|
||||
// calcola la differenza (distanza root -> nodo massima) - (distanza root -> nodo minima)
|
||||
int diff = getMaxMinDiff(bst);
|
||||
|
||||
if (diff >= S)
|
||||
{
|
||||
printf("La differenza tra il cammino più corto e quello più lungo è di %d, bilancio l'albero...\n", diff);
|
||||
bst->root = balanceR(bst->root, bst->z);
|
||||
|
||||
diff = getMaxMinDiff(bst);
|
||||
printf("Bilanciato, ora la differenza è di: %d\n", diff);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("La differenza tra il cammino più corto e quello più lungo è di %d, non è necessario bilanciare l'albero\n", diff);
|
||||
}
|
||||
}
|
||||
|
||||
void ExrateBSTMinMaxW(ExrateBST bst, DateTime dt1, DateTime dt2, int interval) {
|
||||
Exrate min, max;
|
||||
min = max = bst->root->exrate;
|
||||
visitInOrderCheckInterval(bst->root, bst->z, dt1, dt2, interval, &min, &max);
|
||||
printf("Quotazione minima: ");
|
||||
ExratePrint(min);
|
||||
printf("Quotazione massima: ");
|
||||
ExratePrint(max);
|
||||
}
|
||||
|
||||
void ExrateBSTMinMax(ExrateBST bst) {
|
||||
DateTime empty;
|
||||
ExrateBSTMinMaxW(bst, empty, empty, 0);
|
||||
}
|
||||
|
||||
void ExrateBSTMinMaxInInterval(ExrateBST bst, DateTime dt1, DateTime dt2) {
|
||||
ExrateBSTMinMaxW(bst, dt1, dt2, 1);
|
||||
}
|
||||
23
Laboratorio 8/Esercizio 3/ExrateBST.h
Normal file
23
Laboratorio 8/Esercizio 3/ExrateBST.h
Normal file
@@ -0,0 +1,23 @@
|
||||
// Laboratorio 8 - Esercizio 3 - ExrateBST.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef EXRATEBST_DEFINED
|
||||
#define EXRATEBST_DEFINED
|
||||
|
||||
#include "Exrate.h"
|
||||
#include "Datetime.h"
|
||||
|
||||
#define S 5
|
||||
|
||||
typedef struct exrateBST *ExrateBST;
|
||||
ExrateBST ExrateBSTCreate();
|
||||
void ExrateBSTFree(ExrateBST ebst);
|
||||
void ExrateBSTInsert(ExrateBST ebst, Exrate exrate);
|
||||
void ExrateBSTMerge(ExrateBST dst, ExrateBST src);
|
||||
|
||||
Exrate ExrateBSTSearch(ExrateBST ebst, DateTime dt);
|
||||
void ExrateBSTBalance(ExrateBST bst);
|
||||
void ExrateBSTMinMaxInInterval(ExrateBST bst, DateTime dt1, DateTime dt2);
|
||||
void ExrateBSTMinMax(ExrateBST bst);
|
||||
|
||||
#endif
|
||||
11
Laboratorio 8/Esercizio 3/F1.txt
Normal file
11
Laboratorio 8/Esercizio 3/F1.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
3
|
||||
ABC001 4
|
||||
2018/01/01 00:00 100 3
|
||||
2018/01/01 00:01 78 1
|
||||
2018/01/01 00:02 345 9
|
||||
2018/01/01 00:03 13 1
|
||||
ABC002 2
|
||||
2018/01/01 00:00 244 23
|
||||
2018/01/01 00:01 190 56
|
||||
ABC003 1
|
||||
2018/01/02 00:00 1 1
|
||||
11
Laboratorio 8/Esercizio 3/F2.txt
Normal file
11
Laboratorio 8/Esercizio 3/F2.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
3
|
||||
ABC006 4
|
||||
2018/01/03 00:00 100 3
|
||||
2018/01/03 00:01 78 1
|
||||
2018/01/03 00:02 345 9
|
||||
2018/01/03 00:03 13 1
|
||||
ABC003 2
|
||||
2018/01/03 00:00 244 23
|
||||
2018/01/03 00:01 190 56
|
||||
ABC011 1
|
||||
2018/01/06 00:00 1 1
|
||||
17
Laboratorio 8/Esercizio 3/F3.txt
Normal file
17
Laboratorio 8/Esercizio 3/F3.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
4
|
||||
ABC005 4
|
||||
2018/01/07 00:00 100 3
|
||||
2018/01/07 00:01 78 1
|
||||
2018/01/07 00:02 345 9
|
||||
2018/01/07 00:03 13 1
|
||||
ABC002 2
|
||||
2018/01/12 00:00 244 23
|
||||
2018/01/11 00:01 190 56
|
||||
ABC003 1
|
||||
2018/01/20 00:00 1 1
|
||||
ABC001 5
|
||||
2018/01/20 00:00 3 3
|
||||
2018/01/21 00:00 3 3
|
||||
2018/01/22 00:00 3 3
|
||||
2018/01/23 00:00 3 3
|
||||
2018/01/24 00:00 3 3
|
||||
180
Laboratorio 8/Esercizio 3/main.c
Normal file
180
Laboratorio 8/Esercizio 3/main.c
Normal file
@@ -0,0 +1,180 @@
|
||||
// Laboratorio 8 - Esercizio 3 - main.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include<ctype.h>
|
||||
#include<string.h>
|
||||
|
||||
#include"AssetList.h"
|
||||
#include"Asset.h"
|
||||
#include"ExrateBST.h"
|
||||
|
||||
#define MAX_LEN 30
|
||||
|
||||
const int MAXL = 100;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
r_read,
|
||||
r_select_asset,
|
||||
r_get_quot_date,
|
||||
r_get_quot_min_max_date,
|
||||
r_get_min_max,
|
||||
r_balance,
|
||||
r_fine
|
||||
} t_comandi;
|
||||
|
||||
char *toLower(char *s)
|
||||
{
|
||||
for (char *p = s; *p; p++)
|
||||
*p = tolower(*p);
|
||||
return s;
|
||||
}
|
||||
|
||||
t_comandi leggiComando()
|
||||
{
|
||||
t_comandi c;
|
||||
char cmd[MAXL];
|
||||
char tabella[7][30] = {
|
||||
"leggi_file", "seleziona_asset", "quotazione_data", "quotazione_min_max_data", "quotazione_min_max","bilancia", "fine"};
|
||||
printf("comando (leggi_file, seleziona_asset, quotazione_data, quotazione_min_max_data, quotazione_min_max, bilancia, fine): ");
|
||||
scanf("%s", cmd);
|
||||
toLower(cmd);
|
||||
c = r_read;
|
||||
while (c < 7 && strcmp(cmd, tabella[c]) != 0)
|
||||
c++;
|
||||
return (c);
|
||||
}
|
||||
|
||||
void handleRead(AssetList assetList) {
|
||||
FILE *fp;
|
||||
char filename[MAX_LEN+1];
|
||||
int N;
|
||||
|
||||
printf("Filename: ");
|
||||
scanf("%s", filename);
|
||||
|
||||
if ((fp = fopen(filename, "r")) == NULL) {
|
||||
printf("ERROR opening input file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
fscanf(fp, "%d", &N);
|
||||
for (int i = 0; i < N; i++) {
|
||||
Asset newAsset = AssetRead(fp);
|
||||
AssetListInsert(assetList, newAsset);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void handleAssetSelection(AssetList assetList, Asset **selectedAsset) {
|
||||
char title[MAX_LEN+1];
|
||||
|
||||
if (AssetListLength(assetList) == 0)
|
||||
printf("Nessun asset selezionato\n");
|
||||
else {
|
||||
AssetListPrint(assetList);
|
||||
printf("Quale asset vuoi scegliere: ");
|
||||
scanf("%s", title);
|
||||
|
||||
if ((*selectedAsset = AssetListSearch(assetList, title)) == NULL) {
|
||||
printf("Nessun asset trovato\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void handleSingleExrateSearch(AssetList assetList, Asset *selectedAsset) {
|
||||
Exrate dailyexrate;
|
||||
|
||||
if (selectedAsset == NULL)
|
||||
printf("Nessun asset selezionato\n");
|
||||
else {
|
||||
DateTime dt;
|
||||
printf("Data (yyyy/mm/dd): ");
|
||||
DateRead(&dt, stdin);
|
||||
dailyexrate = ExrateBSTSearch(AssetGetExrates(*selectedAsset), dt);
|
||||
ExratePrint(dailyexrate);
|
||||
}
|
||||
}
|
||||
|
||||
void handleMultiExrateSearch(AssetList assetList, Asset *selectedAsset) {
|
||||
if (selectedAsset == NULL)
|
||||
printf("Nessun asset selezionato\n");
|
||||
else {
|
||||
ExrateBSTMinMax(AssetGetExrates(*selectedAsset));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void handleMultiExrateSearchInterval(AssetList assetList, Asset *selectedAsset) {
|
||||
if (selectedAsset == NULL)
|
||||
printf("Nessun asset selezionato\n");
|
||||
else {
|
||||
DateTime dt1;
|
||||
DateTime dt2;
|
||||
printf("Data inizio (yyyy/mm/dd): ");
|
||||
DateRead(&dt1, stdin);
|
||||
printf("Data fine (yyyy/mm/dd): ");
|
||||
DateRead(&dt2, stdin);
|
||||
|
||||
ExrateBSTMinMaxInInterval(AssetGetExrates(*selectedAsset), dt1, dt2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void handleBalance(Asset *asset) {
|
||||
if (asset == NULL)
|
||||
printf("Nessun asset selezionato\n");
|
||||
else
|
||||
ExrateBSTBalance(AssetGetExrates(*asset));
|
||||
}
|
||||
|
||||
void menuParola(AssetList al)
|
||||
{
|
||||
Asset *selected = NULL;
|
||||
t_comandi comando;
|
||||
int i, continua = 1;
|
||||
while (continua)
|
||||
{
|
||||
comando = leggiComando();
|
||||
switch (comando)
|
||||
{
|
||||
case r_read:
|
||||
handleRead(al);
|
||||
break;
|
||||
case r_select_asset:
|
||||
handleAssetSelection(al, &selected);
|
||||
break;
|
||||
case r_get_quot_date:
|
||||
handleSingleExrateSearch(al, selected);
|
||||
break;
|
||||
case r_get_quot_min_max_date:
|
||||
handleMultiExrateSearchInterval(al, selected);
|
||||
break;
|
||||
case r_get_min_max:
|
||||
handleMultiExrateSearch(al, selected);
|
||||
break;
|
||||
case r_balance:
|
||||
handleBalance(selected);
|
||||
break;
|
||||
case r_fine:
|
||||
continua = 0;
|
||||
break;
|
||||
default:
|
||||
puts("Comando non valido\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char filename[MAX_LEN];
|
||||
AssetList al = AssetListCreate();
|
||||
|
||||
menuParola(al);
|
||||
|
||||
AssetListFree(al);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user