Here's a question from the last year's first "Intro to programming" exam at my uni:
Using the
getchar()
function read an input sequence consisting of numbers, + and - signs. The output should be the result of those arithmetical operations.
For example, if the input is 10+13-12+25-5+100
, the output should be 131
.
Now, given that I have a little bit of C experience before attending uni, this problem seems easy to solve using pointers, arrays, etc.
But here's the catch: on the exam you can only use things that the students were taught so far. And given that this exam is only like a month after the start of the school year, your options are fairly limited.
You can only use variables, basic input/output stuff, operators (logical and bitwise), conditional statements and loops, functions.
That means no: arrays, strings, pointers, recursion, structures, or basically any other stuff that makes this easy.
How in the hell do I do this? Today is the second time I've spent 3 hours trying to solve this. I have solved it successfully, but only after "cheating" and using arrays, string functions (strtol
), and pointers. It's important for me to know how to solve it by the rules, as I'll have similar stuff on the upcoming exam.
Edit: my attempts so far have amounted to using the while loop combined with getchar()
for input, after which I just get stuck. I don't have the slightest idea of what I should do without using more "tools".