NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>



#define line_Len 100



FILE *rd,*wr;



char path[] = "data.txt";



int years[line_Len];

int months[line_Len];

int dates[line_Len];

double payments[line_Len];

double outstanding[line_Len];

int readings[line_Len];

int lines = 0;



void startProgram();

int openFile();

void endProgram();

double calcBill(int days, int lastRead, int newRead);

double getDay(int year, int month, int date);

double calcDays(double startDay, double endDay);

void readFile();

void initProgramData();

void writeData();

void parseDataNext();

void whileLoop();

void newRead();

void newPayment();

void printPayments();

void printBills();



int main()

{

startProgram();



int rdStat = 0;

rdStat = openFile();

if (rdStat != 0) readFile();

else(initProgramData());



whileLoop();

return 0;

}



void startProgram()

{

printf(" Welcome to Electricity Bill Calculator ");

printf("n");



int i = 0;

for(i = 0 ; i < line_Len ; i ++ )

{

months[i] = -1;

payments[i] = -1;

readings[i] = -1;

}

}



int openFile()

{

int openStat = 1;

rd = fopen(path,"r");

if (rd == NULL)

{

openStat = 2;

printf("No Data File Found n");



}

else printf("Data File opened successfullyn");

return openStat;

}



void initProgramData()

{

int i = 0;

for(i = 0 ; i < line_Len ; i ++ )

{

years[i] = 0;

months[i] = 0;

dates[i] = 0;

payments[i] = 0;

outstanding[i] = 0;

readings[i] = 0;

}

printf("Please Input the Last Meter Reading Details.nReading Date (yyyy/mm/dd)..............: ");

scanf("%d/%d/%d", &years[1], &months[1], &dates[1]);

printf("kWh Meter Reading......................: ");

scanf("%d", &readings[1]);

printf("Last Month Bill Outstanding............: ");

scanf("%lf", &outstanding[1]);

printf("Total of Payments After Last Month Bill: ");

scanf("%lf", &payments[0]);

years[0] = years[1];

months[0] = months[1];

dates[0] = dates[1];

outstanding[0] = outstanding[1] - payments[0];

fclose(rd);

wr = fopen(path,"w");

fprintf(wr, "%dt%dt%dt%.2lft%.2lft%dn", years[0], months[0], dates[0], payments[0], outstanding[0], readings[0]);

fprintf(wr, "%dt%dt%dt%.2lft%.2lft%dn", years[1], months[1], dates[1], payments[1], outstanding[1], readings[1]);

fclose(rd);

}



void parseDataNext()

{

int i = 0;

for(i = line_Len - 1 ; i > 0 ; i --)

{

if(years[i-1] > 0)

{

years[i] = years[i-1];

months[i] = months[i-1];

dates[i] = dates[i-1];

payments[i] = payments[i-1];

outstanding[i] = outstanding[i-1];

readings[i] = readings[i-1];

}

}



i = 0;

years[i] = 0;

months[i] = 0;

dates[i] = 0;

payments[i] = 0;

outstanding[i] = 0;

readings[i] = 0;

}



void newRead()

{

parseDataNext();

printFullLine();

printf("New Reading Month(mm) : ");

scanf("%d", &months[0]);

printf("New kWh Meter Reading : ");

scanf("%d",&readings[0]);

int i = 1;

while (readings[i] == 0)

{

i ++;

}

printf("i = %dn", i);

double bill = calcBill(30,readings[i],readings[0]);

outstanding[0] = outstanding[1] + bill;

printf("This Month kWh Units : %dn",readings[0]-readings[i]);

printf("This Month Charge : %9.2lfn",bill);

writeData();

}



void newPayment()

{

parseDataNext();

printFullLine();

printf("Payment Month(mm) : ");

scanf("%d/%d/%d",&months[0]);

printf("Payment Amount : ");

scanf("%lf", &payments[0]);

outstanding[0] = outstanding[1] - payments[0];

printf("Total Outstanding : %9.2lfn",outstanding[0]);

writeData();

}



void printBills()

{

int i = 0;

int i1 = 0;

printf("ENERGY CONSUMPTION DETAILS");

for(i = 0 ; i < line_Len ; i ++)

{

if(readings[i] > 0)

{

i1 = i;

while(readings[i1] > 0)

{

i1++;

if(i1 >= line_Len)

{

i1 = i;

break;

}

}

if(i != i1)

{

double consum = readings[i] - readings[i1];

double bills = calcBill(30,readings[i1],readings[i]);

printf("t%d/%.2lft%.2lfn", months[i],consum, bills);

}

}

}

}



void printPayments()

{

int i = 0;

printf("PAYMENT DETAILSn");

printf(" Date tPaymentst Outstandingn");

for(i = 0 ; i < line_Len ; i ++)

{

if(payments[i] > 0)

{

printf("%4d/%2d/%2dt%9.2lftt%9.2lfn",months[i],payments[i]);

}

}

}



void whileLoop()

{

int i = 1;

char in = 0;

while (i)

{

printf("Please select task to executen");

printf("t1 - Add New kWh Readingn");

printf("t2 - Add Paymentn");

printf("t3 - View Consumption And Billsn");

printf("t4 - View Paymentsn");

printf("t5 - EXITn");



scanf("%d",&in);

switch (in)

{

case 1 :

newRead();

i = 1;

break;

case 2 :

newPayment();

i = 1;

break;

case 3 :

printBills();

i = 1;

break;

case 4 :

printPayments();

i = 1;

break;

case 5 :

i = 0;

break;

default :

printf("Wrong Inputn");

i = 1;

break;

}

}

writeData();

}



void writeData()

{



int i = 0;

wr = fopen(path,"w");

for(i = 0 ; i < line_Len ; i ++ )

{

if(years[i] > 0)

{

fprintf(wr, "%dt%dt%dt%.2lft%.2lft%dn", months[i], payments[i], readings[i]);

}

}

fclose(rd);

}



void readFile()

{

int i = 0;

while (fscanf(rd, "%dt%dt%dt%lft%lft%dn", &months[i], &payments[i], &readings[i]) != EOF)

{

i ++;

if(i >= line_Len) break;

}

}



double calcBill(int days, int lastRead, int newRead)

{

days = 30;

double bill = 0;

int units = newRead - lastRead;

if(units <= days )

{

bill = 30.0 + 2.50 * units;

}

else

{

bill = 2.50 * days;

if(units <= (days * 2))

{

bill = bill + 60.0 + 4.85 * (units - days);

}

else

{

bill = 7.85 * 2.0 * days;

if(units <= (days * 3))

{

bill = bill + 90.0 + 10.00 * (units - days * 2.0);

}

else

{

bill = bill + 10.00 * days;

if(units <= (days * 4))

{

bill = bill + 480.0 + 27.75 * (units - days * 3.0);

}

else

{

bill = bill + 27.75 * days;

if(units <= (days * 6))

{

bill = bill + 480.0 + 32.00 * (units - days * 4.0);

}

else

{

bill = bill + 32.00 * days;

if(units > (days * 6))

{

bill = bill + 540.0 + 45.00 * (units - days * 6.0);

}

}

}

}

}

}



return bill;

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.