/* * struct.cpp * * Created on: 17.01.2013 * Author: trifon */ #include using namespace std; #include #include struct Complex { double re, im; }; struct Student { char name[100]; int fn; double grade; }; void printStudent(Student s) { cout << s.name << ' ' << s.fn << ' '; cout << s.grade << endl; s.grade = 6; } Student chooseBetter(Student s1, Student s2) { if (s1.grade > s2.grade) return s1; return s2; } int main1() { cout << sizeof(Complex) << endl; cout << sizeof(Student) << endl; Complex z1, z2 = { 1.2, 3.4 }; Student s1 = { "Иван Колев", 44444, 4.4 }; cout << z2.im << endl; z1 = z2; cout << z1.re << endl; cin >> z2.im; cout << z2.im << endl; char str1[10] = "Hello", str2[10] = "Hi"; char* pp = str1; cout << sizeof(str1) << ' ' << sizeof(pp) << endl; //!!!! str1 = str2; Student s2; s2 = s1; cout << s2.name << endl; int *p = &s1.fn; double *q = &s1.grade; char* s = s1.name; char* ss = s1.name + 5; // = &s1.name[5]; // !!! лошо, но вярно: // !!! int* x = (int*)&s1.grade; // !!! cout << *x << endl; Student sa[10] = { { "Иван Иванов", 80000, 5.5 }, { "Стефан Стефанов", 80001, 6 } }; // sa[1] = sa[0]; // OK // !!! sa[2].name = "Петър Петров"; strcpy(sa[2].name, "Петър Петров"); for(int i = 0; i < 3; i++) cout << sa[i].fn << endl; struct FootballTeam { Student players[11]; char name[100]; }; FootballTeam fteam = { { { "", 0, 0 } } }; fteam.players[1].fn = fteam.players[4].fn; fteam.players[5] = fteam.players[3]; strcpy(fteam.name, "The KNs"); strcpy(fteam.players[10].name, "Петко"); printStudent(s1); printStudent(s1); Student s3 = chooseBetter(sa[0], sa[1]); printStudent(s3); s3.grade = 3; printStudent(s3); printStudent(sa[1]); Student *ps1 = &s1, *ps2 = NULL; printStudent(*ps1); char* pp1 = s1.name; cout << ps1 << ' ' << (void*)pp1 << endl; // лошо char* pp2 = (char*)ps1; cout << pp2 << endl; *ps1 = sa[1]; // <-> s1 = sa[1]; printStudent(s1); Student& s4 = s1; s4.grade = 5; printStudent(s1); cout << (*ps1).name << endl; // !!! *ps1.name <-> *(ps1.name) ps1->grade += 0.5; // <-> (*ps1).grade += 0.5; // <-> s1.grade += 0.5; struct Employee { char name[100]; Employee* boss; }; Employee rector = { "Илчев", NULL }, dean = { "Сосков", &rector }, chair = { "Димитров", &dean }, assistant = { "Трифонов", &chair }; cout << assistant.name << endl << assistant.boss->name << endl << assistant.boss->boss->name << endl << assistant.boss->boss->boss->name << endl; cout << (void*)assistant.boss->boss->boss->boss << endl; Employee* pe = &assistant; while (pe != NULL) { cout << pe->name << endl; pe = pe->boss; } return 0; } const int MAX = 100; int readStudents(Student s[]) { int count = 0; do { cin >> s[count].fn; if (s[count].fn >= 0) { cin >> s[count].grade; cin.getline(s[count].name, MAX); } count++; } while(s[count-1].fn >= 0); // s[count-1].fn < 0 return count-1; } void printStudentsTable(Student s[], int n) { for(int i = 0; i < n; i++) { cout << s[i].fn << '\t' << s[i].grade << '\t'; cout << s[i].name << endl; } } double averageGrade(Student s[], int n) { double sum = 0; for(int i = 0; i < n; i++) sum += s[i].grade; return sum / n; } void arrangeStudents(Student s[], int n) { for(int i = 0; i < n - 1; i++) { // сортираната част е от // 0 до i-1 включително // намираме студента с най-малък Ф№ // и го разменяме с s[i] int min = i; for(int j = i + 1; j < n; j++) { if (s[j].fn < s[min].fn) { min = j; } } // разменяме s[i] със s[min] Student tmp = s[i]; s[i] = s[min]; s[min] = tmp; // Сортираната част е от 0 до i вкл. } } void arrangeStudentsGrade(Student s[], int n) { for(int i = 0; i < n - 1; i++) { // сортираната част е от // 0 до i-1 включително // намираме студента с най-малък Ф№ // и го разменяме с s[i] int max = i; for(int j = i + 1; j < n; j++) { if (s[j].grade > s[max].grade) { max = j; } } // разменяме s[i] със s[min] Student tmp = s[i]; s[i] = s[max]; s[max] = tmp; // Сортираната част е от 0 до i вкл. } } bool isLessByFn(Student s1, Student s2) { return s1.fn < s2.fn; } bool hasBetterGrade(Student s1, Student s2) { return s1.grade > s2.grade; } void arrangeStudentsGeneral(Student s[], int n, bool (*isLess)(Student,Student)) { for(int i = 0; i < n - 1; i++) { // сортираната част е от // 0 до i-1 включително // намираме "минималния" студент // и го разменяме с s[i] int min = i; for(int j = i + 1; j < n; j++) { if (isLess(s[j],s[min])) { min = j; } } // разменяме s[i] със s[min] Student tmp = s[i]; s[i] = s[min]; s[min] = tmp; // Сортираната част е от 0 до i вкл. } } int main2() { Student students[MAX]; int n = readStudents(students); cout << "Прочетени " << n << " студента" << endl; printStudentsTable(students, n); cout << "Среден успех: " << averageGrade(students, n) << endl; arrangeStudentsGeneral(students, n, isLessByFn); printStudentsTable(students, n); arrangeStudentsGeneral(students, n, hasBetterGrade); printStudentsTable(students, n); return 0; } int gcd(int x, int y) { if (x == y) return x; if (x > y) return gcd(x - y, y); return gcd(x, y - x); } struct Rational { int numer, denom; }; Rational createRational(int n, int d) { if (d == 0) { cerr << "Деление на 0!"; exit(1); } int g = gcd(n, d); n /= g; d /= g; Rational r = { n, d }; // r.numer = n; r.denom = d; return r; } int getNumerator(Rational r) { return r.numer; } int getDenominator(Rational r) { return r.denom; } Rational addRational(Rational r1, Rational r2) { return createRational( getNumerator(r1) * getDenominator(r2) + getNumerator(r2) * getDenominator(r1), getDenominator(r1) * getDenominator(r2)); } Rational subtractRational(Rational r1, Rational r2) { return createRational( getNumerator(r1) * getDenominator(r2) - getNumerator(r2) * getDenominator(r1), getDenominator(r1) * getDenominator(r2)); } Rational multRational(Rational r1, Rational r2) { return createRational( getNumerator(r1) * getNumerator(r2), getDenominator(r1) * getDenominator(r2)); } Rational divRational(Rational r1, Rational r2) { return createRational( getNumerator(r1) * getDenominator(r2), getDenominator(r1) * getNumerator(r2)); } void printRational(Rational r) { cout << getNumerator(r) << '/' << getDenominator(r); } Rational readRational() { int n, d; cin >> n >> d; return createRational(n, d); } int main() { Rational r1 = readRational(), r2 = readRational(); Rational r = addRational(r1, r2); printRational(r); return 0; }