计算表达式而不使用分号 [英] calculating expression without using semicolon

查看:135
本文介绍了计算表达式而不使用分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定输入的表达式,如68 + 32,我们必须在我们的程序中不使用分号进行求值。如果它将在if或for循环内部的东西?
参考: https://www.spoj.pl/problems/EXPR2/

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/

推荐答案

您可以使用if和 comma 运算符,如下所示:

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

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

这相当于

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 ) { }
}

请注意,您需要在main中添加,0 则会出现错误,因为不会忽略void返回。

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

这篇关于计算表达式而不使用分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆