2013-03-17 53 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

相關問題