如何用 C++ 创建一个可以翻译以下文本的程序:
main(form(text(hello())text1(new)))
在那里面:
main form text hello
main form text1 new
我已经尝试这样做了,但还没有成功:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string code = "main(form(text(hello(world))text1(new)))";
vector<string> real_code;
string token;
for(char k: code)
{
if(k == '(')
{
real_code.push_back(token);
real_code.push_back("(");
token = "";
}else if(k == ')')
{
real_code.push_back(token);
real_code.push_back(")");
token = "";
}else{token += k;}
}
for(string j: real_code)
{
cout << j << " ";
}cout << endl << endl;
vector<string> names;
for(int i = 0; i < 11; i++)
{
string here = real_code[i];
string next = (i < 10)? real_code[i+1] : real_code[i];
cout << "\n=> ";
if(here == ")")
{
for(string j: names)
{
cout << j << " ";
}//cout << endl;
names.pop_back();
} else if(here == "("){
continue;
} else {
if(next == ")")
{
names.push_back(here);
for(string j: names)
{
cout << j << " ";
}
}
if(next == "(")
{
names.push_back(here);
}
}
}
}
(提前对糟糕的代码表示歉意)