0

在我的网站上,我想包含一个回文查找器,您可以在其中拖入一个 .txt 文件,它会为您提供其中的所有回文。我能够读取这些数据并将其记录在我的控制台中,但无法使用实际数据。当我尝试使用 jquery 时,它会失败,因为它无法将数据识别为字符串。我已经包含了我的代码以获得更好的解释。

HTML:

<div id="drop_zone" ondrop="dropHandler(event);" ondragover="dragOverHandler(event);">

JS:

function dropHandler(ev) {
    console.log('File(s) dropped');
  
    // Prevent default behavior (Prevent file from being opened)
    ev.preventDefault();
  
    if (ev.dataTransfer.files) {
        let files = ev.dataTransfer.files;
        let data = "";
        file = files[0];
        if(file.type.indexOf("text") == 0){
            let reader = new FileReader();
            reader.onload = function(ev){
                data = ev.target.result;
                //console.log(data); GIVES ME CONTENTS OF FILE!
                //console.log(typeof data);
            }
            reader.readAsText(file);
            //console.log(typeof data);
            //$.get("/palindrome" + data, {}, function response(){
                //console.log(response);
            //});
        }
    }

}

CPP(我正在使用 Crow 后端)

CROW_ROUTE(app, "/palindrome/<string>")([](string data){
        crow::json::wvalue x;
        x = getPalindromes(data);
        return x;
});


/////////////////////////////
#include <unordered_map>
#include <vector>
#include <algorithm>
#include "crow_all.h"

using namespace std;

//Globally declared map
unordered_map<string, bool> hashmap;

crow::json::wvalue getPalindromes(string data){
    crow::json::wvalue x;
    string word = "";
    for(int i = 0; i < data.size(); i++){
        if(data[i] == '\n'){
            hashmap.insert({word, true});
            word = "";
        }
        else{
            word += data[i];
        }
    }
    string temp;
    vector<string> words;
    int numValid = 0;
    for(auto& i: hashmap){
        temp = i.first;
        reverse(temp.begin(), temp.end());
        auto got = hashmap.find(temp);
        if(got != hashmap.end()){
            words.push_back(i.first);
            numValid++;
        }
    }
    x["words"] = words;
    x["numValid"] = numValid;
    return x;
}
4

0 回答 0