Ich weiß, dass jeder es leid ist, gefragt zu werden, wie man Strings analysiert, aber das ist anders. Ich versuche, eine Befehlszeilenschnittstelle für ein Zeichenprogramm in C++ zu erstellen. Ich war in der Lage, Linien vorher zu analysieren, aber mein Problem ist, dass es mehrere Befehle gibt, die unterschiedliche Mengen von Parametern haben.Analysieren einer Zeile mit C++
Hier ist mein letzter Versuch, meine Befehlszeilenfunktion (die Fahne jetzt ignorieren):
//Allow user to type in commands
void commandLine(){
bool flag = true;
do {
cout << "Enter a command (format 'command x y z') : ";
vector<string> v;
string line;
cin.ignore() //edit: seg fault without this
getline(cin, line);
char space = ' ';
int p1, p2, p3, p4, p5, p6;
auto i = 0;
auto pos = line.find(space);
while(pos != string::npos){
v.push_back(line.substr(i, pos-i));
i = ++pos;
pos = line.find(space, pos);
if (pos != string::npos)
v.push_back(line.substr(i, line.length()));
}
if (v[0]=="size"){
p1 = stoi(v[1]);
p2 = stoi(v[2]);
//call asm
int ret = mysetSize(p1, p2);
if (ret == 0){
cout << "Something went wrong with the setSize command..." << endl;
} else {
cout << "Size set to " << p1 << "x" << p2 << endl;
}
}else if (v[0] == "clear"){
//call asm
int ret = myclear();
if (ret == 0){
cout << "Something went wrong with the clear command..." << endl;
} else {
cout << "Screen Cleared" << endl;
}
}else if (v[0] == "setBkgndColor"){
p1 = stoi(v[1]);
p2 = stoi(v[2]);
p3 = stoi(v[3]);
//call asm
int ret = mysetBkgndColor(p1, p2, p3);
if (ret==0){
cout << "Something went wrong with the setBkgndColor command..." << endl;
} else {
cout << "Background color set to " << p1 << ", " << p2 << ", " << p3 << endl;
}
}else if (v[0] == "setDrawColor"){
p1 = stoi(v[1]);
p2 = stoi(v[2]);
p3 = stoi(v[3]);
//call asm
int ret = mysetDrawColor(p1, p2, p3);
if (ret == 0){
cout << "Something went wrong with the setDrawColor command..." << endl;
} else {
cout << "Draw color set to " << p1 << ", " << p2 << ", " << p3 << endl;
}
}else if (v[0] == "rect"){
p1 = stoi(v[1]);
p2 = stoi(v[2]);
p3 = stoi(v[3]);
p4 = stoi(v[4]);
vertex a;
a.x = p1;
a.y = p2;
drawRect(a, p3, p4);
}else if (v[0] == "circle"){
p1 = stoi(v[1]);
p2 = stoi(v[2]);
p3 = stoi(v[3]);
vertex a;
a.x = p1;
a.y = p2;
drawCircle(a, p3);
}else if (v[0] == "triangle"){
p1 = stoi(v[1]);
p2 = stoi(v[2]);
p3 = stoi(v[3]);
p4 = stoi(v[1]);
p5 = stoi(v[2]);
p6 = stoi(v[3]);
vertex a, b, c;
a.x = p1;
a.y = p2;
b.x = p3;
b.y = p4;
c.x = p5;
c.y = p6;
drawTriangle(a, b, c);
}
} while(flag == true);
}
Wenn ich in einem Befehl eingeben, es friert nur mit einer leeren Zeile, die ich ctrl + c habe aus. Irgendwelche Ideen?
ANTWORT: Evan Terans Methode funktioniert für diese von diesem Link: Split a string in C++?
Verwenden Lex und Yacc –
Wie über eine vorhandene Bibliothek für diese Verwendung? "Nicht erfunden hier" ist ein häufiges Problem, man muss das Rad nicht neu erfinden, als schon unzählige andere Leute vor dir waren. Es gibt z.B. [Boost Programm Optionen] (http://www.boost.org/doc/libs/release/libs/program_options/) –