QString h(ui->lineEdit->text().toStdString().c_str());
ui->label_3->setText(h);
class IObject
{
public:
virtual void create(void** ret_val) = 0;
virtual std::string name() const = 0;
};
template <typename _Type>
class ObjectT : public IObject
{
protected:
static std::string m_name;
public:
void create(void** ret_val)
{
*ret_val = (void*) new _Type;
}
};
typedef ObjectT<bool> BoolObject;
typedef ObjectT<int> IntObject;
template<> std::string BoolObject::m_name = "bool";
template<> std::string IntObject::m_name = "int";
BoolObject bobj;
IntObject iobj;
std::vector<IObject*> my_vector;
my_vector.push_back (&bobj);
my_vector.push_back (&iobj);
if (my_vector[n].name() == "bool") {
bool* pval = nullptr;
my_vector[n].create((void**) &pval);
*pval = true;
}
#include <iostream>
#include <fstream> // std::fstream
using namespace std;
typedef struct
{
int year;
string fio;
} Anketa;
int main ()
{
Anketa anketa;
anketa.year = 1643;
anketa.fio = "Isaac John Newton";
// *** Writing ***
fstream fs;
fs.open ("anketa.dat", std::fstream::out | std::fstream::binary);
fs.write((char*) &anketa.year, sizeof(int));
size_t len = anketa.fio.length();
fs.write((char*) &len, sizeof(size_t));
fs.write(anketa.fio.data(), len);
fs.close();
// *** Reading ***
fs.open ("anketa.dat", std::fstream::in | std::fstream::binary);
fs.read((char*) &anketa.year, sizeof(int));
fs.read((char*) &len, sizeof(size_t));
char buf[len+1];
fs.read(buf, len);
buf[len] = 0;
anketa.fio = buf;
fs.close();
cout << anketa.year << " " << anketa.fio << endl;
return 0;
}
vector<int> vec;
int& tmp = vec.operator [] (2);
operator = (tmp, 12);
template <class _T>
class FunctionT : public function
{
public:
static const char m_name[];
function* get_function(const string& name) {
if (name == m_name)
return new _T;
return nullptr;
}
};
using namespace std;
vector<function*> functions;
functions.push_back( new FunctionT<sinus> );
functions.push_back( new FunctionT<cosinus> );
functions.push_back( new Function<tangens> );
for(vector<function*>::iterator it = functions.begin(); it != functions.end(); ++it) {
function* f = (*it)->get_function("sinus");
if ( f != nullptr )
f->get_out();
}
QObject::connect(this->add, &QPushButton::clicked, parent, &Rashod::show_add_btn);
#include <iostream>
using namespace std;
int main()
{
unsigned int col = 3;
unsigned int row = 1;
char ch;
unsigned int cell = 0;
// Skip rows less than row
for (unsigned int nRow = 0; nRow < row; ++nRow)
do {
cin.get(ch);
} while (ch != '\n');
// Skip columns less than col
for (unsigned int nCol = 0; nCol < col; ++nCol)
do {
cin.get(ch);
} while (ch != ';');
// Read number
do {
cin.get(ch);
if (isdigit(ch))
cell = cell * 10 + ch - '0';
} while(ch != ';');
cout << cell << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
unsigned int BinDec(char* bin)
{
unsigned int rez(0), n;
for(n = 0; n < strlen(bin); ++n) {
rez <<= 1;
rez += bin[n] - '0';
}
return rez;
}
void DecBin(unsigned int number, char* result)
{
int n;
for(n = 0; n < 32; ++n) {
result[n] = (number & 0x80000000) ? '1' : '0';
number <<= 1;
}
result[32] = 0;
}
int main()
{
char bin[] = "10110110100111100010010110111010";
unsigned int nn = BinDec(bin);
cout << bin << " = " << nn << endl;
char *binn = new char[33];
DecBin(nn, binn);
cout << nn << " = " << binn << endl;
if (strcmp(bin, binn) == 0)
cout << "OK" << endl;
else
cout << "False" << endl;
delete [] binn;
return 0;
}