【问题标题】:Arduino: Splitting a string AND randomly selecting tokensArduino:拆分字符串并随机选择标记
【发布时间】:2015-03-14 19:51:22
【问题描述】:

我在 Processing 中构建了一个程序,它可以伪随机地生成命令式句子。该程序结合了随机选择的动词、所有格形容词和名词,以显示最后一个句子。 这是该程序的摘要版本:

void sentence() {
  String VerbList = "abide accelerate accept accomplish achieve acquire acted etc.”;
  String[] Verbs = VerbList.split("\\s");
  String PossessiveAdjectiveList =  "my your his her its our their";
  String [] PossessiveAdjectives = PossessiveAdjectiveList.split("\\s");
  String NounList = "account achiever acoustics act action activity actor etc.”;
  String[] Nouns = NounList.split("\\s");

  int verb = int(random(Verbs.length));
  int possessiveAdjective = int(random(PossessiveAdjectives.length));
  int noun = int(random(Nouns.length));

  String Sentence = Verbs[verb]+" "+PossessiveAdjectives[possessiveAdjective]+" "+Nouns[noun];
  println(Sentence);

将代码移至 Arduino IDE 后,我立即发现缺少 string.split 函数。我知道我可以使用 strtok 将字符串转换为标记;但是,我不确定如何通过随机生成的整数来选择单个标记。我应该尝试使用 strtok 吗?到目前为止,这是我的代码:

#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define WHITE 0x7

void setup() {
  Serial.begin(9600);
  lcd.setBacklight(WHITE);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  sentence();
}

void sentence() {
  char VerbList[] = "abide accelerate accept accomplish achieve acquire acted etc.";
  char* Verbs = strtok(VerbList, " ");
  char PossessiveAdjectiveList[] =  "my your his her its our their";
  char* PossessiveAdjectives = strtok(PossessiveAdjectiveList, " ");
  char NounList[] = "account achiever acoustics act action activity actor etc.";
  char* Nouns = strtok(NounList, " ");
  //int verb = int(random(Verbs.length));
  //int verb = Verbs.substring(random(Verbs.length));
  //int possessiveAdjective = int(random(PossessiveAdjectives.length));
  //int noun = int(random(Nouns.length));
  //String Sentence = Verbs[verb]+" "+PossessiveAdjectives[possessiveAdjective]+" "+Nouns[noun];
  //lcd.print(Sentence);
}

uint8_t i=0;
void loop() {
  uint8_t buttons = lcd.readButtons();
  if (buttons) {
    lcd.clear();
    lcd.setCursor(0,0);
    if (buttons & BUTTON_SELECT) {
      sentence();
    }
  }
}

【问题讨论】:

    标签: string split arduino token strtok


    【解决方案1】:

    经过大量研究,我找到了解决问题的好方法。 此代码将动词、名词和所有格形容词字符串拆分为标记,然后根据随机整数从每个字符串中选择一个标记。然后将这些标记加起来形成一个祈使句。最后一句显示在 16X2 字符 LCD 屏上。

    #include <Wire.h>
    #include <Adafruit_MCP23017.h>
    #include <Adafruit_RGBLCDShield.h>
    Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
    #define MAX_STRING_LEN  1000
    
    char *Verbs = "abide accelerate accept accomplish achieve";
    char *PossessiveAdjectives = "my your his her its our their";
    char *Nouns = "account achiever acoustics act action activity";
    char *p, *i;
    
    void setup() {
      Serial.begin(9600);
      lcd.begin(16, 2);
    
      int verbCount = 0,vc;
      int adjectiveCount = 0,ac;
      int nounCount = 0,nc;
    
      for(vc=0;vc<strlen(Verbs);vc++){
        if(Verbs[vc] == ' ')
          verbCount++;
      }
      for(ac=0;ac<strlen(PossessiveAdjectives);ac++){
        if(PossessiveAdjectives[ac] == ' ')
          adjectiveCount++;
      }
      for(nc=0;nc<strlen(Nouns);nc++){
        if(Nouns[nc] == ' ')
          nounCount++;
      }
    
      int randVerb = random(1,verbCount+2);
      int randPossessiveAdjective = random(1,adjectiveCount+2);
      int randNoun = random(1,nounCount+2);
    
      String Verb = subStr(Verbs, " ", randVerb);
      String PossessiveAdjective = subStr(PossessiveAdjectives, " ", randPossessiveAdjective);
      String Noun = subStr(Nouns, " ", randNoun);
    
      String ImperativeSentence = Verb+" "+PossessiveAdjective+" "+Noun;
    
      if(Verb.length()+PossessiveAdjective.length()+Noun.length()+2 > 16) {
        if(Verb.length()+PossessiveAdjective.length()+1 > 16) {
          if(Verb.length() > 16) {
            setup;
          } 
          else {
            lcd.print(Verb);
            lcd.setCursor(0, 1);
            lcd.print(PossessiveAdjective);
            lcd.print(" ");
            lcd.print(Noun);
          }
        }
        else {
          lcd.print(Verb);
          lcd.print(" ");
          lcd.print(PossessiveAdjective);
          lcd.setCursor(0, 1);
          lcd.print(Noun);
        }
      }
      else {
        lcd.setCursor(0, 0);
        lcd.print(ImperativeSentence);
      }
    
      Serial.println(ImperativeSentence);
    }
    
    uint8_t a=0;
    void loop() {
      uint8_t buttons = lcd.readButtons();
      if (buttons) {
        lcd.clear();
        lcd.setCursor(0,0);
        if (buttons & BUTTON_SELECT) {
          setup();
        }
      }
    }
    
    char* subStr (char* str, char *delim, int index) {
      char *act, *sub, *ptr;
      static char copy[MAX_STRING_LEN];
      int i;
    
      strcpy(copy, str);
    
      for (i = 1, act = copy; i <= index; i++, act = NULL) {
        sub = strtok_r(act, delim, &ptr);
        if (sub == NULL) break;
      }
      return sub;
    
    }
    

    这显然是一项正在进行的工作。我仍然需要在 SD 卡上存储一个字库,并进一步简化代码。任何建议将不胜感激。

    【讨论】:

      猜你喜欢
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多