I
I
Ixori2020-12-08 13:52:02
C++ / C#
Ixori, 2020-12-08 13:52:02

What is the problem in the lab code?

Exercise

Option-14.

The input file contains a Russian text containing no more than 1000 words. If there are no palindrome words longer than one letter in the text, then output the words of the text in accordance with the increase in the number of vowels, otherwise duplicate the consonant letters in the words of the text and output the resulting words in reverse alphabetical order. Output words one per line


the code
#include  <iostream>
#include  <vector>
#include  <iomanip>
#include <iostream>
#include <locale>
#include <set>


using namespace std;

bool if_lit(char c){ // проверка на букву
    if(c >= 'а' && c <= 'я') return 1;
    if(c >= 'А' && c <= 'Я') return 1;
    return 0;
}

bool check(char c){ /// проверка на гласную букву
    if(c == 'а') return 1;
    if(c == 'о') return 1;
    if(c == 'у') return 1;
    if(c == 'ы') return 1;
    if(c == 'и') return 1;
    if(c == 'э') return 1;
    if(c == 'я') return 1;
    if(c == 'ю') return 1;
    if(c == 'ё') return 1;
    if(c == 'е') return 1;
    return 0;
}

signed main(){
    setlocale( LC_CTYPE, "rus" );
    freopen("input.txt","r",stdin);
    string s;
    bool iss = 0;
    vector<string> vv;
    while(cin >> s){ /// считываем строки до пробела
        bool is = 0;
        for(int i = 0;i<s.size();i++){
            if(s[i] != s[s.size()-1-i]) is = 1; /// строка не палиндром
        }
        if(!is){
            if(s.size() > 1) is = 1;
        }
        if(!is) iss = 1;
        vv.push_back(s);
    }
    if(!iss){ /// если нету строк палиндромов длинны более 1 буквы
        vector<pair<int,string> > v;
        for(string ss : vv){
            v.push_back(mp(0,ss));
            for(int i = 0;i<ss.size();i++){
                if(check(ss[i])) v.back().first++; /// считаем кол-во гласных
            }
        }
        sort(v.begin(),v.end());
        for(string ss : v) cout << ss << endl;
    }
    else{ /// иначе
        vector<string> v;
        for(string ss : vv){
            string s1 = "";
            for(int i = 0;i<ss.size();i++){
                s1 += ss[i];
                if(!check(ss[i])){
                    s1 += ss[i]; /// подсчёт кол-ва согласных
                }
            }
            v.push_back(s1);
        }
        sort(v.begin(),v.end());
        reverse(v.begin(),v.end());
        for(string ss : v) cout << ss << endl; /// выводим строки
        return 0;
    }

}

St9iIpM.jpeg
for some reason sort and mp are displayed as an error (

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Adamos, 2020-12-08
@Adamos

https://en.cppreference.com/w/cpp/algorithm/sort
Defined in header <algorithm>

M
Mors Clamor, 2020-12-08
@66demon666

Because you don't have a sort function in your code, obviously

U
User700, 2020-12-08
@User700

Include (#include) files algoritm, and perhaps utility is more correct here: mp is more likely to be meant as make_pair. Instead of re-storing strings, you can store the corresponding string_view (or possibly reference_wrapper) in v . Or, given that vv is no longer needed, they can be moved: Instead
for(string ss : v) cout << ss << endl;
for (const string& ss : v) cout << ss << endl;
v.push_back(make_pair(0,move(ss)));
v.push_back(move(ss));

sort(v.begin(),v.end());
        reverse(v.begin(),v.end());

You can just write Here, when v is an array of pairs, you first need to print correctly. Secondly, you can sort only by the first field of the pair.
sort(v.begin(),v.end(), greater<string>());
sort(v.begin(),v.end(), [](const pair<int,string>& x, const pair<int,string>& y>{return x.first < y.first;});
        for (const auto& ss : v) cout << ss.second << endl;

Probably for such sorting there is a standard comparer on the first/second field.
When working with the bool type, use the constants true, false instead of 1, 0.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question