| mikewazowski Temat założony przez niniejszego użytkownika | inkrementacja numeru na liście dwukierunkowej » 2016-12-27 17:09:46 dlaczego każda kolejna osoba dodawana na koniec listy dwukierunkowej otrzymuje numer ID 1 mimo że istnieje linijka i++? FRAGMENT Z INKREMENTACJĄ id = "1";cout << "ID: " << id << endl;
 gets( bufor );
 id =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( id, bufor );
 id++;
 
CAŁY KOD #include <stdio.h>#include <stdlib.h>
 #include <string.h>
 #include <iostream>
 using namespace std;
 typedef struct DANE
 {
 char * id;
 char * imie;
 char * nazwisko;
 char * pesel;
 char * data_ur;
 char * adres;
 char * miasto;
 char * inne;
 } Dane;
 typedef struct STUDENT Student;
 struct STUDENT
 {
 Dane dane;
 Student * next;
 Student * prev;
 };
 
 Student * stworz_element()
 {
 Student * element;
 char bufor[ 30 + 1 ];
 
 char * id;
 char * imie;
 char * nazwisko;
 char * pesel;
 char * data_ur;
 char * adres;
 char * miasto;
 char * inne;
 system( "cls" );
 
 id = "1";
 cout << "ID: " << id << endl;
 gets( bufor );
 id =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( id, bufor );
 id++;
 
 printf( "Imie: " );
 gets( bufor );
 imie =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( imie, bufor );
 cout << endl;
 
 printf( "Nazwisko: " );
 gets( bufor );
 nazwisko =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( nazwisko, bufor );
 
 printf( "Pesel: " );
 gets( bufor );
 pesel =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( pesel, bufor );
 
 printf( "Data urodzenia: " );
 gets( bufor );
 data_ur =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( data_ur, bufor );
 
 printf( "Adres: " );
 gets( bufor );
 adres =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( adres, bufor );
 
 printf( "Miasto: " );
 gets( bufor );
 miasto =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( miasto, bufor );
 
 printf( "Inne: " );
 gets( bufor );
 inne =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( inne, bufor );
 
 element =( Student * ) malloc( sizeof( Student ) );
 element->next = NULL;
 element->prev = NULL;
 element->dane.imie = imie;
 element->dane.nazwisko = nazwisko;
 element->dane.pesel = pesel;
 element->dane.data_ur = data_ur;
 element->dane.adres = adres;
 element->dane.miasto = miasto;
 element->dane.inne = inne;
 return element;
 }
 
 void dodaj_na_koniec( Student ** head )
 {
 Student * pom, * tmp = stworz_element();
 if( * head == NULL )
 * head = tmp;
 else
 {
 pom = * head;
 while( pom->next != NULL )
 pom = pom->next;
 
 tmp->prev = pom;
 pom->next = tmp;
 }
 }
 
 void wyswietlanie_listy( Student * head )
 {
 Student * tmp;
 tmp = head;
 if( tmp == NULL )
 printf( "Brak elementow" );
 else
 {
 int n = 1;
 while( tmp != NULL )
 {
 cout << endl;
 cout << "ID: " << n << endl;
 cout << "Nazwisko: " << tmp->dane.nazwisko << endl;
 cout << "Imie: " << tmp->dane.imie << endl;
 cout << "Pesel: " << tmp->dane.pesel << endl;
 cout << "Data urodzenia: " << tmp->dane.data_ur << endl;
 cout << "Adres: " << tmp->dane.adres << endl;
 cout << "Miasto: " << tmp->dane.miasto << endl;
 cout << "Inne: " << tmp->dane.inne << endl;
 cout << endl;
 tmp = tmp->next;
 n++;
 }
 }
 getchar();
 }
 
 Student * wyszukaj_element( Student * head, char * nazwisko )
 {
 Student * pom;
 pom = head;
 while( pom != NULL && strcmp( pom->dane.nazwisko, nazwisko ) != 0 )
 pom = pom->next;
 
 return pom;
 }
 
 void usuwan_wybrany_element( Student ** head )
 {
 Student * tmp, * pom;
 char * nazwisko;
 char bufor[ 30 + 1 ];
 printf( "Podaj nazwisko: " );
 gets( bufor );
 nazwisko =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( nazwisko, bufor );
 tmp = wyszukaj_element( * head, nazwisko );
 if( tmp == NULL )
 printf( "Nie ma takiej osoby" );
 else
 {
 if( tmp == * head )
 {
 cout << "usunieto osobe z bazy";
 * head =( * head )->next;
 if( tmp->dane.imie )
 free( tmp->dane.imie );
 
 if( tmp->dane.nazwisko )
 free( tmp->dane.nazwisko );
 
 free( tmp );
 
 }
 else
 {
 cout << "usunieto osobe z bazy";
 pom = * head;
 while(( strcmp( pom->next->dane.nazwisko, tmp->dane.nazwisko ) != 0 ) )
 pom = pom->next;
 
 if( tmp->next != NULL )
 pom->next->prev = tmp->prev;
 
 pom->next = tmp->next;
 if( tmp->dane.imie )
 free( tmp->dane.imie );
 
 if( tmp->dane.nazwisko )
 free( tmp->dane.nazwisko );
 
 free( tmp );
 
 }
 }
 }
 void sortowanie_listy( Student ** head )
 {
 Student * nowa = NULL, * tmp, * pom;
 while( * head != NULL )
 {
 tmp =( Student * ) malloc( sizeof( Student ) );
 tmp->dane.imie =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.imie ) + 1 );
 strcpy( tmp->dane.imie,( * head )->dane.imie );
 tmp->dane.nazwisko =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.nazwisko ) + 1 );
 strcpy( tmp->dane.nazwisko,( * head )->dane.nazwisko );
 tmp->dane.pesel =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.pesel ) + 1 );
 strcpy( tmp->dane.pesel,( * head )->dane.pesel );
 tmp->dane.data_ur =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.data_ur ) + 1 );
 strcpy( tmp->dane.data_ur,( * head )->dane.data_ur );
 tmp->dane.adres =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.adres ) + 1 );
 strcpy( tmp->dane.adres,( * head )->dane.adres );
 tmp->dane.miasto =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.miasto ) + 1 );
 strcpy( tmp->dane.miasto,( * head )->dane.miasto );
 tmp->dane.inne =( char * ) malloc( sizeof( char ) * strlen(( * head )->dane.inne ) + 1 );
 strcpy( tmp->dane.inne,( * head )->dane.inne );
 tmp->next = NULL;
 tmp->prev = NULL;
 if( nowa == NULL )
 nowa = tmp;
 else if( strcmp( nowa->dane.imie, tmp->dane.imie ) > 0 )
 {
 tmp->next = nowa;
 nowa->prev = tmp;
 nowa = tmp;
 }
 else
 {
 pom = nowa;
 while( pom->next != NULL && strcmp( pom->next->dane.imie, tmp->dane.imie ) < 0 )
 pom = pom->next;
 
 tmp->prev = pom;
 tmp->next = pom->next;
 if( pom->next != NULL )
 pom->next->prev = tmp;
 
 pom->next = tmp;
 }
 pom = * head;
 * head =( * head )->next;
 if( pom->dane.imie )
 free( pom->dane.imie );
 
 if( pom->dane.nazwisko )
 free( pom->dane.nazwisko );
 
 free( pom );
 }
 * head = nowa;
 }
 
 void zapisz_do_pliku( Student * head )
 {
 FILE * zapisz = NULL;
 Student * tmp;
 char nazwa_pliku[] = "lista_studentow.txt";
 if( head == NULL )
 printf( "Nie ma elementow do zapisania" );
 else
 {
 zapisz = fopen( nazwa_pliku, "w" );
 if( zapisz == NULL )
 printf( "Blad otwarcia pliku" );
 else
 {
 tmp = head;
 while( tmp != NULL )
 {
 fprintf( zapisz, "%s\n", tmp->dane.imie );
 fprintf( zapisz, "%s\n", tmp->dane.nazwisko );
 fprintf( zapisz, "%s\n", tmp->dane.pesel );
 fprintf( zapisz, "%s\n", tmp->dane.data_ur );
 fprintf( zapisz, "%s\n", tmp->dane.adres );
 fprintf( zapisz, "%s\n", tmp->dane.miasto );
 fprintf( zapisz, "%s\n", tmp->dane.inne );
 tmp = tmp->next;
 }
 printf( "Lista zostala zapisana" );
 }
 fclose( zapisz );
 }
 }
 
 void odczyt_z_pliku( Student ** head )
 {
 FILE * wczytaj = NULL;
 char nazwa_pliku[] = "lista_studentow.txt";
 char bufor[ 30 + 1 ];
 Student * tmp, * pom;
 wczytaj = fopen( nazwa_pliku, "r" );
 if( wczytaj == NULL )
 printf( "Blad otwarcia pliku" );
 else
 {
 while( fscanf( wczytaj, "%s", bufor ) != EOF )
 {
 
 tmp =( Student * ) malloc( sizeof( Student ) );
 tmp->next = NULL;
 tmp->prev = NULL;
 
 tmp->dane.imie =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( tmp->dane.imie, bufor );
 
 fscanf( wczytaj, "%s", bufor );
 tmp->dane.nazwisko =( char * ) malloc( sizeof( char ) *( strlen( bufor ) + 1 ) );
 strcpy( tmp->dane.nazwisko, bufor );
 if( * head == NULL )
 * head = tmp;
 else
 {
 pom = * head;
 while( pom->next != NULL )
 pom = pom->next;
 
 tmp->prev = pom;
 pom->next = tmp;
 }
 }
 printf( "Plik zostal wczytany" );
 }
 fclose( wczytaj );
 }
 
 void menu( Student * head )
 {
 int n;
 do
 {
 cout << "1- dodaj osobe" << endl;
 cout << "2-usun osobe" << endl;
 cout << "3-wyszukaj osobe po nazwisku" << endl;
 cout << "4-wyswietl wg nazwisk" << endl;
 cout << "5-modyfikuj po id" << endl;
 cout << "6-zapisz do pliku" << endl;
 cout << "7-odczytaj z pliku" << endl;
 cout << "podaj numer: ";
 cin >> n;
 
 switch( n )
 {
 
 case 1:
 dodaj_na_koniec( & head );
 break;
 case 2:
 usuwan_wybrany_element( & head );
 break;
 case 3:
 cout << "po nazwisku" << endl;
 break;
 case 4:
 cout << "nie alfabetycznie" << endl;
 wyswietlanie_listy( head );
 break;
 case 5:
 cout << "id";
 break;
 case 6:
 zapisz_do_pliku( head );
 break;
 case 7:
 odczyt_z_pliku( & head );
 break;
 }
 
 }
 while( n != 8 );
 
 }
 
 int main()
 {
 Student * head = NULL;
 menu( head );
 return 0;
 }
 
 |