Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct Student {
- char *imie;
- char *nazwisko;
- int nr_in;
- };
- struct Node {
- struct Node* next;
- struct Student student;
- };
- int show_menu() {
- int choice;
- printf("\n\n\t*] -------------------------------- [*");
- printf("\n\n\t1. Wyswietl wszystkich studentow.\n");
- printf("\t2. Dodanie nowego studenta.\n");
- printf("\t3. Usuniecie studenta o podanym nr indeksu\n");
- printf("\t4. Zakonczenie programu\n");
- printf("\n\t*] -------------------------------- [*\n\n");
- printf("\tTwoj wybor: ");
- scanf("%d", &choice);
- return choice;
- }
- void remove_student() {
- FILE *file = fopen("studenci.bin", "rb");
- struct Node *head = NULL;
- int index;
- printf("\n\tPodaj index: ");
- scanf("%d", &index);
- struct Student student;
- while(fread(&student, sizeof(struct Student), 1, file)) {
- if (index == student.nr_in) {
- continue;
- }
- do_listy(&head, student);
- }
- fclose(file);
- FILE *wrFile = fopen("studenci.bin", "wb");
- while (head != NULL) {
- fwrite(&(head->student), sizeof(struct Student), 1, wrFile);
- head = head->next;
- }
- fclose(wrFile);
- }
- void add_student() {
- FILE *file = fopen("studenci.bin", "ab");
- struct Student student;
- student.imie = (char *) malloc(sizeof(char));
- student.nazwisko = (char *) malloc(sizeof(char));
- printf("\n\tPodaj imie: ");
- scanf("%s", student.imie);
- printf("\n\tPodaj Nazwisko: ");
- scanf("%s", student.nazwisko);
- printf("\n\tPodaj index: ");
- scanf("%d", &(student.nr_in));
- fwrite(&student, sizeof(struct Student), 1, file);
- fclose(file);
- }
- void print_students() {
- FILE *file = fopen("studenci.bin", "rb");
- struct Student student;
- while(fread(&student, sizeof(struct Student), 1, file)) {
- printf("\n\tImie: %s, Nazwisko: %s, Numer Indeksu: %d \n", student.imie, student.nazwisko, student.nr_in);
- }
- fclose(file);
- }
- void do_listy(struct Node** head_ref, struct Student student)
- {
- struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
- struct Node *last = *head_ref;
- new_node->student = student;
- new_node->next = NULL;
- if (*head_ref == NULL)
- {
- *head_ref = new_node;
- return;
- }
- while (last->next != NULL)
- last = last->next;
- last->next = new_node;
- return;
- }
- int main(void) {
- int choice = show_menu();
- while (choice != 4) {
- switch(choice) {
- case 1:
- print_students();
- break;
- case 2:
- add_student();
- break;
- case 3:
- remove_student();
- break;
- default:
- break;
- }
- printf("\n");
- choice = show_menu();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement