我在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();
}
}
}
答案 0 :(得分:0)
经过大量研究,我找到了解决问题的好方法。 此代码将动词,名词和所有格形容词字符串拆分为标记,然后根据随机整数从每个字符串中选择一个标记。然后将这些令牌加起来形成一个命令性的句子。最后一句显示在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卡上存储一个字库,并进一步简化代码。任何建议都将不胜感激。