Skip to content

Commit

Permalink
working on function parser
Browse files Browse the repository at this point in the history
  • Loading branch information
DoomKitty87 committed May 24, 2023
1 parent 29c6bf5 commit 74b8eb8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"iosfwd": "cpp",
"string": "cpp",
"ostream": "cpp"
}
}
26 changes: 16 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
#include <iostream>
#include <string>
#include "parser.h"
using namespace std;

static class functions
/*
class functions
{
public:
inline static const string functions1[1] = {"f(x)=ax+3"};
inline static const string functions2[1] = {"f(x)=ax+bx-2"};
inline static const int functions1size = 1;
inline static const int functions2size = 1;
};
*/

/*
Generating functions
int a = rand() % 10;
*/

void game_1() {
cout << "Generating hidden constants..." << endl;
int func = rand() % functions::functions1size;
cout << "The function is: " << functions::functions1[func] << endl;
int constant = rand() % 10;
string func = "3*x+2";
cout << "The function is: " << func << endl;
int constant = 3;

for (int i = 0; i < 10; i++) {
int inputval = rand() % 10;
cout << "x = " << inputval << ": ";
switch (func) {
case 0:
cout << "f(x) = " << (constant * inputval + 3) << endl;
break;
}
cout << "x = " << inputval << ": " << Parser::parseFunction(func, inputval) << endl;
}

while (true) {
Expand Down
66 changes: 66 additions & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "parser.h"

int ctoi(char a) {
char *c = &a;
int i = atoi(c);
return i;
}

string Parser::parseFunction(string functionin, int x) {
string function = functionin;
for (int i = 0; i < function.length(); i++) {
if (function[i] == '*') {
int mult1 = 0;
int mult2 = 0;
if (function[i - 1] == 'x') mult1 = x;
else mult1 = ctoi(function[i - 1]);
if (function[i + 1] == 'x') mult2 = x;
else mult2 = ctoi(function[i + 1]);
function[i] = to_string(mult1 * mult2)[0];
function[i + 1] = ' ';
function[i - 1] = ' ';
i--;
}
else if (function[i] == '/') {
int div1 = 0;
int div2 = 0;
if (function[i - 1] == 'x') div1 = x;
else div1 = ctoi(function[i - 1]);
if (function[i + 1] == 'x') div2 = x;
else div2 = ctoi(function[i + 1]);
function[i] = to_string(div1 / div2)[0];
function[i + 1] = ' ';
function[i - 1] = ' ';
i--;
}
remove_if(function.begin(), function.end(), isspace);
}
for (int i = 0; i < function.length(); i++) {
if (function[i] == '+') {
int add1 = 0;
int add2 = 0;
if (function[i - 1] == 'x') add1 = x;
else add1 = ctoi(function[i - 1]);
if (function[i + 1] == 'x') add2 = x;
else add2 = ctoi(function[i + 1]);
function[i] = to_string(add1 + add2)[0];
function[i + 1] = ' ';
function[i - 1] = ' ';
i--;
}
else if (function[i] == '-') {
int sub1 = 0;
int sub2 = 0;
if (function[i - 1] == 'x') sub1 = x;
else sub1 = ctoi(function[i - 1]);
if (function[i + 1] == 'x') sub2 = x;
else sub2 = ctoi(function[i + 1]);
function[i] = to_string(sub1 - sub2)[0];
function[i + 1] = ' ';
function[i - 1] = ' ';
i--;
}
remove_if(function.begin(), function.end(), isspace);
}
return function;
}
8 changes: 8 additions & 0 deletions parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <string>
using namespace std;

class Parser
{
public:
static string parseFunction(string functionin, int x);
};

0 comments on commit 74b8eb8

Please sign in to comment.