Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Zadanie 28

Ostatnio zmodyfikowano 2014-07-02 14:33
Autor Wiadomość
snajperek130
Temat założony przez niniejszego użytkownika
Zadanie 28
» 2014-06-30 15:49:42

C/C++
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;

string konwertuj( string & sTekst )
{
   
    size_t a = sTekst.find( "<b>" );
    size_t b = sTekst.find( "</b>" );
    size_t c = sTekst.find( "  " );
   
    for( int i = 0; i < sTekst.size(); i++ )
    {
        if( a != string::npos )
        {
            sTekst.erase( a, 3 );
            sTekst.insert( a, "[b]" );
        }
        if( b != string::npos )
        {
            sTekst.erase( b, 4 );
            sTekst.insert( b, "[/b]" );
        }
       
        if( sTekst.find( "  " ) != string::npos )
        {
            sTekst.erase( sTekst.find( "  " ), 1 );
        }
       
    }
   
   
    return sTekst;
}
int main()
{
    string tekst = "<b>to jest </b> testowy        napis     :)";
    cout << konwertuj( tekst ) << endl;
    return 0;
}

Kod oczywiscie dziala, ale dlaczego, jesli zamienic we fragmencie "sTekst.find("  ")" w "c" to nie wyswietla sie caly napis, a jedynie "<b>to jest </b> testowy"???
C/C++
if( sTekst.find( "  " ) != string::npos )
{
    sTekst.erase( sTekst.find( "  " ), 1 );
P-112989
pekfos
» 2014-06-30 17:50:23
jesli zamienic we fragmencie "sTekst.find("  ")" w "c"
Najlepiej podaj kody, który nie działają, a nie kody, który działają i 'co by było gdyby'. Wartość c wciąż jest taka sama, jeśli jej nie zaktualizujesz, więc co przebieg pętli będzie kasować jakieś znaki, niekoniecznie spacje.
P-112996
snajperek130
Temat założony przez niniejszego użytkownika
» 2014-07-01 11:43:06
Dzialajacy nieprawidlowo kod to:
myslalem, ze nie ma roznicy czy podam zmienna c, czy s.Tekst.find("  ")...
C/C++
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;

string konwertuj( string & sTekst )
{
   
    size_t a = sTekst.find( "<b>" );
    size_t b = sTekst.find( "</b>" );
    size_t c = sTekst.find( "  " );
   
    for( int i = 0; i < sTekst.size(); i++ )
    {
        if( a != string::npos )
        {
            sTekst.erase( a, 3 );
            sTekst.insert( a, "[b]" );
        }
        if( b != string::npos )
        {
            sTekst.erase( b, 4 );
            sTekst.insert( b, "[/b]" );
        }
       
        if( c != string::npos )
        {
            sTekst.erase( c, 1 );
        }
       
    }
   
   
    return sTekst;
}
int main()
{
    string tekst = "<b>to jest </b> testowy        napis     :)";
    cout << konwertuj( tekst ) << endl;
    return 0;
}
P-113019
snajperek130
Temat założony przez niniejszego użytkownika
» 2014-07-01 22:28:36
Kurde, tylko czemu po dopisaniu sobie do tekstu jeszcze jedno <b> i </b> nie poprawia mi ich??? Tak mnie cos natknelo i wrocilem do tego zadania
C/C++
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;

string konwertuj( string & sTekst )
{
   
    size_t a = sTekst.find( "<b>" );
    size_t b = sTekst.find( "</b>" );
    size_t c = sTekst.find( "  " );
   
    for( int i = 0; i < sTekst.size(); i++ )
    {
        if( a != string::npos )
        {
            sTekst.erase( a, 3 );
            sTekst.insert( a, "[b]" );
        }
        if( b != string::npos )
        {
            sTekst.erase( b, 4 );
            sTekst.insert( b, "[/b]" );
        }
       
        if( sTekst.find( "  " ) != string::npos )
        {
            sTekst.erase( sTekst.find( "  " ), 1 );
        }
       
    }
   
   
    return sTekst;
}
int main()
{
    string tekst = "<b>to<b> jest </b> testowy        napis     :) </b>";
    cout << konwertuj( tekst ) << endl;
    return 0;
}
P-113073
pekfos
» 2014-07-01 23:01:45
Wartość [..] wciąż jest taka sama, jeśli jej nie zaktualizujesz
P-113076
snajperek130
Temat założony przez niniejszego użytkownika
» 2014-07-02 00:19:14
Hmmmm rozwiazałem zadanie metoda prob i bledow, ale dalej nie wiem, czego np to dziala poprawnie:
C/C++
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;

string konwertuj( string & sTekst )
{
   
    size_t a = sTekst.find( "<b>" );
    size_t b = sTekst.find( "</b>" );
    size_t c = sTekst.find( "  " );
   
    for( int i = 0; i < a + b + c; i++ ) //a+b+c = sTekst.size()
    {
        if( sTekst.find( "<b>" ) != string::npos )
        {
            sTekst.erase( sTekst.find( "<b>" ), 3 );
            sTekst.insert( a, "[b]" );
           
        }
        if( sTekst.find( "</b>" ) != string::npos )
        {
            sTekst.erase( sTekst.find( "</b>" ), 4 );
            sTekst.insert( b, "[/b]" );
           
        }
       
        if( sTekst.find( "  " ) != string::npos )
        {
            sTekst.erase( sTekst.find( "  " ), 1 );
        }
       
    }
   
   
    return sTekst;
}
int main()
{
    string tekst = "<b><b>to jest </b></b> testowy        napis     :)";
    cout << konwertuj( tekst ) << endl;
    return 0;
}
a to juz nie... Prosze o cierpliwosc i dokladne napisanie o co chodzi :):
C/C++
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;

string konwertuj( string & sTekst )
{
   
    size_t a = sTekst.find( "<b>" );
    size_t b = sTekst.find( "</b>" );
    size_t c = sTekst.find( "  " );
   
    for( int i = 0; i < a + b + c; i++ ) //a+b+c = sTekst.size()
    {
        if( sTekst.find( "<b>" ) != string::npos )
        {
            sTekst.erase( sTekst.find( "<b>" ), 3 );
            sTekst.insert( sTekst.find( "<b>" ), "[b]" );
           
        }
        if( sTekst.find( "</b>" ) != string::npos )
        {
            sTekst.erase( sTekst.find( "</b>" ), 4 );
            sTekst.insert( b, "[/b]" );
           
        }
       
        if( sTekst.find( "  " ) != string::npos )
        {
            sTekst.erase( sTekst.find( "  " ), 1 );
        }
       
    }
   
   
    return sTekst;
}
int main()
{
    string tekst = "<b><b>to jest </b></b> testowy        napis     :)";
    cout << konwertuj( tekst ) << endl;
    return 0;
}
P-113080
pekfos
» 2014-07-02 09:59:41
Oba są błędne. Rob bez zmiennych a, b i c, bo tylko próbujesz je wcisnąć na siłę.
P-113087
snajperek130
Temat założony przez niniejszego użytkownika
» 2014-07-02 14:23:36
okej, ale jak zastapie zmienne a,b,c, jka np w:
C/C++
if( sTekst.find( "<b>" ) != string::npos )
{
    sTekst.erase( sTekst.find( "<b>" ), 3 );
    sTekst.insert( sTekst.find( "<b>" ), "[b]" );
   
}
to pojawia mi sie cos takiego i program sie wylacza:
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::insert

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)   execution time : 3.235 s
Press any key to continue.
P-113105
« 1 » 2
  Strona 1 z 2 Następna strona