C++ 用C++;
我正在为我正在开发的一种语言构建一个简单的解释器,但是我如何能够在一个单词之后完成一系列的事情,并用“”四舍五入,比如:C++ 用C++;,c++,file-io,interpreter,C++,File Io,Interpreter,我正在为我正在开发的一种语言构建一个简单的解释器,但是我如何能够在一个单词之后完成一系列的事情,并用“”四舍五入,比如: #include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main( int argc, char* argv[] ) { if(argc != 2) { cout <
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
ifstream file(argv[ 1 ]);
if (!file.good()) {
cout << "File " << argv[1] << " does not exist.\n";
return 0;
}
string linha;
while(!file.eof())
{
getline(file, linha);
if(linha == "print")
{
cout << text after print;
}
}
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
如果(argc!=2)
{
我不明白你的问题。关于
print "Hello, World"
您对linha==“print”
的测试将永远不会为真(因为linha包含该行的其余部分,所以equaltry永远不会为真)
您是否正在寻求字符串处理方面的帮助,即拆分输入行
或者您正在寻找正则表达式帮助?有一些库可用于后者。我不理解您的问题。关于
print "Hello, World"
您对linha==“print”
的测试将永远不会为真(因为linha包含该行的其余部分,所以equaltry永远不会为真)
您是否正在寻求字符串处理方面的帮助,即拆分输入行
或者您正在寻找正则表达式帮助?有一些库可以用于后者。我希望这个简单的示例会有所帮助
std::string code = " print \" hi \" ";
std::string::size_type beg = code.find("\"");
std::string::size_type end = code.find("\"", beg+1);
// end-beg-1 = the length of the string between ""
std::cout << code.substr(beg+1, end-beg-1);
std::string code=“打印\”hi\”;
std::string::size\u type beg=code.find(“\”);
std::string::size\u type end=code.find(“\”,beg+1);
//end-beg-1=介于“”之间的字符串的长度
我希望这个简单的例子会有所帮助
std::string code = " print \" hi \" ";
std::string::size_type beg = code.find("\"");
std::string::size_type end = code.find("\"", beg+1);
// end-beg-1 = the length of the string between ""
std::cout << code.substr(beg+1, end-beg-1);
std::string code=“打印\”hi\”;
std::string::size\u type beg=code.find(“\”);
std::string::size\u type end=code.find(“\”,beg+1);
//end-beg-1=介于“”之间的字符串的长度
std::cout我假设您想要的是识别文件中带引号的字符串,并在不带引号的情况下打印它们
这将进入而(!file.eof())
循环:
string linha;
while(!file.eof())
{
getline(file, linha);
string::size_type idx = linha.find("\""); //find the first quote on the line
while ( idx != string::npos ) {
string::size_type idx_end = linha.find("\"",idx+1); //end of quote
string quotes;
quotes.assign(linha,idx,idx_end-idx+1);
// do not print the start and end " strings
cout << "quotes:" << quotes.substr(1,quotes.length()-2) << endl;
//check for another quote on the same line
idx = linha.find("\"",idx_end+1);
}
}
stringlinha;
而(!file.eof())
{
getline(文件,linha);
string::size\u type idx=linha.find(“\”);//查找行中的第一个引号
while(idx!=string::npos){
string::size\u type idx\u end=linha.find(“\”,idx+1);//引号末尾
字符串引号;
quotes.assign(linha、idx、idx_end-idx+1);
//不要打印“开始”和“结束”字符串
cout我假设您想要的是识别文件中带引号的字符串,并在不带引号的情况下打印它们
这将进入而(!file.eof())
循环:
string linha;
while(!file.eof())
{
getline(file, linha);
string::size_type idx = linha.find("\""); //find the first quote on the line
while ( idx != string::npos ) {
string::size_type idx_end = linha.find("\"",idx+1); //end of quote
string quotes;
quotes.assign(linha,idx,idx_end-idx+1);
// do not print the start and end " strings
cout << "quotes:" << quotes.substr(1,quotes.length()-2) << endl;
//check for another quote on the same line
idx = linha.find("\"",idx_end+1);
}
}
stringlinha;
而(!file.eof())
{
getline(文件,linha);
string::size\u type idx=linha.find(“\”);//查找行中的第一个引号
while(idx!=string::npos){
string::size\u type idx\u end=linha.find(“\”,idx+1);//引号末尾
字符串引号;
quotes.assign(linha、idx、idx_end-idx+1);
//不要打印“开始”和“结束”字符串
我可以建立我自己的解释器吗?我想这样做:只显示linha上“”中的内容,因为这是我正在开发的语言。谢谢!我正在建立我自己的解释器,我想这样做:只显示“”中的内容关于linha,因为这是我正在开发的语言。谢谢!我回到StackOverflow!我回到StackOverflow!我还没有测试过它,但它显然是一种简单的方法,很有效。没有测试过,但它显然是一种简单的方法,很有效。