#include <cstring>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cctype>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stdexcept>
#include <algorithm>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <memory>
using namespace std;

class CDataType
{
    public:
        CDataType(string type, size_t size);
       
        size_t getSize() const;
       
        bool operator == (CDataType &x) const;
        virtual bool equal (CDataType &x) const;
        
        bool operator != (CDataType &x) const;
        bool notEqual (CDataType &x) const;
        
        friend ostream& operator << (ostream &os, CDataType &x);
        virtual ostream& print (ostream &os) const;

        const string getType();

    protected:
        string m_Type;
        size_t m_Size;
};

CDataType::CDataType(string type, size_t size)
: m_Type(type),
  m_Size(size)
{
}

bool CDataType::operator == (CDataType &x) const
{
    return equal(x);
}

bool CDataType::equal (CDataType &x) const
{
    return m_Type == x.m_Type;
}

bool CDataType::operator != (CDataType &x) const
{
    return notEqual(x);
}

bool CDataType::notEqual (CDataType &x) const
{
    return m_Type != x.m_Type;
}

size_t CDataType::getSize() const
{
    return m_Size;
}

ostream& operator << (ostream &os, CDataType &x) 
{
    x.print(os);
    return os;
}

ostream& CDataType::print (ostream &os) const
{
    os << m_Type;
    return os;
}

const string CDataType::getType()
{
    return m_Type;
}

// int 
class CDataTypeInt : public CDataType
{
    public:
        CDataTypeInt();
};

CDataTypeInt::CDataTypeInt()
: CDataType("int", 4)
{
}

// double
class CDataTypeDouble : public CDataType
{
    public:
        CDataTypeDouble();
};

CDataTypeDouble::CDataTypeDouble()
: CDataType("double", 8)
{
}

// enum
class CDataTypeEnum : public CDataType
{
    public:
        CDataTypeEnum();
        CDataTypeEnum& add(string x);
        virtual ostream& print (ostream &os) const;
    protected:
        vector<string> listEnums;
        set<string> listEnumsNames;
};

CDataTypeEnum::CDataTypeEnum()
: CDataType("enum", 4)
{
}

ostream& CDataTypeEnum::print(ostream &os) const
{
    os << m_Type << "{\n";
    for (auto i=listEnums.begin(); i != listEnums.end(); ++i )
    {
        os << *i;
        if(i != listEnums.end()-1)
        {
            os << ",";
        }
        os << "\n";
    }
    os << "}";
   return os;
}

CDataTypeEnum& CDataTypeEnum::add(string x)
{
    if(listEnumsNames.find(x) == listEnumsNames.end())
    {
        listEnums.push_back(x);
        listEnumsNames.emplace(x);
    }
    else
       cout << "vyjimkaa" << endl;
       // CSyntaxException e("Duplicate enum value: " + x);

    return *this;
}

// struktura
class CDataTypeStruct : public CDataType
{
    public:
        CDataTypeStruct();
        CDataTypeStruct& addField(const string &name, const CDataType &type);
        CDataType& field(const string name) const;

        virtual ostream& print (ostream &os) const;
        bool equal (CDataTypeStruct &x) const;
        bool notEqual (CDataTypeStruct &x) const;

        list<unique_ptr<CDataType>>& getField();
        
    protected:
        list<unique_ptr<CDataType>> m_Field;
        unordered_set<string> m_Field_names;
};

CDataTypeStruct::CDataTypeStruct()
:CDataType("struct", 0)
{
}


CDataTypeStruct& CDataTypeStruct::addField(const string &name, const CDataType &type) 
{
    if( m_Field_names.find(name) == m_Field_names.end() )
    {   
        m_Field.push_back(make_unique<CDataType>(type));
        m_Field_names.emplace(name);
        m_Size += type.getSize();
    }
   // else
        //throw CSyntaxException("Duplicate field: " + name); 
    return *this;      
}

CDataType& CDataTypeStruct::field(const string name) const
{
    list<unique_ptr<CDataType>>::const_iterator itr = m_Field.begin();
    for(; itr != m_Field.end(); ++itr)
    {
        
        if(itr->getType() == name)
            break;
    }

    if(itr == m_Field.end())
        cout << "Vyjimka" << endl;
    
    return *itr;
        
}

list<unique_ptr<CDataType>>&  CDataTypeStruct::getField()
{
    return m_Field;
}

bool CDataTypeStruct::equal (CDataTypeStruct &x) const
{
    if ( m_Size != x.getSize() )
        return false;
    
    if ( x.getField().size() != m_Size)
        return false;
    
   auto k = x.getField().begin(); 
  
   for(auto i=m_Field.begin(); i != m_Field.end(); ++i)
   {
       if( i->notEqual(*k) )
            return false;
   }

   return true;

}  

bool CDataTypeStruct::notEqual (CDataTypeStruct &x) const
{
    return equal(x);
}


ostream& CDataTypeStruct::print (ostream &os) const
{
    os << m_Type << "{\n";
    for( auto i=m_Field.begin(); i != m_Field.end(); ++i )
    {
        i->print(os)  << " "   /*<< "{\n"*/;
    }

    os << "}";
    return os;
}


static bool whitespaceMatch ( const string &a, const string &b)
{
    const char *o = a.c_str();
    const char *r = b.c_str();
  
    while (true)
    {
        while(isspace(*o)) o++;
        while(isspace(*r)) r ++;
        if (!*r && !*o) return true;
        if (!*r || !*o || *r++ != *o++ ) return false;
  }
}

template <typename T_>
static bool whitespaceMatch ( const T_ &x, const string &ref )
{
  ostringstream oss;
  oss << x;
  return whitespaceMatch ( oss . str (), ref );
}

int main()
{
/*    CDataTypeInt inta;
    CDataTypeInt intb;
    cout << inta.getSize() << endl;
    CDataTypeDouble doublea;
    cout << doublea.getSize() << endl;

    if(inta.equal(doublea))
        cout << "Shodne" << endl;
    else 
        cout << "Nejsou shodne" << endl;

    if(inta.equal(intb))
        cout << "Shodne" << endl;
    else 
        cout << "Nejsou shodne" << endl;

    cout << inta << endl;
    cout << doublea << endl;

    cout << inta.getSize() << endl;

    CDataTypeEnum enumos;
    enumos.add("NEW").add("UPDATE");
    cout << enumos << endl;
*/

    CDataTypeInt inta;
    CDataTypeInt intb;

    CDataTypeStruct struktura;
    CDataTypeEnum enumos;
    enumos.add( "NEW" ).add ( "FIXED" ) .add ( "BROKEN" ) .add ( "DEAD" );
    
    struktura.addField("integera", inta);
    struktura.addField("integerb", intb);
    struktura.addField("bbb", enumos);

    cout << enumos << endl;
    cout << "Velikost struktury: " << struktura.getSize() << endl;

    cout << "Struktura vypis ---" << endl;
    cout << struktura << endl;
    
/*      CDataTypeStruct  a = CDataTypeStruct () .
                        addField ( "m_Length", CDataTypeInt () ) .
                        addField ( "m_Status", CDataTypeEnum () . 
                          add ( "NEW" ) . 
                          add ( "FIXED" ) . 
                          add ( "BROKEN" ) . 
                          add ( "DEAD" ) ).
                        addField ( "m_Ratio", CDataTypeDouble () );
 
  CDataTypeStruct b = CDataTypeStruct () .
                        addField ( "m_Length", CDataTypeInt () ) .
                        addField ( "m_Status", CDataTypeEnum () . 
                          add ( "NEW" ) . 
                          add ( "FIXED" ) . 
                          add ( "BROKEN" ) . 
                          add ( "READY" ) ).
                        addField ( "m_Ratio", CDataTypeDouble () );
  
  CDataTypeStruct c =  CDataTypeStruct () .
                        addField ( "m_First", CDataTypeInt () ) .
                        addField ( "m_Second", CDataTypeEnum () . 
                          add ( "NEW" ) . 
                          add ( "FIXED" ) . 
                          add ( "BROKEN" ) . 
                          add ( "DEAD" ) ).
                        addField ( "m_Third", CDataTypeDouble () );

  CDataTypeStruct  d = CDataTypeStruct () .
                        addField ( "m_Length", CDataTypeInt () ) .
                        addField ( "m_Status", CDataTypeEnum () . 
                          add ( "NEW" ) . 
                          add ( "FIXED" ) . 
                          add ( "BROKEN" ) . 
                          add ( "DEAD" ) ).
                        addField ( "m_Ratio", CDataTypeInt () );
  assert ( whitespaceMatch ( a, "struct\n"
    "{\n"
    "  int m_Length;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Status;\n"
    "  double m_Ratio;\n"
    "}") );

  assert ( whitespaceMatch ( b, "struct\n"
    "{\n"
    "  int m_Length;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    READY\n"
    "  } m_Status;\n"
    "  double m_Ratio;\n"
    "}") );

  assert ( whitespaceMatch ( c, "struct\n"
    "{\n"
    "  int m_First;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Second;\n"
    "  double m_Third;\n"
    "}") );

  assert ( whitespaceMatch ( d, "struct\n"
    "{\n"
    "  int m_Length;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Status;\n"
    "  int m_Ratio;\n"
    "}") );

  assert ( a != b );
  assert ( a == c );
  assert ( a != d );
  assert ( a . field ( "m_Status" ) == CDataTypeEnum () . add ( "NEW" ) . add ( "FIXED" ) . add ( "BROKEN" ) . add ( "DEAD" ) );
  assert ( a . field ( "m_Status" ) != CDataTypeEnum () . add ( "NEW" ) . add ( "BROKEN" ) . add ( "FIXED" ) . add ( "DEAD" ) );
  assert ( a != CDataTypeInt() );
  assert ( whitespaceMatch ( a . field ( "m_Status" ), "enum\n"
    "{\n"
    "  NEW,\n"
    "  FIXED,\n"
    "  BROKEN,\n"
    "  DEAD\n"
    "}") );

  CDataTypeStruct aOld = a;
  b . addField ( "m_Other", CDataTypeDouble ());

  a . addField ( "m_Sum", CDataTypeInt ());

  assert ( a != aOld );
  assert ( a != c );
  assert ( aOld == c );
  assert ( whitespaceMatch ( a, "struct\n"
    "{\n"
    "  int m_Length;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Status;\n"
    "  double m_Ratio;\n"
    "  int m_Sum;\n"
    "}") );

  assert ( whitespaceMatch ( b, "struct\n"
    "{\n"
    "  int m_Length;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    READY\n"
    "  } m_Status;\n"
    "  double m_Ratio;\n"
    "  double m_Other;\n"
    "}") );

  c . addField ( "m_Another", a . field ( "m_Status" ));

  assert ( whitespaceMatch ( c, "struct\n"
    "{\n"
    "  int m_First;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Second;\n"
    "  double m_Third;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Another;\n"
    "}") );

  d . addField ( "m_Another", a . field ( "m_Ratio" ));

  assert ( whitespaceMatch ( d, "struct\n"
    "{\n"
    "  int m_Length;\n"
    "  enum\n"
    "  {\n"
    "    NEW,\n"
    "    FIXED,\n"
    "    BROKEN,\n"
    "    DEAD\n"
    "  } m_Status;\n"
    "  int m_Ratio;\n"
    "  double m_Another;\n"
    "}") );

  assert ( a . getSize () == 20 );
  assert ( b . getSize () == 24 );
  try
  {
    a . addField ( "m_Status", CDataTypeInt () );
    assert ( "addField: missing exception!" == nullptr );
  }
  catch ( const invalid_argument & e )
  {
    assert ( e . what () == "Duplicate field: m_Status"sv );
  }

  try
  {
    cout << a . field ( "m_Fail" ) << endl;
    assert ( "field: missing exception!" == nullptr );
  }
  catch ( const invalid_argument & e )
  {
    assert ( e . what () == "Unknown field: m_Fail"sv );
  }

  try
  {
    CDataTypeEnum en;
    en . add ( "FIRST" ) .
         add ( "SECOND" ) .
         add ( "FIRST" );
    assert ( "add: missing exception!" == nullptr );
  }
  catch ( const invalid_argument & e )
  {
    assert ( e . what () == "Duplicate enum value: FIRST"sv );
  }
*/

} 

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
}