Facebook
From ss, 7 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 261
  1. clc
  2. close all
  3. clear all
  4. % Definiranje maksimalnog broja predmeta
  5. MAX_PREDMETA = 5;
  6.  
  7. % Inicijalizacija strukture za studenta
  8. student.ime = '';
  9. student.prezime = '';
  10. student.adresa = '';
  11. student.predmeti = cell(1, MAX_PREDMETA);
  12. student.ocjene = zeros(1, MAX_PREDMETA);
  13. student.prosjek = 0;
  14.  
  15. % Unos podataka sa tastature
  16. fprintf('Unesite podatke o studentu:\n');
  17. student.ime = input('Ime: ', 's');
  18. student.prezime = input('Prezime: ', 's');
  19. student.adresa = input('Adresa: ', 's');
  20.  
  21.  
  22. for i = 1:MAX_PREDMETA
  23.     predmet = input(sprintf('Unesite ime %d. predmeta: ', i), 's');
  24.     ocjena = input(sprintf('Unesite ocjenu za predmet %s: ', predmet));
  25.     student.predmeti{i} = predmet;
  26.     student.ocjene(i) = ocjena;
  27. end
  28.  
  29. student.prosjek = mean(student.ocjene);
  30.  
  31. fprintf('\nPodaci o studentu:\n');
  32. fprintf('Ime: %s\n', student.ime);
  33. fprintf('Prezime: %s\n', student.prezime);
  34. fprintf('Adresa: %s\n', student.adresa);
  35. fprintf('Položeni predmeti i ocjene:\n');
  36. for i = 1:MAX_PREDMETA
  37.     fprintf('%s: %d\n', student.predmeti{i}, student.ocjene(i));
  38. end
  39. fprintf('Prosjek ocjena: %.2f\n', student.prosjek);
  40.  
  41. % Upit korisnika za spremanje podataka
  42. odgovor = input('Želite li spremiti podatke u datoteku? (Y/N): ', 's');
  43. if strcmpi(odgovor, 'Y');
  44.     ime_datoteke = sprintf('%s_%s.txt', student.ime, student.prezime);
  45.    
  46.     % Otvaranje datoteke za pisanje
  47.     fid = fopen(ime_datoteke, 'w');
  48.    
  49.     % Provjera da li je uspješno otvorena datoteka
  50.     if fid == -1
  51.         error('Nije moguće otvoriti datoteku za pisanje.');
  52.     end
  53.    
  54.     % Spremanje podataka
  55.     fprintf(fid, 'Podaci o studentu:\n');
  56.     fprintf(fid, 'Ime: %s\n', student.ime);
  57.     fprintf(fid, 'Prezime: %s\n', student.prezime);
  58.     fprintf(fid, 'Adresa: %s\n', student.adresa);
  59.     fprintf(fid, 'Položeni predmeti i ocjene:\n');
  60.     for i = 1:MAX_PREDMETA
  61.         fprintf(fid, '%s: %d\n', student.predmeti{i}, student.ocjene(i));
  62.     end
  63.     fprintf(fid, 'Prosjek ocjena: %.2f\n', student.prosjek);
  64.    
  65.     % Zatvaranje datoteke
  66.     fclose(fid);
  67.    
  68.     fprintf('Podaci su uspješno spremljeni u datoteku %s.\n', ime_datoteke);
  69. else
  70.     fprintf('Podaci nisu spremljeni.\n');
  71. end
  72.