#ifndef __PROGTEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <memory>
using namespace std;
#endif /* __PROGTEST__ */


struct cmpByCnt {
    bool operator() (int a, int b) const {
        return a < b;
    }
};
struct cmpByCnt2 {
    bool operator() (int a, int b) const {
        return a > b;
    }
};

class CDate
{
    public:
        CDate(const int &y,const int &m,const  int &d);
        int m_Day;
        int m_Month;
        int m_Year;
        int m_Time;
};

CDate::CDate(const int &y,const int &m,const  int &d)
: m_Day(d),
  m_Month(m),
  m_Year(y),
  m_Time(0)
{ 
    
    time_t rawtime;
    struct tm  timeinfo;

    time ( &rawtime );
    timeinfo = *localtime ( &rawtime );
    timeinfo.tm_year = y - 1900;
    timeinfo.tm_mon = m - 1;
    timeinfo.tm_mday = d;
    timeinfo.tm_sec = 1; timeinfo.tm_min = 1; timeinfo.tm_hour = 1;

    m_Time = mktime ( &timeinfo );
}

class CItem
{
    public:
        CItem(const string &name, const  CDate &expireDate, const  int &cnt);

        string m_Name;
        CDate m_Date;
        int m_Cnt;
};

CItem::CItem(const string &name, const CDate &expireDate ,const  int &cnt)
:m_Name(name), m_Date(expireDate), m_Cnt(cnt)
{
}

class CSupermarket
{
    public:
        CSupermarket();
        CSupermarket& store(const string &name,const CDate &date,const int &cnt);
        list<pair<string,int>> expired(const CDate &date);
        void sell(list<pair<string,int>> &list);

        map<string, map<int, CItem, cmpByCnt>> m_Items;
};

CSupermarket::CSupermarket()
{
}

CSupermarket& CSupermarket::store(const string &name,const CDate &date,const int &cnt)
{
    CItem a(name, date, cnt);
    auto itr = m_Items.find(name);

    if ( itr != m_Items.end())
        itr->second.emplace(a.m_Date.m_Time, a);
    else
        m_Items.emplace(name, std::map<int, CItem, cmpByCnt>{{a.m_Date.m_Time, a}});

    return *this;
}


list<pair<string,int>> CSupermarket::expired(const CDate &date)
{
    map<string, int> beforeExp;
    
    // Vytvoření mapy podle data
    
    for(auto itr = m_Items.begin(); itr != m_Items.end(); ++itr)
    {
        for(auto i = itr->second.begin(); i != itr->second.end(); ++i)
        {
            if( date.m_Time > i->first )
            {
                if( beforeExp.find(itr->first) == beforeExp.end() ) 
                    beforeExp.emplace( itr->first, i->second.m_Cnt);
                else
                {
                    auto f = beforeExp.find(itr->first);
                    f->second += i->second.m_Cnt;
                 } 
            }
        }
    }    
        
    map<int, string, cmpByCnt2> beforeExpSorted;

    // Seřazení
    for(auto itr = beforeExp.begin(); itr != beforeExp.end(); ++itr)
        beforeExpSorted.emplace(itr->second, itr->first);

    list<pair<string,int>> ex;

    //Přesunutí do listu
    for(auto itr = beforeExpSorted.begin(); itr !=beforeExpSorted.end(); ++itr)
        ex.emplace_back(itr->second, itr->first);    

    // Vypsání listu
    /*for(auto itr = ex.begin(); itr !=ex.end(); ++itr)
        cout << itr->first << " " << itr->second <<  endl;*/

    return ex;
}



void CSupermarket::sell(list<pair<string,int>> &list)
{   
    for(auto listIterator = list.begin(); listIterator != list.end(); ++listIterator)
    {
        auto itemIterator = m_Items.find(listIterator->first);
        if( itemIterator != m_Items.end() ) 
        {
            for(auto itr = itemIterator->second.begin(); itr != itemIterator->second.end(); ++itr)
            {
                //Ošetřit
                if( listIterator->second > itr->second.m_Cnt)
                {
                    int odecteno = itr->second.m_Cnt;
                    listIterator->second -= odecteno;
                    itr->second.m_Cnt -= odecteno;
                    //auto toDelete = m_Items.find(itemIterator->first)->second.find(itr);   
                }
                else if ( listIterator->second < itr->second.m_Cnt )
                {
                    int odecteno = listIterator->second;
                    listIterator->second -= odecteno;
                    itr->second.m_Cnt -= odecteno;
                    break;
                }
                else
                {
                    listIterator->second = 0;
                    itr->second.m_Cnt = 0;
                    break;
                }
                        
               // cout << "List: " << listIterator->first << " Kolik chci: " << listIterator->second << endl;
               // cout << "Jmeno: " << itr->second.m_Name << " time: " << itr->first << " na skladu: " << itr->second.m_Cnt << endl;
            } 
        }

        // Odstraneni polozek s nulou
            
            /*for(auto itr = itemIterator->second.begin(); itr != itemIterator->second.end(); ++itr)
            {
                if(itr->second.m_Cnt == 0)
                {
                    cout <<"Tohle: " << m_Items.find(itemIterator->first)->second.find(itr->first)->second.m_Name << endl; 
                    

                    //auto r = itemIterator->second.find(itr->first);
                    //cout << r->second.m_Cnt << endl;
                    itr = itemIterator->second.erase(itr);
                   
                }
                cout << "d" << endl;
            }*/
    }
}


int main()
{
    CSupermarket s;
  s . store ( "bread", CDate ( 2016, 4, 30 ), 100 )
    . store ( "butter", CDate ( 2016, 5, 10 ), 10 )
    . store ( "beer", CDate ( 2016, 8, 10 ), 50 )
    . store ( "bread", CDate ( 2016, 4, 25 ), 100 )
    . store ( "okey", CDate ( 2016, 7, 18 ), 5 );

    for(auto itr = s.m_Items.begin(); itr != s.m_Items.end(); ++itr)
    {
        cout << itr->first << " ";
        for(auto i = itr->second.begin(); i != itr->second.end(); ++i)
        {
            cout << " date: " << i->first << " cnt: " << i->second.m_Cnt;
        }
        cout << endl;
    }

  cout << "-------" << endl;
  list<pair<string,int> > l0 = s . expired ( CDate ( 2018, 4, 30 ) );
  assert ( l0 . size () == 4 );
  assert ( ( l0 == list<pair<string,int> > { { "bread", 200 }, { "beer", 50 }, { "butter", 10 }, { "okey", 5 } } ) );


  list<pair<string,int> > l1 { { "bread",  100}, { "bread", 20 }, { "butter", 20 } };
  s . sell ( l1 );

    for(auto itr = s.m_Items.begin(); itr != s.m_Items.end(); ++itr)
    {
        cout << itr->first << " ";
        for(auto i = itr->second.begin(); i != itr->second.end(); ++i)
        {
            cout << " date: " << i->first << " count: " << i->second.m_Cnt;
        }
        cout << endl;
    }
  return 0;
} 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}