#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <cstring>
#define IND " "
#define NOT_SYMB '/'
#define TBL_IND "│"
#define TBL_H_IND "─"
#define TBL_CUL "┐"
#define TBL_OUR "┌"
#define SEP_IND "┬"
int currentTask = 1;
std::string strFormat(std::string txt, const int &n)
{
char result[16] = {};
if (txt.find("%d") == std::string::npos)
return txt;
sprintf(result, txt.c_str(), n);
return std::string(result);
}
int uaSize(std::string str)
{
int size = 0;
int dsize = 0;
for(auto el : str)
{
if(el >= 0x30 && el <= 0x39)
dsize++;
else
++size;
}
return size/2 + dsize;
}
std::map<int, std::vector<std::string>> names =
{
{1, {"НЕ","%dІ","%dАБО"} },
{2, {"НЕ","2І","2АБО"} },
{3, {"2I-НЕ","%dІ-НЕ","НЕ-%dАБО"} },
{4, {"2I-НЕ","2І-НЕ","НЕ-2АБО"} },
{5, {"2АБО-НЕ","%dАБО-НЕ","НЕ-%dІ"} },
{6, {"2АБО-НЕ","2АБО-НЕ","НЕ-2І"} }
};
void popBack(std::string &str)
{
str.pop_back();
str.pop_back();
str.pop_back();
}
int getTermSize(std::string term)
{
int result = 0;
for (int i = 0; i < term.size(); ++i)
result += (term[i] == NOT_SYMB ? 0 : 1);
return result;
}
std::string getSectionsSeparator(std::multimap<int, std::string> &tokens)
{
std::string format = "┌────────┬";
std::string tmpSection = TBL_H_IND;
for (const auto &i : tokens)
{
int c = 0;
int var = (i.first > 5) ? 2 : ((i.first > 1) ? 1 : 0);
for (int j = 0; j <= uaSize(strFormat(names[currentTask][var], i.first)); ++j, ++c)
tmpSection += TBL_H_IND;
++c;
while (c < getTermSize(i.second))
{
tmpSection += TBL_H_IND;
++c;
}
tmpSection += SEP_IND;
format += tmpSection;
tmpSection = TBL_H_IND;
}
popBack(format);
format += TBL_CUL;
return format+"\n";
}
std::string getSections(std::multimap<int, std::string> &tokens)
{
std::string format = "│ Набори │";
std::string tmpSection = IND;
for (const auto &i : tokens)
{
int var = (i.first > 5) ? 2 : ((i.first > 1) ? 1 : 0);
tmpSection += strFormat(names[currentTask][var], i.first) + IND;
// Make some space to print term
while (tmpSection.size()-1 < getTermSize(i.second))
tmpSection += IND;
tmpSection += TBL_IND;
format += tmpSection;
tmpSection = IND;
}
return format;
}
int main()
{
std::string input = "/ab v /cd v /ab/cde v abcdh v /ab/cd/egh v ab/cd/eg v eh v ab/cd/eg v eh v ab/cd/eg v abeh v ab/cd/eg v eh v ab/cd/eg v eh v ab/cd/eg v eh";
const std::vector<std::string> CL_VAR = {"ab","cd","e","g","h"};
std::string delimiter = " v ";
std::vector<std::string> tokens;
std::vector<int> tokensSize;
size_t startPos = 0;
size_t foundPos = input.find(delimiter);
while (foundPos != std::string::npos) {
std::string token = input.substr(startPos, foundPos - startPos);
tokens.push_back(token);
startPos = foundPos + delimiter.length();
foundPos = input.find(delimiter, startPos);
}
std::string lastToken = input.substr(startPos);
tokens.push_back(lastToken);
std::multimap<int, std::string, std::less<int>> mappedTokens;
int varCount = 0;
for (const std::string &token : tokens) {
std::cout << token;
for (const std::string &el : CL_VAR) {
if (token.find(el) != std::string::npos)
++varCount;
}
tokensSize.push_back(varCount);
std::cout << " = " << getTermSize(token) << std::endl;
mappedTokens.insert(std::make_pair(varCount, token));
varCount = 0;
}
std::cout << getSectionsSeparator(mappedTokens);
std::cout << getSections(mappedTokens);
// ┌────────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬─────┬─────┬─────┬──────┬──────┬──────┬──────┬──────┬──────┬──────┬──────┐
// │ Набори │ НЕ │ НЕ │ НЕ │ НЕ │ НЕ │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2І │ 2АБО │ 2АБО │ 2АБО │ 2АБО │ 2АБО │
// │ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼─────┼─────┼─────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
// │ │ - │ - │ - │ - │ - │ -- │ - │ - │ - │ - │ │ - │ - │ - │ -- │ - │ - │ - - │ - │ - - │ │ │ │ │ │
// │ edcba │ a │ b │ c │ d │ e │ ac │ cd │ ad │ ab │ cd │ ab │ de │ ab │ cd │ ace │ cde │ ade │ abcd │ abde │ abcd │ T0 │ T1 │ T2 │ T3 │ f │
// ├────────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼────┼─────┼─────┼─────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤
// │ 00000 │ 1 │ 1 │ 1 │ 1 │ 1 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │
// │ 01111 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 1 │ 1 │
// │ 10110 │ 0 │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │
// │ 11001 │ 0 │ 0 │ 1 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 0 │ 1 │
// │ 11011 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 1 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 1 │ 1 │
// └────────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴─────┴─────┴─────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┘
return 0;
} 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
}