0

我有一个包含字符串值的二维数组。我在迭代时使用枚举来跟踪数组的第一个索引。我试图找到给定字符串匹配的枚举(数组的第一维)。现在,我迭代数组和检查值的方式似乎并不是 100% 有效,因为它会返回错误的枚举。有谁看到我做错了什么,或者知道基于第二个匹配字符串获取第一个数组索引的更好方法?

注意:我使用的是 Arduino,并且使用的是字符串对象而不是 char*。

    enum conditionType {
      CLEAR = 0,
      OVERCAST,
      CLOUDY,
      RAIN,
      THUNDERSTORM,
      SNOW
    };

    int conditionsIndex[6] = { 
      CLEAR, OVERCAST, CLOUDY, RAIN, THUNDERSTORM, SNOW}; 

    const char *conditions[][20] = {
      // CLEAR
      {
        "Clear"          }
      ,
      // OVERCAST
      {
        "Partly Cloudy"  }
      ,
      // CLOUDY
      { 
        "Shallow Fog",
        "Partial Fog",
        "Mostly Cloudy",
        "Fog","Overcast",
        "Scattered Clouds" }
      ,
      // RAIN
      {
        "Drizzle",
        "Rain",
        "Hail",
        "Mist",
        "Freezing Drizzle",
        "Patches of Fog",
        "Rain Mist",
        "Rain Showers",
        "Unknown Precipitation",
        "Unknown",
        "Low Drifting Widespread Dust",

        "Low Drifting Sand"          }
      ,
      // THUNDERSTORM
      {
        "Thunderstorm",
        "Thunderstorms and Rain",
        "Thunderstorms and Snow",
        "Thunderstorms and Ice Pellets",
        "Thunderstorms with Hail",
        "Thunderstorms with Small Hail",
        "Blowing Widespread Dust",
        "Blowing Sand",
        "Small Hail",
        "Squalls",
        "Funnel Cloud"          }
      ,
      // SNOW
      {
        "Volcanic Ash",
        "Widespread Dust",
        "Sand",
        "Haze",
        "Spray",
        "Dust Whirls",
        "Sandstorm",
        "Freezing Rain",
        "Freezing Fog",
        "Blowing Snow",    
        "Snow Showers",
        "Snow Blowing Snow Mist",
        "Ice Pellet Showers",
        "Hail Showers",
        "Small Hail Showers",
        "Snow",
        "Snow Grains",
        "Low Drifting Snow",
        "Ice Crystals",
        "Ice Pellets"          }
    };




      int currentCondition;
      for ( int i = 0; i < ( sizeof(conditionsIndex) / sizeof(int) ); i++ ) {
         int idx = conditionsIndex[i];
         for (int j = 0; j < ( sizeof(conditions[idx]) / sizeof(String) ); j++ ) {
         if ( forecast.equals(conditions[idx][j]) ) {
            currentCondition = idx;
           }
         }
       }
4

2 回答 2

0

您的代码的另一个问题是您很可能会用完 ram。你可能想看看程序。

于 2013-09-17T18:29:43.433 回答
0

首先,条件是一个 6 x 20 的字符串指针矩阵。某些行,例如 cloudy 仅分配了一个指针(共 20 个)。所以循环需要测试一个空指针并转到下一行。

不会写你的代码,但在伪代码中应该是这样的:

 memfill(conditions, 0, 6*20*sizeof(char *));
 for (i = 0 to 5)

   for (j = 0 to 20)

       if (conditions[i][j] == NULL) {break;}

       print("%s/n", conditions[i][j]);
       // test/compare strings, i is the enum value if a match
       // return i;

   end for j

end for i
于 2013-09-17T02:39:05.183 回答