Thread: Nice use for the conditonal operaror

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    Nice use for the conditonal operaror

    Hi I came across a nice use for conditional operator. I always hated dealing text for "You have 1 units" or "You have 2 units" - ie, dealing with plural for single items.

    Code:
    int main(void)
    {
      const double unit_price = 3.50;   /* unit price in euro */
      const double discount1 = 0.05;    /* discount for more than 10 */
      const double discount2 = 0.10;    /* discount for more than 20 */
      const double discount3 = 0.15;    /* discount for more than 50 */
      double total_price = 0.0;
      int quantity = 0;
    
      printf("\nWelcome to the shop. Basic unit costs %.2f. Discounts available.",unit_price);
      printf("\n5%% off on more than 10. 10%% off on more than 20. 15%% off on more than 50!");
      printf("\nEnter the number of units you want to buy: ");
      scanf("%d",&quantity);
    
      total_price = quantity*unit_price*(1.0 -
                      (quantity>50 ? discount3 : (
                         quantity>20 ? discount2 : (
                           quantity>10 ? discount1 : 0.0))));
    
      printf("The price for %d unit%s is %.2f euro.\n", quantity, quantity>1 ? "s" : "", total_price);
      /* quantity>1 check shows how to deal single or plural units */
      return 0;
    }
    The code segment:
    Code:
    quantity, quantity>1 ? "s" : ""
    is pretty cool. It's one in my bookmarks. I love that! Going back I always neglected the shorthand conditional op.

    Welcome to the shop. Basic unit costs 3.50. Discounts available.
    5% off on more than 10. 10% off on more than 20. 15% off on more than 50!
    Enter the number of units you want to buy: 1
    The price for 1 unit is 3.50 euro.
    Last edited by hamsteroid; 04-01-2007 at 03:39 AM. Reason: showing the output part.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in printf it increases readability...
    but in total_price calculations - I prefer if/else for readability...
    Code:
    coeff = 1.0;
    if(quantity>50 ) coeff -= discount3 ;
    else if (quantity>20 ) coeff -= discount2;
    else if (quantity>10 ) coeff -= discount1;
    
    total_price = quantity*unit_price*coeff;
    for me is a lot more easier to understand than the original code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Actually I agree with you vart. Though I do like the single/plural trick.

    Code:
    quantity>1 ? "s" : ""
    The nested discount example above in the main code reminds me of that cursed nested DECODE statement in PLSQL where I come across it a lot - it's usually subnested to the high heavens!

    The simple plural example at the end is nice though I must admit.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Keep in mind that 0 should also be plural, but your check is for >1. 1 is the only case in which it is not plural.

    0 dollars
    1 dollar
    2 dollars
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also bear in mind, that if you come to translate this code into other languages, a simple search for all strings isn't going to work so well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by Salem View Post
    Also bear in mind, that if you come to translate this code into other languages, a simple search for all strings isn't going to work so well.
    That is problem is close to heart sometimes, I'm dealing with 2-3 byte Kanji characters in some rather sticky invoices of late

  7. #7
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by itsme86 View Post
    Keep in mind that 0 should also be plural, but your check is for >1. 1 is the only case in which it is not plural.

    0 dollars
    1 dollar
    2 dollars
    Thanks! I want to hire you as my code tester!

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    as well as 21 dollar should be singular
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    euros vs euro..... in Euroland - most folk say "5 euro" and a few say "5 euros". All I notice is that I get less for my euro!

Popular pages Recent additions subscribe to a feed