CoddityTeam / movaicode

Concours mensuel du Pire Développeur de France
123 stars 10 forks source link

[movaicode/15] Un défilé de piles #259

Open BaptisteP31 opened 1 year ago

BaptisteP31 commented 1 year ago

Un joli code, bien mauvais qui pile et dépile dans tous les sens. Je n'ai pas joué sur le formatage du code, cela aurait été trop simple ...

Bonne chance à celui qui devra le relire !

Edit : c'est du c++ bien évidemment.

/*
 * Fonction de distribution de coups de poing
 * Elle remplace toute lettre 'p' ou caractère '.' ou ';' ou ':' trouvés dans la chaîne de caractères str,
 * par le mot 'poing', 'poingvirgule' ou 'deuxpoings' et retourne la chaîne de caractères produite
 */

#include <iostream>
#include <stack>
#include <vector>
#include <sstream>

std::string distribuer_des_coups_de_poing(std::string str) {

    std::stack<std::vector<char>> phrase_inversee;
    for(auto i : str) {
        std::vector<char> lettre_courante;
        lettre_courante.push_back(i);
        phrase_inversee.push(lettre_courante);
    }

    std::stack<std::vector<char>> phrase_non_inversee;
    for (unsigned short int i = 0; i < str.length(); ++i) {
        std::vector<char> lettre_courante;
        lettre_courante = phrase_inversee.top();
        phrase_inversee.pop();
        phrase_non_inversee.push(lettre_courante);
    }

    char *pc;
    unsigned short int tabSize = phrase_non_inversee.size();
    pc = new char[tabSize];

    for (unsigned short int i = 0; i < tabSize; ++i) {
        std::vector<char> lettre_courante;
        lettre_courante = phrase_non_inversee.top();
        phrase_non_inversee.pop();
        pc[i] = lettre_courante[0];
    }

    std::vector<unsigned short int> point_index;
    std::vector<unsigned short int> poing_index;
    std::vector<unsigned short int> deuxpoings_index;
    std::vector<unsigned short int> poingvirgule_index;
    for (unsigned short int i = 0; i < tabSize; ++i) {
        if (pc[i] == 'p')
            poing_index.push_back(i);
        else if (pc[i] == ':')
            deuxpoings_index.push_back(i);
        else if (pc[i] == ';')
            poingvirgule_index.push_back(i);
        else if(pc[i] == '.')
            point_index.push_back(i);
    }

    for(unsigned short int i = 0; i < tabSize; ++i) {
        std::vector<char> lettre_courante;
        lettre_courante.push_back(pc[i]);
        phrase_inversee.push(lettre_courante);
    }

    for (unsigned short int i = 0; i < tabSize; ++i) {
        std::vector<char> lettre_courante;
        lettre_courante = phrase_inversee.top();
        phrase_non_inversee.push(lettre_courante);
        phrase_inversee.pop();
    }

    std::vector<std::vector<char>> output;
    for (unsigned short int i = 0; i < tabSize; ++i) {
        output.push_back(phrase_non_inversee.top());
        phrase_non_inversee.pop();
    }

    unsigned short int poing[5] = {112,111,105,110,103};
    unsigned short int s[1] = {115};
    unsigned short int deux[4] = {100,101,117,120};
    unsigned short int virgule[7] = {118,105,114,103,117,108,101};

    for (auto i : point_index) {
        output[i].clear();
        for (unsigned short int j = 0; j < 5; ++j)
            output[i].push_back(poing[j]);
    }

    for (auto i : poing_index) {
        output[i].clear();
        for (unsigned short int j = 0; j < 5; ++j)
            output[i].push_back(poing[j]);
    }

    for (auto i : deuxpoings_index) {
        output[i].clear();
        for (unsigned short int j = 0; j < 4; ++j)
            output[i].push_back(deux[j]);
        for (unsigned short int j = 0; j < 5; ++j)
            output[i].push_back(poing[j]);

        output[i].push_back(s[0]);
    }

    for (auto i : poingvirgule_index) {
        output[i].clear();
        for (unsigned short int j = 0; j < 5; ++j)
            output[i].push_back(poing[j]);
        for (unsigned short int j = 0; j < 7; ++j)
            output[i].push_back(virgule[j]);
    }

    delete [] pc;

    std::stringstream ss;
    for (auto i : output) {
        for (auto j : i) {
            ss << j;
        }
    }

    return ss.str();
}

int main() {
    std::string str = "Synopsys : dans les quartiers populaires de Philadelphie, Rocky Balboa collecte des dettes non payées pour Tony Gazzo, un usurier ; il dispute de temps à autre, pour quelques dizaines de dollars, des combats de boxe sous l'appellation de l'étalon italien.";
    std::cout << distribuer_des_coups_de_poing(str) << std::endl;
    return 0;
}
BaptisteP31 commented 1 year ago

👨‍🍳 Et bon appétit bien sur !