Arduino:拆分字符串并随机选择标记
Arduino: Splitting a string AND randomly selecting tokens
我在 Processing 中构建了一个伪随机生成祈使句的程序。该程序将随机 selected 动词、所有格形容词和名词组合在一起,以显示最后一句话。
这是程序的摘要版本:
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 将字符串转换为标记;但是,我不确定如何通过随机生成的整数 select 单个标记。我应该尝试使用 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字符的液晶屏上。
#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卡上存一个词库,进一步简化代码。任何建议将不胜感激。
我在 Processing 中构建了一个伪随机生成祈使句的程序。该程序将随机 selected 动词、所有格形容词和名词组合在一起,以显示最后一句话。 这是程序的摘要版本:
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 将字符串转换为标记;但是,我不确定如何通过随机生成的整数 select 单个标记。我应该尝试使用 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字符的液晶屏上。
#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卡上存一个词库,进一步简化代码。任何建议将不胜感激。