#ifndef __PROGTEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cassert>
#include <iostream> 
#include <iomanip> 
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <memory>
using namespace std;
#endif /* __PROGTEST__ */


class Company
{
    public:
        Company(const string &id, size_t nameAddr,const string &add,const string &name);
        string getId() const;  
        size_t getNameAddr() const;
        bool operator == (Company cmpx) const;
        bool operator < (Company cmpx) const;
        void saveInvoice(unsigned int amount);
        unsigned int getTotalIncome() const;
        bool static compareByNA(const Company &a, const Company &b);
        
    private:
        string id;
        size_t nameAddr;
        string add;
        string name;
        unsigned int totalIncome;
        unsigned int numberOrders;
        unsigned int amount;
};


void Company::saveInvoice(unsigned int amount)
{
    numberOrders++;
    totalIncome+=amount;
}

string Company::getId() const
{
    return id;
}

size_t Company::getNameAddr() const
{
    return nameAddr;
}

Company::Company(const string &id, size_t nameAddr,const string &add,const string &name) 
:id(id), nameAddr(nameAddr), add(add), name(name), totalIncome(0), numberOrders(0), amount(0)
{
}

unsigned int Company::getTotalIncome() const
{
    return totalIncome;
}
/*
bool Company::operator == (Company cmpx)  const
{ 

    return ( ( (strcasecmp(addr.c_str(), cmpx.addr.c_str()) == 0) 
            && (strcasecmp(name.c_str(), cmpx.name.c_str()) == 0) )
            || (id == cmpx.id)
    );
    
}*/

bool Company::operator < (Company cmpx) const
{

    return id < cmpx.id;
}

bool Company::compareByNA(const Company &a, const Company &b)
{
    return a.getNameAddr() < b.getNameAddr();
}

class CVATRegister
{
  public:
      CVATRegister   ( void );
      ~CVATRegister  ( void );
    bool  newCompany     ( const string    & name, const string    & addr, const string    & taxID );
    bool          cancelCompany  ( const string    & name,
                                   const string    & addr );
    bool          cancelCompany  ( const string    & taxID );
    bool          invoice        ( const string    & taxID,
                                   unsigned int      amount );
    bool          invoice        ( const string    & name,
                                   const string    & addr,
                                   unsigned int      amount );
    bool          audit          ( const string    & name,
                                   const string    & addr,
                                   unsigned int    & sumIncome ) const;
    bool          audit          ( const string    & taxID,
                                   unsigned int    & sumIncome ) const;
    bool          firstCompany   ( string          & name,
                                   string          & addr ) const;
    bool          nextCompany    ( string          & name,
                                   string          & addr ) const;
    unsigned int  medianInvoice  ( void ) const;

    void sortC (vector<Company> &c) const;
    void sortI (vector<unsigned int> &TotalInvoice) const;
  private:
    vector<Company> CompanyIDList;
    vector<Company> CompanyNameList;
    vector<unsigned int> TotalInvoice; 
};

void CVATRegister::sortI (vector<unsigned int> &TotalInvoice) const 
{
    sort(TotalInvoice.begin(), TotalInvoice.end(), greater<unsigned int>());
}

unsigned int CVATRegister::medianInvoice (void) const
{
    vector<unsigned int> c = TotalInvoice;
    sortI(c);

    unsigned int n = c.size();


    if(!n)
        return 0;

    if(!(n%2))
    {
        return c[(n/2)-1];
    }
    return c[n/2];
}

CVATRegister::CVATRegister (void)
{
}

CVATRegister::~CVATRegister (void)
{
}

/*
bool CVATRegister::cancelCompany  ( const string  &name, const string &addr )
{
    Company cmp(name, addr, "-1");
    auto itr = find(CompanyIDList.begin(), CompanyIDList.end(), cmp);

    if(itr != CompanyIDList.end())
     {  
        CompanyIDList.erase(itr);
        return true;
     }
     return false;
}

bool CVATRegister::cancelCompany ( const string &taxID )
{
    Company cmp("", "", taxID);

    auto itr = lower_bound(CompanyIDList.begin(), CompanyIDList.end(), cmp);
    if(itr != CompanyIDList.end() && itr->getId() == taxID )
     {   
        CompanyIDList.erase(itr);
        return true;
     }
     return false;
}*/

bool CVATRegister::newCompany ( const string &name, const string &addr, const string &taxID )
{
    
    string _name = name;
    string _addr = addr;
    
    std::transform(_name.begin(), _name.end(), _name.begin(), ::tolower);
    std::transform(_addr.begin(), _addr.end(), _addr.begin(), ::tolower);
    
    size_t _nameAddr = std::hash<std::string>{}(_name + _addr);
    Company cmp(taxID, _nameAddr, addr, name);

    if ( (! binary_search(CompanyIDList.begin(), CompanyIDList.end(), cmp)) &&
        (! binary_search(CompanyNameList.begin(), CompanyNameList.end(), cmp, Company::compareByNA))
    )
    {       cout << _nameAddr << endl;
        
        CompanyIDList.insert(lower_bound(CompanyIDList.begin(), CompanyIDList.end(), cmp), cmp); 
        CompanyNameList.insert(lower_bound(CompanyNameList.begin(), CompanyIDList.end(), cmp, Company::compareByNA), cmp);

        return true;
    }
    return false;
}

bool CVATRegister::invoice ( const string &taxID, unsigned int amount )
{
    Company cmp(taxID, -1, "", "");

    auto itr = lower_bound(CompanyIDList.begin(), CompanyIDList.end(), cmp);
    
    if(itr != CompanyIDList.end() && itr->getId() == taxID)
     {   
        TotalInvoice.push_back(amount);
        CompanyIDList[distance(CompanyIDList.begin(), itr)].saveInvoice(amount);        
        return true;
     }
     
     return false;    
}
/*
bool CVATRegister::invoice ( const string  &name, const string &addr, unsigned int amount )
{
    Company cmp(name, addr,"-1");

    auto itr = find(CompanyIDList.begin(), CompanyIDList.end(), cmp);

    if(itr != CompanyIDList.end())
     {   
        TotalInvoice.push_back(amount); 
        CompanyIDList[distance(CompanyIDList.begin(), itr)].saveInvoice(amount);
        return true;
     }
     return false;
}

bool CVATRegister::audit ( const string &name, const string  &addr, unsigned int &sumIncome ) const
{
    Company cmp(name, addr,"-1");

    auto itr = find(CompanyIDList.begin(), CompanyIDList.end(), cmp);

    if(itr != CompanyIDList.end())
     {   
        sumIncome = itr->getTotalIncome(); 
        return true;
     }
     return false;

}

bool CVATRegister::audit ( const string  &taxID, unsigned int &sumIncome) const
{
    Company cmp(taxID, -1, "", "");

    auto const itr = lower_bound(CompanyIDList.begin(), CompanyIDList.end(), cmp);

    if(itr != CompanyIDList.end() && itr->getId() == taxID)
     {   
        sumIncome = CompanyIDList[distance(CompanyIDList.begin(), itr)].getTotalIncome();
        return true;
     }
     
     return false;

}

void CVATRegister::sortC (vector<Company> &c) const
{
   sort(c.begin(), c.end());
}

*/
/*bool CVATRegister::firstCompany ( string &name, string &addr ) const
{
    //sesortovat
    vector<Company>tmp = CompanyIDList;

    sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b)
        { 
            string s1 = a.getName();
            string s2 = b.getName();

            transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 
            transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 
            
            if(s1.compare(s2) == 0)
            {
                string d1 = a.getAddr();
                string d2 = b.getAddr();
                
                transform(d1.begin(), d1.end(), d1.begin(), ::tolower); 
                transform(d2.begin(), d2.end(), d2.begin(), ::tolower);
                return d1.compare(d2) < 0;
            }

            return s1.compare(s2) < 0;
        });

    if( tmp.size() > 0 )
    {   
        name = tmp[0].getName();
        addr = tmp[0].getAddr();
        return true;
    }
    return false;
} 

bool CVATRegister::nextCompany ( string &name, string &addr ) const
{
    Company cmp(name,addr,"-1");
    //sesortovat
    vector<Company>tmp = CompanyIDList;

    sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b)
        { 
            string s1 = a.getName();
            string s2 = b.getName();

            transform(s1.begin(), s1.end(), s1.begin(), ::tolower); 
            transform(s2.begin(), s2.end(), s2.begin(), ::tolower); 
            
            if(s1.compare(s2) == 0)
            {
                string d1 = a.getAddr();
                string d2 = b.getAddr();
                
                transform(d1.begin(), d1.end(), d1.begin(), ::tolower); 
                transform(d2.begin(), d2.end(), d2.begin(), ::tolower);
                return d1.compare(d2) < 0;
            }

            return s1.compare(s2) < 0;
        });

    auto itr = find(tmp.begin(), tmp.end(), cmp);

    if(itr == tmp.end())
        return false;

    unsigned int i = distance(tmp.begin(), itr);
    i++;
    

    if( i == tmp.size() )
        return false;    

    name = tmp[i].getName();
    addr = tmp[i].getAddr();
    return true;
}
*/

#ifndef __PROGTEST__
int               main           ( void )
{
  string name, addr;
  unsigned int sumIncome;

  CVATRegister b1;
  assert ( b1 . newCompany ( "ACME", "Thakurova", "666/666" ) );
  assert ( b1 . newCompany ( "ACME", "Kolejni", "666/666/666" ) );
  assert ( b1 . newCompany ( "Dummy", "Thakurova", "123456" ) );
  assert ( b1 . invoice ( "666/666", 2000 ) );
  assert ( b1 . medianInvoice () == 2000 );
  assert ( b1 . invoice ( "666/666/666", 3000 ) );
 /* assert ( b1 . medianInvoice () == 3000 );
  assert ( b1 . invoice ( "123456", 4000 ) );
  assert ( b1 . medianInvoice () == 3000 );
  assert ( b1 . invoice ( "aCmE", "Kolejni", 5000 ) );
  assert ( b1 . medianInvoice () == 4000 );
  assert ( b1 . audit ( "ACME", "Kolejni", sumIncome ) && sumIncome == 8000 );*/
 /* assert ( b1 . audit ( "123456", sumIncome ) && sumIncome == 4000 );
  assert ( b1 . firstCompany ( name, addr ) && name == "ACME" && addr == "Kolejni" );
  assert ( b1 . nextCompany ( name, addr ) && name == "ACME" && addr == "Thakurova" );
  assert ( b1 . nextCompany ( name, addr ) && name == "Dummy" && addr == "Thakurova" );
  assert ( ! b1 . nextCompany ( name, addr ) );
  assert ( b1 . cancelCompany ( "ACME", "KoLeJnI" ) );
  assert ( b1 . medianInvoice () == 4000 );
  assert ( b1 . cancelCompany ( "666/666" ) );
  assert ( b1 . medianInvoice () == 4000 );
  assert ( b1 . invoice ( "123456", 100 ) );
  assert ( b1 . medianInvoice () == 3000 );
  assert ( b1 . invoice ( "123456", 300 ) );
  assert ( b1 . medianInvoice () == 3000 );
  assert ( b1 . invoice ( "123456", 200 ) );
  assert ( b1 . medianInvoice () == 2000 );
  assert ( b1 . invoice ( "123456", 230 ) );
  assert ( b1 . medianInvoice () == 2000 );
  assert ( b1 . invoice ( "123456", 830 ) );
  assert ( b1 . medianInvoice () == 830 );
  assert ( b1 . invoice ( "123456", 1830 ) );
  assert ( b1 . medianInvoice () == 1830 );
  assert ( b1 . invoice ( "123456", 2830 ) );
  assert ( b1 . medianInvoice () == 1830 );
  assert ( b1 . invoice ( "123456", 2830 ) );
  assert ( b1 . medianInvoice () == 2000 );
  assert ( b1 . invoice ( "123456", 3200 ) );
  assert ( b1 . medianInvoice () == 2000 );
  assert ( b1 . firstCompany ( name, addr ) && name == "Dummy" && addr == "Thakurova" );
  assert ( ! b1 . nextCompany ( name, addr ) );
  assert ( b1 . cancelCompany ( "123456" ) );
  assert ( ! b1 . firstCompany ( name, addr ) );

  CVATRegister b2;
  assert ( b2 . newCompany ( "ACME", "Kolejni", "abcdef" ) );
  assert ( b2 . newCompany ( "Dummy", "Kolejni", "123456" ) );
  assert ( ! b2 . newCompany ( "AcMe", "kOlEjNi", "1234" ) );
  assert ( b2 . newCompany ( "Dummy", "Thakurova", "ABCDEF" ) );
  assert ( b2 . medianInvoice () == 0 );
  assert ( b2 . invoice ( "ABCDEF", 1000 ) );
  assert ( b2 . medianInvoice () == 1000 );
  assert ( b2 . invoice ( "abcdef", 2000 ) );
  assert ( b2 . medianInvoice () == 2000 );
  assert ( b2 . invoice ( "aCMe", "kOlEjNi", 3000 ) );
  assert ( b2 . medianInvoice () == 2000 );
  assert ( ! b2 . invoice ( "1234567", 100 ) );
  assert ( ! b2 . invoice ( "ACE", "Kolejni", 100 ) );
  assert ( ! b2 . invoice ( "ACME", "Thakurova", 100 ) );
  assert ( ! b2 . audit ( "1234567", sumIncome ) );
  assert ( ! b2 . audit ( "ACE", "Kolejni", sumIncome ) );
  assert ( ! b2 . audit ( "ACME", "Thakurova", sumIncome ) );
  assert ( ! b2 . cancelCompany ( "1234567" ) );
  assert ( ! b2 . cancelCompany ( "ACE", "Kolejni" ) );
  assert ( ! b2 . cancelCompany ( "ACME", "Thakurova" ) );
  assert ( b2 . cancelCompany ( "abcdef" ) );
  assert ( b2 . medianInvoice () == 2000 );
  assert ( ! b2 . cancelCompany ( "abcdef" ) );
  assert ( b2 . newCompany ( "ACME", "Kolejni", "abcdef" ) );
  assert ( b2 . cancelCompany ( "ACME", "Kolejni" ) );
  assert ( ! b2 . cancelCompany ( "ACME", "Kolejni" ) );

  return EXIT_SUCCESS;*/
}
#endif /* __PROGTEST__ */
 

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
}