#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 &name, const string &addr, const string &id); string getId() const; string getName() const; string getAddr() const; bool operator == (Company cmpx) const; bool operator < (Company cmpx) const; static bool cmpNA (const Company &a, const Company &b); void saveInvoice(unsigned int amount); unsigned int getTotalIncome() const; private: string name; string addr; string id; unsigned int totalIncome; unsigned int numberOrders; unsigned int amount; }; bool Company::cmpNA (const Company &a, const Company &b) { if ( a.getName() < b.getName()) return a.getName() < b.getName(); return a.getAddr() < b.getAddr(); } string Company::getName() const { return name; } string Company::getAddr() const { return addr; } void Company::saveInvoice(unsigned int amount) { numberOrders++; totalIncome+=amount; } string Company::getId() const { return id; } Company::Company(const string &name, const string &addr, const string &id) :name(name), addr(addr), id(id), 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; } 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> DCompany; 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 = lower_bound(DCompany.begin(), DCompany.end(), cmp); auto itr = find(DCompany.begin(), DCompany.end(), cmp); if(itr != DCompany.end()) { DCompany.erase(itr); sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b) { return a.getId() < b.getId(); }); return true; } return false; } bool CVATRegister::cancelCompany ( const string &taxID ) { Company cmp("", "", taxID); auto const itr = lower_bound(DCompany.begin(), DCompany.end(), cmp); if(itr != DCompany.end() && itr->getId() == taxID) { DCompany.erase(itr); return true; } return false; } bool CVATRegister::newCompany ( const string &name, const string &addr, const string &taxID ) { Company cmp(name, addr, taxID); if ( find(DCompany.begin(), DCompany.end(), cmp) == DCompany.end() ) { DCompany.push_back(cmp); sort(DCompany.begin(), DCompany.end(), [](const Company & a, const Company & b) { return a.getId() < b.getId(); }); return true; } return false; } bool CVATRegister::invoice ( const string &taxID, unsigned int amount ) { Company cmp("", "", taxID); auto itr = find(DCompany.begin(), DCompany.end(), cmp); if(itr != DCompany.end()) { TotalInvoice.push_back(amount); DCompany[distance(DCompany.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(DCompany.begin(), DCompany.end(), cmp); if(itr != DCompany.end()) { TotalInvoice.push_back(amount); DCompany[distance(DCompany.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"); vector<Company> tmp = DCompany; sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b) { if ( a.getName() < b.getName()) return a.getName() < b.getName(); return a.getAddr() < b.getAddr(); }); auto itr = lower_bound(tmp.begin(), tmp.end(), cmp, &Company::cmpNA); if(itr != tmp.end() && addr == itr->getAddr() && itr->getName() == name) { sumIncome = itr->getTotalIncome(); return true; } return false; } bool CVATRegister::audit ( const string &taxID, unsigned int &sumIncome) const { Company cmp("", "", taxID); auto const itr = lower_bound(DCompany.begin(), DCompany.end(), cmp); if(itr != DCompany.end() && itr->getId() == taxID) { sumIncome = DCompany[distance(DCompany.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 { vector<Company> tmp = DCompany; sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b) { if (a.getName() == b.getName()) return a.getAddr() < b.getAddr(); return a.getName() < b.getName(); }); if( DCompany.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"); vector<Company> tmp = DCompany; sort(tmp.begin(), tmp.end(), [](const Company & a, const Company & b) { if (a.getName() == b.getName()) return a.getAddr() < b.getAddr(); return a.getName() < b.getName(); }); 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__ */
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
}