-3

私のプログラムでは、センサーから風向を読み取るだけです。指示の英語版を印刷するのに問題があります。基本的なアルゴリズムは次のとおりです。

(値は度単位で、構造体から読み取られます)

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

4 に答える 4

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);

1バイトのメモリを節約したい場合は、動的に行うことができます。

#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.
于 2012-09-18T13:08:58.527 に答える
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して文字列を入力する必要があります。

于 2012-09-18T13:20:18.233 に答える
1

コードに追加する必要があります:

#include <string.h>

..。

char direction[4];

..。

strcpy (direction, "NNE");
于 2012-09-18T13:21:17.430 に答える
1

あなたが何を求めているのかを理解するのは難しく、あなたの論理は数学的に/地理的に正確ではないようです。前提条件は次のようです。

  • センサーから読み取った角度は0〜360度です。
  • この角度の方向を印刷します。ここで、真東は角度0です。
  • コンパスの方向は、(反時計回りに)E、ENE、NE、NNE、Nなどです。
  • 全体として、16のそのような方向がコンパスに存在します。したがって、360度を16の異なる方向に分割する必要があります。残念ながら、360/16 = 22.5であり、偶数ではありません。
  • 360/16は偶数ではないため、floatタイプを使用するか、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番号を使用すると、コードがはるかに読みやすくなるため、そのオプションがある場合は使用する必要があることに注意してください。しかし、これはローエンドの組み込みシステム、つまり8ビットまたは16ビットのMCUアプリケーションであると想定しています。

于 2012-09-18T14:49:20.167 に答える