0

私は自分が開発している言語の簡単なインタプリタを作成していますが、次のように、単語の後に「」で囲まれたものの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;
}

また、テキストを印刷するときに「」を削除するにはどうすればよいですか。ファイルの例を次に示します。

印刷「Hello、World」

答えの真ん中で私の投稿を読んでください!

ありがとう

4

3 に答える 3

2

この簡単な例がお役に立てば幸いです。

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);

このコードは、Thenの最初の出現を検出し、最初の出現の".に次の出現を検出します。最後に、間に必要な文字列を抽出して出力します。""

于 2009-08-01T03:08:20.400 に答える
1

ファイル内の引用符で囲まれた文字列を識別し、引用符なしで印刷することが必要だと思います。もしそうなら、以下のスニペットがうまくいくはずです。

これはあなたのwhile(!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); 
    }       
}
于 2009-08-01T10:26:01.883 に答える
0

私はあなたの問題を理解していません。の入力について

print "Hello, World"

のテストlinha == "print"が真になることはありません(linhaには残りの行が含まれているため、等式は決して真ではありません)。

文字列処理、つまり入力行の分割に関するヘルプをお探しですか?

または、正規表現のヘルプを探していますか?後者に使用できるライブラリがあります。

于 2009-08-01T02:42:34.747 に答える