Przykład z programu do obliczenia elektryczności

Link

/*
Vladimir Poplavskij. Wykladowca
* -----------------
* Obliczenie platy za miesiac elektrycznosc.
* -----------------
2022-10-18
*/

#include 
#include 
#include 
#include 
#include 

typedef enum { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec } month;

struct Elektrum {
    month mm;
    int year;
    float kwtD;
    float kwtN;
    struct Elektrum *next;
};

struct Price {
    char *name;
    float kwtD;
    float kwtN;
};

struct MonthBills {
    float bill;
    month mm;
    int year;
    struct Price *price;
    struct Elektrum *elektra;
    struct MonthBills *next;
};

extern void appendElektra(struct Elektrum **, month, int, float, float);
extern void appendBill(struct MonthBills **, month, int, float, struct Price *, struct Elektrum *);
extern void printElektraList(struct Elektrum *);
extern void printMonthBillsList(struct MonthBills *);
extern void used(struct Elektrum *);
extern void calculateBill(struct Elektrum *, struct Price *, struct MonthBills *);

int main(void) {
	clock_t t;
	struct Elektrum* elektra = NULL;
	struct MonthBills* bills = NULL;
	struct Price* fixed = (struct Price*) malloc(sizeof(struct Price));
	struct Price* fixedBase = (struct Price*) malloc(sizeof(struct Price));
	struct Price* green = (struct Price*) malloc(sizeof(struct Price));
	struct Price* ignitis = (struct Price*) malloc(sizeof(struct Price));

	t = clock();

// TODO refactor Plans as double linked list
// TODO Realise add new plan
// TODO Realise update price
	fixed->name = "Fiksuotas";
	fixed->kwtD = 0.73072;
	fixed->kwtN = 0.51542;

	fixedBase->name = "Fiksuotas without discount";
	fixedBase->kwtD = 0.75578;
	fixedBase->kwtN = 0.53342;

	green->name = "Green";
	green->kwtD = 0.42442;
	green->kwtN = 0.38542;

	ignitis->name = "Ignitis";
	ignitis->kwtD = 0.561;
	ignitis->kwtN = 0.404;

// TODO Implement insert inside Electrum list
// TODO Implement update Electrum item
// TODO Implement remove Electrum item
	appendElektra(&elektra, Aug, 2022, 504, 185);
	appendElektra(&elektra, Sep, 2022, 620, 195);
	appendElektra(&elektra, Oct, 2022, 852, 205);
	printElektraList(elektra);
	used(elektra);
	printf("----------------------- \n");
	calculateBill(elektra, fixed, bills);
	printf("----------------------- \n");
	calculateBill(elektra, fixedBase, bills);
	printf("----------------------- \n");
	calculateBill(elektra, green, bills);
	printf("----------------------- \n");
	calculateBill(elektra, ignitis, bills);
	printf("----------------------- \n");
	printMonthBillsList(bills);
	printf("----------------------- \n");

	t = clock() - t;
	double time_taken = ((double)t)/CLOCKS_PER_SEC;

	printf("spend time %f seconds \n", time_taken);

	exit(EXIT_SUCCESS);
	return 0;
}

// Append electricity items to the list
void appendElektra(struct Elektrum** head_ref, month mm, int year, float kwtD, float kwtN){
    struct Elektrum* new_node = (struct Elektrum*) malloc(sizeof(struct Elektrum));
    struct Elektrum *last = *head_ref;

    new_node->mm  = mm;
    new_node->year  = year;
    new_node->kwtD  = kwtD;
    new_node->kwtN  = kwtN;
    new_node->next = NULL;

    if (*head_ref == NULL){
       *head_ref = new_node;
       return;
    }

    while (last->next != NULL)
        last = last->next;

    last->next = new_node;
    return;
}

// Append bill items to the list
void appendBill(struct MonthBills** head_ref, month mm, int year, float bill, struct Price* price, struct Elektrum* elektra){
    struct MonthBills* new_node = (struct MonthBills*) malloc(sizeof(struct MonthBills));
    struct MonthBills *last = *head_ref;

    new_node->mm  = mm;
    new_node->year  = year;
    new_node->bill  = bill;
    new_node->price = price;
    new_node->elektra = elektra;
    new_node->next = NULL;

    if (*head_ref == NULL){
       *head_ref = new_node;
       return;
    }

    while (last->next != NULL)
        last = last->next;

    last->next = new_node;
    return;
}

// Print elektrum list
void printElektraList(struct Elektrum *node) {
	while (node != NULL) {
		printf("Counts %d-%d (%.2f,%.2f) \n", node->year, node->mm, node->kwtD, node->kwtN);
		node = node->next;
	}
}

// Print bills list
void printMonthBillsList(struct MonthBills *node) {
	while (node != NULL) {
		printf("Prices for \"%s\" %d-%d (%.2f) \n", node->price->name, node->year, node->mm, node->bill);
		node = node->next;
	}
}

// Used elektricity
void used(struct Elektrum *node) {
	int i = 1;
	float prevD = 0;
	float prevN = 0;

	while (node != NULL) {
		float diffD = 0;
		float diffN = 0;
		if (i == 1) {
			prevD = node->kwtD;
			prevN = node->kwtN;
		} else {
			diffD = node->kwtD - prevD;
			diffN = node->kwtN - prevN;
			prevD = node->kwtD;
			prevN = node->kwtN;
			printf("Used %d-%d: \n day - %.2f Kwt/h \n night - %.2f Kwt/h \n", node->year, node->mm , diffD, diffN);
		}
		i++;
		node = node->next;
	}

	return;
}

// Calculate elektricity bill bu months for plan
void calculateBill(struct Elektrum *node, struct Price *price, struct MonthBills *bills) {
	int i = 1;
	float prevD = 0;
	float prevN = 0;
	printf("Cost for %s plan \n", price->name);

	while (node != NULL) {
		float costD = 0;
		float costN = 0;
		if (i == 1) {
			prevD = node->kwtD;
			prevN = node->kwtN;
		} else {
			costD = price->kwtD * (node->kwtD - prevD);
			costN = price->kwtN * (node->kwtN - prevN);
			prevD = node->kwtD;
			prevN = node->kwtN;
			printf("Bill %d-%d: \n day - %.2f Eur \n night - %.2f Eur. Total %.2f Eur. \n", node->year, node->mm , costD, costN, (costD + costN));
			appendBill(&bills, node->mm, node->year, (costD + costN), price, node);
		}
		i++;
		node = node->next;
	}

	return;
}
  

Link

Na glówna