2013-03-17 55 views
0

我一直在寻找一些RobotC代码,它非常类似于C(并且我没有足够的信誉来制作新的RobotC标签),并且我遇到了 * =操作符。我已经使用了相当多的代码,但我只能得到它在C语言中是一个按位运算符。似乎没有人确切地说明它的作用,但是如果你们能够提供帮助,我会很感激。“* =”在C编程中究竟意味着什么?

rot *= 5; 

这里是我找到它的代码。所有功能的作用是重新定向机器人始终面向北方。

//Turns back to North 
void TurnStraight(int cdegree) //cdegree is the sensor value read by the compass sensor 
{ 
    int rot = cdegree % 360; 
    int mot = 1; 
    //stop when the NXT facing North 
    if (cdegree == 0){ 
    return; 
    } 
    //reset the encoders value to avoid overflaow 
    clear_motor_encoders(); 

    if (cdegree > 180 && cdegree < 360){ 
     rot = 360 - rot; 
     mot = 0; 
    } 

    rot *= 5; // ratio between the circumference of the tire to the circumference of the  rotation circle around itself 
    switch (mot){ 
    case 1: 
    moveTo(rot/2,1); 
    break; 
    case 0: 
    moveTo(rot/2,-1); 
    break; 
    case -1: 
    moveTo(rot,1); 
    break; 
    } 
} 


void clear_motor_encoders() 
{ 
    nMotorEncoder[motorA] = 0; 
} 

void moveTo(int rot, int direction) 
{ 
    nSyncedMotors = synchAC; 
    nSyncedTurnRatio = -100; 
    nMotorEncoderTarget[motorA] = rot; 
    motor[motorA] = direction * 50; 
    while (nMotorRunState[motorA] != runStateIdle) ; 
    motor[motorA] = 0; 

} 

这不是我的代码当然,我只是想知道它是如何工作的。

+7

乘以它可能很难谷歌搜索对于'* =',但是“C运营商”可以很容易地让你[维基百科](http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Compound_assignment_operators) – 2013-03-17 01:22:46

+1

查看Steve Summit的[“Introduc tory C programming“](http://www.eskimo.com/~scs/cclass/cclass.html),过时但非常相关。你开始问,为什么你的程序崩溃是由于明显的指针问题之前,请阅读并_understand_特德Jensen的[“关于C指针和数组的指南”(http://pw1.netcom.com/~tjensen/ptr/pointers。 HTM)(也相当过时,但必不可少)。 – vonbrand 2013-03-17 03:35:26

回答

8

它等同于:

rot = rot * 5; 

这是一个家族经营的所谓“复合赋值”运营商的一部分。您可以在这里看到它们的完整列表:Compound Assignment Operators (Wikipedia)

请注意,*=不是一个按位运算符,因为*不是。但是一些复合运算符是按位进行的 - 例如,&=运算符是按位进行的,因为&是。

+0

@dasblinkenlight哦哇从来没有发生过,你可以使用* =就像+ =谢谢! – 2013-03-17 01:34:07

2

与大多数编程语言一样,这是var = var * 5的简写形式。

所以其他例子var += 3等于var = var + 3的陈述。

2

这是乘法赋值运算符。这意味着同样的事情

rot = rot * 5; 

这不是位运算符,虽然有同类位运算符:

  • &= - 和分配,
  • |= - 或分配,
  • ^= - xor-assign。

家庭的其他运营商包括+=-=/=%=

1

如果你理解的代码

rot += 5; 

你应该明白

rot *= 5; 

而不是增加5腐烂的,你是5

相关问题