Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does anyone know how to do this c++ problem
#20
Keane Wrote:Hire a programmer Smile
way to necro a topic Dodgy

.kevlar Wrote:An ISP has 3 different subscription packages...
I like how no one posted any code. Tongue

Here's something I put together in 15 mins:

Code:
#include <stdio.h>
#include <math.h>

double PackageA(int hours)
{
   double cost = 19.95;

   // 5-20 hours @ 0.75 extra
   cost += fmax(0, (fmin(hours, 20) - 5) * 0.75);

   // 20+ hours @ 1.00 extra
   cost += fmax(0, (hours - 20) * 1.00);

   return cost;
}

double PackageB(int hours)
{
   double cost = 24.95;

   // 15-25 hours @ 0.75 extra
   cost += fmax(0, (fmin(hours, 25) - 15) * 0.75);

   // 25+ hours @ 0.50 extra
   cost += fmax(0, (hours - 25) * 0.50);

   return cost;
}

double PackageC(int hours)
{
   return 29.75;
}

int main()
{
   // repeat forever!
   while (1)
   {
       printf("How many hours?\n");
       int hours;
       scanf("%i", &hours);

       double A_Total = PackageA(hours);
       double B_Total = PackageB(hours);
       double C_Total = PackageC(hours);

       char package;
       double cost;

       // repeat until valid package
       while(1)
       {
           printf("What package: A, B, or C?\n");
           scanf(" %c", &package);
           package = toupper(package);

           if (package == 'A')
           {
               cost = A_Total;
           }
           else if (package == 'B')
           {
               cost = B_Total;
           }
           else if (package == 'C')
           {
               cost = C_Total;
           }
           else
           {
               printf("invalid package...\n");
               continue;
           }

           break;
       }

       printf("----------------\n");
       printf("Package %c; Hours %i; Cost %.2f\n", package, hours, cost);

       // determine cheapest package
       if (A_Total >= B_Total)
       {
           if (B_Total >= C_Total)
           {
               printf("Cheapest Package: C\n");
           }
           else
           {
               printf("Cheapest Package: B\n");
           }
       }
       else if (A_Total >= C_Total)
       {
           printf("Cheapest Package: C\n");
       }
       else
       {
           printf("Cheapest Package: A\n");
       }

       printf("----------------\n");
   }

   return 0;
}
Steam Wrote: 4:02 PM - George, of the jungle: was out
4:02 PM - George, of the jungle: bison, dude
4:02 PM - Brawl Bashin’ Bison: ???
4:02 PM - George, of the jungle: you're very rude towards alina
4:02 PM - George, of the jungle: how about unbanning her friend?
4:02 PM - George, of the jungle: I mean
4:02 PM - George, of the jungle: it's only gamebanana skins
4:02 PM - Brawl Bashin’ Bison: LOL
4:02 PM - George, of the jungle: ^^
4:02 PM - Brawl Bashin’ Bison: LOLOL
4:02 PM - George, of the jungle: lol

Messages In This Thread
RE: Does anyone know how to do this c++ problem - by M. Bison - May 05 2015, 03:37 AM

Users browsing this thread: 5 Guest(s)