The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

Yet another programming question by Schol-R-LEA;22001-07-31 03:21:51
  Tertiary Operators!!! by beez2001-07-31 17:39:18
    These are fun! by BoisePaul 2001-07-31 19:19:23
Here's one: Explain what this does. The prompts are taken out so that this isn't too obvious. BTW, this is C++, not PHP just in case you didn't pick up on that.

#include<iostream>

int calc(int a, int b, char op) {
return op == '+' ? a + b : ( op == '-' ? a - b : ( op == '*' ? a * b :op == '/' ? a / b : a % b ));
}

int main () {
int a, b;
char op;
cin >> a >> op >> b;
cout << calc(a, b, op) << endl;
}

If you want to compile this for some awful reason, and for another awful reason you're using VC++, throw using namespace std; in there before the I/O. I use GNU g++ 2.95.3 on AIX 4.3.3, so I get away without all of the requirements of the standard.

It's actually kinda useful, I do really use this on a regular basis. And yes, I know this could have been all done in main, but that takes the fun of a one line function away. Just something I did a few months ago to demonstrate ?: and it's ability to be nested, though much more gets a little unreadable.

BPM
[ Reply ]
      That's easy (warning, spoilers) by Schol-R-LEA;22001-07-31 20:12:07
        I forgot to mention that by Schol-R-LEA;22001-08-10 10:27:02
          ARRRGH! Double faulting again! by Schol-R-LEA;22001-08-10 11:32:55

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)