0

I have been doing exercises on some online judges, and I encounter this question with this default answer.

Question description:
Finally, Hansbug has finally reached the moment to do the last math problem, and there are a bunch of messy addition and subtraction equations in front of him. Obviously success is at hand. But his brain cells have been exhausted, so this important task is left to you.

Input format:
One line, containing a string of addition and subtraction polynomials (the range of each item is 0-32767).

Output format:
An integer, which is the result of the calculation (guarantee that the result of the calculation will not exceed the range of the long integer).

Input and output sample:

Enter #1: 
1+2-3
Output #1: 
0

And the default answer is :

#include<bits/stdc++.h>

using namespace std;
int ans;
int c;

int main() {
    while (cin >> c)
        ans += c;
    cout << ans;
    return 0;
}

How is this even possible!?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
knlao
  • 3
  • 2
  • Can you share the link of the problem on the online judge – risingStark Jun 27 '21 at 10:26
  • 3
    You just need to write a smart parser, of course it is possible. – Silidrone Jun 27 '21 at 10:27
  • 3
    Don't use so-called "competetive" sites as a way to learn programming in general, or specific languages. That's not what they're for. Instead read [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take classes. – Some programmer dude Jun 27 '21 at 10:31
  • The question expects you to take the input expression as a string input and then parse the input to evaluate the expression. – kiner_shah Jun 27 '21 at 10:34
  • 1
    The default answer shown is completely wrong for the question asked, and will not produce the correct result. – Remy Lebeau Jun 27 '21 at 10:35
  • This is the link: https://www.luogu.com.cn/problem/P2788 but it's Chinese... – knlao Jun 27 '21 at 10:36
  • 1
    Learn to debug using things like this: ```while (cin >> c) { ans += c; cout << "adding: " << c << endl; }``` and it will become clear what that code is doing (it is parsing "-3" as an integer and then adding it). – Andy Newman Jun 27 '21 at 10:41
  • By **competitive**, we can deduce that the online judges want to challenge peoples. Here the default answer would be the simpler readable possible answer and that answer do assume that the input is a string is properly formatted. In production code, one would probably want code with better error handling and ability to handle more complex input like `1 + -2`. – Phil1970 Jun 27 '21 at 12:44

1 Answers1

1

All right, let's try it, let's put there some expression with addition and subtraction of ints only, after that press Return, then ctrl+D (end of input):

$ ./a.out 
111-222+1
-110$

The loop while (cin >> c) will parse integers including the sign one by one using iostream capabilities until an end of input (you also have to terminate the last number by pressing Return, effectively putting the newline there and triggering the last cin >> c).

Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28