Advertisement
Five_NT

[ FII ] [ IP ] Tema 1

Oct 15th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.43 KB | None | 0 0
  1. #include <iostream>
  2. #include "header1.h"
  3.  
  4. using namespace std;
  5.  
  6. unsigned int computeDigitSum(unsigned int number) // Exercitiul 1
  7. {
  8.     int sum = 0;
  9.     while(number) {
  10.         sum += number % 10;
  11.         number /= 10;
  12.     }
  13.  
  14.     return sum;
  15. }
  16.  
  17. bool isLeap(unsigned int year) // Exercitiul 2
  18. {
  19.     if(year % 4 == 0) {
  20.         if(year % 100 == 0 && year % 400) return false;
  21.         return true;
  22.     }
  23.     return false;
  24. }
  25.  
  26. bool isPalindrome(int number) // Exercitiul 3 !!!
  27. {
  28.     int og = 0;
  29.     int number2 = number;
  30.     while(number2) {
  31.         og = og * 10 + number2 % 10;
  32.         number2 /= 10;
  33.     }
  34.     if(og == number) return true;
  35.     return false;
  36. }
  37.  
  38. unsigned int computeNumberOfDays(unsigned int year, unsigned int month) // Exercitiul 4
  39. {
  40.  
  41.  
  42.     if(month < 1 || month > 12) {
  43.         cout << "Luna invalida.";
  44.         return false;
  45.     }
  46.     if(month == 2) {
  47.         if(isLeap(year) == true) return 29;
  48.         return 28;
  49.     }
  50.     if(month < 8) {
  51.         if(month % 2) return 31;
  52.         return 30;
  53.     }
  54.     if(month > 7) {
  55.         if(month % 2) return 30;
  56.         return 31;
  57.     }
  58.     return false;
  59. }
  60. char const*MonthName(unsigned int month) // numar luna -> nume luna
  61. {
  62.     if(month == 1)
  63.         return "Ianuarie";
  64.     else if(month == 2)
  65.         return "Februarie";
  66.     else if(month == 3)
  67.         return "Martie";
  68.     else if(month == 4)
  69.         return "Aprilie";
  70.     else if(month == 5)
  71.         return "Mai";
  72.     else if(month == 6)
  73.         return "Iunie";
  74.     else if(month == 7)
  75.         return "Iulie";
  76.     else if(month == 8)
  77.         return "August";
  78.     else if(month == 9)
  79.         return "Septembrie";
  80.     else if(month == 10)
  81.         return "Octombrie";
  82.     else if(month == 11)
  83.         return "Noiembrie";
  84.     else if(month == 12)
  85.         return "Decembrie";
  86.     else return "Unknown";
  87. }
  88.  
  89. unsigned int computeControlDigit(unsigned int number) // Exercitiul 5
  90. {
  91.     int s = 0;
  92.     while(number > 9)
  93.     {
  94.         s = 0;
  95.         while(number > 0)
  96.         {
  97.             s += number%10;
  98.             number /= 10;
  99.         }
  100.         number = s;
  101.     }
  102.     return s;
  103. }
  104.  
  105. bool isPerfect(unsigned int number) // Exercitiul 6
  106. {
  107.  
  108.  
  109.     unsigned int sum = 0;
  110.     for(unsigned int d = 1; d <= number/2; d++)
  111.         if(number % d == 0)
  112.             sum += d;
  113.     if(sum == number) return true;
  114.     return false;
  115. }
  116. vector getPerfects(unsigned int upperLimit) // Exercitiul 6
  117. {
  118.     vector a;
  119.     a.length = 0;
  120.     for(unsigned int i=1; i<upperLimit; i++)
  121.         if(isPerfect(i) == true)
  122.             a.values[a.length++] = i;
  123.     return a;
  124. }
  125.  
  126. bool isPrime(int number) // Exercitiul 7
  127. {
  128.     if(number % 2 == 0) return false;
  129.     for(int d=3; d<=sqrt(number); d=d+2)
  130.         if(number % d == 0)
  131.             return false;
  132.     return true;
  133. }
  134.  
  135. bool areTwinPrimes(unsigned int number1, unsigned int number2) // Exercitiul 8
  136. {
  137.     if(isPrime(number1) == true && isPrime(number2) == true) return true;
  138.     return false;
  139. }
  140.  
  141. matrix getTwinPrimes(unsigned int count) // Exercitiul 8
  142. {
  143.     matrix a;
  144.     unsigned int i = 3;
  145.     while(a.lines < count) {
  146.         if(areTwinPrimes(i, i+2) == true) {
  147.             a.values[a.lines][0] = i;
  148.             a.values[a.lines][1] = i + 2;
  149.             a.lines++;
  150.         }
  151.         i = i + 2;
  152.     }
  153.     return a;
  154. }
  155.  
  156. unsigned int getMaxDifference(vector v) // Exercitiul 9
  157. {
  158.     int maxi = v.values[0];
  159.     int mini = v.values[0];
  160.     for(unsigned int i=0; i<v.length; i++) {
  161.         if(maxi < v.values[i]) maxi = v.values[i];
  162.         if(mini > v.values[i]) mini = v.values[i];
  163.     }
  164.     return maxi - mini;
  165. }
  166.  
  167. sequence getMaxSequence(vector v) // Exercitiul 10
  168. {
  169.     int seq_val = 0;//, seq_len = 0;
  170.     sequence s1, s2;
  171.     unsigned int i = 0;
  172.     s2.length = 0;
  173.     while(i < v.length) {
  174.         seq_val = v.values[i];
  175.         s1.length = 1;
  176.         s1.startPosition = i;
  177.         i++;
  178.         while(seq_val == v.values[i]) {
  179.             s1.length++;
  180.             i ++;
  181.         }
  182.         if(s1.length > s2.length) {
  183.             //seq_len = seq_val;
  184.             s2.startPosition = s1.startPosition;
  185.             s2.length = s1.length;
  186.         }
  187.         if(s1.length == s2.length) {
  188.             if(s1.startPosition < s2.startPosition) {
  189.                 s1.startPosition = s2.startPosition;
  190.                 s1.length = s2.length;
  191.             }
  192.         }
  193.     }
  194.     return s2;
  195. }
  196.  
  197. sequence getMaxSumSequence(vector v) // Exercitiul 11
  198. {
  199.     sequence a;
  200.     int bestSum = 0, minimum = 0, idx, start, stop;
  201.     for(unsigned int i = 0; i < v.length; i++) {
  202.         v.values[i] += v.values[i - 1];
  203.         if (bestSum < v.values[i] - minimum) {
  204.             bestSum = v.values[i] - minimum;
  205.             start = idx + 1;
  206.             stop = i;
  207.         }
  208.         if (minimum > v.values[i]) {
  209.             minimum = v.values[i];
  210.             idx = i;
  211.         }
  212.  
  213.     }
  214.     a.startPosition = start;
  215.     a.length = stop - start;
  216.     return a;
  217. }
  218.  
  219. vector orderElements(vector v) // Exercitiul 12
  220. {
  221.     vector par, impar;
  222.     par.length = 0; impar.length = 0;
  223.     for(unsigned int i = 0; i < v.length; i++) {
  224.         if(v.values[i] % 2 == 0)
  225.             par.values[par.length++] = v.values[i];
  226.         else
  227.             impar.values[impar.length++] = v.values[i];
  228.     }
  229.     for(unsigned int i = 0; i < par.length; i++)
  230.         v.values[i] = par.values[i];
  231.     for(unsigned int i = par.length; i < v.length; i++)
  232.         v.values[i] = impar.values[i-par.length];
  233.     return v;
  234. }
  235.  
  236. bool containsMajorElement(vector v) // Exercitiul 13
  237. {
  238.     unsigned int nr = 1;
  239.     int cand = v.values[0];
  240.     for(unsigned int i = 1; i < v.length; i++)
  241.     {
  242.         if(cand == v.values[i]) nr++;
  243.         else nr--;
  244.         if(nr == 0)
  245.         {
  246.             cand = v.values[i];
  247.             nr = 1;
  248.         }
  249.     }
  250.     nr = 0;
  251.     for(unsigned int i = 0; i < v.length; i++)
  252.         if(cand == v.values[i]) nr++;
  253.     if(nr > v.length/2) return true;
  254.     return false;
  255.  
  256. }
  257.  
  258. matrix sortDiagonalElements(matrix m) // Exercitiul 14
  259. {
  260.     int aux;
  261.     for (unsigned int i = 0; i < m.lines - 1; i++)
  262.         for (unsigned int j = i + 1; j < m.lines; j++)
  263.             if (m.values[i][i] < m.values[j][j]) {
  264.                 for (unsigned int x = 0; x < m.lines; x++) {
  265.                     aux = m.values[x][j];
  266.                     m.values[x][j] = m.values[x][i];
  267.                     m.values[x][i] = aux;
  268.                 }
  269.                 for (unsigned int x = 0; x < m.lines; x++) {
  270.                     aux = m.values[j][x];
  271.                     m.values[j][x] = m.values[i][x];
  272.                     m.values[i][x] = aux;
  273.                 }
  274.             }
  275.     return m;
  276.  
  277. }
  278.  
  279. vector getSpiralVector(matrix m)// Exercitiul 15
  280. {
  281.     vector v;
  282.     v.length = 0;
  283.  
  284.     for(unsigned int i=1; i<=(m.lines/2)+(m.lines%2); i++)
  285.     {
  286.         for (unsigned int j = i; j <= m.columns-i+1; j++) v.values[v.length++] = m.values[i][j];
  287.         for (unsigned int j = 1+i; j <= m.lines-i+1; j++) v.values[v.length++] = m.values[j][m.columns-i+1];
  288.         for (unsigned int j = m.columns-i; j >= i; j--) v.values[v.length++] = m.values[m.lines-i+1][j];
  289.         for (unsigned int j = m.lines-i; j >= i+1; j--) v.values[v.length++] = m.values[j][i];
  290.     }
  291.     return v;
  292. }
  293.  
  294. date getNextDate(date currentDate) // Exercitiul 16
  295. {
  296.     int edit[2] = {0};
  297.     if(isLeap(currentDate.year) == true) {
  298.         if(currentDate.month == 2 && edit[0] == 0 && edit[1] == 0) {
  299.             if(currentDate.day == 29) {
  300.                 currentDate.day = 1;
  301.                 currentDate.month ++;
  302.                 edit[0] = 1;
  303.                 edit[1] = 1;
  304.             }
  305.             else {
  306.                 currentDate.day ++;
  307.                 edit[0] = 1;
  308.             }
  309.         }
  310.     }
  311.     if(currentDate.month == 1 || currentDate.month == 3 || currentDate.month == 5 || currentDate.month == 7 || currentDate.month == 8 || currentDate.month == 10 || currentDate.month == 12) {
  312.         if(currentDate.day == 31) {
  313.             if(edit[0] == 0) {
  314.                 currentDate.day = 1;
  315.                 currentDate.month ++;
  316.                 edit[0] = 1, edit[1] = 1;
  317.             }
  318.         }
  319.         else
  320.             if(edit[0] == 0) {
  321.                 currentDate.day ++;
  322.                 edit[0] = 1;
  323.             }
  324.     }
  325.     else {
  326.         if(currentDate.day == 30) {
  327.             if(edit[0] == 0 && edit[1] == 0) {
  328.                 currentDate.day = 1;
  329.                 currentDate.month ++;
  330.                 edit[0] = 1, edit[1] = 1;
  331.             }
  332.         }
  333.         else
  334.             if(edit[0] == 0) {
  335.                 currentDate.day ++;
  336.                 edit[0] = 1;
  337.             }
  338.     }
  339.     if(currentDate.month >= 13)
  340.         if(edit[1] == 1)
  341.             currentDate.month = 1;
  342.     if(currentDate.day == 1 && currentDate.month == 1)
  343.         currentDate.year ++;
  344.  
  345.     return currentDate;
  346. }
  347.  
  348. equation computeEquation(point point1, point point2) // Exercitiul 17
  349. {
  350.     equation ec;
  351.     ec.a = point2.y - point1.y;
  352.     ec.b = -1 * (point2.x - point1.x);
  353.     ec.c = point1.y * point2.x - point1.x * point2.y;
  354.     return ec;
  355. }
  356.  
  357. int main()
  358. {
  359.     unsigned int n;
  360.     cout << "* Exercitiul 01: Sa se afiseze suma cifrelor unui numar n." << '\n';
  361.     cout << "\nIntroduceti numarul: "; cin >> n;
  362.     cout << "\tSuma cifrelor numarului " << n << " este " << computeDigitSum(n) << "\n\n";
  363.  
  364.  
  365.     cout << "* Exercitiul 02: Sa se verifice daca un an este bisect." << '\n';
  366.     cout << "\nIntroduceti anul: "; cin >> n;
  367.     if(isLeap(n) == true) cout << "\tAnul " << n << " este an bisect." << '\n';
  368.     else cout << "\tAnul " << n << " nu este an bisect." << "\n\n";
  369.  
  370.  
  371.     int m;
  372.     cout << "* Exercitiul 03: Sa se verifice daca un numar este palindrom." << '\n';
  373.     cout << "\nIntroduceti numarul: "; cin >> m;
  374.     if(isPalindrome(m) == true) cout << "\tNumarul " << m << " este palindrom." << '\n';
  375.     else cout << "\tNumarul " << m << " nu este palindrom." << "\n\n";
  376.  
  377.  
  378.     cout << "* Exercitiul 04: Sa se calculeze numarul de zile dintr-o luna si un an dat." << '\n';
  379.     unsigned mo;
  380.     cout << "\nIntroduceti anul: "; cin >> n;
  381.     cout << "Introduceti luna: "; cin >> mo;
  382.     cout << "\tLuna " << MonthName(mo) << " din anul " << n << " are " << computeNumberOfDays(n, mo) << " zile." << "\n\n";
  383.  
  384.  
  385.     cout << "* Exercitiul 05: Sa se calculeze cifra de control a unui numar." << '\n';
  386.     cout << "\nIntroduceti numarul: "; cin >> n;
  387.     cout << "\tCifra de control a numarului " << n << " este " << computeControlDigit(n) << "." << "\n\n";
  388.  
  389.  
  390.     cout << "* Exercitiul 06: Sa se afiseze toate numerele perfect pana la un n dat." << '\n';
  391.     cout << "\nIntruduceti numarul n: "; cin >> n;
  392.     vector a = getPerfects(n);
  393.     for(unsigned int i = 0; i < a.length; i++)
  394.         cout << a.values[i] << " ";
  395.     cout << "\n\n";
  396.  
  397.  
  398.     cout << "* Exercitiul 07: Sa se testeze daca un numar este prim." << '\n';
  399.     cout << "\nIntroduceti numarul: "; cin >> n;
  400.     if(isPrime(n) == true) cout << "\tNumarul " << n << " este prim." << '\n';
  401.     else cout << "\t Numarul " << n << " nu este prim." << "\n\n";
  402.  
  403.  
  404.     cout << "* Exercitiul 08: Determinati primele n perechi de numere gemene." << '\n';
  405.     cout << "\nIntroduceti numarul de perechi: "; cin >> n;
  406.     matrix b = getTwinPrimes(n);
  407.     for(unsigned int i = 0; i < b.lines; i++)
  408.             cout << i+1 << ". (" << b.values[i][0] << "," << b.values[i][1] << ") " << '\n';
  409.     cout << "\n\n";
  410.  
  411.  
  412.     vector p9;
  413.     cout << "* Exercitiul 09: Determinati diferenta maxima(in modul) din oricare doua elemente ale unui vector." << '\n';
  414.     cout << "Numarul de elemente: "; cin >> p9.length;
  415.     cout << "Elementele vectorului: ";
  416.     for(unsigned int i = 0; i < p9.length; i++)
  417.         cin >> p9.values[i];
  418.     cout << "Diferenta maxima: " << getMaxDifference(p9) << "." << "\n\n";
  419.  
  420.  
  421.     vector p10;
  422.     cout << "* Exercitiul 10: Sa se determine pozitia de inceput si lungimea celei mai lungi secvente de elemente egale dintr-un vector." << '\n';
  423.     cout << "Numarul de elemente: "; cin >> p10.length;
  424.     cout << "Elementele vectorului: ";
  425.     for(unsigned int i = 0; i < p10.length; i++)
  426.         cin >> p10.values[i];
  427.     sequence c = getMaxSequence(p10);
  428.     cout << "Cea mai lunga secventa incepe din pozitia " << c.startPosition << " si contine " << c.length << " elemente.\n\n";
  429.  
  430. /*
  431.     vector p11;
  432.     cout << "* Exercitiul 11: Sa se determine subsecventa pentru care suma elementelor componente este maxima." << '\n';
  433.     cout << "Numarul de elemente: "; cin >> p11.length;
  434.     cout << "Elementele vectorului: ";
  435.     cin >> p11.length;
  436.     for(unsigned int i = 0; i < p11.length; i++)
  437.         cin >> p11.values[i];
  438.     sequence d = getMaxSumSequence(p11);
  439.     cout << "Rezultat:\n";
  440.     unsigned int sum = 0;
  441.     for(unsigned int i = d.startPosition; i <= d.length; i++) {
  442.         cout << d.values[i] << " + ";
  443.         sum += d.values[i];
  444.     }
  445.     cout << sum;
  446. */
  447.  
  448.     vector p12;
  449.     cout << "* Exercitiul 12: Sa se rearanjeze elementele unui vector astfel incat cele pare sa apara inaintea celor impare. " << '\n';
  450.     cout << "Numarul de elemente: "; cin >> p12.length;
  451.     cout << "Elementele vectorului: ";
  452.     for(unsigned int i = 0; i < p12.length; i++)
  453.         cin >> p12.values[i];
  454.     vector e = orderElements(p12);
  455.     cout << "Vectorul rearanjat: ";
  456.     for(unsigned int i = 0; i < e.length; i++)
  457.         cout << e.values[i] << " ";
  458.     cout << "\n\n";
  459.  
  460.  
  461.     vector p13;
  462.     cout << "* Exercitiul 13: Sa se decida daca exista un element majoritar." << '\n';
  463.     cout << "Numarul de elemente: "; cin >> p13.length;
  464.     cout << "Elementele vectorului: ";
  465.     for(unsigned int i = 0; i < p13.length; i++)
  466.         cin >> p13.values[i];
  467.     if(containsMajorElement(p13) == true)
  468.         cout << "Exista un element majoritar.\n\n";
  469.     else cout<<"Nu exista niciun element majoritar\n\n";
  470.  
  471.  
  472.     matrix p14;
  473.     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';
  474.     cout << "Numarul de linii: "; cin >> p14.lines;
  475.     cout << "Numarul de coloane: "; cin >> p14.columns;
  476.     cout << "Elementele matricei: \n";
  477.     for(unsigned int i=0; i<p14.lines; i++)
  478.         for(unsigned int j=0; j<p14.columns; j++)
  479.             cin >> p14.values[i][j];
  480.     matrix f = sortDiagonalElements(p14);
  481.     cout << "Matricea rearanjata: \n";
  482.     for(unsigned int i=0; i<f.lines; i++)
  483.     {
  484.         for(unsigned int j=0; j<f.columns; j++)
  485.             cout << f.values[i][j] << " ";
  486.         cout << '\n';
  487.     }
  488.     cout << "\n\n";
  489.  
  490.     matrix p15;
  491.     cout << "*Exercitiul 15: Parcurgerea unei matrici in spitala.";
  492.     cout << "Dimensiune matrice: "; cin >> p15.lines;
  493.     cout << "Numar coloane: "; cin >> p15.columns;
  494.     cout << "Introduceti matricea: \n";
  495.     for(unsigned int i=1; i <= p15.lines; i++)
  496.         for(unsigned int j=1; j <= p15.columns; j++)
  497.             cin >> p15.values[i][j];
  498.     vector g = getSpiralVector(p15);
  499.     cout << "Elementele matricei in spirala: \n";
  500.     for(unsigned int i=0; i<g.length; i++)
  501.         cout << g.values[i] << " ";
  502.     cout << "\n\n";
  503.  
  504.  
  505.     date p16;
  506.     cout << "* Exercitiul 16: Sa se scrie o functie care primeste la intrare o data calendaristica si returneaza succesorul acesteia." << '\n';
  507.     cout << "Introduceti ziua: "; cin >> p16.day;
  508.     if(p16.day < 1 || p16.day > 31) cout << "Atentie ! Data eronata. Programul nu va functiona corect.";
  509.     cout << "Introduceti luna: "; cin >> p16.month;
  510.     if(p16.month < 1 || p16.month > 12) cout << "Atentie ! Data eronata. Programul nu va functiona corect.";
  511.     cout << "Introduceti anul: "; cin >> p16.year;
  512.     date h = getNextDate(p16);
  513.     cout << "\nDupa data de " << p16.day << MonthName(p16.month) << p16.year << " urmeaza " << h.day << MonthName(h.month) << h.year << ".\n\n";
  514.     cout << "\n\n";
  515.  
  516. //    equation p17;
  517.     point point1;
  518.     point point2;
  519.     cout << "* Exercitiul 17: Sa se determine ecuatia unei drepte care trece prin doua puncte in plan.\n";
  520.     cout << "Coordonatele punctului A: "; cin >> point1.x >> point1.y;
  521.     cout << "Coordonatele punctului B: "; cin >> point2.x >> point2.y;
  522.     equation dreapta = computeEquation(point1, point2);
  523.     cout << "Dreapta care trece prin punctele A(" << point1.x << "," << point1.y << ") si B(" << point2.x << "," << point2.y << ") are ecuatia ";
  524.     cout << dreapta.a << "x ";
  525.     if(dreapta.b < 0) cout << dreapta.b << "y ";
  526.     else cout << "+ " << dreapta.b << "y ";
  527.  
  528.     if(dreapta.c < 0) cout << dreapta.c << " = 0.";
  529.     else cout << "+ " << dreapta.c << " = 0.";
  530.  
  531.     return 0;
  532. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement