#pragma once
#include <vector>
#include <iostream
#include <cmath
#include <algorithm>
#include <string>
#include <conio.h
#include <chrono
#include <fstream
enum class Operations
{
add,
sub,
div,
mult
};
classFunctor
{
private:
public:
float operator()(float a, float b, Operations op)
{
switch (op)
{
case Operations::add:
return a + b;
break;
case Operations::div:
return a / b;
break;
case Operations::sub:
return a - b;
break;
case Operations::mult:
return a * b;
break;
}
return -1;
}
};
class Operation
{
private:
struct Pair
{
public:
Pair(std::string str, int val)
:
str(str),
val(val)
{}
std::string str;
int val;
operator int()
{
return val;
}
operator int()const
{
return val;
}
};
public:
Operation()
{
std::cout < "Laddar in data..." < std::endl;
std::vector<Operations> operations;
valAnswer.reserve(131072);
combinAnswer.reserve(131072);
int count = 0;
for (int a = 0; a < 2; ++a)
{
for (int b = 0; b < 4; ++b)
{
for (int c = 0; c < 4; ++c)
{
for (int d = 0; d < 4; ++d)
{
for (int e = 0; e < 4; ++e)
{
for (int f = 0; f < 4; ++f)
{
for (int g = 0; g < 4; ++g)
{
for (int h = 0; h < 4; ++h)
{
for (inti = 0; i < 4; ++i)
{
operations = { Operations(a),Operations(b),Operations(c),Operations(d),
Operations(e),Operations(f),Operations(g),Operations(h),Operations(i) };
floatval = Operate(operations);
if (val != 0.1f & val >= 0)//& std::find(valAnswer.begin(), valAnswer.end(), (int)val) == valAnswer.end())
{
valAnswer.emplace_back((int)val);
combinAnswer.emplace_back(operations);
}
++count;
}
}
}
}
}
}
}
}
}
for (size_ti = 0; ivalAnswer.size(); ++i)
{
std::string str;
for (inti_inner = 0; i_inner < 9; ++i_inner)
{
switch (combinAnswer[i][i_inner])
{
case Operations::add:
str +=("+");
break;
case Operations::div:
str += ("/");
break;
case Operations::sub:
str += ("-");
break;
case Operations::mult:
str += ("*");
break;
}
}
data.emplace_back(Pair{ str,valAnswer[i] });
}
std::sort(data.begin(), data.end());
dataNoCopy = data;
dataNoCopy.erase(std::unique(dataNoCopy.begin(), dataNoCopy.end()), dataNoCopy.end());
std::cout < "Klar!" < std::endl;
}
voidFindValue(intval)
{
autoitToData = std::find(data.begin(), data.end(), val);
if (itToData != data.end())
{
chararr[9];
for (int i = 0; i < 9; ++i)
{
arr[i] = itToData->str[i];
}
std::cout < val < " = ";
for (int i = 0; i < 9; ++i)
{
std::coutarr[i] < i + 1;
}
std::coutstd::endl;
}
else
{
std::cout < "Talet finns inte..." std::endl;
}
}
void Print(Pair& p)
{
char arr[9];
intlastIndex = 0;
for (int i = 0; i < 9; ++i)
{
arr[i] = p.str[i];
}
std::coutp.val < " = ";
for (inti = 0; i < 9; ++i)
{
std::cout < '(';
}
for (inti = 0; i < 9; ++i)
{
if (!(arr[i] == '+' & i == 0))
{
std::coutarr[i];
}
std::couti + 1;
std::cout < ')';
}
std::coutstd::endl;
}
std::string GetStr(Pair& p)
{
std::string string;
char arr[9];
for (int i = 0; i < 9; ++i)
{
arr[i] = p.str[i];
}
string += std::to_string(p.val);
string += "=";
for (inti = 0; i < 9; ++i)
{
string += arr[i];
string += std::to_string(i + 1);
}
string += "\n";
return string;
}
voidFindAll(intval)
{
auto first = std::find(data.begin(), data.end(), val);
if (first != data.end())
{
for (; *first == val; ++first)
{
Print(*first);
}
std::coutstd::count(data.begin(), data.end(), val) < " träffar!" std::endl;
}
else
{
std::cout < "Talet finns inte..." < std::endl;
}
}
void Sequence(intval)
{
if (std::find(dataNoCopy.begin(), dataNoCopy.end(), val) != dataNoCopy.end())
{
autoloc = std::find(dataNoCopy.begin(), dataNoCopy.end(), val);
constint start = loc - dataNoCopy.begin();
int loop = loc - dataNoCopy.begin();
for (; dataNoCopy[loop].val == val; ++val, ++loop);
for (inti_inner = start; i_inner < loop; ++i_inner)
{
Print(dataNoCopy[i_inner]);
}
std::cout < "Från ";
std::coutdataNoCopy[start].val;
std::cout < " till " < val - 1 < std::endl;
}
else
{
std::cout < "Talet finns inte..." std::endl;
}
}
std::vector<Pair> GetData()
{
return data;
}
private:
float Operate(std::vector<Operations> operations)
{
floattmp = OperateHelp(1, 2, 0, operations);
float integral;
if (std::modf(tmp, &integral) == 0)
{
returntmp;
}
else
{
return 0.1f;
}
}
floatOperateHelp(float a, float b, int index, std::vector<Operations> operations)
{
if (index == 0 & operations.front() == Operations::sub)
{
a = -a;
++index;
}
else if (index == 0)
{
++index;
}
Functorfunctor;
floattmp = functor(a, b, operations[index]);
if (index >= 8 || b == 9)
{
returntmp;
}
returnOperateHelp(tmp, b + 1, index + 1, operations);
}
private:
std::vector<intvalAnswer;
std::vector<std::vector<Operations> combinAnswer;
std::vector<Pair> data;
std::vector<Pair> dataNoCopy;
};
______
include "Operation.h"
#include <clocale
boolisNumber(std::string str)
{
for (auto loop = str.begin(); loop != str.end(); ++loop)
{
if (!(*loop >= '0' & *loop <= '9'))
{
return false;
}
}
return true;
}
int main()
{
std::setlocale(LC_ALL, "swedish");
Operation operation;
std::string key;
while (key != "exit")
{
std::cout < "<Kommandon>" < std::endl;
std::cout < "\"tal\" \"antal\" \"sekvens\" \"exit\"" < std::endl;
std::cin > key;
if (key == "exit")
{
continue;
}
else
{
if (key == "tal")
{
std::cout < "Skriv ett tal: ";
std::cin > key;
if (isNumber(key))
{
operation.FindValue(std::stoi(key));
}
else
{
std::wcoutL"Det där är inget tal!" std::endl;
}
}
if (key == "antal")
{
std::cout < "Hitta alla kombinationer för: ";
std::cin > key;
if (isNumber(key))
{
operation.FindAll(std::stoi(key));
}
else
{
std::cout < "Det där är inget tal!" std::endl;
}
}
if (key == "sekvens")
{
std::cout < "Sekvensen för: ";
std::cinkey;
if (isNumber(key))
{
operation.Sequence(std::stoi(key));
}
else
{
std::cout < "Det där är inget tal!" < std::endl;
}
}
}
}
std::cin.get();
return 0;
}