Advertisement
RlCK

plecak

May 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5. struct product
  6. {
  7.     string N;
  8.     float P;
  9.     float V;
  10.     int LP;
  11.     float P_V;
  12. };
  13. void sortuj(vector <product> &tab,int n)
  14. {
  15.     bool czy=1;
  16.     int k=n-1;
  17.     while(czy)
  18.     {
  19.         czy=0;
  20.         for(int i=0;i<k;i++)
  21.         {
  22.             if(tab[i].P_V>tab[i+1].P_V)
  23.             {
  24.                 swap(tab[i],tab[i+1]);
  25.                 czy=1;
  26.             }
  27.         }
  28.         k--;
  29.     }
  30. }
  31. void pakowanie(vector <product> &tab, vector <product> &plecak, float s)
  32. {
  33.     float suma=0;
  34.     for(int i=0;i<tab.size();i++)
  35.     {
  36.         plecak.push_back(tab[i]);
  37.         plecak[i].LP=0;
  38.         for(tab[i].LP;tab[i].LP>0;tab[i].LP--)
  39.         {
  40.             if(suma+tab[i].V<s)
  41.             {
  42.                 suma+=tab[i].V;
  43.                 plecak[i].LP++;
  44.             }
  45.             else
  46.                 break;
  47.         }
  48.     }
  49.     cout<<"Suma: "<<suma<<endl;
  50. }
  51. void wypisz(vector <product> &tab)
  52. {
  53.     cout<<"nazwa\tcena\tobj\tLP\tstosunek ceny do objetosci"<<endl;
  54.     for(int i=0;i<tab.size();i++)
  55.     {
  56.         if(tab[i].LP>0)
  57.         cout<<tab[i].N<<"\t"<<tab[i].P<<"\t"<<tab[i].V<<"\t"<<tab[i].LP<<"\t"<<tab[i].P_V<<endl;
  58.     }
  59. }
  60. int main()
  61. {
  62.     vector <product> tab;
  63.     vector <product> plecak;
  64.     float s;
  65.     ifstream plik_z("hurtownia.txt");
  66.     cin>>s;
  67.     product X;
  68.     while(!plik_z.eof())
  69.     {
  70.         plik_z>>X.N>>X.P>>X.V>>X.LP;
  71.         X.P_V=X.P/X.V;
  72.         tab.push_back(X);
  73.     }
  74.     sortuj(tab,tab.size());
  75.  
  76.     cout<<endl;
  77.     pakowanie(tab,plecak,s);
  78.     cout<<"\nplecak:"<<endl;
  79.     wypisz(plecak);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement