Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- struct product
- {
- string N;
- float P;
- float V;
- int LP;
- float P_V;
- };
- void sortuj(vector <product> &tab,int n)
- {
- bool czy=1;
- int k=n-1;
- while(czy)
- {
- czy=0;
- for(int i=0;i<k;i++)
- {
- if(tab[i].P_V>tab[i+1].P_V)
- {
- swap(tab[i],tab[i+1]);
- czy=1;
- }
- }
- k--;
- }
- }
- void pakowanie(vector <product> &tab, vector <product> &plecak, float s)
- {
- float suma=0;
- for(int i=0;i<tab.size();i++)
- {
- plecak.push_back(tab[i]);
- plecak[i].LP=0;
- for(tab[i].LP;tab[i].LP>0;tab[i].LP--)
- {
- if(suma+tab[i].V<s)
- {
- suma+=tab[i].V;
- plecak[i].LP++;
- }
- else
- break;
- }
- }
- cout<<"Suma: "<<suma<<endl;
- }
- void wypisz(vector <product> &tab)
- {
- cout<<"nazwa\tcena\tobj\tLP\tstosunek ceny do objetosci"<<endl;
- for(int i=0;i<tab.size();i++)
- {
- if(tab[i].LP>0)
- cout<<tab[i].N<<"\t"<<tab[i].P<<"\t"<<tab[i].V<<"\t"<<tab[i].LP<<"\t"<<tab[i].P_V<<endl;
- }
- }
- int main()
- {
- vector <product> tab;
- vector <product> plecak;
- float s;
- ifstream plik_z("hurtownia.txt");
- cin>>s;
- product X;
- while(!plik_z.eof())
- {
- plik_z>>X.N>>X.P>>X.V>>X.LP;
- X.P_V=X.P/X.V;
- tab.push_back(X);
- }
- sortuj(tab,tab.size());
- cout<<endl;
- pakowanie(tab,plecak,s);
- cout<<"\nplecak:"<<endl;
- wypisz(plecak);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement