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;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Polynom : public vector<int>
{
public:
Polynom() {}
Polynom(int _degree, int* koef = 0);
inline int degree() const {
return (size() > 0) ? size() - 1 : 0;
}
string toString () const;
Polynom& operator << (const string& src);
void addDegree(int _degree, int koef);
protected:
static string itoa(int value);
static int readNumber(string::const_iterator& it, string::const_iterator end);
};
Polynom::Polynom(int _degree, int* koef)
{
if (_degree < 0)
_degree = 0;
resize(_degree + 1);
if (koef) {
for (int n = 0; n <= _degree; ++n)
(*this)[n] = koef[n];
}
else {
for (int n = 0; n < size(); ++n)
(*this)[n] = 0;
}
}
string Polynom::itoa(int value)
{
char buf[50];
sprintf(buf, "%d", value);
return string(buf);
}
string Polynom::toString () const
{
string result;
for(int n = degree(); n >= 0; --n) {
if (at(n) != 0) {
if (at(n) > 0) {
if (n != degree())
result += "+";
}
result += itoa(at(n));
if (n > 0) {
result += "x";
if (n > 1) {
result += "^";
result += itoa(n);
}
}
}
}
return result;
}
inline basic_ostream<char>& operator << (basic_ostream<char>& os,const Polynom& p)
{
os << p.toString();
return os;
}
void Polynom::addDegree(int _degree, int koef)
{
if (_degree >= degree()) {
int oldSize = size();
resize(_degree + 1);
for(int n = oldSize; n < size(); ++n )
(*this)[n] = 0;
}
(*this)[_degree] += koef;
}
int Polynom::readNumber(string::const_iterator& it, string::const_iterator end)
{
enum { ReadSign, ReadDigit } state = ReadSign;
enum { Positive, Negative } sign = Positive;
int result = 0;
while(it != end)
{
switch (state) {
case ReadSign:
if (*it == '+' || *it == '-') {
if (*it == '+')
sign = Positive;
else
sign = Negative;
state = ReadDigit;
}
else if (*it == 'x') {
return 1;
}
else if (*it < '0' || *it > '9') {
string err("Not a digit '");
err += *it;
err += "' while reading number";
throw(err);
}
else {
state = ReadDigit;
result = *it - '0';
}
break;
case ReadDigit:
if (*it == 'x' || *it == '+' || *it == '-') {
return result * ((sign == Positive) ? 1 : -1);
}
else if (*it < '0' || *it > '9') {
string err("Not a digit '");
err += *it;
err += "' while reading number";
throw(err);
}
else {
state = ReadDigit;
result = result * 10 + *it - '0';
}
break;
}
it++;
}
return result * ((sign == Positive) ? 1 : -1);
}
Polynom& Polynom::operator << (const string& src)
{
enum { ReadKoef, ReadPower } state = ReadKoef;
int koef;
int power = 0;
for(string::const_iterator it = src.begin(); it != src.end(); it++)
{
switch(state) {
case ReadKoef:
koef = readNumber(it, src.end());
if (it == src.end()) {
addDegree(0, koef);
return *this;
}
if (*it == 'x') {
state = ReadPower;
}
else if (*it == '+' || *it == '-') {
addDegree(0, koef);
}
break;
case ReadPower:
if (*it == '^') {
it++;
if (*it == '+') {
it++;
}
if (*it >= '0' && *it <= '9') {
power = readNumber(it, src.end());
addDegree(power, koef);
if (it == src.end())
return *this;
it--;
state = ReadKoef;
}
else {
string err("Unexpected symbol '");
err += *it;
err += "' while reading polynom from \"";
err += src;
err += '"';
throw(err);
}
}
else if (*it == '+' || *it == '-') {
addDegree(1, koef);
it--;
state = ReadKoef;
}
else {
string err("Unexpected symbol '");
err += *it;
err += "' while reading polynom from \"";
err += src;
err += '"';
throw(err);
}
break;
}
}
if (state == ReadPower) {
string err("Unexpected end of string while reading polynom from \"");
err += src;
err += '"';
throw(err);
}
return *this;
}
int main()
{
int deg = 2;
int koef[3] = { 3, 4, 5 };
Polynom polynom(deg, koef);
cout << polynom << endl;
Polynom p2;
p2 << "-18x^3+4x-2";
cout << p2 << endl;
return 0;
}