String Arduino:拆分字符串并随机选择标记

String Arduino:拆分字符串并随机选择标记,string,split,arduino,token,strtok,String,Split,Arduino,Token,Strtok,我在Processing中构建了一个伪随机生成祈使句的程序。该程序结合随机选择的动词、所有格形容词和名词来显示最后一个句子。 以下是该计划的摘要版本: void sentence() { String VerbList = "abide accelerate accept accomplish achieve acquire acted etc.”; String[] Verbs = VerbList.split("\\s"); String PossessiveAdjectiveL

我在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();
    }
  }
}

在做了大量的研究之后,我找到了我的问题的一个像样的答案。 此代码将动词、名词和所有格形容词字符串拆分为标记,然后基于随机整数从每个字符串中选择一个标记。这些标记被加在一起形成祈使句。最后一句话显示在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卡上存储一个单词库,并进一步简化代码。如有任何建议,将不胜感激