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

如果你想潜在地节省一个字节的内存,你可以动态地做到这一点:

#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

很难理解你在问什么,而且你的逻辑在数学/地理上似乎并不准确。先决条件似乎是:

  • 你有一个从传感器读取的角度,0-360 度。
  • 你想打印这个角度的方向,直东是角度 0。
  • 指南针的方向是(逆时针)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();
}

与检查每个间隔相比,这样做的优点是执行时间是确定的,并且与巨大的 switch-case 相比,分支预测更少。

请注意,浮点数将使代码更具可读性,如果您有该选项,则应使用该选项。但我假设这是一个低端嵌入式系统,即 8 位或 16 位 MCU 应用程序。

于 2012-09-18T14:49:20.167 回答
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 回答