Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "header1.h"
- using namespace std;
- unsigned int computeDigitSum(unsigned int number) // Exercitiul 1
- {
- int sum = 0;
- while(number) {
- sum += number % 10;
- number /= 10;
- }
- return sum;
- }
- bool isLeap(unsigned int year) // Exercitiul 2
- {
- if(year % 4 == 0) {
- if(year % 100 == 0 && year % 400) return false;
- return true;
- }
- return false;
- }
- bool isPalindrome(int number) // Exercitiul 3 !!!
- {
- int og = 0;
- int number2 = number;
- while(number2) {
- og = og * 10 + number2 % 10;
- number2 /= 10;
- }
- if(og == number) return true;
- return false;
- }
- unsigned int computeNumberOfDays(unsigned int year, unsigned int month) // Exercitiul 4
- {
- if(month < 1 || month > 12) {
- cout << "Luna invalida.";
- return false;
- }
- if(month == 2) {
- if(isLeap(year) == true) return 29;
- return 28;
- }
- if(month < 8) {
- if(month % 2) return 31;
- return 30;
- }
- if(month > 7) {
- if(month % 2) return 30;
- return 31;
- }
- return false;
- }
- char const*MonthName(unsigned int month) // numar luna -> nume luna
- {
- if(month == 1)
- return "Ianuarie";
- else if(month == 2)
- return "Februarie";
- else if(month == 3)
- return "Martie";
- else if(month == 4)
- return "Aprilie";
- else if(month == 5)
- return "Mai";
- else if(month == 6)
- return "Iunie";
- else if(month == 7)
- return "Iulie";
- else if(month == 8)
- return "August";
- else if(month == 9)
- return "Septembrie";
- else if(month == 10)
- return "Octombrie";
- else if(month == 11)
- return "Noiembrie";
- else if(month == 12)
- return "Decembrie";
- else return "Unknown";
- }
- unsigned int computeControlDigit(unsigned int number) // Exercitiul 5
- {
- int s = 0;
- while(number > 9)
- {
- s = 0;
- while(number > 0)
- {
- s += number%10;
- number /= 10;
- }
- number = s;
- }
- return s;
- }
- bool isPerfect(unsigned int number) // Exercitiul 6
- {
- unsigned int sum = 0;
- for(unsigned int d = 1; d <= number/2; d++)
- if(number % d == 0)
- sum += d;
- if(sum == number) return true;
- return false;
- }
- vector getPerfects(unsigned int upperLimit) // Exercitiul 6
- {
- vector a;
- a.length = 0;
- for(unsigned int i=1; i<upperLimit; i++)
- if(isPerfect(i) == true)
- a.values[a.length++] = i;
- return a;
- }
- bool isPrime(int number) // Exercitiul 7
- {
- if(number % 2 == 0) return false;
- for(int d=3; d<=sqrt(number); d=d+2)
- if(number % d == 0)
- return false;
- return true;
- }
- bool areTwinPrimes(unsigned int number1, unsigned int number2) // Exercitiul 8
- {
- if(isPrime(number1) == true && isPrime(number2) == true) return true;
- return false;
- }
- matrix getTwinPrimes(unsigned int count) // Exercitiul 8
- {
- matrix a;
- unsigned int i = 3;
- while(a.lines < count) {
- if(areTwinPrimes(i, i+2) == true) {
- a.values[a.lines][0] = i;
- a.values[a.lines][1] = i + 2;
- a.lines++;
- }
- i = i + 2;
- }
- return a;
- }
- unsigned int getMaxDifference(vector v) // Exercitiul 9
- {
- int maxi = v.values[0];
- int mini = v.values[0];
- for(unsigned int i=0; i<v.length; i++) {
- if(maxi < v.values[i]) maxi = v.values[i];
- if(mini > v.values[i]) mini = v.values[i];
- }
- return maxi - mini;
- }
- sequence getMaxSequence(vector v) // Exercitiul 10
- {
- int seq_val = 0;//, seq_len = 0;
- sequence s1, s2;
- unsigned int i = 0;
- s2.length = 0;
- while(i < v.length) {
- seq_val = v.values[i];
- s1.length = 1;
- s1.startPosition = i;
- i++;
- while(seq_val == v.values[i]) {
- s1.length++;
- i ++;
- }
- if(s1.length > s2.length) {
- //seq_len = seq_val;
- s2.startPosition = s1.startPosition;
- s2.length = s1.length;
- }
- if(s1.length == s2.length) {
- if(s1.startPosition < s2.startPosition) {
- s1.startPosition = s2.startPosition;
- s1.length = s2.length;
- }
- }
- }
- return s2;
- }
- sequence getMaxSumSequence(vector v) // Exercitiul 11
- {
- sequence a;
- int bestSum = 0, minimum = 0, idx, start, stop;
- for(unsigned int i = 0; i < v.length; i++) {
- v.values[i] += v.values[i - 1];
- if (bestSum < v.values[i] - minimum) {
- bestSum = v.values[i] - minimum;
- start = idx + 1;
- stop = i;
- }
- if (minimum > v.values[i]) {
- minimum = v.values[i];
- idx = i;
- }
- }
- a.startPosition = start;
- a.length = stop - start;
- return a;
- }
- vector orderElements(vector v) // Exercitiul 12
- {
- vector par, impar;
- par.length = 0; impar.length = 0;
- for(unsigned int i = 0; i < v.length; i++) {
- if(v.values[i] % 2 == 0)
- par.values[par.length++] = v.values[i];
- else
- impar.values[impar.length++] = v.values[i];
- }
- for(unsigned int i = 0; i < par.length; i++)
- v.values[i] = par.values[i];
- for(unsigned int i = par.length; i < v.length; i++)
- v.values[i] = impar.values[i-par.length];
- return v;
- }
- bool containsMajorElement(vector v) // Exercitiul 13
- {
- unsigned int nr = 1;
- int cand = v.values[0];
- for(unsigned int i = 1; i < v.length; i++)
- {
- if(cand == v.values[i]) nr++;
- else nr--;
- if(nr == 0)
- {
- cand = v.values[i];
- nr = 1;
- }
- }
- nr = 0;
- for(unsigned int i = 0; i < v.length; i++)
- if(cand == v.values[i]) nr++;
- if(nr > v.length/2) return true;
- return false;
- }
- matrix sortDiagonalElements(matrix m) // Exercitiul 14
- {
- int aux;
- for (unsigned int i = 0; i < m.lines - 1; i++)
- for (unsigned int j = i + 1; j < m.lines; j++)
- if (m.values[i][i] < m.values[j][j]) {
- for (unsigned int x = 0; x < m.lines; x++) {
- aux = m.values[x][j];
- m.values[x][j] = m.values[x][i];
- m.values[x][i] = aux;
- }
- for (unsigned int x = 0; x < m.lines; x++) {
- aux = m.values[j][x];
- m.values[j][x] = m.values[i][x];
- m.values[i][x] = aux;
- }
- }
- return m;
- }
- vector getSpiralVector(matrix m)// Exercitiul 15
- {
- vector v;
- v.length = 0;
- for(unsigned int i=1; i<=(m.lines/2)+(m.lines%2); i++)
- {
- for (unsigned int j = i; j <= m.columns-i+1; j++) v.values[v.length++] = m.values[i][j];
- for (unsigned int j = 1+i; j <= m.lines-i+1; j++) v.values[v.length++] = m.values[j][m.columns-i+1];
- for (unsigned int j = m.columns-i; j >= i; j--) v.values[v.length++] = m.values[m.lines-i+1][j];
- for (unsigned int j = m.lines-i; j >= i+1; j--) v.values[v.length++] = m.values[j][i];
- }
- return v;
- }
- date getNextDate(date currentDate) // Exercitiul 16
- {
- int edit[2] = {0};
- if(isLeap(currentDate.year) == true) {
- if(currentDate.month == 2 && edit[0] == 0 && edit[1] == 0) {
- if(currentDate.day == 29) {
- currentDate.day = 1;
- currentDate.month ++;
- edit[0] = 1;
- edit[1] = 1;
- }
- else {
- currentDate.day ++;
- edit[0] = 1;
- }
- }
- }
- if(currentDate.month == 1 || currentDate.month == 3 || currentDate.month == 5 || currentDate.month == 7 || currentDate.month == 8 || currentDate.month == 10 || currentDate.month == 12) {
- if(currentDate.day == 31) {
- if(edit[0] == 0) {
- currentDate.day = 1;
- currentDate.month ++;
- edit[0] = 1, edit[1] = 1;
- }
- }
- else
- if(edit[0] == 0) {
- currentDate.day ++;
- edit[0] = 1;
- }
- }
- else {
- if(currentDate.day == 30) {
- if(edit[0] == 0 && edit[1] == 0) {
- currentDate.day = 1;
- currentDate.month ++;
- edit[0] = 1, edit[1] = 1;
- }
- }
- else
- if(edit[0] == 0) {
- currentDate.day ++;
- edit[0] = 1;
- }
- }
- if(currentDate.month >= 13)
- if(edit[1] == 1)
- currentDate.month = 1;
- if(currentDate.day == 1 && currentDate.month == 1)
- currentDate.year ++;
- return currentDate;
- }
- equation computeEquation(point point1, point point2) // Exercitiul 17
- {
- equation ec;
- ec.a = point2.y - point1.y;
- ec.b = -1 * (point2.x - point1.x);
- ec.c = point1.y * point2.x - point1.x * point2.y;
- return ec;
- }
- int main()
- {
- unsigned int n;
- cout << "* Exercitiul 01: Sa se afiseze suma cifrelor unui numar n." << '\n';
- cout << "\nIntroduceti numarul: "; cin >> n;
- cout << "\tSuma cifrelor numarului " << n << " este " << computeDigitSum(n) << "\n\n";
- cout << "* Exercitiul 02: Sa se verifice daca un an este bisect." << '\n';
- cout << "\nIntroduceti anul: "; cin >> n;
- if(isLeap(n) == true) cout << "\tAnul " << n << " este an bisect." << '\n';
- else cout << "\tAnul " << n << " nu este an bisect." << "\n\n";
- int m;
- cout << "* Exercitiul 03: Sa se verifice daca un numar este palindrom." << '\n';
- cout << "\nIntroduceti numarul: "; cin >> m;
- if(isPalindrome(m) == true) cout << "\tNumarul " << m << " este palindrom." << '\n';
- else cout << "\tNumarul " << m << " nu este palindrom." << "\n\n";
- cout << "* Exercitiul 04: Sa se calculeze numarul de zile dintr-o luna si un an dat." << '\n';
- unsigned mo;
- cout << "\nIntroduceti anul: "; cin >> n;
- cout << "Introduceti luna: "; cin >> mo;
- cout << "\tLuna " << MonthName(mo) << " din anul " << n << " are " << computeNumberOfDays(n, mo) << " zile." << "\n\n";
- cout << "* Exercitiul 05: Sa se calculeze cifra de control a unui numar." << '\n';
- cout << "\nIntroduceti numarul: "; cin >> n;
- cout << "\tCifra de control a numarului " << n << " este " << computeControlDigit(n) << "." << "\n\n";
- cout << "* Exercitiul 06: Sa se afiseze toate numerele perfect pana la un n dat." << '\n';
- cout << "\nIntruduceti numarul n: "; cin >> n;
- vector a = getPerfects(n);
- for(unsigned int i = 0; i < a.length; i++)
- cout << a.values[i] << " ";
- cout << "\n\n";
- cout << "* Exercitiul 07: Sa se testeze daca un numar este prim." << '\n';
- cout << "\nIntroduceti numarul: "; cin >> n;
- if(isPrime(n) == true) cout << "\tNumarul " << n << " este prim." << '\n';
- else cout << "\t Numarul " << n << " nu este prim." << "\n\n";
- cout << "* Exercitiul 08: Determinati primele n perechi de numere gemene." << '\n';
- cout << "\nIntroduceti numarul de perechi: "; cin >> n;
- matrix b = getTwinPrimes(n);
- for(unsigned int i = 0; i < b.lines; i++)
- cout << i+1 << ". (" << b.values[i][0] << "," << b.values[i][1] << ") " << '\n';
- cout << "\n\n";
- vector p9;
- cout << "* Exercitiul 09: Determinati diferenta maxima(in modul) din oricare doua elemente ale unui vector." << '\n';
- cout << "Numarul de elemente: "; cin >> p9.length;
- cout << "Elementele vectorului: ";
- for(unsigned int i = 0; i < p9.length; i++)
- cin >> p9.values[i];
- cout << "Diferenta maxima: " << getMaxDifference(p9) << "." << "\n\n";
- vector p10;
- cout << "* Exercitiul 10: Sa se determine pozitia de inceput si lungimea celei mai lungi secvente de elemente egale dintr-un vector." << '\n';
- cout << "Numarul de elemente: "; cin >> p10.length;
- cout << "Elementele vectorului: ";
- for(unsigned int i = 0; i < p10.length; i++)
- cin >> p10.values[i];
- sequence c = getMaxSequence(p10);
- cout << "Cea mai lunga secventa incepe din pozitia " << c.startPosition << " si contine " << c.length << " elemente.\n\n";
- /*
- vector p11;
- cout << "* Exercitiul 11: Sa se determine subsecventa pentru care suma elementelor componente este maxima." << '\n';
- cout << "Numarul de elemente: "; cin >> p11.length;
- cout << "Elementele vectorului: ";
- cin >> p11.length;
- for(unsigned int i = 0; i < p11.length; i++)
- cin >> p11.values[i];
- sequence d = getMaxSumSequence(p11);
- cout << "Rezultat:\n";
- unsigned int sum = 0;
- for(unsigned int i = d.startPosition; i <= d.length; i++) {
- cout << d.values[i] << " + ";
- sum += d.values[i];
- }
- cout << sum;
- */
- vector p12;
- cout << "* Exercitiul 12: Sa se rearanjeze elementele unui vector astfel incat cele pare sa apara inaintea celor impare. " << '\n';
- cout << "Numarul de elemente: "; cin >> p12.length;
- cout << "Elementele vectorului: ";
- for(unsigned int i = 0; i < p12.length; i++)
- cin >> p12.values[i];
- vector e = orderElements(p12);
- cout << "Vectorul rearanjat: ";
- for(unsigned int i = 0; i < e.length; i++)
- cout << e.values[i] << " ";
- cout << "\n\n";
- vector p13;
- cout << "* Exercitiul 13: Sa se decida daca exista un element majoritar." << '\n';
- cout << "Numarul de elemente: "; cin >> p13.length;
- cout << "Elementele vectorului: ";
- for(unsigned int i = 0; i < p13.length; i++)
- cin >> p13.values[i];
- if(containsMajorElement(p13) == true)
- cout << "Exista un element majoritar.\n\n";
- else cout<<"Nu exista niciun element majoritar\n\n";
- matrix p14;
- cout << "* Exercitiul 14: Sa se interschimbe liniile si coloanele unei matrici astfel incat, in matricea finala, elementele de pe diagonala principala sa fie in ordine descrescatoare." << '\n';
- cout << "Numarul de linii: "; cin >> p14.lines;
- cout << "Numarul de coloane: "; cin >> p14.columns;
- cout << "Elementele matricei: \n";
- for(unsigned int i=0; i<p14.lines; i++)
- for(unsigned int j=0; j<p14.columns; j++)
- cin >> p14.values[i][j];
- matrix f = sortDiagonalElements(p14);
- cout << "Matricea rearanjata: \n";
- for(unsigned int i=0; i<f.lines; i++)
- {
- for(unsigned int j=0; j<f.columns; j++)
- cout << f.values[i][j] << " ";
- cout << '\n';
- }
- cout << "\n\n";
- matrix p15;
- cout << "*Exercitiul 15: Parcurgerea unei matrici in spitala.";
- cout << "Dimensiune matrice: "; cin >> p15.lines;
- cout << "Numar coloane: "; cin >> p15.columns;
- cout << "Introduceti matricea: \n";
- for(unsigned int i=1; i <= p15.lines; i++)
- for(unsigned int j=1; j <= p15.columns; j++)
- cin >> p15.values[i][j];
- vector g = getSpiralVector(p15);
- cout << "Elementele matricei in spirala: \n";
- for(unsigned int i=0; i<g.length; i++)
- cout << g.values[i] << " ";
- cout << "\n\n";
- date p16;
- cout << "* Exercitiul 16: Sa se scrie o functie care primeste la intrare o data calendaristica si returneaza succesorul acesteia." << '\n';
- cout << "Introduceti ziua: "; cin >> p16.day;
- if(p16.day < 1 || p16.day > 31) cout << "Atentie ! Data eronata. Programul nu va functiona corect.";
- cout << "Introduceti luna: "; cin >> p16.month;
- if(p16.month < 1 || p16.month > 12) cout << "Atentie ! Data eronata. Programul nu va functiona corect.";
- cout << "Introduceti anul: "; cin >> p16.year;
- date h = getNextDate(p16);
- cout << "\nDupa data de " << p16.day << MonthName(p16.month) << p16.year << " urmeaza " << h.day << MonthName(h.month) << h.year << ".\n\n";
- cout << "\n\n";
- // equation p17;
- point point1;
- point point2;
- cout << "* Exercitiul 17: Sa se determine ecuatia unei drepte care trece prin doua puncte in plan.\n";
- cout << "Coordonatele punctului A: "; cin >> point1.x >> point1.y;
- cout << "Coordonatele punctului B: "; cin >> point2.x >> point2.y;
- equation dreapta = computeEquation(point1, point2);
- cout << "Dreapta care trece prin punctele A(" << point1.x << "," << point1.y << ") si B(" << point2.x << "," << point2.y << ") are ecuatia ";
- cout << dreapta.a << "x ";
- if(dreapta.b < 0) cout << dreapta.b << "y ";
- else cout << "+ " << dreapta.b << "y ";
- if(dreapta.c < 0) cout << dreapta.c << " = 0.";
- else cout << "+ " << dreapta.c << " = 0.";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement