Advertisement
Guest User

main.cpp Cukier

a guest
Feb 22nd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. // cukier w c++.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <stdlib.h>
  8. #include<string>
  9. #include <fstream>
  10. #include<map>
  11. #include <iomanip>
  12. using namespace std;
  13.  
  14. class cuk {
  15. public:
  16.     string data;
  17.     string nip;
  18.     float ilosc;
  19.     cuk(string a, string b, float c) {
  20.         this->data = a;
  21.         this->nip = b;
  22.         this->ilosc = c;
  23.     }
  24.     void zwroc() {
  25.         cout << this->data << " " << this->nip << " " << this->ilosc << endl;
  26.     }
  27.    
  28. };
  29.  
  30. int main()
  31. {
  32.     fstream cukier;
  33.     cukier.open("cukier.txt");
  34.     fstream cennikPlik;
  35.     cennikPlik.open("cennik.txt");
  36.     vector <cuk> bazaCukier;
  37.     map<string, float> bazaCennik;
  38.     string tmp;
  39.     while (cukier >> tmp) {
  40.         if(tmp != ""){
  41.             string a = tmp;
  42.             cukier >> tmp;
  43.             string b = tmp;
  44.             cukier >> tmp;
  45.             float c = stof(tmp);
  46.             bazaCukier.emplace_back(cuk(a,b,c));
  47.         }
  48.     }
  49.     while (cennikPlik >> tmp) {
  50.         string a = tmp;
  51.         cennikPlik >> tmp;
  52.         tmp[1] = '.';
  53.         float b = stof(tmp);
  54.         bazaCennik.emplace(a, b);
  55.     }
  56.  
  57.     //zadanie 1
  58.     map<string, float> iloscCukru;
  59.     for (auto start = bazaCukier.begin(); start != bazaCukier.end(); start++) {
  60.         iloscCukru.emplace(start->nip, iloscCukru[start->nip] += start->ilosc);
  61.     }
  62.     string indexMax1, indexMax2, indexMax3;
  63.     float Max1 = 0, Max2 = 0, Max3 = 0;
  64.     for(const auto& element : iloscCukru) {
  65.         if (element.second > Max1) {
  66.             Max3 = Max2;
  67.             indexMax3 = indexMax2;
  68.             Max2 = Max1;
  69.             indexMax2 = indexMax1;
  70.             Max1 = element.second;
  71.             indexMax1 = element.first;
  72.         }
  73.         else if (element.second > Max2) {
  74.             Max3 = Max2;
  75.             indexMax3 = indexMax2;
  76.             Max2 = element.second;
  77.             indexMax2 = element.first;
  78.         }
  79.         else if(element.second > Max3){
  80.             Max3 = element.second;
  81.             indexMax3 = element.first;
  82.         }
  83.     }
  84.     cout << "Zadanie 1" << endl;
  85.     cout << "\t" << indexMax1 << " -> " << Max1 << endl;
  86.     cout << "\t" << indexMax2 << " -> " << Max2 << endl;
  87.     cout << "\t" << indexMax3 << " -> " << Max3 << endl;
  88.  
  89.     //zadanie 2
  90.     double zarobek = 0;
  91.     for (const auto& elementy : bazaCukier) {
  92.         string rok;
  93.         rok += elementy.data[0];
  94.         rok += elementy.data[1];
  95.         rok += elementy.data[2];
  96.         rok += elementy.data[3];
  97.         zarobek += elementy.ilosc * bazaCennik[rok];
  98.     }
  99.     cout  << "Zadanie 2\n\tZarobek na przestrzeni lat wyniosl " << fixed << setprecision(2) << zarobek << " zl" << endl;
  100.  
  101.  
  102.     //zadanie 3 - wypisanie danych do pliku w celu łatwego zbudowania wykresu
  103.     fstream zadanie3;
  104.     zadanie3.open("zadanie3DaneDoWykresu.txt");
  105.     float iloscCukruWRok = 0;
  106.     string ostatniRok = "2005";
  107.     for (const auto& elementy : bazaCukier) {
  108.         string rok;
  109.         rok += elementy.data[0];
  110.         rok += elementy.data[1];
  111.         rok += elementy.data[2];
  112.         rok += elementy.data[3];
  113.         if (rok != ostatniRok) {
  114.             zadanie3 << ostatniRok << " " << iloscCukruWRok << endl;
  115.             ostatniRok = rok;
  116.             iloscCukruWRok = elementy.ilosc;
  117.         }
  118.         else {
  119.             iloscCukruWRok += elementy.ilosc;
  120.         }
  121.     }
  122.     zadanie3 << ostatniRok << " " << iloscCukruWRok << endl; //ostatni rok, nie przeskoczy w if'ie
  123.     cout << "Zadanie 3 \n\tOdpowiedz w pliku\n";
  124.  
  125.     //zadanie 4
  126.     float sumaRabatow = 0;
  127.     vector<string> unikalneNIP;
  128.     map<string, int> iloscNaNip;
  129.     for (const auto& element : bazaCukier) {
  130.         bool czyJuzBylo = false;
  131.         for (const auto& elementy : unikalneNIP) {
  132.             if (elementy == element.nip) czyJuzBylo = true;
  133.            
  134.         }
  135.         if(!czyJuzBylo)unikalneNIP.emplace_back(element.nip);
  136.     }
  137.     for (const auto& NIP : unikalneNIP) {
  138.         for (const auto& elementy : bazaCukier) {
  139.             if (NIP == elementy.nip) {
  140.                 if(!iloscNaNip.count(NIP) > 0){
  141.                     iloscNaNip.emplace(NIP, elementy.ilosc);
  142.                     if (iloscNaNip[NIP] >= 10000) {
  143.                         sumaRabatow += elementy.ilosc*0.2;
  144.                     }
  145.                     else if (iloscNaNip[NIP] >= 1000) {
  146.                         sumaRabatow += elementy.ilosc*0.1;
  147.                     }
  148.                     else if(iloscNaNip[NIP] >= 100) {
  149.                         sumaRabatow += elementy.ilosc*0.05;
  150.                     }
  151.                 }
  152.                 else {
  153.                     iloscNaNip[NIP] += elementy.ilosc;
  154.                     if (iloscNaNip[NIP] >= 10000) {
  155.                         sumaRabatow += elementy.ilosc*0.2;
  156.                     }
  157.                     else if (iloscNaNip[NIP] >= 1000) {
  158.                         sumaRabatow += elementy.ilosc*0.1;
  159.                     }
  160.                     else if(iloscNaNip[NIP] >= 100){
  161.                         sumaRabatow += elementy.ilosc*0.05;
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.     }
  167.     cout.precision(2);
  168.     cout << "Zadanie 4\n\t" << sumaRabatow << endl;
  169.     system("pause");
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement