Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 : https://www.spoj.pl/problems/EXPR2/

like image 489
d3vdpro Avatar asked May 15 '26 13:05

d3vdpro


2 Answers

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.

like image 66
Juan Avatar answered May 18 '26 03:05

Juan


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.

like image 45
Joey Adams Avatar answered May 18 '26 03:05

Joey Adams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!