2017-11-09 2 views
-2

Newbeので、厳しくしないでください、私は可能な限り私の問題を説明しようとします。私のコードはC++の予期しない入力にどのように反応しますか?

私は入力として0,1、またはnの引数を受け取る長いコードを持っています。 これらの入力は、私はこのように私のプログラムを実行するがあれば、文字列のことになっている: ./task2は^ ..;:!! は私が単に取得: bashのを:..:イベント

どのようにすることができますが見つかりませんこのエラーを防ぎ、エラーメッセージを出力するだけですか?

これはtask2.cpp

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <vector> 
#include <array> 
#include <functional> 
#include <math.h> 
#include <stdint.h> 

using namespace std; 

/// Build suffix array from text. 
/// Input is an emtpy suffix array and the text. 
/// Output is a suffix array (sorted). 
void construct(std::vector<uint32_t>& sa, const std::string& text) 
{ 
    if(text.empty()) { 
     cout << "Text is empty" << endl; 
     return; 
    } 

    if (sa.size()!=0) { 
     sa.clear(); 
    } 

    for (uint32_t a = 0; a<text.length(); ++a) { 
     sa.push_back(a); 
    } 

    sort(sa.begin(),sa.end(),[&text] (uint32_t s1, uint32_t s2) { 
    return(text.substr(s1)<=text.substr(s2)); 
    }); 
    return; 
} 


//Wir brauchen kein $ am Ende unseres Strings, da wir keinen Suffixbaum aufbauen und wir somit nicht das Problem haben 
//werden, dass ein Suffix ein Prefix eines aderen Suffix ist. 

/// search for a 'query' string in a suffix array 'sa' build from 'text'. 
/// Results are returned in 'hits'. 

void findmlr(uint32_t& mlr, const uint32_t L, const uint32_t R, const std::string& query, const std::vector<uint32_t>& sa, const std::string& text) { 
    uint32_t mlrl = mlr; 
    uint32_t mlrr = mlr; 


    /* for L */ 
    for(uint32_t l = mlr; l<query.size(); ++l) { 
     if (query[mlr] == text[sa[L]+mlrl]) { 
      mlrl+=1; 
     } 
     else{ 
      break; 
     } 
    } 

    /* for R */ 
    for(uint32_t r = mlr; r<query.size(); ++r) { 
     //cout << query[mlr] << text[sa[R]+mlrr] << endl; 
     if (query[mlr] == text[sa[R]+mlrr]) { 
      mlrr+=1; 
     } 
     else{ 
      break; 
     } 
    } 

    mlr = min(mlrl,mlrr); 
    return; 
} 


void find(std::vector<uint32_t>& hits, const std::string& query, const std::vector<uint32_t>& sa, const std::string& text){ 


    if(hits.size()!=0) { 
    hits.clear(); 
    } 

    if (cin.fail()) { 
     cin.clear(); 
     cout << "unexpected input" << endl; 
    } 

    if (text.size()==0) { 
     cout << "text is empty"; 
     return; 
    } 

    else if (query.size()==0) { 
     cout << "query is empty"; 
     return; 
    } 


    /* finding Lp */ 
    uint32_t Lp; 
    if (query <= text.substr(sa[0])){ 
      Lp = 0; 
    } 

    else if (query > text.substr(sa[text.length()-1])){ 
      Lp = text.length(); 
    } 

    else { 
     uint32_t L = 0; 
     uint32_t R = text.length()-1; 
     uint32_t mlr = 0; 
     while (R-L > 1) { 


       uint32_t M = ceil((L+R)/2); 

       findmlr(mlr,L,R,query,sa,text); 

       if (query.substr(mlr) <= (text.substr(sa[M]+mlr))) { 
        R = M; 
       } 
       else { 
        L = M; 
       } 
     } 
     Lp = R; 
    } 

    for (uint32_t i = Lp; i < text.length(); ++i) { 
     if ((text.substr(sa[i])).substr(0,query.length()) == query) { 
      if(find(hits.begin(), hits.end(), sa[i]) != hits.end()){ 
       //do nothing 
      } 
      else{ 
      hits.push_back(sa[i]); 
      } 
     } 
     else { 
      break; 
     } 
    } 

    sort(hits.begin(), hits.end()); 
    return; 
} 

であり、これはtask2_main.cpp

#include <iostream> 
#include <string> 
#include <algorithm> 
#include <vector> 
#include "aufgabe2.hpp" 
#include <stdint.h> 
//#include <typeinfo> 

using namespace std; 

int main(int argc, char* argv[]){ 

if (argc < 2){ 
    cout << "not enough arguments were given" << endl; 
    return 1;} 

if (argc == 2){ 

    vector<uint32_t> sa; 
    string input_string(argv[1]); 
    construct(sa,input_string); 

    for(uint32_t i = 0; i<input_string.length(); ++i) { 
     cout << sa[i] << endl; 
    } 

    return 0; 
    } 

if (argc > 2){ 
    vector<uint32_t> hits; 
    string text(argv[1]); 
    vector<uint32_t> sa; 

    if (sa.size() != 0) { 
     sa.clear(); 
    } 
    if (hits.size() != 0) { 
     hits.clear(); 
    } 

    int z; 
    z = 2; 
     while(z < argc){ 
      /* 
      if(typeid(argv[z])!=typeid(text)) { 
       cout << typeid(text).name() << z << " is not of type string" << endl; 
      } 
      */ 

      if (sa.size()!=0) { 
      sa.clear(); 
      } 


       for (int unsigned a = 0; a<text.length(); ++a) 
       { 
       sa.push_back(a); 
       } 
       sort(sa.begin(),sa.end(),[&text] (uint32_t s1, uint32_t s2) { 
       return(text.substr(s1)<=text.substr(s2)); 
       }); 

      string query(argv[z]); 
      string input_string(argv[1]); 

      if (query.size() == 0) { 
       //do nothing 
      } 
      else if (input_string.size() == 0) { 
       //do nothing 
      } 
      else { 
      cout << (argv[z]) << ":"; 
      } 

      find(hits,query,sa,input_string); 

       for (uint32_t i = 0; i < hits.size(); ++i) { 
       cout << " " << hits[i]; 
       } 

      cout << endl; 
      ++z; 
     } 
    } 
    return 0; 
} 
+1

コードを表示してください。関連する部分が長い場合。 :) –

+0

'私は長いコードを持っています'この長いコードのいくつかを見ることができますか?それ以外の場合は、あなたを助けることは不可能です。 – DimChtz

+0

ようこそStackOverflowへ。コードをコピーして貼り付けてください。 – ABcDexter

答えて

0

あなたのプログラムに対して完全に外部にある "バッシュイベントが見つからない" です。 GNU Bashは、コマンドラインで文字が二重引用符で囲まれていても、!文字に反応する(誤った)機能を持っています。

$ echo "blah!blah!" 
bash: !blah!: event not found 
$ echo 'blah!blah!' 
blah!blah! 

最初のケースでは、echoは呼び出されていないことに注意してください。 Bashはコマンドラインの処理に失敗し、echoは呼び出されませんでした。このメッセージが表示された場合、あなたのプログラムはまったく呼び出されていないので、あなたのプログラムがどんな種類の入力をどのように扱うのかは分かりません。

!を一重引用符またはバックスラッシュでエスケープする必要があります。 Bashがここでやろうとしているのは、blahで始まるコマンド履歴のコマンド履歴でblahを探し、それを置き換えることです。これは「歴史の拡大」です。

この機能の詳細は、BashのマニュアルページのHISTORY EXPANSIONに記載されています。

+0

ありがとうございます。予期しない入力を出力する無効な入力に対して例外を含めるよう依頼されています。今、私はそれを置く別の場所を考えることができません、そしてあなたが0引数を入力した場合。 –

+0

@DanielGießingRight; 「イベントが見つかりません」というケースでは、あなたのプログラムはまったく呼び出されていません。 Bashはそのようなコマンドラインを処理できませんでした。このような状況では、プログラムの動作について何も示されません。 – Kaz

関連する問題