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;
}
コードを表示してください。関連する部分が長い場合。 :) –
'私は長いコードを持っています'この長いコードのいくつかを見ることができますか?それ以外の場合は、あなたを助けることは不可能です。 – DimChtz
ようこそStackOverflowへ。コードをコピーして貼り付けてください。 – ABcDexter