Untitled
unknown
plain_text
2 years ago
1.7 kB
10
Indexable
enum class Plec {kobieta,mezczyzna};
enum class StopienStudiow{I,II,III};
struct Student
{
char imie[40];
char nazwisko[40];
Plec plec;
StopienStudiow stopien;
int semestr;
};
void wypelnijDane(Student* st, const char* imie, const char* nazwisko, int s, Plec p, StopienStudiow stop)
{
strcpy_s(st->imie, imie);
strcpy_s(st->nazwisko, nazwisko);
st->plec = p;
st->semestr = s;
st->stopien = stop;
}
int kodowanie(Student st)
{
int kod = 0;
kod = kod << 2;
kod = (kod << 1);
kod += (int)st.plec;
kod = (kod << 2);
kod += (int)st.stopien;
kod = (kod << 3);
kod += (int)st.semestr;
return kod;
}
void ZapisDoPliku(Student st, FILE* plik)
{
if (plik != nullptr)
{
int kod = kodowanie(st);
fprintf(plik, "%s %s %d\n", st.imie, st.nazwisko,kod);
}
}
void dekodowanie(Student* st,int kod)
{
st->semestr = (int)(kod % 8);
kod = kod >> 3;
st->stopien = (StopienStudiow)(kod % 4);
kod = kod >> 2;
st->plec = (Plec)(kod % 2);
kod = kod >> 1;
}
void odczytZpliku(Student* st, FILE* plik)
{
if (plik != nullptr)
{
fscanf(plik, "%s", st->imie);
fscanf(plik, "%s", st->nazwisko);
int kod;
fscanf(plik, "%d\n", &kod);
dekodowanie(st, kod);
}
}
int main()
{
Student mw, sd;
wypelnijDane(&mw, "Michal", "Wilkosz", 5, Plec::mezczyzna, StopienStudiow::I);
FILE* plik = nullptr;
plik = fopen("student.txt", "a");
ZapisDoPliku(mw, plik);
fclose(plik);
plik = fopen("student.txt", "r");
odczytZpliku(&sd, plik);
std::cout << sd.imie<<std::endl;
std::cout << sd.nazwisko<<std::endl;
std::cout << sd.semestr << std::endl << (int)sd.plec << std::endl << (int)sd.stopien;
}
#endifEditor is loading...