ostapdontstop

ruslan

Jun 24th, 2018
121
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstring>
  3. #include <windows.h>
  4. using namespace std;
  5.  
  6.  
  7. struct holod{
  8.     char country[80], model[80];
  9.     int stock;
  10. };
  11.  
  12. void print (const holod &a)
  13. {
  14.     cout << a.country<<"  "<< a.model <<"  "<< a.stock <<endl;
  15. }
  16.  
  17.  
  18. int search (holod *a, int n, const char *old, int position)
  19. {
  20.     for (int i=position; i<n; i++) if (strncmp(a[i].model, old, 4)==0) return i;
  21.     return n;  
  22. }
  23.  
  24. main()
  25. {
  26. SetConsoleCP(1251);
  27. SetConsoleOutputCP(1251);
  28. int n = 4;
  29. holod a[n] = {
  30.     {"russian","2018samsung", 2},
  31.     {"german","2016Bosh", 2},
  32.     {"japan","2017sharp", 2},
  33.     {"french","2016LG",2}
  34. };
  35.  
  36. for (int i = 0; i<n ; i++)
  37.     print(a[i]);
  38. char old[5];
  39. strncpy(old,a[0].model,4);
  40. for (int i = 0; i < n; i++) {
  41.     if(strncmp(old,a[i].model,4)>0) strncpy(old,a[i].model,4);
  42. }
  43. cout << "search:\n";
  44. int s = search(a, n, old,0);
  45. while (s!=n) {
  46.     print (a[s]);
  47.     s = search(a, n, old,s+1);
  48. }
  49.  
  50. }
RAW Paste Data