问题描述
如何从z中减去x和y之和,然后在C ++中用单个语句递增它
我尝试过:
How to subtract sum of x and y from z and then increment it in a single statement in C++
What I have tried:
#include<iostream>
using namespace std;
int main()
{
int x,y,z,p;
x=5;
y=6;
z=20;
p=(z-(x+y))++;
cout<<"value of z="<<p;
}
显示分配的左操作数所需的错误值
it's showing error value required as left operand of assignment
推荐答案
我做过这个z = z-(x + y);但是可以在同一行增加它吗?????
I've done this z=z-(x+y); but is it possible to increment it in the same line?????
否你需要所谓的左值 - 因为++运算符是一个快捷方式操作,基本上说复制此变量,将其递增1,然后使用您记住的值。你只能预先修改一个变量(或减少一个变量),而不是一个结果。
基本上,你试图通过自动增量运算符来创造:别。这种方式存在一些非常令人讨厌的问题。请参阅此处: []
No. You need what is called an lvalue - because the ++ operator is a "shortcut" operation which basically says "copy this variable, increment it by one, then use the value you remembered". You can only pre-or post fix increment (or decrement) a variable, not an result.
Basically, you are trying to get creative with the auto increment operators: don't. That way lies some really nasty problems. See here: Why does x = ++x + x++ give me the wrong answer?[^]
z = z - (x + y); // Subtract sum of x and y from z; or z-= x + y
z = z + 1; // increment z; or z++
现在想想如何将它放入一行。
提示:从值中减去负数相当于添加数字的绝对值。
Think now how this can be put into one line.
Tip: Subtracting a negative number from a value is equivalent to adding the absolute value of the number.
这篇关于如何从另一个整数中减去两个整数之和然后递增呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!