233k views
4 votes
(Wattan Corporation) is an Internet service provider that charges customers a flat rate of $7.99 for up to 10

hours of connection time. Additional hours or partial hours are charged at $1.99 each.
Write a function charges that computes the total charge for a customer based on the number of hours of
connection time used in a month. The function should also calculate the average cost per hour of the time
used (rounded to the nearest 0.01), so use two output parameters to send back these results.
You should write a second function
round_money that takes a real number as an input argument and returns as the function value the number
rounded to two decimal places. Write a main function that takes data from an input file usage.txt and
produces an output file charges.txt. The data file format is as follows:
Line 1: current month and year as two integers
Other lines: customer number (a five-digit number) and number of hours used
Here is a sample data file and the corresponding output file:
Data file usage.txt
10 2009
15362 4.2
42768 11.1
11111 9.9
Output file charges.txt
Charges for 10/2009
15362 4.2 7.99 1.90
42768 11.1 10.18 0.92
11111 9.9 7.99 0.81

1 Answer

2 votes

Answer:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

void do_charges(int j, double data[10], double *cph, double *ave_cost);

double round_money(double money);

int main(void)

{

int j=1, n=0, day, month;

double id_buffer=0.0, val_buffer=0.0, data[10], cph = 0.0, ave_cost = 0.0;

char c;

FILE *fp = fopen("usage.txt", "r");

if(fp == NULL ) printf("error\\");

fscanf(fp,"%d %d", &day, &month);

// By using conditional statement

while ( c != EOF )

{

fscanf(fp,"%lf %lf", &id_buffer, &val_buffer);

data[j] = id_buffer;

data[j+4] = val_buffer;

c = getc(fp);

j++;

}

fclose(fp);

fp = fopen("charges.txt", "w");

if(fp == NULL ) printf("error\\");

fprintf(fp,"Charges for %d/%d\\\\\\", day, month);

fprintf(fp,"Customer\t hrs used\t C.P.H.\t\t Ave. Cost\\\\");

printf("Charges for %d/%d\\\\\\", day, month);

printf("Customer\t hrs used\t C.P.H.\t\t Ave. Cost\\\\");

for(j=1;j<4;j++)

{

cph = 0;

do_charges(j, data, &cph, &ave_cost);

fprintf(fp,"%1.0lf\t\t %0.1lf\t\t %0.2lf\t\t %0.2lf\\\\",data[j], data[j+4], cph, ave_cost);

printf("%1.0lf\t\t %0.1lf\t\t %0.2lf\t\t %0.2lf\\\\",data[j], data[j+4], cph, ave_cost);

}

fclose(fp);

return 0;

}

void do_charges(int j, double data[10], double *cph, double *ave_cost)

{

int base_rate = 7.99, partial_rate = 1.99;

double remain=0.0, total_hrs=0.0, money=0.0;

/*

printf("%6.0lf %6.0lf\\", data[0], data[4]);

for(j=1;j<4;j++)

{

printf("%6.0lf %6.2lf\\", data[j], data[j+4]);

}

*/

if(data[j+4] > 10)

{

remain = data[j+4] - 10;

while(remain > 0)

{

*cph = *cph + 1.99;

if(remain/10>0) remain--;

}

*cph = *cph + 7.99;

money = *cph;

*cph = round_money(money);

total_hrs = (remain * *cph);

} else {

*cph = 7.99;

money = *cph;

*cph = round_money(money);

total_hrs = data[j+4] * 7.99;

}

*ave_cost = *cph / data[j+4];

money = *ave_cost;

*ave_cost = round_money(money);

}

double round_money(double money)

{

money = money * 100;

money = round(money);

money = money / 100;

return money;

}

Step-by-step explanation:

User Rasel
by
4.0k points