Тема: Виправлення слів
Окремо код працює, а разом ні.
1) Визначення процентного співвідношення слів
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool check_preff(string preffix, string word)
{
    if (word.size() >= preffix.size() && word.substr(0, preffix.size()) == preffix) return 1;
    return 0;
}
double per_internal(const string &shorter, const string &longer) {
    int count = 0;
    for(int i = 0; i < shorter.size(); i++)
        if(shorter[i] == longer[i]) 
            count++;
            
    double d_ret = count * 100.0 / shorter.size(); 
    if(shorter.size() == longer.size()) 
        return d_ret;
    double len_percent = shorter.size() * 100.0 / longer.size(); 
    if(count == shorter.size()) 
    {
        if(check_preff(shorter, longer)) return len_percent;
    }
    return (d_ret + len_percent) / 2.0;
}
inline double per(const string &word, const string &word2) {
    return word.size() > word2.size() ? per_internal(word2, word) : per_internal(word, word2);
}
int main()
{
    std::string s1, s2;
    cin >> s1 >> s2;
    cout << per(s1, s2);
    return 0;
}2) Визначення індекса масива double з максимальним значенням:
#include <iostream>
#include <vector>
using namespace std;
int index_with_max(std::vector<double> array, int n)
{
    double max_value=0;
    int r_max_index=0, i;
    for (i=0; i<n; i++)
    {
        if(max_value<array[i])
        {
            max_value=array[i];
            r_max_index=i;
        }
    }
    return r_max_index;
}
int main()
{
    vector<double> array;
    for (int i=2; i<5; i++)
    {
        array.push_back((double)1/(double)i);
        //cout << array[i-2] << "\n";
    }
    int max_in=index_with_max(array, array.size());
    cout << endl << max_in << ": " << array[max_in] << endl;
    return 0;
    
}Разом:
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <fstream>
#include <sstream>
#define NO_MATCH -1
using namespace std;
bool check_preff(string preffix, string word)
{
    if (word.size() >= preffix.size() && word.substr(0, preffix.size()) == preffix) return 1;
    return 0;
}
int index_with_max(std::vector<double> array, int n)
{
    double max_value=0;
    int r_max_index=0, i;
    for (i=0; i<n; i++)
    {
        if(max_value<array[i])
        {
            max_value=array[i];
            r_max_index=i;
        }
    }
    return r_max_index;
}
double per_internal(const string &shorter, const string &longer) {
    int count = 0;
    for(int i = 0; i < shorter.size(); i++)
        if(shorter[i] == longer[i]) 
            count++;
            
    double d_ret = count * 100.0 / shorter.size(); 
    if(shorter.size() == longer.size()) 
        return d_ret;
    double len_percent = shorter.size() * 100.0 / longer.size(); 
    if(count == shorter.size()) 
    {
        if(check_preff(shorter, longer)) return len_percent;
    }
    return (d_ret + len_percent) / 2.0;
}
inline double per(const string &word, const string &word2) {
    return word.size() > word2.size() ? per_internal(word2, word) : per_internal(word, word2);
}
int index_with_word_match(std::vector<std::string> voc_words, string test_word)
{
    int index_total=voc_words.size();
    vector <double> percents;
    for (int i=0; i<index_total; i++)
    {
        percents.push_back(per(voc_words[i], test_word));
    }
    int in_with_max=index_with_max(percents, percents.size());
    if(percents[in_with_max]<70) in_with_max=NO_MATCH;
    return in_with_max;
}
std::vector<std::string> LoadFile(string FileName) {
    std::vector<std::string> result;
    std::ifstream inputFile(FileName.c_str());
    if (inputFile.is_open()) {
        std::string line;
        while (std::getline(inputFile, line)) {
            result.push_back(line);
        }
        inputFile.close();
    }
    return result;
}
std::vector<std::string> delim(const std::string str) {
    std::vector<std::string> result;
    std::istringstream iss(str);
    std::string word;
    while (iss >> word) {
        result.push_back(word);
    }
    return result;
}
int main()
{
    std::string s;
    std::vector<std::string> voc_words = LoadFile("v.txt");
    std::cout << "Text: ";
    std::cin.ignore(); 
    std::getline(std::cin, s);
    std::vector<std::string> text_words = delim(s);
    int index=0;
    for (int i = 0; i < text_words.size(); i++) {
        index=index_with_word_match(voc_words, text_words[i]);
        if(index!=NO_MATCH) cout << voc_words[index] << " ";
    }
    return 0;
}Файл v.txt містить англійські слова кодування ANSI. Наприклад
understand
calculateЯкщо ввести ці самі слова, які мають на 100 співпадати з словниковими програма завершується без вивода.