#ifndef __PROGTEST__ #include <cstring> #include <cstdlib> #include <cstdio> #include <cassert> #include <cctype> #include <cmath> #include <iostream> #include <iomanip> #include <sstream> using namespace std; #endif /* __PROGTEST__ */ class CStr { public: CStr(const char *str = ""); CStr(int x); ~CStr(void) noexcept; CStr(const CStr &src); CStr (CStr &&src) noexcept; CStr& operator = (CStr &src) noexcept; CStr &operator += (const CStr &str); CStr &operator += (char x); CStr &operator += (const char *str); CStr &operator += (int x); CStr &operator = (const char *str); CStr &operator = (int x); bool operator == (const CStr &rhs) const; char &operator [] (size_t idx); char operator [] (size_t idx) const; size_t size(void) const; const char * c_str(void) const; friend ostream &operator << (ostream &os, const CStr &x); private: char *m_Str; size_t m_Len; size_t m_Capacity; void append(const char *add, size_t addLen); }; CStr::CStr(const char *str) : m_Str(nullptr), m_Len(0), m_Capacity(0) { append(str, strlen(str)); } CStr::CStr(int x) : m_Str(nullptr), m_Len(0), m_Capacity(0) { char intStr[20]; snprintf(intStr, sizeof(intStr), "%d", x); append(intStr, strlen(intStr)); } CStr::~CStr(void) { delete [] m_Str; } CStr::CStr(const CStr &src) : m_Str(new char[src.m_Len + 1]), m_Len(src.m_Len), m_Capacity(src.m_Len+1) { memcpy(m_Str, src.m_Str, m_Len+1); } // movovani CStr::CStr (CStr &&src) noexcept : m_Str(src.m_Str), m_Len(src.m_Len), m_Capacity(src.m_Capacity) { src.m_Str = nullptr; } CStr& CStr::operator = (CStr &src) noexcept { swap(m_Str, src.m_Str); swap(m_Len, src.m_Len); swap(m_Capacity, src.m_Capacity); return *this; } CStr &CStr::operator += (const CStr &str) { append(str.m_Str, str.m_Len); return *this; } CStr &CStr::operator += (const char *str) { append(str, strlen(str)); return *this; } CStr &CStr::operator += (int x) { char intStr[20]; snprintf(intStr, sizeof(intStr), "%d", x); append(intStr, strlen(intStr)); return *this; } CStr &CStr::operator += (char x) { append(&x, 1); return *this; } CStr & CStr::operator = (const char *str) { m_Len = 0; append(str, strlen(str)); return *this; } CStr & CStr::operator = (int x) { char intStr[20]; snprintf(intStr, sizeof(intStr), "%d", x); m_Len = 0; append(intStr, strlen(intStr)); return *this; } bool CStr::operator == (const CStr &rhs) const { return strcmp(m_Str, rhs.m_Str) == 0; } char & CStr::operator [] (size_t idx) { if(idx >= m_Len) throw out_of_range ("out"); return m_Str[idx]; } char CStr::operator [] (size_t idx) const { if(idx >= m_Len) throw out_of_range ("out"); return m_Str[idx]; } size_t CStr::size(void) const { return m_Len; } const char * CStr::c_str(void) const { return m_Str; } ostream &operator << (ostream &os, const CStr &x) { os.write(x.m_Str, x.m_Len); return os; } void CStr::append(const char *add, size_t addLen) { if(addLen + m_Len + 1 > m_Capacity) { while(addLen + m_Len + 1 > m_Capacity) m_Capacity += m_Capacity/2 + 10; char *tmp = new char[m_Capacity]; memcpy(tmp, m_Str, m_Len); memcpy(tmp+m_Len, add, addLen); delete [] m_Str; m_Str = tmp; } else memcpy(m_Str+m_Len, add, addLen); m_Len += addLen; m_Str[m_Len] = '\0'; } //-------------------------------------------------------------------------------------------------------- class CMail { public: CMail (const char *from, const char *to, const char *body); CMail(const CMail &src); ~CMail(); bool operator == (const CMail &x ) const; CMail& operator = (CMail &&src) noexcept; friend ostream & operator << ( ostream &os, const CMail &m); bool compareByFrom(const CMail &x) const; bool compareByTo(const CMail &x) const; void setNext(CMail &x); CStr getFrom() const; CMail *getLast(); private: CStr m_From; CStr m_To; CStr m_Body; CMail *m_Next; }; CMail::~CMail() { } CMail::CMail (const char *from, const char *to, const char *body) : m_From(from), m_To(to), m_Body(body), m_Next(nullptr) { } CMail::CMail(const CMail &src) : m_From(src.m_From), m_To(src.m_To), m_Body(src.m_Body), m_Next(src.m_Next) { } bool CMail::operator == (const CMail &x ) const { if(x.m_From == m_From && m_To == x.m_To && m_Body == x.m_Body) return true; return false; } CMail& CMail::operator = (CMail &&src) noexcept { if(this == &src) return *this; m_From = src.m_From; m_To = src.m_To; m_Body = src.m_Body; m_Next = src.m_Next; // mozna chyba return *this; } bool CMail::compareByFrom(const CMail &x) const { if(m_From == x.m_From) return true; return false; } bool CMail::compareByTo(const CMail &x) const { if(m_To == x.m_To) return true; return false; } CMail *CMail::getLast() { CMail *tmp; CMail *x; while(tmp) { x = tmp; tmp = tmp->m_Next; } return x; } void CMail::setNext(CMail &x) { m_Next = &x; } CStr CMail::getFrom() const { return m_From; } ostream & operator << ( ostream &os, const CMail &m) { return os << m; } //-------------------------------------------------------------------------------------------------------------------------------- /* class CMailIterator { public: explicit operator bool ( void ) const; bool operator !(void) const; const CMail &operator *( void ) const; CMailIterator &operator ++( void ); private: // todo };**/ class CMailServer { public: CMailServer (void); //CMailServer (const CMailServer &src); //CMailServer & operator = (const CMailServer & src); ~CMailServer (void); void sendMail (const CMail &m); // CMailIterator outbox (const char *email) const; // CMailIterator inbox (const char *email) const; void print() const; private: CMail **m_Sent; CMail **m_Acept; size_t m_Capacity_Sent; size_t m_Capacity_Acept; size_t m_Len_Sent; size_t m_Len_Acept; }; CMailServer::CMailServer (void) : m_Capacity_Sent(1000), m_Capacity_Acept(1000), m_Len_Sent(0), m_Len_Acept(0) { m_Sent = (CMail**) malloc(sizeof(CMail*)*m_Capacity_Sent); m_Acept = (CMail**) malloc(sizeof(CMail*)*m_Capacity_Acept); } CMailServer::~CMailServer (void) { } void CMailServer::sendMail (const CMail &m) { // nastaveni send for(size_t i=0; i<m_Len_Sent; i++) { if(m_Sent[i]->compareByFrom(m)) { CMail a = m; m_Sent[i]->getLast()->setNext(a); return; } } CMail a = m; m_Sent[m_Len_Sent++] = &a; cout << "Zde: " << m_Sent[0]->getFrom() << endl; } void CMailServer::print() const { // for(size_t i=0; i<m_Len_Sent; i++) // cout << m_Sent[i]->getFrom() << " "; } #ifndef __PROGTEST__ bool matchOutput ( const CMail &m, const char *str ) { ostringstream oss; oss << m; return oss . str () == str; } int main ( void ) { //char from[100], to[100], body[1024]; CMail a( "john", "M", "progtest deadline" ); CMail b( "peter", "N", "progtest deadline" ); CMailServer server; server.sendMail(a); server.sendMail(b); cout << "m" << endl; // server.print(); /* assert ( CMail ( "john", "peter", "progtest deadline" ) == CMail ( "john", "peter", "progtest deadline" ) ); assert ( !( CMail ( "john", "peter", "progtest deadline" ) == CMail ( "john", "progtest deadline", "peter" ) ) ); assert ( !( CMail ( "john", "peter", "progtest deadline" ) == CMail ( "peter", "john", "progtest deadline" ) ) ); assert ( !( CMail ( "john", "peter", "progtest deadline" ) == CMail ( "peter", "progtest deadline", "john" ) ) ); assert ( !( CMail ( "john", "peter", "progtest deadline" ) == CMail ( "progtest deadline", "john", "peter" ) ) ); assert ( !( CMail ( "john", "peter", "progtest deadline" ) == CMail ( "progtest deadline", "peter", "john" ) ) ); CMailServer s0; s0 . sendMail ( CMail ( "john", "peter", "some important mail" ) ); strncpy ( from, "john", sizeof ( from ) ); strncpy ( to, "thomas", sizeof ( to ) ); strncpy ( body, "another important mail", sizeof ( body ) ); s0 . sendMail ( CMail ( from, to, body ) ); strncpy ( from, "john", sizeof ( from ) ); strncpy ( to, "alice", sizeof ( to ) ); strncpy ( body, "deadline notice", sizeof ( body ) ); s0 . sendMail ( CMail ( from, to, body ) ); s0 . sendMail ( CMail ( "alice", "john", "deadline confirmation" ) ); s0 . sendMail ( CMail ( "peter", "alice", "PR bullshit" ) ); CMailIterator i0 = s0 . inbox ( "alice" ); assert ( i0 && *i0 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i0, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i0 && *i0 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i0, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ! ++i0 ); CMailIterator i1 = s0 . inbox ( "john" ); assert ( i1 && *i1 == CMail ( "alice", "john", "deadline confirmation" ) ); assert ( matchOutput ( *i1, "From: alice, To: john, Body: deadline confirmation" ) ); assert ( ! ++i1 ); CMailIterator i2 = s0 . outbox ( "john" ); assert ( i2 && *i2 == CMail ( "john", "peter", "some important mail" ) ); assert ( matchOutput ( *i2, "From: john, To: peter, Body: some important mail" ) ); assert ( ++i2 && *i2 == CMail ( "john", "thomas", "another important mail" ) ); assert ( matchOutput ( *i2, "From: john, To: thomas, Body: another important mail" ) ); assert ( ++i2 && *i2 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i2, "From: john, To: alice, Body: deadline notice" ) ); assert ( ! ++i2 ); CMailIterator i3 = s0 . outbox ( "thomas" ); assert ( ! i3 ); CMailIterator i4 = s0 . outbox ( "steve" ); assert ( ! i4 ); CMailIterator i5 = s0 . outbox ( "thomas" ); s0 . sendMail ( CMail ( "thomas", "boss", "daily report" ) ); assert ( ! i5 ); CMailIterator i6 = s0 . outbox ( "thomas" ); assert ( i6 && *i6 == CMail ( "thomas", "boss", "daily report" ) ); assert ( matchOutput ( *i6, "From: thomas, To: boss, Body: daily report" ) ); assert ( ! ++i6 ); CMailIterator i7 = s0 . inbox ( "alice" ); s0 . sendMail ( CMail ( "thomas", "alice", "meeting details" ) ); assert ( i7 && *i7 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i7, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i7 && *i7 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i7, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ! ++i7 ); CMailIterator i8 = s0 . inbox ( "alice" ); assert ( i8 && *i8 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i8, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i8 && *i8 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i8, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ++i8 && *i8 == CMail ( "thomas", "alice", "meeting details" ) ); assert ( matchOutput ( *i8, "From: thomas, To: alice, Body: meeting details" ) ); assert ( ! ++i8 ); CMailServer s1 ( s0 ); s0 . sendMail ( CMail ( "joe", "alice", "delivery details" ) ); s1 . sendMail ( CMail ( "sam", "alice", "order confirmation" ) ); CMailIterator i9 = s0 . inbox ( "alice" ); assert ( i9 && *i9 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i9, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i9 && *i9 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i9, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ++i9 && *i9 == CMail ( "thomas", "alice", "meeting details" ) ); assert ( matchOutput ( *i9, "From: thomas, To: alice, Body: meeting details" ) ); assert ( ++i9 && *i9 == CMail ( "joe", "alice", "delivery details" ) ); assert ( matchOutput ( *i9, "From: joe, To: alice, Body: delivery details" ) ); assert ( ! ++i9 ); CMailIterator i10 = s1 . inbox ( "alice" ); assert ( i10 && *i10 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i10, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i10 && *i10 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i10, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ++i10 && *i10 == CMail ( "thomas", "alice", "meeting details" ) ); assert ( matchOutput ( *i10, "From: thomas, To: alice, Body: meeting details" ) ); assert ( ++i10 && *i10 == CMail ( "sam", "alice", "order confirmation" ) ); assert ( matchOutput ( *i10, "From: sam, To: alice, Body: order confirmation" ) ); assert ( ! ++i10 ); CMailServer s2; s2 . sendMail ( CMail ( "alice", "alice", "mailbox test" ) ); CMailIterator i11 = s2 . inbox ( "alice" ); assert ( i11 && *i11 == CMail ( "alice", "alice", "mailbox test" ) ); assert ( matchOutput ( *i11, "From: alice, To: alice, Body: mailbox test" ) ); assert ( ! ++i11 ); s2 = s0; s0 . sendMail ( CMail ( "steve", "alice", "newsletter" ) ); s2 . sendMail ( CMail ( "paul", "alice", "invalid invoice" ) ); CMailIterator i12 = s0 . inbox ( "alice" ); assert ( i12 && *i12 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i12, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i12 && *i12 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i12, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ++i12 && *i12 == CMail ( "thomas", "alice", "meeting details" ) ); assert ( matchOutput ( *i12, "From: thomas, To: alice, Body: meeting details" ) ); assert ( ++i12 && *i12 == CMail ( "joe", "alice", "delivery details" ) ); assert ( matchOutput ( *i12, "From: joe, To: alice, Body: delivery details" ) ); assert ( ++i12 && *i12 == CMail ( "steve", "alice", "newsletter" ) ); assert ( matchOutput ( *i12, "From: steve, To: alice, Body: newsletter" ) ); assert ( ! ++i12 ); CMailIterator i13 = s2 . inbox ( "alice" ); assert ( i13 && *i13 == CMail ( "john", "alice", "deadline notice" ) ); assert ( matchOutput ( *i13, "From: john, To: alice, Body: deadline notice" ) ); assert ( ++i13 && *i13 == CMail ( "peter", "alice", "PR bullshit" ) ); assert ( matchOutput ( *i13, "From: peter, To: alice, Body: PR bullshit" ) ); assert ( ++i13 && *i13 == CMail ( "thomas", "alice", "meeting details" ) ); assert ( matchOutput ( *i13, "From: thomas, To: alice, Body: meeting details" ) ); assert ( ++i13 && *i13 == CMail ( "joe", "alice", "delivery details" ) ); assert ( matchOutput ( *i13, "From: joe, To: alice, Body: delivery details" ) ); assert ( ++i13 && *i13 == CMail ( "paul", "alice", "invalid invoice" ) ); assert ( matchOutput ( *i13, "From: paul, To: alice, Body: invalid invoice" ) ); assert ( ! ++i13 );*/ return EXIT_SUCCESS; } #endif /* __PROGTEST__ */
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!
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;
}
C++ is a widely used middle-level programming language.
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.
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;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
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
}
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);
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.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}