2012-09-18 67 views
-4

在我的程序中,我只是從傳感器讀取風向。我無法打印英文版本的方向。基本的算法是這樣的:如何根據傳感器讀數動態打印字符串

(值以度,從結構看)

string direction; (I know you have to create a char array, just not sure how) 

if(sensor.windir > 11 && sensor.windspeed < 34) 
{ 
direction = "NNE"; 
} 

    if(sensor.windir > 34 && sensor.windspeed < 57) 
{ 
direction = "NE"; 
} 



    ..... 


printf(" Current windir is %s\n", direction); 

我第C生鏽,需要對如何打印風向串進修根據其值範圍在「if」語句中定義。我的字符串中不需要超過3個字符。

+4

傳感器有一個屬性'windspeed',實際上是一個方向看起來......錯了! –

+0

解釋什麼不起作用以及您嘗試過什麼,並將您的windspeed變量重命名! –

+3

如果風速等於'34'會怎麼樣? –

回答

1

手頭您的問題,以下將這樣做:

char const * s = "[error]"; 

if (speed => 1 && speed < 13)  { s = "NW"; } 
else if (speed >= 13 && speed < 27) { s = "NE"; } 
else if (speed >= 27 && speed < 39) { s = "NS"; } 

printf("The direction is %s.\n", s); 

這隻適用於編譯時常量字符串文字。

如果您需要創建動態字符串,您需要創建一個char數組(如char buf[1024];),並使用類似snprintf的東西來填充字符串。

+0

非常好用,非常感謝! –

4

首先,你應該包括string.h使用的strcpy功能:

#include <string.h>

可以聲明char數組是這樣的:

char direction[4]; //4 char array (4th is the NULL string terminator) 

代替direction = "NE";direction = "NNE";你應該使用strcpy

strcpy(direction, "NE"); 
strcpy(direction, "NNE"); 

所以,你的程序將是這個樣子:

#include <string.h> 

char direction[4]; 

if(sensor.windspeed > 11 && sensor.windspeed < 34) 
{ 
    strcpy(direction, "NNE"); 
} 

if(sensor.windspeed > 34 && sensor.windspeed < 57) 
{ 
    strcpy(direction, "NE"); 
} 
printf("%s", direction); 

如果你想可能挽救一個字節的存儲,你可以動態地做到這一點:

#include <string.h> 

char *direction; 

if(sensor.windspeed > 11 && sensor.windspeed < 34) 
{ 
    if(!(direction = malloc(4))) //4 for NULL string terminator 
    { 
     /*allocation failed*/ 
    } 
    strcpy(direction, "NNE"); 
} 

if(sensor.windspeed > 34 && sensor.windspeed < 57) 
{ 
    if(!(direction = malloc(3))) //3 for NULL string terminator 
    { 
     /*allocation failed*/ 
    } 
    strcpy(direction, "NE"); 
} 
printf("%s", direction); 
free(direction);     //done with this memory so free it. 
+0

動態分配爲這個適度的問題增加了大量的精神開銷。一個固定的自動緩衝區會簡單得多,而且不易出錯...... –

1

您必須添加到您的代碼:

#include <string.h> 

...

char direction[4]; 

...

strcpy (direction, "NNE"); 
+0

'strcpy'在這種情況下不會導致緩衝區溢出。 –

+0

@Cheatah更好的是,如果你有它,請使用strlcpy。 – Scooter

1

這是很難理解你的要求,而你的邏輯在數學和地理上看起來並不準確。的先決條件似乎是:

  • 你必須從一個傳感器,0-360度的角度
  • 要打印此角度的方向,其中直東部是角度0.
  • compass的方向是(逆時鐘方向)E,ENE,NE,NNE,N等。總而言之,指南針上存在16個這樣的方向。因此我們需要將360度分成16個不同的方向。不幸的是,360/16 = 22.5,而不是偶數。
  • 由於360/16不是偶數,我們將不得不使用浮點類型,或者在CPU限制的低端嵌入式系統的情況下(最有可能是這種情況),將所有整數乘以10 。

如果上述假設是正確的,那麼你可以做這樣的事情:

const char* DIRECTION [16] = 
{ 
    "E", 
    "ENE", 
    "NE", 
    "NNE", 
    "N", 
    "NNW", 
    "NW", 
    "WNW", 
    "W", 
    "WSW", 
    "SW", 
    "SSW", 
    "S", 
    "SSE", 
    "SE", 
    "ESE" 
}; 

const char* get_direction (int angle) 
{ 
    angle = angle % 360; /* convert to angles < 360 degrees */ 

    /* Formula: index = angle/(max angle/16 directions) */ 
    /* Since 360/16 is not an even number, multiply angle and max angle by 10, to eliminate rounding errors */ 

    int index = angle*10/(3600/16); 

    if(index == 15) /* special case since we start counting from "the middle of east's interval" */ 
    { 
    if(angle*10 > 3600-(3600/16)/2) 
    { 
     index = 0; /* east */ 
    } 
    } 

    return DIRECTION [index]; 
} 

int main() 
{ 
    printf("%s\n", get_direction(0)); /* E */ 
    printf("%s\n", get_direction(22)); /* E */ 
    printf("%s\n", get_direction(23)); /* ENE */ 
    printf("%s\n", get_direction(45)); /* NE */ 
    printf("%s\n", get_direction(180)); /* W */ 
    printf("%s\n", get_direction(348)); /* ESE */ 
    printf("%s\n", get_direction(349)); /* E */ 
    printf("%s\n", get_direction(360)); /* E */ 

    getchar(); 
} 

其優點,相對於檢查每間隔,是執行時間是確定的,並且還將有比在一個巨大的開關情況下更少的分支預測。

請注意 float number會使代碼更具可讀性,如果您有該選項,應該使用它。但我認爲這是一款低端嵌入式系統,即8位或16位MCU應用程序。