Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cukier w c++.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
- //
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <stdlib.h>
- #include<string>
- #include <fstream>
- #include<map>
- #include <iomanip>
- using namespace std;
- class cuk {
- public:
- string data;
- string nip;
- float ilosc;
- cuk(string a, string b, float c) {
- this->data = a;
- this->nip = b;
- this->ilosc = c;
- }
- void zwroc() {
- cout << this->data << " " << this->nip << " " << this->ilosc << endl;
- }
- };
- int main()
- {
- fstream cukier;
- cukier.open("cukier.txt");
- fstream cennikPlik;
- cennikPlik.open("cennik.txt");
- vector <cuk> bazaCukier;
- map<string, float> bazaCennik;
- string tmp;
- while (cukier >> tmp) {
- if(tmp != ""){
- string a = tmp;
- cukier >> tmp;
- string b = tmp;
- cukier >> tmp;
- float c = stof(tmp);
- bazaCukier.emplace_back(cuk(a,b,c));
- }
- }
- while (cennikPlik >> tmp) {
- string a = tmp;
- cennikPlik >> tmp;
- tmp[1] = '.';
- float b = stof(tmp);
- bazaCennik.emplace(a, b);
- }
- //zadanie 1
- map<string, float> iloscCukru;
- for (auto start = bazaCukier.begin(); start != bazaCukier.end(); start++) {
- iloscCukru.emplace(start->nip, iloscCukru[start->nip] += start->ilosc);
- }
- string indexMax1, indexMax2, indexMax3;
- float Max1 = 0, Max2 = 0, Max3 = 0;
- for(const auto& element : iloscCukru) {
- if (element.second > Max1) {
- Max3 = Max2;
- indexMax3 = indexMax2;
- Max2 = Max1;
- indexMax2 = indexMax1;
- Max1 = element.second;
- indexMax1 = element.first;
- }
- else if (element.second > Max2) {
- Max3 = Max2;
- indexMax3 = indexMax2;
- Max2 = element.second;
- indexMax2 = element.first;
- }
- else if(element.second > Max3){
- Max3 = element.second;
- indexMax3 = element.first;
- }
- }
- cout << "Zadanie 1" << endl;
- cout << "\t" << indexMax1 << " -> " << Max1 << endl;
- cout << "\t" << indexMax2 << " -> " << Max2 << endl;
- cout << "\t" << indexMax3 << " -> " << Max3 << endl;
- //zadanie 2
- double zarobek = 0;
- for (const auto& elementy : bazaCukier) {
- string rok;
- rok += elementy.data[0];
- rok += elementy.data[1];
- rok += elementy.data[2];
- rok += elementy.data[3];
- zarobek += elementy.ilosc * bazaCennik[rok];
- }
- cout << "Zadanie 2\n\tZarobek na przestrzeni lat wyniosl " << fixed << setprecision(2) << zarobek << " zl" << endl;
- //zadanie 3 - wypisanie danych do pliku w celu łatwego zbudowania wykresu
- fstream zadanie3;
- zadanie3.open("zadanie3DaneDoWykresu.txt");
- float iloscCukruWRok = 0;
- string ostatniRok = "2005";
- for (const auto& elementy : bazaCukier) {
- string rok;
- rok += elementy.data[0];
- rok += elementy.data[1];
- rok += elementy.data[2];
- rok += elementy.data[3];
- if (rok != ostatniRok) {
- zadanie3 << ostatniRok << " " << iloscCukruWRok << endl;
- ostatniRok = rok;
- iloscCukruWRok = elementy.ilosc;
- }
- else {
- iloscCukruWRok += elementy.ilosc;
- }
- }
- zadanie3 << ostatniRok << " " << iloscCukruWRok << endl; //ostatni rok, nie przeskoczy w if'ie
- cout << "Zadanie 3 \n\tOdpowiedz w pliku\n";
- //zadanie 4
- float sumaRabatow = 0;
- vector<string> unikalneNIP;
- map<string, int> iloscNaNip;
- for (const auto& element : bazaCukier) {
- bool czyJuzBylo = false;
- for (const auto& elementy : unikalneNIP) {
- if (elementy == element.nip) czyJuzBylo = true;
- }
- if(!czyJuzBylo)unikalneNIP.emplace_back(element.nip);
- }
- for (const auto& NIP : unikalneNIP) {
- for (const auto& elementy : bazaCukier) {
- if (NIP == elementy.nip) {
- if(!iloscNaNip.count(NIP) > 0){
- iloscNaNip.emplace(NIP, elementy.ilosc);
- if (iloscNaNip[NIP] >= 10000) {
- sumaRabatow += elementy.ilosc*0.2;
- }
- else if (iloscNaNip[NIP] >= 1000) {
- sumaRabatow += elementy.ilosc*0.1;
- }
- else if(iloscNaNip[NIP] >= 100) {
- sumaRabatow += elementy.ilosc*0.05;
- }
- }
- else {
- iloscNaNip[NIP] += elementy.ilosc;
- if (iloscNaNip[NIP] >= 10000) {
- sumaRabatow += elementy.ilosc*0.2;
- }
- else if (iloscNaNip[NIP] >= 1000) {
- sumaRabatow += elementy.ilosc*0.1;
- }
- else if(iloscNaNip[NIP] >= 100){
- sumaRabatow += elementy.ilosc*0.05;
- }
- }
- }
- }
- }
- cout.precision(2);
- cout << "Zadanie 4\n\t" << sumaRabatow << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement