开发者

calculating expression without using semicolon

Given the expression by input like 68+32 we have to evaluate without using a semicolon in our program. If it will be something inside the if or for loop? Reference : h开发者_StackOverflow社区ttps://www.spoj.pl/problems/EXPR2/


You can use if and the comma operator, something like this:

if( expr1, expr2, expr3, ... ) {}

It would be equivalent to

expr1;
expr2;
expr3;
...

To use variables without any warnings you can define a function the recieves the data types you need that you call from your main, like so:

void myFunc(int a, double b) {
    if ( expr1, expr2 ) { }
}
int main() {
    if ( myFunc(0, 0), 0 ) { }
}

Note that you need to add , 0 in main, otherwise an error is raised because a void return is not ignored.


You can use constructs like this:

if (i++) {}

However, that still doesn't answer how to declare variables. However, one trick you can do is:

#include <iostream>

int main(double x, double y) {
    if ((x = 1)) {}
    if ((y = 2)) {}
    if (std::cout << x << ", " << y << std::endl) {}
}

Sure, you'll get warnings for using the wrong types in the main argument list, but it'll work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜