Code contest 2

Pages: 12
This time with a little changed rules (IMHO, I tried to make it much more clear).

The aim is to write a program that behaves as a simple expression calculator.
Set of supported operations: +, -, *, /, ^. It must support a correct order of operators, and grouping subexpressions with brackets (). If incorrect expression is given, it should print "error". It must not support anything besides this.

Examples (input = output):
1 + 2 = 3
1 + 2 * 3 = 7
(1.5 + 1.5) * 3 = 9
2 + ( = error

The criteria are simple: who creates the shortest program for the requirements wins. Comments and whitespace don't count. You can use whatever language you wish (this is a Lounge section), but you must not use any external, non-standard libraries.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;

string cinall(){
    string ret = "";
    char buf = cin.get();
    while (buf != '\n'){
        ret += buf
        buf = cin.get();
   }
    return ret.substr(0,ret.size()-1);
}

int main(){
    string input = cinall();
    cout << input;
    if (input == "1 + 2")
      cout <<" = 3";
    else if (input == "1 + 2 * 3")
      cout << " = 7";
    else if (input == "(1.5 + 1.5) * 3")
      cout << " = 9";
    else
      cout << " = error";
}


edit: fixed error =>
Last edited on
Does it have to loop?
Is "+2" a valid expression?
Should "5/0" print "error"?
Do you intent to evaluate programs by counting the number of symbols used?

Also, I feel that this would measure not the skill of programmer or the expressiveness of the language, but the size of the standard library. In Ruby puts eval gets would be pretty close to the solution and I'm sure this isn't the kind of solution you were expecting (To be honest I have no idea whether "eval" counts as part of the language or the standard library. It's kind of hard to tell sometimes..).

Of course, if there was no restriction on the use of libraries, I'd write one myself for this task and if you couldn't use any libraries other than io, then .. I'm not sure what would happen. Ruby would either remain unchanged or become totally useless.

Perhaps with some effort we could adopt better restrictions. I don't have any clue what that could be though.. If we don't manage to do that, we should stick with a single language. If you don't want that either, I'll just write my own language with a keyword "e" which performs the task and win.

@ultifinitus
lol. Did you test that? Line 8 will probably segfault causes an assertion failure and you have several =s instead of ==s..
Last edited on
@THE hamsterman: haha no i did not, I just typed it up here... had assumed my fingers wouldn't let me down...
edit: fixed error =>
Doesn't fix the assertion failure.. Why not just getline instead of that mess?
Will not post code until problem description is clarified and OP admits that this is just another way to spark a language debate. :)

-Albatross
It is not a way to spark a language debate. It is rather to spark a compiler/interpreter construction debate. The solution with eval does not count, because it doesn't conform to the "It must not support anything besides this" rule - which was added just to rule out all the "my language has a function that does that" solutions, which would not be interesting at all. Anyway, using "eval" function in a production code to evaluate something given by the user, is an extremely risky idea, everyone from the QA would consider a vulnerability.


Should "5/0" print "error"?

5/0 should return +Infinity, but can also return error. I don't mind. Whatever is easier/shorter to code. All numbers should be treated as floating point.


Is "+2" a valid expression?


"+2" - it is correct, because the "+" is a part of a number. However there is no need to support unary + and -, so this can yield error: "-(2+3)".


Does it have to loop?

Yes.

The length of the code will be counted by counting symbols (lexems) used. I think it is a good measure, because it doesn't force anyone to shorten variable and funciton names, rendering the code unreadable.

@ultifinitus: nice try, but these were only examples, not specification. So your code obviously doesn't conform.

Last edited on
My point was not that eval solves the problem, but that you don't "rule out all the "my language has a function that does that" solutions" well enough (I'm not suggesting that using eval is a good idea either).
The length of the code will be counted by counting symbols (lexems) used. I think it is a good measure, because it doesn't force anyone to shorten variable and funciton names, rendering the code unreadable.

So the code can be as long and complex as we want, provided we keep the number of symbols we define to a minimum?

So just to clarify:
1) using the conditional operator in place of if statements will not be considered to make the program shorter
2) reusing the same old variable rather than creating a new one will make the code shorter
3) reducing the number of variable names used by any other means no matter how contrived and complicated would result in the code being considered shorter?

Thanks,
-Xander314
This sounds fun. I'm going to try it in Haskell.

So the code can be as long and complex as we want, provided we keep the number of symbols we define to a minimum?


No, the total number of lexems (words), not only defined but also used:

int myVariable = 5;

counts as 5 lexems: "int", "myVariable", "=", "5", ";".




Last edited on
Oh, I see. Thanks for the clarification :) So basically we're talking word count.

I hope to have a go at this, but it depends, as usual, on how much work my upcoming exams throw at me :(
How many lexems are here: 'm/^[\d \+\-\*\/\(\)\.]+$/' ?
I would guess that was just one, since it is simply a string.
Let's count it as one - a string literal.
Last edited on
And without the quotes?
Without the quotes. Just like 2.5678e+54 is also a single literal.
Does it have to use infix notation or is reverse Polish notation ok?
Infix, as in the examples.
Ok. I thought RPN was better but actually this way I can do it really quickly.
Pages: 12