Author Topic: [C] Power consumption calculator  (Read 4047 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[C] Power consumption calculator
« on: December 18, 2012, 12:49:48 pm »
So I have started learning C for a bit, then onto C++. This is one of the projects to learn from.
It is a simple theoretical power usage calculator that calculates the cost of running some device based on kilowatt hours.

Why theoretical? because the watts written on devices are rated to operate at the maximum of that amount of watts, so that does not mean the device will always run on the maximum watt value. The device may use lower values of watts but not higher.

Here is the code (can also be found here):
Code: (c) [Select]
/*
Author: Kulverstukas;
Simple tool to calculate theoretical power consumption;

Below are formulas (lt == Lithuanian currency):
    0,25 (kWh) * 0,49 (lt/kWh) = 0,1225 LT [How much money it costs per hour];
    0,1225 * 24 = 2,94 LT [How much per day (24 hours)];
    2,94 LT * 30 = 88LT [How much per month (30 days)];
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HOURS_PER_DAY 24
#define DAYS_PER_MONTH 30

void printHelp(char progName[]);
void getProgramName(char * progName, char * res);
void calculatePowerUsage(float kWh, float cost, float * calcs);

int main(int argc, char * argv[]) {
    if ((argc > 3) || argc < 3) {
        printHelp(argv[0]);
        return 1;
    }

    float kWh = atof(argv[1]);
    float cost = atof(argv[2]);

    if ((kWh <= 0.0) || (cost <= 0.0)) {
        printf("Error: kWh or cost cannot be less than 0.1\n\n");
        printHelp(argv[0]);
        return 1;
    } else {
        float calcs[3];
        calculatePowerUsage(kWh, cost, calcs);

        printf("kWh: %.2f;  Cost: %.2f;\n\n", kWh, cost);
        printf("Cost per hour : %.2f\n", calcs[0]);
        printf("Cost per day  : %.2f\n", calcs[1]);
        printf("Cost per month: %.2f\n\n", calcs[2]);
    }
    return 0;
}

void getProgramName(char * progName, char * res) {
    int i;
    int argLen = strlen(progName)-1;
    int slashPos = 0;
    int counter = 0;
    // this steps through each char checking if it's a slash
    // stores slash position for later use
    // and determine how many chars are there from last found slash to end of str
    for (i = 0; i <= argLen; i++) {
        char c = progName[i];
        counter++;
        if ((c == 47) || (c == 92)) {
            slashPos = i;
            counter = 0;
        }
    }
    int src = slashPos;
    // copy chars from last found slash to the end
    for (i = 0; i <= counter; i++) {
        res[i] = progName[src];
        src++;
    }
}

void printHelp(char progName[]) {
    char res[255];
    getProgramName(progName, &res);
    printf("Tool to calculate theoretical power consumption of your machine\n\n");
    printf("Usage:\n    %s kilowatthours(kWh) cost\n", res);
    printf("Example:\n    %s 0.25 0.49\n", res);
}

void calculatePowerUsage(float kWh, float cost, float * calcs) {
    float perHour = 0;
    float perDay = 0;
    float perMonth = 0;

    perHour = kWh * cost;
    perDay  = perHour * HOURS_PER_DAY;
    perMonth = perDay * DAYS_PER_MONTH;

    calcs[0] = perHour;
    calcs[1] = perDay;
    calcs[2] = perMonth;
}
« Last Edit: December 18, 2012, 12:54:17 pm by Kulverstukas »