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

Sortowanie nazwisk [C]

Ostatnio zmodyfikowano 2017-02-17 04:51
Autor Wiadomość
Nazgul
» 2017-02-17 00:51:19
Bo chciałeś żonglowanie, to dałem!;P
Oj brachu, trzymaj:
C/C++
for( unsigned int i = 0; i < n; i++ )
     printf( "\n %s %s", tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i ] ], tablica_studentow[ 1 ][ posortowane_indexy_studentow[ i ] ] );

P-157877
Nazgul
» 2017-02-17 00:54:31
Cały kod:
C/C++
#include <stdio.h>
#include <stdlib.h>

const unsigned int MAX_DLUGOSC_WYRAZU = 25;
const unsigned int MAX_LICZBA_STUDENTOW = 30;


int main() {
    unsigned int n = 0;
    char tablica_studentow[ 2 ][ MAX_LICZBA_STUDENTOW ][ MAX_DLUGOSC_WYRAZU ];
    unsigned int posortowane_indexy_studentow[ MAX_LICZBA_STUDENTOW ];
   
    for( unsigned int i = 0; i < MAX_LICZBA_STUDENTOW; i++ )
         posortowane_indexy_studentow[ i ] = i;
   
    printf( "Podaj liczbe studentow: " );
    scanf( "%d", & n );
    if( n > MAX_LICZBA_STUDENTOW )
         return 1;
   
    for( unsigned int i = 0; i < n; i++ )
    {
        printf( "\nPodaj imie studenta nr %d: ", i + 1 );
        scanf( "%24s", & tablica_studentow[ 1 ][ i ][ 0 ] );
        printf( "Podaj nazwisko studenta nr %d: ", i + 1 );
        scanf( "%24s", & tablica_studentow[ 0 ][ i ][ 0 ] );
    }
   
   
   
    unsigned int rozmiar = n;
    while( rozmiar > 1 )
    {
        for( unsigned int i = 0; i < rozmiar - 1; i++ )
        {
            for( unsigned int j = 0; j < MAX_DLUGOSC_WYRAZU; j++ )
            {
                if( tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i ] ][ j ] > tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i + 1 ] ][ j ] )
                {
                    unsigned int zamiennik = posortowane_indexy_studentow[ i ];
                    posortowane_indexy_studentow[ i ] = posortowane_indexy_studentow[ i + 1 ];
                    posortowane_indexy_studentow[ i + 1 ] = zamiennik;
                }
            }
        }
        rozmiar--;
    }
   
   
    for( unsigned int i = 0; i < n; i++ )
         printf( "\n %s %s", tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i ] ], tablica_studentow[ 1 ][ posortowane_indexy_studentow[ i ] ] );
   
    return 1;
}
P-157878
Nazgul
» 2017-02-17 04:51:52
o rety rety! Tobie cisnę, a sam lepszy nie jestem.. pomagam, ale błędnie;( ehh

zapomniałem dodać jednego czegoś.. ten poprzedni kod działa, ale sortuje biorąc pod uwagę tylko ostatnią literkę z nazwiska...

C/C++
#include <stdio.h>
#include <stdlib.h>

const unsigned int MAX_DLUGOSC_WYRAZU = 25;
const unsigned int MAX_LICZBA_STUDENTOW = 30;


int main() {
    unsigned int n = 0;
    char tablica_studentow[ 2 ][ MAX_LICZBA_STUDENTOW ][ MAX_DLUGOSC_WYRAZU ];
    unsigned int posortowane_indexy_studentow[ MAX_LICZBA_STUDENTOW ];
   
    for( unsigned int i = 0; i < MAX_LICZBA_STUDENTOW; i++ )
         posortowane_indexy_studentow[ i ] = i;
   
    printf( "Podaj liczbe studentow: " );
    scanf( "%d", & n );
    if( n > MAX_LICZBA_STUDENTOW )
         return 1;
   
    for( unsigned int i = 0; i < n; i++ )
    {
        printf( "\nPodaj imie studenta nr %d: ", i + 1 );
        scanf( "%24s", & tablica_studentow[ 1 ][ i ][ 0 ] );
        printf( "Podaj nazwisko studenta nr %d: ", i + 1 );
        scanf( "%24s", & tablica_studentow[ 0 ][ i ][ 0 ] );
    }
   
   
   
    unsigned int rozmiar = n;
    while( rozmiar > 1 )
    {
        for( unsigned int i = 0; i < rozmiar - 1; i++ )
        {
            for( unsigned int j = 0; j < MAX_DLUGOSC_WYRAZU; j++ )
            {
                if( tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i ] ][ j ] > tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i + 1 ] ][ j ] )
                {
                    unsigned int zamiennik = posortowane_indexy_studentow[ i ];
                    posortowane_indexy_studentow[ i ] = posortowane_indexy_studentow[ i + 1 ];
                    posortowane_indexy_studentow[ i + 1 ] = zamiennik;
                    break;
                }
                else if( tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i ] ][ j ] < tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i + 1 ] ][ j ] )
                {
                    break;
                }
            }
        }
        rozmiar--;
    }
   
   
    for( unsigned int i = 0; i < n; i++ )
         printf( "\n %s %s", tablica_studentow[ 0 ][ posortowane_indexy_studentow[ i ] ], tablica_studentow[ 1 ][ posortowane_indexy_studentow[ i ] ] );
   
    return 1;
}

teraz powinno być dobrze (OBY), bardzo przepraszam za złe kody
P-157882
1 2 3 « 4 »
Poprzednia strona Strona 4 z 4