Question:
Can anyone make a program in turbo C with the following problem?
Chardy
2008-01-17 02:58:50 UTC
the ZTE Company has the following rate structure for long distance calls:

a) Any call activated after 6:00 pm yet before 8:00 am is discounted by 50%.
b) Any call activated after 8:00 am yet before 6:00 pm is charged with a full price.
c) All calls are subject to 4% Tax.
d) The regular rate for a call is P0.40 per minute.
e) Any call longer than 60 minutes receives a 15% discount; that is, after any other discount is given and before tax is taken upon.

Write a C program that reads the starting time and ending time of a conversation based on a twenty four-hour clock operation and compute the length of the call.
The Gross cost (before any discount on tax) should be printed, followed by the net cost (after discounts are given and tax is taken upon).
Three answers:
Random Malefactor
2008-01-17 07:08:37 UTC
Your spec is incomplete, where does the program acquire the start and end times of the call? Other missing details: What is to be done with fractions of a minute? Also, I assume the start and end times are expressed as local time? This becomes a problem for calls that span the daylight savings time adjustment points. On the time-change day in the fall local times between 1AM and 2AM are ambiguous, and in the spring, the end time of a call that started before 2AM and ended after 2AM would be inaccurate. In a real system, times would be logged as UTC, and the program would have to convert UTC to local time to determine what hour of the day the call started.



I'm not going to deal with the DST issues. I'll assume that start and end are passed as the first and second command line arguments, respectively, and I will further assume that those values are always passed as YYYY-MM-DD HH:MM:SS, e.g., one second after midnight on new years day of this year would be passed as 2008-01-01 00:00:01. (Because these values contain an embedded space, they'll need to be enclosed in double-quotes when passed on the command line, or as an alternative, replace the space with any other character.) Lastly, I'm merely going to truncate any fraction of a minute.



(No telling how badly y!a will butcher this code, so a copy of it is at http://www.pagespy.com/blogstuff/test4.c )



#include

#include

#include

#include



// business rules constants

#define RATE_PER_MIN 0.40

#define PRIMETIME_START_HR 6

#define PRIMETIME_END_HR 20

#define NONPRIMETIME_DISC_PCTG 0.5

#define OVER60MINS_DISC_PCTG 0.15

#define TAX_PCTG 0.04



// offsets into date/time string

#define SECS_OFFSET 17

#define MINS_OFFSET 14

#define HRS_OFFSET 11

#define DAYS_OFFSET 8

#define MONTHS_OFFSET 5

#define YEARS_OFFSET 0





//expected format: 2008-01-01 00:00:00



time_t ParseTimeString(char *inbuf, struct tm *intime)

{

char timestr[21];

// make a local copy, not a good idea to alter cmnd line param

strncpy(timestr, inbuf, 20);



intime->tm_sec = atoi(×tr[SECS_OFFSET]);

// change end-point of string

timestr[SECS_OFFSET - 1] = 0;

intime->tm_min = atoi(×tr[MINS_OFFSET]);

timestr[MINS_OFFSET - 1] = 0;

intime->tm_hour = atoi(×tr[HRS_OFFSET]);

timestr[HRS_OFFSET - 1] = 0;

intime->tm_mday = atoi(×tr[DAYS_OFFSET]);

timestr[DAYS_OFFSET - 1] = 0;

// zero-based month, Jan == 0, Dec == 11

intime->tm_mon = atoi(×tr[MONTHS_OFFSET]) - 1;

timestr[MONTHS_OFFSET - 1] = 0;

// compensate for mega-lame C-runtime 2-digit year throwback

intime->tm_year = atoi(×tr[YEARS_OFFSET]) - 1900;



intime->tm_isdst = -1;

intime->tm_yday = 0;

intime->tm_wday = 0;



return mktime(intime);

}







int main(int argc, char **argv)

{

struct tm Start;

struct tm End;

time_t Tstart;

time_t Tend;

double GrossCost = 0.0;

double NetCost = 0.0;

double NonPrimeDisc = 0.0;

double Over60MinsDiscPctg = 0.0;

int DurationMins = 0;



Tstart = ParseTimeString(argv[1], &Start);

Tend = ParseTimeString(argv[2], &End);



if ((Tstart < 0) || (Tend < 0))

{

printf("call to mktime() failed.");

return -1;

}



DurationMins = (Tend - Tstart) / 60;



GrossCost = DurationMins * RATE_PER_MIN;



if ((Start.tm_hour < PRIMETIME_START_HR)

|| (Start.tm_hour >= PRIMETIME_END_HR))

NonPrimeDisc = GrossCost * NONPRIMETIME_DISC_PCTG;



if (DurationMins >= 60)

Over60MinsDiscPctg = OVER60MINS_DISC_PCTG;



NetCost = GrossCost - NonPrimeDisc;

NetCost -= (NetCost * Over60MinsDiscPctg);

NetCost += (NetCost * TAX_PCTG);



printf("%f", NetCost);



return 0;

}









Note that in real life, this would be a database app, call details would be queried from a table, and nearly all the business logic would be implemented in SQL code. An example of this, that works in SQL Server (7.0 or better) is available here: http://www.pagespy.com/blogstuff/drsexample.html (iow, imho, this is a very contrived and somewhat ridiculous assignment... for whatever that's worth.)
Elisa P
2008-01-17 05:51:25 UTC
Or else you may contact a C expert at website like http://askexpert.info/ to help you code your assignment.
Shell Answer Man
2008-01-17 03:05:56 UTC
Do your own darned homework.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...