C++ 图形计算器解析

C++ 图形计算器解析,c++,parsing,C++,Parsing,这是一个学校项目,我应该创建一个计算器,将图形功能。图形化是可行的,但是我的函数实际上是将函数(数学类)转换成数据的,它不适用于两种主要的测试用例类型,我不知道如何解决这个问题。测试用例是input=-x^2和input=x^2+2x+1,当仔细考虑我所写的内容时,这两种情况都应该有效。我已经被这个问题困扰了一段时间了,我还没有找到一个解决方案,它不会搞乱其他方程 double Solver(string input, double xValue) { int pos1; int pos2; i

这是一个学校项目,我应该创建一个计算器,将图形功能。图形化是可行的,但是我的函数实际上是将函数(数学类)转换成数据的,它不适用于两种主要的测试用例类型,我不知道如何解决这个问题。测试用例是input=-x^2和input=x^2+2x+1,当仔细考虑我所写的内容时,这两种情况都应该有效。我已经被这个问题困扰了一段时间了,我还没有找到一个解决方案,它不会搞乱其他方程

double Solver(string input, double xValue)
{
int pos1;
int pos2;
int symbolpos;

string substring;

double value1;
double value2;

int count;

while (input.find_first_of(" ") != string::npos)
{
    input.erase(input.find_first_of(" "), 1);
}

while (input.find("(") != string::npos && input.find(")") != string::npos)
{
    pos1 = input.find_first_of("(");

    if (pos1 > 0 && input[pos1 - 1] != '-')
    {
        input.insert(pos1, "*");
        pos1++;
    }
    if (pos1 == 2 && input[0] == '-')
    {
        input.insert(pos1 - 1, "1");
        pos1++;
    }
    pos2 = pos1;
    count = 1;

    while (count != 0)
    {
        pos2++;
        if (input[pos2] == '(')
            count++;
        if (input[pos2] == ')')
            count--;
    }

    substring = to_string(Solver(input.substr(pos1 + 1, pos2 - pos1 - 1), xValue));
    input.replace(pos1, pos2 - pos1 + 1, substring);
}

while (input.find("x") != string::npos)
{
    pos1 = input.find_first_of("x");
    if (pos1 > 0)
    {
        input.insert(pos1, "*");
        pos1++;
    }
    if (pos1 == 2 && input[0] == '-')
    {
        input.insert(pos1 - 1, "1");
        pos1++;
    }
    input.replace(pos1, 1, to_string(xValue));
}

while (input.find("--") != string::npos)
{
    input.replace(input.find("--"), 2, "+");
}

input = parsing(input, '^');
input = parsing(input, '*');
input = parsing(input, '/');
input = parsing(input, '+');
input = parsing(input, '-');


return stod(input);
}


string parsing(string tInput, char searchChar)
{
string input = tInput;
int pos1, pos2, symbolpos;
double value1, value2;
string substring;

while (input.substr(1).find_first_of(searchChar) != string::npos)
{
    symbolpos = input.find_last_of(searchChar);
    pos1 = 0;
    for (int a = 0; a < symbolpos - 1; a++)
    {
        if (input[a] == '^')
            pos1 = a++;
        if (input[a] == '*')
            pos1 = a++;
        if (input[a] == '/')
            pos1 = a++;
        if (input[a] == '+')
            pos1 = a++;
        if (input[a] == '-')
            pos1 = a;
    }

    pos2 = input.size();
    for (int a = input.size(); a > symbolpos + 1; a--)
    {
        if (input[a] == '^')
            pos2 = a--;
        if (input[a] == '*')
            pos2 = a--;
        if (input[a] == '/')
            pos2 = a--;
        if (input[a] == '+')
            pos2 = a--;
        if (input[a] == '-')
            pos2 = a--;
    }
    value1 = stod(input.substr(pos1, symbolpos - pos1));
    value2 = stod(input.substr(symbolpos + 1, pos2 - symbolpos));
    if (searchChar == '^')
        substring = to_string(pow(value1, value2));
    if (searchChar == '*')
        substring = to_string(value1 * value2);
    if (searchChar == '/')
        substring = to_string(value1 / value2);
    if (searchChar == '+')
        substring = to_string(value1 + value2);
    if (searchChar == '-')
        substring = to_string(value1 - value2);
    input.replace(pos1, pos2 - pos1, substring);
}

return input;
双解算器(字符串输入,双xValue)
{
int-pos1;
int-pos2;
int-symbolpos;
字符串子串;
双值1;
双重价值2;
整数计数;
while(input.find_first_of(“”)=string::npos)
{
input.erase(input.find_,first_of(“”,1);
}
while(input.find(“”)=string::npos&&input.find(“”)=string::npos)
{
pos1=输入。首先查找(“”)中的\u;
if(pos1>0&&input[pos1-1]!='-')
{
输入。插入(位置1,“*”);
pos1++;
}
if(pos1==2&&input[0]='-')
{
输入。插入(位置1-1,“1”);
pos1++;
}
pos2=pos1;
计数=1;
while(计数!=0)
{
pos2++;
如果(输入[pos2]=='(')
计数++;
如果(输入[pos2]==')')
计数--;
}
substring=to_字符串(解算器(input.substr(pos1+1,pos2-pos1-1),xValue));
输入.替换(pos1,pos2-pos1+1,子字符串);
}
while(input.find(“x”)!=string::npos)
{
pos1=输入。首先查找(“x”)中的;
如果(位置1>0)
{
输入。插入(位置1,“*”);
pos1++;
}
if(pos1==2&&input[0]='-')
{
输入。插入(位置1-1,“1”);
pos1++;
}
input.replace(pos1,1,to_字符串(xValue));
}
while(input.find(“--”)=string::npos)
{
input.replace(input.find(“”),2,“+”;
}
输入=解析(输入“^”);
输入=解析(输入“*”);
输入=解析(输入“/”);
输入=解析(输入“+”);
输入=解析(输入“-”);
返回stod(输入);
}
字符串解析(字符串tInput、char searchChar)
{
字符串输入=tInput;
int pos1、pos2、symbolpos;
双重价值1,价值2;
字符串子串;
while(input.substr(1).find_first_of(searchChar)!=string::npos)
{
symbolpos=输入。查找(searchChar)的最后一个;
pos1=0;
对于(int a=0;asymbolpos+1;a--)
{
如果(输入[a]='^')
pos2=a--;
如果(输入[a]='*')
pos2=a--;
如果(输入[a]=='/'))
pos2=a--;
如果(输入[a]=='+')
pos2=a--;
如果(输入[a]='-')
pos2=a--;
}
value1=stod(input.substr(pos1,symbolpos-pos1));
值2=stod(输入子命令(symbolpos+1,pos2-symbolpos));
如果(searchChar=='^')
子字符串=到_字符串(pow(值1,值2));
如果(searchChar=='*')
子字符串=到字符串(值1*2);
如果(searchChar=='/'))
子字符串=到字符串(值1/2);
如果(searchChar=='+')
子字符串=到字符串(值1+值2);
如果(searchChar=='-')
子字符串=到字符串(值1-值2);
输入.替换(pos1,pos2-pos1,子字符串);
}
返回输入;
}


感谢您的帮助!

您有错误案例,这使您能够很好地使用开发环境中应该附带的调试软件。请仔细阅读该程序,看看该程序在何处执行了您意想不到的操作。即使您不使用Bison,它也包含一些优秀的资源,用于在g中进行解析当然,它有一个完整的计算器演示。你必须自己翻转解析器还是使用一个库?如果使用一个库是一个选项,你看看我的项目。我曾经写过一个类似的问题的答案:(我建议(如何显示)用纯的C++写,而不是使用那些cPARSE LIB。同时,我认识到其他类似的Q/As已经存在。