[challenge] Da Impossibru Math

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2260 Views - Last Post: 20 October 2015 - 07:07 PM

#1 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

[challenge] Da Impossibru Math

Posted 17 October 2015 - 09:09 PM

So, if you want to kill a bit of time, have a go at this - rather easy - challenge!

Rules:

You shall not directly use:
- following unary operators: + - ! ~
- multiplicative operators: * / %
- additive operators: + -
- shift operators: >> <<
- relational operators: > < >= <=
- equality operators: == !=
- bitwise AND, XOR, OR operators: & ^ |
- logical AND, OR operators: && ||
- conditional operator: ?:
- compound assignment operators: *= /= %= += -= >>= <<= &= ^= |=
- unary prefix/postfix increment/decrement operators: -- ++
- alternative names for any of the above operators: and or xor...
- ...or their overloaded forms, which includes standard_library_object.operator==() ; )
- integer literals: decimal, octal, hexadecimal or binary
- floating point literals
- boolean literals: true false
- pointer literal: nullptr
- macros that are to be expanded/replaced to floating, integer or pointer literals/constant values, like RAND_MAX, NULL, etc
- preprocessor magic that will be expanded to any of the above
- if you wish to #include stuff - only standard C++ library headers are permitted. No GMP.

Otherwise, anything is fair game.

Data shall be provided on standard input in form:
<signed integer> <operator> <signed integer>
...

Where:
- signed integer will be a random signed integer in range <-100..100>;
- operator will be one of: +, -;
- operator will always be separated from integers with at least one whitespace;

The program should evaluate the math expression and return correct result.

Example input:
5 + 3
10 - 43
-10 - 2
5 + -15

Example output:
8
-33
-12
-10


You shall use this template:
#include <iostream>
#include <string>

// your optional code starts here:
// ...
// your optional code ends here


int main() {
    using namespace std;
    int n, m, result;
    string op;
    while (cin >> n >> op >> m) {
        // your code starts here:

        // your code ends here
        cout << result;
    }
    return 0;
}


Please post your solutions in [ spoiler ] tags.

I know you *can* do it. I'm curious though - how will *you* approach it?

This post has been edited by Xupicor: 19 October 2015 - 05:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [challenge] Da Impossibru Math

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2129
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 02:40 AM

Here be dragons.

Spoiler


Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 07:23 AM

@Salem_c: Can't accept that. Sorry. : P

them rules said:

You shall not directly use:
...
- following unary operators: + - ! ~
- macros that are to be converted to floating, integer or pointer literals/constant values, like RAND_MAX, NULL, etc
- equality operators: == !=
- relational operators: > < >= <=
- shift operators: >> <<
- integer literals: decimal, octal, hexadecimal or binary

Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2129
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 08:07 AM

I suppose inline assembler is out of the question as well then.
Was This Post Helpful? 0
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 08:14 AM

Well, only explicitly mentioned things are forbidden. Anything I did not mention is fair game. ; ) Of course I have made a working code conforming to my own rules, so it's definitely possible.

I'd give you a hint, but I have a feeling you really don't need one. ; P

This post has been edited by Xupicor: 18 October 2015 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,880
  • Joined: 05-May 12

Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 09:19 AM

Since you only mentioned the standard library, but not other libraries, then I could use a nuke to kill an ant hill by using GMP or MPIR. :)

And for people who have Visual Studio, I could use C++/CLI and use the .NET framework versions of the integer, instead of the native integer.

The requirements above only disqualified preprocessor magic, but said nothing about template metaprogramming.
Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 10:07 AM

Didn't I... oh, I didn't - well, I meant to add - use only what's available in standard C++ library. Since there aren't any attempts with GMP yet, I shall add that. ;)
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,880
  • Joined: 05-May 12

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 06:52 AM

My modification of Salem_c answer to get rid of the NULL literal.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3701
  • View blog
  • Posts: 13,378
  • Joined: 08-August 08

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 08:07 AM

Still using ==. Switch?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,880
  • Joined: 05-May 12

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 08:59 AM

I thought that the restriction were only for the actual math operations. I didn't see any restrictions on identifying the inputs.
Was This Post Helpful? 0
  • +
  • -

#11 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2129
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 10:35 AM

Even with CTphpnwb's switch/case, it still leaves the problem of the unary minus.
Was This Post Helpful? 0
  • +
  • -

#12 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 05:08 PM

View PostSkydiver, on 19 October 2015 - 06:59 PM, said:

I thought that the restriction were only for the actual math operations. I didn't see any restrictions on identifying the inputs.

It's not a restriction on any particular operation, be it math or inputs identifying. It's flat out restriction on contester provided code. ;)

This post has been edited by Xupicor: 19 October 2015 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,880
  • Joined: 05-May 12

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 05:20 PM

Ah. Interesting. So even using ++ on a STL container iterator would be forbidden. That makes things a lot tougher.
Was This Post Helpful? 0
  • +
  • -

#14 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 456
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 05:26 PM

Well, yes, the way the rules are - these mentioned operators are forbidden along with any overloaded version that is actually a function/member function call. That means - you can't type them in. (Neither ++iter nor iter.operator++())

This post has been edited by Xupicor: 19 October 2015 - 05:33 PM

Was This Post Helpful? 0
  • +
  • -

#15 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1739
  • View blog
  • Posts: 5,895
  • Joined: 03-August 09

Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 07:03 PM

Well I can just use all the things from <functional> like std::plus and std::negate and what not to do this. Is that cheating?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2