Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <fstream>
- #include <vector>
- #include <regex>
- #include<conio.h>
- using namespace std;
- struct produkty {
- string nazwa;
- float kalorycznosc;
- float zwartosc_tluszczu;
- };
- float oblicz_kalorie(vector<produkty> &baza, string nazwa, float n) {
- //cout << "Podah baze , nazwe produktu oraz jego ilosc w gramach: " << endl;
- float wynik;
- for (auto itr = baza.begin(); itr != baza.end(); itr++) {
- if ((*itr).nazwa == nazwa) {
- wynik = (*itr).kalorycznosc*n;
- }
- // else return -1 ; // nie dziala jak powinno
- }
- return wynik / 100;
- }
- int main() {
- fstream plik;
- vector<produkty>lista;
- plik.open("produkty.txt", ios::in); // do odczytu plik
- while (!plik.eof()) {
- produkty dane;
- float kalorie;
- float tluszcz;
- string srednik = ";";
- string enter = "\n";
- getline(plik, dane.nazwa, ';');
- plik >> kalorie;
- dane.kalorycznosc = kalorie;
- getline(plik, srednik, ';');
- plik >> tluszcz;
- dane.zwartosc_tluszczu = tluszcz;
- getline(plik, enter, '\n');
- lista.push_back(dane);
- }
- plik.close();
- // for (auto itr = lista.begin(); itr != lista.end(); itr++) { // TEST DO WYSWIETLANIA
- // cout << (*itr).nazwa << " " << (*itr).kalorycznosc << " " << (*itr).zwartosc_tluszczu ; }
- for (auto itr = lista.begin(); itr != lista.end(); ) {
- if ((*itr).zwartosc_tluszczu < 0.3) { // POZBYWANIE SIE TLUSZCZU MNIEJSZEGO OD 0.3
- itr = lista.erase(itr);
- }
- else {
- itr++;
- }
- }
- // SORTOWANIE MALEJACO
- vector<pair<float, int>> temp;
- for (int j = 0; j < lista.size(); j++) {
- pair<float, int>b = pair<float, int>(lista[j].kalorycznosc, j);
- temp.push_back(b);
- }
- sort(temp.begin(), temp.end());
- std::reverse(temp.begin(), temp.end());
- //TWORZENIE NOWEGO PLIKU GDZIE JEST ZAPISANA MODYFIKACJA
- plik.open("przerobione.txt", ios::out|ios::in);
- for (int l = 0; l < temp.size(); l++) {
- int indeks = temp[l].second;
- plik << lista[indeks].nazwa << " " <<
- lista[indeks].kalorycznosc << " " <<
- lista[indeks].zwartosc_tluszczu<< endl;
- //TEST
- // cout << lista[indeks].nazwa << " " <<
- // lista[indeks].kalorycznosc << " " <<
- // lista[indeks].zwartosc_tluszczu << endl;
- }
- plik.close();
- //KALKULATOR OBLICZENIOWY
- cout << " Podaj ilosc produktow: " << endl;
- string nazwaa;
- float wynik=0;
- float ilosc;
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- cout << "Wpisz nazwe produktu oraz jego ilosc wedlug podanego wzoru: maslo = 100 " << endl;
- char temp ; // to jest nasze "=" np maslo=100
- cin >> nazwaa >> temp >> ilosc;
- float cos = 0;
- cos= oblicz_kalorie(lista, nazwaa, ilosc);
- /* tez nie dziala jak powinno
- if (cos==-1){
- cout << "Wpisz nazwe produktu oraz jego ilosc wedlug podanego wzoru: maslo = 100 " << endl;
- char temp ; // to jest nasze "=" np maslo=100
- cin >> nazwaa >> temp >> ilosc;
- float cos = 0;
- cos= oblicz_kalorie(lista, nazwaa, ilosc);
- }
- */
- wynik = wynik + cos;
- }
- cout << wynik << " kcal" << endl;
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement