prefix-operator

Why Does My Prefix Expression Evaluator Program Miscalculate Any Addition or Subtraction?

吃可爱长大的小学妹 提交于 2020-05-17 05:56:28
问题 I have a program that is meant to take in a Queue<String> that contains the elements of a prefix operation (i.e. an operation that is stated "+ 4 2" instead of "4 + 2", which is infix), then output the integer answer (just assume that a double is never needed, okay?). It works correctly for any multiplication or division operation, but fails in any addition or subtraction operation, and worse, in an inconsistent manner. For example, the output to "- 5 1" is 5, which would suggest it's adding

Javascript increment while assigning

别说谁变了你拦得住时间么 提交于 2020-01-02 03:55:07
问题 I was having a conversation about the prefix increment operator, and we seem to have run into a disagreement. When running this code: var x = 0; x = ++x; is the second line equivalent to: x = (x = x + 1) OR x = (x + 1) It is hard to tell the difference because the results are identical (both result in x having a value of 1) I believe that the value is not saved to the original variable when the left hand side of the assignment is the variable itself. My counterpart disagrees and thinks the

unsequenced modification and access to pointer

Deadly 提交于 2019-12-23 08:29:31
问题 I'm getting a warning for this C expression: *p0++ = mult(*p0, psign[i1]); The warning is: unsequenced modification and access to 'p0' [-Wunsequenced] I think the expression should be modified to this: *p0 = mult(*p0, psign[i1]); p0++; Is the behavior (after modification) going to be as intended? I would think that the pointer increment should happen after the value pointed to by p0 has been updated. 回答1: The snippet you have provided above invokes undefined behavior. According to C standard

compilation order and post prefix opertors

懵懂的女人 提交于 2019-12-22 05:06:34
问题 I was wondering why the following outputs 7 7 6 7 instead of 5 6 6 7 my $a = 5; printf("%d %d %d %d",$a,++$a , $a++ , $a); I'm pretty sure it has something to do with the order of parameters compilation Thanks, 回答1: Before I start, let me point out that one should generally avoid situations where one you both sets and reads a variable within an expression. First, let's look at operand evaluation order. This isn't defined for many operators, but it is defined for the list operator. It's

How to differentiate (when overloading) between prefix and postfix forms of operator++? (C++)

♀尐吖头ヾ 提交于 2019-12-17 12:35:11
问题 Because I've overloaded the operator++ for an iterator class template<typename T> typename list<T>::iterator& list<T>::iterator::operator++() { //stuff } But when I try to do list<int>::iterator IT; IT++; I get a warning about there being no postifx ++ , using prefix form. How can I specifically overload the prefix/postifx forms? 回答1: Write a version of the same operator overload, but give it a parameter of type int . You don't have to do anything with that parameter's value. If you're

c++ postfix / prefix operator overload as non-member function

只谈情不闲聊 提交于 2019-12-11 01:45:57
问题 I am writing my own array class as an exercise. Since, I read non-member functions are actually better in some ways than member functions. (Scott Meyers) I am trying to write as many operator overloads as non-member functions as possible. The operator overloads + , - all work out fine as non-member functions. my_array operator+(const my_array & left, const my_array & right); my_array operator-(const my_array & operand); my_array & operator++(); // prefix my_array operator++(int); //postfix,

Java: Prefix - Postfix issue

﹥>﹥吖頭↗ 提交于 2019-12-10 23:27:05
问题 I have a small issue performing a subtraction on numbers using prefix and postfix operators. This is my program: public class postfixprefix { public static void main (String args[]) { int a = 5; int b; b = (a++) - (++a); System.out.println("B = " +b); } } Doing this, theoretically I am supposed to get 0 as the answer, but however, I am getting a -2. When I try to individually try to increment the values like in this program: public class postfixprefix { public static void main (String args[])

Why chained prefix increment/decrement for builtin type is not UB for C++?

有些话、适合烂在心里 提交于 2019-12-08 16:31:50
问题 In cpprefernce.com example for prefix increment there is such code: int n1 = 1; ... int n3 = ++ ++n1; Why chained increment in this case does not lead to UB? Is rule for at most once modified not violated in this case? 回答1: In C++11 and later, UB occurs when there are two writes or a write and a read that are unsequenced and access the same memory location. But ++x is equivalent to x+=1 , so ++ ++n1 is equivalent to (n1+=1)+=1 , and here the reads and writes happen in a strict sequence

Javascript increment while assigning

℡╲_俬逩灬. 提交于 2019-12-05 07:50:53
I was having a conversation about the prefix increment operator, and we seem to have run into a disagreement. When running this code: var x = 0; x = ++x; is the second line equivalent to: x = (x = x + 1) OR x = (x + 1) It is hard to tell the difference because the results are identical (both result in x having a value of 1) I believe that the value is not saved to the original variable when the left hand side of the assignment is the variable itself. My counterpart disagrees and thinks the value is saved to the original variable whenever the ++ operator is used. Which one of us is right? It is

compilation order and post prefix opertors

淺唱寂寞╮ 提交于 2019-12-05 04:36:23
I was wondering why the following outputs 7 7 6 7 instead of 5 6 6 7 my $a = 5; printf("%d %d %d %d",$a,++$a , $a++ , $a); I'm pretty sure it has something to do with the order of parameters compilation Thanks, Before I start, let me point out that one should generally avoid situations where one you both sets and reads a variable within an expression. First, let's look at operand evaluation order. This isn't defined for many operators, but it is defined for the list operator. It's documented to evaluate its operands in left-to-right order [1] . That means that printf 's arguments are evaluated