C++ zamiana napisów

0

Hej,
Robię zadanie i mam problem z zamianą napisów.
Możecie mnie nakierować gdzie robię błąd?

Treść zadania:
Napisać program, który z pliku new.txt wczytuje tablicę napisów A[w][k] (w, k - stałe) – przyjąć, że
danych w pliku jest dostatecznie dużo. Następnie w każdym wierszu należy znaleźć najkrótszy napis
i zamienić go miejscami z napisem z kolumny o indeksie jz w tym wierszu, gdzie jz należy wylosować. Na
koniec wydrukować tablicę wierszami.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>

using namespace std;

int main()
{
    const int w = 10, k = 5;
    string A[w][k];
    int ileLiter, najkrotszy;
    int losowaWartosc;

    ifstream plikWE;
    plikWE.open("new.txt");
    if ( !plikWE.is_open() )
    {
        cout << "Nie ma takiego pliku!" << endl ;
        return 1;
    }

    for (int x=0; x<w; x++)
    {
        for (int y=0; y<k; y++)
        {
            plikWE >> A[x][y];
        }
    }

    srand (time(0));
    string temp;
    for (int x=0; x<w; x++)
    {
        najkrotszy = A[x][0].size();
        for (int y=0; y<k; y++)
        {
            if(A[x][y].size() < najkrotszy)
            {
                najkrotszy = A[x][y].size();
            }

        }   cout << najkrotszy; cout << endl;
        losowaWartosc = 1 + rand() % 5 + 1 - 1;

        temp = najkrotszy;
        najkrotszy = A[x][losowaWartosc];
        A[x][losowaWartosc] = temp;

    }

    for (int x=0; x<w; x++)
    {
        for (int y=0; y<k; y++)
        {
            cout << setw(15)<< A[x][y];
        } cout << endl;
    }


    plikWE.close();

    return 0;
}


1

najkrotszy - u ciebie to długość najkrótszego napisu, zaś ty potrzebujesz indeks najkrótszego lub referencje/wskaźnik na sam napis.

1

Zalecałbym nauczyć się dzielić kod na mniejsze funkcje.
Każda funkcja powinna robić jedną małą rzecz.
Kod będzie czytelniejszy łatwiejszy w zrozumieniu i utrzymaniu.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <random>
#include <array>
#include <filesystem>

constexpr int w = 10;
constexpr int k = 5;

using Matrix = std::array<std::array<std::string, k>, w>;


std::istream& read(std::istream& in, Matrix& m)
{
    for(auto& r : m) {
        for (auto& s : r) {
            in >> s;
        }
    }
    return in;
}

bool read(const std::filesystem::path& p, Matrix& m)
{
    std::ifstream f{p};

    if (!f) {
        std::perror(p.c_str());
    }
    return !read(f, m).fail();
}

std::ostream& print(std::ostream& out, const Matrix& m)
{
    for(auto& r : m) {
        for (auto& s : r) {
            out << s << ' ';
        }
        out << '\n';
    }
    return out;
}

bool print(const std::filesystem::path& p, const Matrix& m)
{
    std::ofstream f{p};

    if (!f) {
        std::perror(p.c_str());
    }
    return !print(f, m).fail();
}

void flipLongestWithIndex(std::array<std::string, k>& m, size_t jz)
{
    auto it = std::min_element(m.begin(), m.end(), [](const auto& a, const auto& b)
    {
        return a.size() < b.size();
    });
    std::iter_swap(it, m.begin() + jz);
}

void flipLongestWithIndex(Matrix& m, size_t jz)
{
    for (auto& r : m)
        flipLongestWithIndex(r, jz);
}

size_t selectColumn()
{
    return rand() % k;
}

int main()
{
    srand (time(0));

    Matrix m;
    if (!read("old.txt", m)) {
        return 1;
    }
    flipLongestWithIndex(m, selectColumn());
    print(std::cout, m);
    if (!print("new.txt", m)) {
        return 2;
    }

    return 0;
}

https://wandbox.org/permlink/6jGlU5i2ZrDFJAoe

0
MarekR22 napisał(a):

Zalecałbym nauczyć się dzielić kod na mniejsze funkcje.
Każda funkcja powinna robić jedną małą rzecz.
Kod będzie czytelniejszy łatwiejszy w zrozumieniu i utrzymaniu.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <random>
#include <array>
#include <filesystem>

constexpr int w = 10;
constexpr int k = 5;

using Matrix = std::array<std::array<std::string, k>, w>;


std::istream& read(std::istream& in, Matrix& m)
{
    for(auto& r : m) {
        for (auto& s : r) {
            in >> s;
        }
    }
    return in;
}

bool read(const std::filesystem::path& p, Matrix& m)
{
    std::ifstream f{p};

    if (!f) {
        std::perror(p.c_str());
    }
    return !read(f, m).fail();
}

std::ostream& print(std::ostream& out, const Matrix& m)
{
    for(auto& r : m) {
        for (auto& s : r) {
            out << s << ' ';
        }
        out << '\n';
    }
    return out;
}

bool print(const std::filesystem::path& p, const Matrix& m)
{
    std::ofstream f{p};

    if (!f) {
        std::perror(p.c_str());
    }
    return !print(f, m).fail();
}

void flipLongestWithIndex(std::array<std::string, k>& m, size_t jz)
{
    auto it = std::min_element(m.begin(), m.end(), [](const auto& a, const auto& b)
    {
        return a.size() < b.size();
    });
    std::iter_swap(it, m.begin() + jz);
}

void flipLongestWithIndex(Matrix& m, size_t jz)
{
    for (auto& r : m)
        flipLongestWithIndex(r, jz);
}

size_t selectColumn()
{
    return rand() % k;
}

int main()
{
    srand (time(0));

    Matrix m;
    if (!read("old.txt", m)) {
        return 1;
    }
    flipLongestWithIndex(m, selectColumn());
    print(std::cout, m);
    if (!print("new.txt", m)) {
        return 2;
    }

    return 0;
}

https://wandbox.org/permlink/6jGlU5i2ZrDFJAoe

Dziękuję za poradę, lecz potrzebuję napisać to bez funkcji oraz "prostym językiem", bez jakichś zaawansowanych funkcji, głównie na for oraz if powinienem rozwiązać to zadanie.

0

Mam problem z tą częścią kodu. Gdzie robię błąd?

    srand (time(0));
    losowaWartosc = 1 + rand( )%(5+1-1);
    int indeksNajkrotszego = 0;
    int z;
    string temp;
    for (int x=0; x<w; x++)
    {
        najkrotszy = A[x][0].size();
        for (int y=0; y<k; y++)
        {
            if(A[x][y].size() < najkrotszy)
            {
                najkrotszy = A[x][y].size();
                indeksNajkrotszego = z;
                for (int z=0; z<k; z++)
                {
                    A[x][indeksNajkrotszego] = losowaWartosc;
                }
            }
        }
    }

0
    srand(time(0));
    for(int y=0;y<w;++y)
    {
        int indeksNajkrotszego=0;
        for(int x=0;x<k;++x) if(A[y][x].size()<A[y][indeksNajkrotszego].size()) indeksNajkrotszego = x;
        int losowaWartosc=rand()%k;
        /*
        string temp=A[y][indeksNajkrotszego];
        A[y][indeksNajkrotszego]=A[y][losowaWartosc];
        A[y][losowaWartosc]=temp;
        */
        swap(A[y][indeksNajkrotszego],A[y][losowaWartosc]);
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1