# [challenge] Da Impossibru Math

• (2 Pages)
• 1
• 2

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

### #1 Xupicor

• Nasal Demon

Reputation: 457
• 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: * / %
- 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) {

cout << result;
}
return 0;
}
```

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

• void main'ers are DOOMED

Reputation: 2170
• Posts: 4,236
• Joined: 30-May 10

## Re: [challenge] Da Impossibru Math

Posted 18 October 2015 - 02:40 AM

Here be dragons.

Spoiler

Spoiler

### #3 Xupicor

• Nasal Demon

Reputation: 457
• 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

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2170
• Posts: 4,236
• 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.

### #5 Xupicor

• Nasal Demon

Reputation: 457
• 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

### #6 Skydiver

• Code herder

Reputation: 6293
• Posts: 21,632
• 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.

### #7 Xupicor

• Nasal Demon

Reputation: 457
• 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.

### #8 Skydiver

• Code herder

Reputation: 6293
• Posts: 21,632
• 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

### #9 CTphpnwb

• D.I.C Lover

Reputation: 3796
• Posts: 13,742
• Joined: 08-August 08

## Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 08:07 AM

Still using ==. Switch?

### #10 Skydiver

• Code herder

Reputation: 6293
• Posts: 21,632
• 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.

### #11 Salem_c

• void main'ers are DOOMED

Reputation: 2170
• Posts: 4,236
• 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.

### #12 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: [challenge] Da Impossibru Math

Posted 19 October 2015 - 05:08 PM

Skydiver, 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

### #13 Skydiver

• Code herder

Reputation: 6293
• Posts: 21,632
• 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.

### #14 Xupicor

• Nasal Demon

Reputation: 457
• 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

### #15 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• 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?