Initial commit

This commit is contained in:
Andrew
2022-11-25 13:35:18 +02:00
commit 4045d93797
9 changed files with 9145 additions and 0 deletions

176
Main/Library.c Normal file
View File

@@ -0,0 +1,176 @@
/************************************************************************
* LES10A110 Principles of C-programming
* Name: Trieu Huynh Ba Nguyen
* Student number: 000405980
* Email: Trieu.Huynh.Ba.Nguyen@student.lut.fi
* Date: 16.04.2022
* By submitting this work for evaluation, I certify that
* 1) I myself wrote all the code in this file
* 2) I have not given this code to anyone else
*
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Electric struct, with each attribute corresponds to each column in the csv file
struct Electric
{
char timestamp[20];
int week;
long int consumption;
long int solar;
long int wind;
long int hydro;
long int nuclear;
long int chp;
long int thermal;
};
typedef struct Electric electric;
// Node struct for linked list
struct Node
{
electric data;
struct Node *next;
};
typedef struct Node node;
// Add new node to linked list
node *addNode(node *p_first, node *p_last, electric data)
{
node *new = (node *)malloc(sizeof(node));
new->data = data;
new->next = NULL;
if (p_first == NULL)
{
p_last = new;
}
else
{
p_last->next = new;
p_last = new;
}
return (p_last);
}
// Print linked list function, implemented for testing, not in use
// void print(node *p_first, char name[])
// {
// FILE *f;
// f = fopen(name, "w");
// for (node *itr = p_first; itr != NULL; itr = itr->next)
// {
// if (itr->data.timestamp != NULL && itr->next->data.timestamp != NULL)
// fprintf(f, "%s %d\n", itr->data.timestamp, itr->data.consumption);
// }
// fclose(f);
// return;
// }
// Clear linked list function
node *clear(node *p_first)
{
node *itr = p_first;
while (itr != NULL)
{
p_first = itr->next;
free(itr);
itr = p_first;
}
return (p_first);
}
// Consumption function to calculate total, average, highest and lowest consumption. The function iterates through the linked list, summing comparing all consumption values
void consumption(node *p_first, int count)
{
long long int total = 0;
FILE *f, *h;
node *max = p_first;
node *min = p_first;
char line[1024];
f = fopen("consumption.txt", "w");
for (node *itr = p_first; itr != NULL; itr = itr->next)
{
if (itr->data.timestamp != NULL && itr->next->data.timestamp != NULL)
{
total += itr->data.consumption;
if (max->data.consumption < itr->next->data.consumption)
{
max = itr;
}
if (min->data.consumption > itr->data.consumption)
{
min = itr;
}
}
}
// Remove extra zeros from timestamp
char maxtime[20], mintime[20];
strcpy(maxtime, max->data.timestamp);
strcpy(mintime, min->data.timestamp);
memmove(&maxtime[3], &maxtime[4], strlen(maxtime) - 3);
memmove(&maxtime[10], &maxtime[11], strlen(maxtime) - 10);
memmove(&mintime[3], &mintime[4], strlen(mintime) - 3);
memmove(&mintime[10], &mintime[11], strlen(mintime) - 10);
fprintf(f, "Statistics on %d measurements:\n", count);
fprintf(f, "Consumption totaled %lld kWh, and averaged %.1f kWh.\n", total, ((double)total) / ((double)count));
fprintf(f, "The highest consumption, %ld kWh, occurred on %s.\n", max->data.consumption, maxtime);
fprintf(f, "The lowest consumption, %ld kWh, occurred on %s.", min->data.consumption, mintime);
fclose(f);
h = fopen("consumption.txt", "r");
while (fgets(line, 1024, h) != NULL)
{
printf("%s", line);
}
printf("\n");
fclose(h);
}
// Production function iterates thorugh the linked list and sums the total production per week for all types of electricity production
void production(node *p_first)
{
FILE *f;
long double solar = 0, wind = 0, hydro = 0, nuclear = 0, chp = 0, thermal = 0;
f = fopen("production.csv", "w");
fprintf(f, "Week;Solar power;Wind power;Hydropower;Nuclear power;CHP;Thermal power\n");
int week;
for (node *itr = p_first; itr != NULL; itr = itr->next)
{
if (itr->next->data.timestamp != NULL)
{
week = itr->data.week;
solar += ((long double)itr->data.solar) / 1000000;
wind += ((long double)itr->data.wind) / 1000000;
hydro += ((long double)itr->data.hydro) / 1000000;
nuclear += ((long double)itr->data.nuclear) / 1000000;
chp += ((long double)itr->data.chp) / 1000000;
thermal += ((long double)itr->data.thermal) / 1000000;
if (week != itr->next->data.week)
{
fprintf(f, "Week %d;%.2Lf;%.2Lf;%.2Lf;%.2Lf;%.2Lf;%.2Lf\n", week, solar, wind, hydro, nuclear, chp, thermal);
solar = 0;
wind = 0;
hydro = 0;
nuclear = 0;
chp = 0;
thermal = 0;
}
}
else
{
fprintf(f, "Week %d;%.2Lf;%.2Lf;%.2Lf;%.2Lf;%.2Lf;%.2Lf\n", week, solar, wind, hydro, nuclear, chp, thermal);
}
}
fclose(f);
}

40
Main/Library.h Normal file
View File

@@ -0,0 +1,40 @@
/************************************************************************
* LES10A110 Principles of C-programming
* Name: Trieu Huynh Ba Nguyen
* Student number: 000405980
* Email: Trieu.Huynh.Ba.Nguyen@student.lut.fi
* Date: 16.04.2022
* By submitting this work for evaluation, I certify that
* 1) I myself wrote all the code in this file
* 2) I have not given this code to anyone else
*
*************************************************************************/
// Electric struct, with each attribute corresponds to each column in the csv file
struct Electric
{
char timestamp[20];
int week;
long int consumption;
long int solar;
long int wind;
long int hydro;
long int nuclear;
long int chp;
long int thermal;
};
typedef struct Electric electric;
// Node struct for linked list
struct Node
{
electric data;
struct Node *next;
};
typedef struct Node node;
node *addNode(node *p_first, node *p_last, electric data);
node *clear(node *p_first);
void consumption(node *p_first, int count);
void production(node *p_first);

78
Main/Main.c Normal file
View File

@@ -0,0 +1,78 @@
/************************************************************************
* LES10A110 Principles of C-programming
* Name: Trieu Huynh Ba Nguyen
* Student number: 000405980
* Email: Trieu.Huynh.Ba.Nguyen@student.lut.fi
* Date: 16.04.2022
* By submitting this work for evaluation, I certify that
* 1) I myself wrote all the code in this file
* 2) I have not given this code to anyone else
*
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Library.h"
int main()
{
node *head = NULL;
node *tail = NULL;
node *itr;
FILE *ptr;
electric e;
int count = -1, choice;
char buffer[1024], name[1024];
memset(buffer, 0, 1024);
while (1)
{
printf("1)Read the file\n2)Analyze consumption\n3)Analyze production\n0)Stop\nSelect an Item:\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
clear(head);
printf("Please enter the file name\n");
scanf("%s", &name);
ptr = fopen(name, "r");
if (ptr != NULL)
{
fgets(buffer, 1024, ptr);
while (!feof(ptr))
{
fgets(buffer, 1024, ptr);
sscanf(buffer, "%[^;];%ld;%ld;%ld;%ld;%ld;%ld;%ld;%ld", e.timestamp, &e.week, &e.consumption, &e.solar, &e.wind, &e.hydro, &e.nuclear, &e.chp, &e.thermal);
tail = addNode(head, tail, e);
count++;
if (head == NULL)
head = tail;
}
}
else
{
printf("File not found.\n");
}
break;
case 2:
if (head == NULL)
printf("List is empty.\n");
else
consumption(head, count);
break;
case 3:
if (head == NULL)
printf("List is empty.\n");
else
production(head);
break;
case 0:
clear(head);
return 0;
default:
printf("Unknown option.\n");
break;
}
}
}

8785
Main/electric8700.csv Normal file

File diff suppressed because it is too large Load Diff

8
Main/makefile Normal file
View File

@@ -0,0 +1,8 @@
# Makefile for "Main.c"
project: Main.o Library.o
gcc Library.o Main.o -o project
Library.o: Library.c
gcc -c Library.c
Main.o: Main.c
gcc -c Main.c

BIN
Main/project.exe Normal file

Binary file not shown.

4
Output/consumption.txt Normal file
View File

@@ -0,0 +1,4 @@
Statistics on 8784 measurements:
Consumption totaled 80884331022 kWh, and averaged 9208143.3 kWh.
The highest consumption, 12718885 kWh, occurred on 28.2.2020 8:00.
The lowest consumption, 5620157 kWh, occurred on 20.6.2020 3:00.

54
Output/production.csv Normal file
View File

@@ -0,0 +1,54 @@
Week;Solar power;Wind power;Hydropower;Nuclear power;CHP;Thermal power
Week 1;0.14;198.64;175.81;335.94;341.69;26.07
Week 2;0.22;254.74;277.71;470.43;493.51;38.20
Week 3;0.21;186.10;315.66;470.48;481.33;39.53
Week 4;0.49;227.21;303.99;469.71;474.66;38.49
Week 5;0.30;171.70;286.53;470.37;366.38;32.93
Week 6;0.88;212.21;303.72;470.33;380.62;36.91
Week 7;0.71;187.55;345.42;470.42;377.73;55.27
Week 8;1.06;236.68;369.33;468.83;411.80;35.08
Week 9;2.30;107.51;366.36;470.32;556.94;66.38
Week 10;1.66;178.04;357.56;470.16;490.72;46.29
Week 11;2.28;190.57;355.73;470.27;427.83;40.10
Week 12;3.78;194.17;352.02;469.44;431.51;39.07
Week 13;4.60;230.17;332.16;447.23;413.72;20.88
Week 14;4.42;156.58;351.14;405.43;451.84;18.23
Week 15;5.16;187.50;294.78;454.80;371.07;21.73
Week 16;5.05;168.37;318.69;452.56;390.74;26.18
Week 17;6.82;119.78;277.88;458.44;355.14;26.05
Week 18;5.74;89.83;303.17;455.01;377.46;26.14
Week 19;9.12;102.71;340.28;442.61;322.91;22.78
Week 20;7.51;68.06;403.92;308.21;367.18;14.12
Week 21;10.28;73.77;385.58;351.44;308.42;25.58
Week 22;11.76;79.40;317.90;317.62;262.04;21.25
Week 23;9.50;117.87;293.79;315.63;247.59;29.97
Week 24;12.93;69.82;298.18;435.17;229.84;23.42
Week 25;13.72;68.51;265.73;458.77;199.65;31.54
Week 26;16.61;79.41;215.97;455.68;216.18;84.14
Week 27;8.73;128.72;186.48;455.83;214.57;28.32
Week 28;10.26;73.54;262.43;459.75;201.24;23.44
Week 29;12.47;56.53;298.52;458.56;182.56;25.41
Week 30;8.34;106.44;235.41;457.90;167.84;28.66
Week 31;8.98;74.99;266.90;447.94;188.48;26.29
Week 32;9.51;79.94;249.02;373.56;212.47;33.66
Week 33;9.67;96.25;238.00;371.42;206.41;53.95
Week 34;9.78;132.25;237.76;367.06;215.21;62.20
Week 35;7.82;59.43;224.60;370.82;232.45;63.54
Week 36;6.02;125.16;204.36;372.63;255.60;66.68
Week 37;4.84;161.42;202.27;378.09;275.17;41.33
Week 38;4.85;211.58;220.34;380.15;274.34;33.79
Week 39;4.35;172.55;233.22;380.17;254.96;41.73
Week 40;3.41;126.02;236.66;380.13;276.46;59.43
Week 41;2.21;178.83;244.71;380.48;284.97;32.08
Week 42;2.10;105.38;261.24;380.47;370.91;66.17
Week 43;1.15;124.37;294.47;412.67;376.95;71.83
Week 44;0.86;201.21;364.46;467.22;270.17;66.44
Week 45;1.26;286.19;356.50;466.19;242.86;51.60
Week 46;0.72;176.92;357.55;465.35;378.22;69.51
Week 47;0.45;279.97;346.97;466.74;308.52;45.98
Week 48;0.31;126.37;374.88;468.81;448.10;56.55
Week 49;0.16;206.63;355.70;469.39;467.07;53.50
Week 50;0.22;108.31;328.59;394.68;537.37;65.22
Week 51;0.12;153.07;305.67;333.60;494.03;77.99
Week 52;0.15;183.18;319.93;466.01;379.06;40.63
Week 53;0.08;116.03;182.42;267.18;248.30;29.79
1 Week Solar power Wind power Hydropower Nuclear power CHP Thermal power
2 Week 1 0.14 198.64 175.81 335.94 341.69 26.07
3 Week 2 0.22 254.74 277.71 470.43 493.51 38.20
4 Week 3 0.21 186.10 315.66 470.48 481.33 39.53
5 Week 4 0.49 227.21 303.99 469.71 474.66 38.49
6 Week 5 0.30 171.70 286.53 470.37 366.38 32.93
7 Week 6 0.88 212.21 303.72 470.33 380.62 36.91
8 Week 7 0.71 187.55 345.42 470.42 377.73 55.27
9 Week 8 1.06 236.68 369.33 468.83 411.80 35.08
10 Week 9 2.30 107.51 366.36 470.32 556.94 66.38
11 Week 10 1.66 178.04 357.56 470.16 490.72 46.29
12 Week 11 2.28 190.57 355.73 470.27 427.83 40.10
13 Week 12 3.78 194.17 352.02 469.44 431.51 39.07
14 Week 13 4.60 230.17 332.16 447.23 413.72 20.88
15 Week 14 4.42 156.58 351.14 405.43 451.84 18.23
16 Week 15 5.16 187.50 294.78 454.80 371.07 21.73
17 Week 16 5.05 168.37 318.69 452.56 390.74 26.18
18 Week 17 6.82 119.78 277.88 458.44 355.14 26.05
19 Week 18 5.74 89.83 303.17 455.01 377.46 26.14
20 Week 19 9.12 102.71 340.28 442.61 322.91 22.78
21 Week 20 7.51 68.06 403.92 308.21 367.18 14.12
22 Week 21 10.28 73.77 385.58 351.44 308.42 25.58
23 Week 22 11.76 79.40 317.90 317.62 262.04 21.25
24 Week 23 9.50 117.87 293.79 315.63 247.59 29.97
25 Week 24 12.93 69.82 298.18 435.17 229.84 23.42
26 Week 25 13.72 68.51 265.73 458.77 199.65 31.54
27 Week 26 16.61 79.41 215.97 455.68 216.18 84.14
28 Week 27 8.73 128.72 186.48 455.83 214.57 28.32
29 Week 28 10.26 73.54 262.43 459.75 201.24 23.44
30 Week 29 12.47 56.53 298.52 458.56 182.56 25.41
31 Week 30 8.34 106.44 235.41 457.90 167.84 28.66
32 Week 31 8.98 74.99 266.90 447.94 188.48 26.29
33 Week 32 9.51 79.94 249.02 373.56 212.47 33.66
34 Week 33 9.67 96.25 238.00 371.42 206.41 53.95
35 Week 34 9.78 132.25 237.76 367.06 215.21 62.20
36 Week 35 7.82 59.43 224.60 370.82 232.45 63.54
37 Week 36 6.02 125.16 204.36 372.63 255.60 66.68
38 Week 37 4.84 161.42 202.27 378.09 275.17 41.33
39 Week 38 4.85 211.58 220.34 380.15 274.34 33.79
40 Week 39 4.35 172.55 233.22 380.17 254.96 41.73
41 Week 40 3.41 126.02 236.66 380.13 276.46 59.43
42 Week 41 2.21 178.83 244.71 380.48 284.97 32.08
43 Week 42 2.10 105.38 261.24 380.47 370.91 66.17
44 Week 43 1.15 124.37 294.47 412.67 376.95 71.83
45 Week 44 0.86 201.21 364.46 467.22 270.17 66.44
46 Week 45 1.26 286.19 356.50 466.19 242.86 51.60
47 Week 46 0.72 176.92 357.55 465.35 378.22 69.51
48 Week 47 0.45 279.97 346.97 466.74 308.52 45.98
49 Week 48 0.31 126.37 374.88 468.81 448.10 56.55
50 Week 49 0.16 206.63 355.70 469.39 467.07 53.50
51 Week 50 0.22 108.31 328.59 394.68 537.37 65.22
52 Week 51 0.12 153.07 305.67 333.60 494.03 77.99
53 Week 52 0.15 183.18 319.93 466.01 379.06 40.63
54 Week 53 0.08 116.03 182.42 267.18 248.30 29.79

BIN
Output/project.exe Normal file

Binary file not shown.